// testPatch checks if patch can be merged to base repository without conflit. // FIXME: make a mechanism to clean up stable local copies. func (pr *PullRequest) testPatch() (err error) { if pr.BaseRepo == nil { pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID) if err != nil { return fmt.Errorf("GetRepositoryByID: %v", err) } } patchPath, err := pr.BaseRepo.PatchPath(pr.Index) if err != nil { return fmt.Errorf("BaseRepo.PatchPath: %v", err) } // Fast fail if patch does not exist, this assumes data is cruppted. if !com.IsFile(patchPath) { log.Trace("PullRequest[%d].testPatch: ignored cruppted data", pr.ID) return nil } log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath) if err := pr.BaseRepo.UpdateLocalCopy(); err != nil { return fmt.Errorf("UpdateLocalCopy: %v", err) } // Checkout base branch. _, stderr, err := process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(), fmt.Sprintf("PullRequest.Merge (git checkout): %v", pr.BaseRepo.ID), "git", "checkout", pr.BaseBranch) if err != nil { return fmt.Errorf("git checkout: %s", stderr) } pr.Status = PULL_REQUEST_STATUS_CHECKING _, stderr, err = process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(), fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID), "git", "apply", "--check", patchPath) if err != nil { for i := range patchConflicts { if strings.Contains(stderr, patchConflicts[i]) { log.Trace("PullRequest[%d].testPatch (apply): has conflit", pr.ID) fmt.Println(stderr) pr.Status = PULL_REQUEST_STATUS_CONFLICT return nil } } return fmt.Errorf("git apply --check: %v - %s", err, stderr) } return nil }
// DeleteReleaseByID deletes a release and corresponding Git tag by given ID. func DeleteReleaseByID(id int64) error { rel, err := GetReleaseByID(id) if err != nil { return fmt.Errorf("GetReleaseByID: %v", err) } repo, err := GetRepositoryByID(rel.RepoID) if err != nil { return fmt.Errorf("GetRepositoryByID: %v", err) } _, stderr, err := process.ExecDir(-1, repo.RepoPath(), fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID), "git", "tag", "-d", rel.TagName) if err != nil && !strings.Contains(stderr, "not found") { return fmt.Errorf("git tag -d: %v - %s", err, stderr) } if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil { return fmt.Errorf("Delete: %v", err) } return nil }
// Merge merges pull request to base repository. func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error) { if err = pr.GetHeadRepo(); err != nil { return fmt.Errorf("GetHeadRepo: %v", err) } else if err = pr.GetBaseRepo(); err != nil { return fmt.Errorf("GetBaseRepo: %v", err) } sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } if err = pr.Issue.changeStatus(sess, doer, pr.Issue.Repo, true); err != nil { return fmt.Errorf("Issue.changeStatus: %v", err) } headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name) headGitRepo, err := git.OpenRepository(headRepoPath) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } pr.MergedCommitID, err = headGitRepo.GetBranchCommitID(pr.HeadBranch) if err != nil { return fmt.Errorf("GetBranchCommitID: %v", err) } if err = mergePullRequestAction(sess, doer, pr.Issue.Repo, pr.Issue); err != nil { return fmt.Errorf("mergePullRequestAction: %v", err) } pr.HasMerged = true pr.Merged = time.Now() pr.MergerID = doer.Id if _, err = sess.Id(pr.ID).AllCols().Update(pr); err != nil { return fmt.Errorf("update pull request: %v", err) } // Clone base repo. tmpBasePath := path.Join(setting.AppDataPath, "tmp/repos", com.ToStr(time.Now().Nanosecond())+".git") os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm) defer os.RemoveAll(path.Dir(tmpBasePath)) var stderr string if _, stderr, err = process.ExecTimeout(5*time.Minute, fmt.Sprintf("PullRequest.Merge (git clone): %s", tmpBasePath), "git", "clone", baseGitRepo.Path, tmpBasePath); err != nil { return fmt.Errorf("git clone: %s", stderr) } // Check out base branch. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath), "git", "checkout", pr.BaseBranch); err != nil { return fmt.Errorf("git checkout: %s", stderr) } // Add head repo remote. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git remote add): %s", tmpBasePath), "git", "remote", "add", "head_repo", headRepoPath); err != nil { return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) } // Merge commits. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath), "git", "fetch", "head_repo"); err != nil { return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) } if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath), "git", "merge", "--no-ff", "--no-commit", "head_repo/"+pr.HeadBranch); err != nil { return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr) } sig := doer.NewGitSig() if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath), "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)); err != nil { return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr) } // Push back to upstream. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath), "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil { return fmt.Errorf("git push: %s", stderr) } if err = sess.Commit(); err != nil { return fmt.Errorf("Commit: %v", err) } // Compose commit repository action l, err := headGitRepo.CommitsBetweenIDs(pr.MergedCommitID, pr.MergeBase) if err != nil { return fmt.Errorf("CommitsBetween: %v", err) } p := &api.PushPayload{ Ref: "refs/heads/" + pr.BaseBranch, Before: pr.MergeBase, After: pr.MergedCommitID, CompareUrl: setting.AppUrl + pr.BaseRepo.ComposeCompareURL(pr.MergeBase, pr.MergedCommitID), Commits: ListToPushCommits(l).ToApiPayloadCommits(pr.BaseRepo.FullRepoLink()), Repo: pr.BaseRepo.ComposePayload(), Pusher: &api.PayloadAuthor{ Name: pr.HeadRepo.MustOwner().DisplayName(), Email: pr.HeadRepo.MustOwner().Email, UserName: pr.HeadRepo.MustOwner().Name, }, Sender: &api.PayloadUser{ UserName: doer.Name, ID: doer.Id, AvatarUrl: setting.AppUrl + doer.RelAvatarLink(), }, } if err = PrepareWebhooks(pr.BaseRepo, HOOK_EVENT_PUSH, p); err != nil { return fmt.Errorf("PrepareWebhooks: %v", err) } go HookQueue.Add(pr.BaseRepo.ID) return nil }