Change target branch for pull request (#6488)
* Adds functionality to change target branch of created pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use const instead of var in JavaScript additions Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check if branches are equal and if PR already exists before changing target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Make sure to check all commits Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Print error messages for user as error flash message Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Disallow changing target branch of closed or merged pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Resolve conflicts after merge of upstream/master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change order of branch select fields Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes duplicate check Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use ctx.Tr for translations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Recompile JS Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use correct translation namespace Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove redundant if condition Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves most change branch logic into pull service Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Completes comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Add Ref to ChangesPayload for logging changed target branches instead of creating a new struct Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Revert changes to go.mod Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Directly use createComment method Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return 404 if pull request is not found. Move written check up Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove variable declaration Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return client errors on change pull request target errors Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error in commit.HasPreviousCommit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds blank line Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Test patch before persisting new target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update patch before testing (not working) Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes patch calls when changeing pull request target Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unneeded check for base name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ChangeTargetBranch completely to pull service. Update patch status. Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Set webhook mode after errors were validated Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update PR in one transaction Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Move logic for check if head is equal with branch to pull model Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment and simplify return Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjust CreateComment method call Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
This commit is contained in:
parent
59d6401486
commit
61db834904
19 changed files with 461 additions and 19 deletions
|
@ -46,6 +46,94 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
|
|||
return nil
|
||||
}
|
||||
|
||||
// ChangeTargetBranch changes the target branch of this pull request, as the given user.
|
||||
func ChangeTargetBranch(pr *models.PullRequest, doer *models.User, targetBranch string) (err error) {
|
||||
// Current target branch is already the same
|
||||
if pr.BaseBranch == targetBranch {
|
||||
return nil
|
||||
}
|
||||
|
||||
if pr.Issue.IsClosed {
|
||||
return models.ErrIssueIsClosed{
|
||||
ID: pr.Issue.ID,
|
||||
RepoID: pr.Issue.RepoID,
|
||||
Index: pr.Issue.Index,
|
||||
}
|
||||
}
|
||||
|
||||
if pr.HasMerged {
|
||||
return models.ErrPullRequestHasMerged{
|
||||
ID: pr.ID,
|
||||
IssueID: pr.Index,
|
||||
HeadRepoID: pr.HeadRepoID,
|
||||
BaseRepoID: pr.BaseRepoID,
|
||||
HeadBranch: pr.HeadBranch,
|
||||
BaseBranch: pr.BaseBranch,
|
||||
}
|
||||
}
|
||||
|
||||
// Check if branches are equal
|
||||
branchesEqual, err := pr.IsHeadEqualWithBranch(targetBranch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if branchesEqual {
|
||||
return models.ErrBranchesEqual{
|
||||
HeadBranchName: pr.HeadBranch,
|
||||
BaseBranchName: targetBranch,
|
||||
}
|
||||
}
|
||||
|
||||
// Check if pull request for the new target branch already exists
|
||||
existingPr, err := models.GetUnmergedPullRequest(pr.HeadRepoID, pr.BaseRepoID, pr.HeadBranch, targetBranch)
|
||||
if existingPr != nil {
|
||||
return models.ErrPullRequestAlreadyExists{
|
||||
ID: existingPr.ID,
|
||||
IssueID: existingPr.Index,
|
||||
HeadRepoID: existingPr.HeadRepoID,
|
||||
BaseRepoID: existingPr.BaseRepoID,
|
||||
HeadBranch: existingPr.HeadBranch,
|
||||
BaseBranch: existingPr.BaseBranch,
|
||||
}
|
||||
}
|
||||
if err != nil && !models.IsErrPullRequestNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set new target branch
|
||||
oldBranch := pr.BaseBranch
|
||||
pr.BaseBranch = targetBranch
|
||||
|
||||
// Refresh patch
|
||||
if err := TestPatch(pr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update target branch, PR diff and status
|
||||
// This is the same as checkAndUpdateStatus in check service, but also updates base_branch
|
||||
if pr.Status == models.PullRequestStatusChecking {
|
||||
pr.Status = models.PullRequestStatusMergeable
|
||||
}
|
||||
if err := pr.UpdateCols("status, conflicted_files, base_branch"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create comment
|
||||
options := &models.CreateCommentOptions{
|
||||
Type: models.CommentTypeChangeTargetBranch,
|
||||
Doer: doer,
|
||||
Repo: pr.Issue.Repo,
|
||||
Issue: pr.Issue,
|
||||
OldRef: oldBranch,
|
||||
NewRef: targetBranch,
|
||||
}
|
||||
if _, err = models.CreateComment(options); err != nil {
|
||||
return fmt.Errorf("CreateChangeTargetBranchComment: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *models.User, branch string) error {
|
||||
repo, err := models.GetRepositoryByID(repoID)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue