diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-12-07 10:44:10 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-12-06 21:44:10 -0500 |
commit | 82e0383d2104f454af5b3e0e768f0497113f3b13 (patch) | |
tree | 0bb945b408668867e24db2429cc7099d57eaa6f6 /models/pull.go | |
parent | 1583c48e3a89fcb9aeb81a57295982ee64d5859f (diff) | |
download | gitea-82e0383d2104f454af5b3e0e768f0497113f3b13.tar.gz gitea-82e0383d2104f454af5b3e0e768f0497113f3b13.zip |
Move some pull request functions from models to services (#9266)
* Move some pull request functions from models to services
* Fix test
Diffstat (limited to 'models/pull.go')
-rw-r--r-- | models/pull.go | 131 |
1 files changed, 5 insertions, 126 deletions
diff --git a/models/pull.go b/models/pull.go index 388ee16b46..2bd79202f0 100644 --- a/models/pull.go +++ b/models/pull.go @@ -8,7 +8,6 @@ package models import ( "bufio" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -20,14 +19,11 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/sync" "code.gitea.io/gitea/modules/timeutil" "github.com/unknwon/com" ) -var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength) - // PullRequestType defines pull request type type PullRequestType int @@ -485,102 +481,6 @@ func (pr *PullRequest) SetMerged() (err error) { return nil } -// manuallyMerged checks if a pull request got manually merged -// When a pull request got manually merged mark the pull request as merged -func (pr *PullRequest) manuallyMerged() bool { - commit, err := pr.getMergeCommit() - if err != nil { - log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err) - return false - } - if commit != nil { - pr.MergedCommitID = commit.ID.String() - pr.MergedUnix = timeutil.TimeStamp(commit.Author.When.Unix()) - pr.Status = PullRequestStatusManuallyMerged - merger, _ := GetUserByEmail(commit.Author.Email) - - // When the commit author is unknown set the BaseRepo owner as merger - if merger == nil { - if pr.BaseRepo.Owner == nil { - if err = pr.BaseRepo.getOwner(x); err != nil { - log.Error("BaseRepo.getOwner[%d]: %v", pr.ID, err) - return false - } - } - merger = pr.BaseRepo.Owner - } - pr.Merger = merger - pr.MergerID = merger.ID - - if err = pr.SetMerged(); err != nil { - log.Error("PullRequest[%d].setMerged : %v", pr.ID, err) - return false - } - log.Info("manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s", pr.ID, pr.BaseRepo.Name, pr.BaseBranch, commit.ID.String()) - return true - } - return false -} - -// getMergeCommit checks if a pull request got merged -// Returns the git.Commit of the pull request if merged -func (pr *PullRequest) getMergeCommit() (*git.Commit, error) { - if pr.BaseRepo == nil { - var err error - pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID) - if err != nil { - return nil, fmt.Errorf("GetRepositoryByID: %v", err) - } - } - - indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond())) - defer os.Remove(indexTmpPath) - - headFile := pr.GetGitRefName() - - // Check if a pull request is merged into BaseBranch - _, err := git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch).RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}) - if err != nil { - // Errors are signaled by a non-zero status that is not 1 - if strings.Contains(err.Error(), "exit status 1") { - return nil, nil - } - return nil, fmt.Errorf("git merge-base --is-ancestor: %v", err) - } - - commitIDBytes, err := ioutil.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile) - if err != nil { - return nil, fmt.Errorf("ReadFile(%s): %v", headFile, err) - } - commitID := string(commitIDBytes) - if len(commitID) < 40 { - return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID) - } - cmd := commitID[:40] + ".." + pr.BaseBranch - - // Get the commit from BaseBranch where the pull request got merged - mergeCommit, err := git.NewCommand("rev-list", "--ancestry-path", "--merges", "--reverse", cmd).RunInDirWithEnv("", []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}) - if err != nil { - return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v", err) - } else if len(mergeCommit) < 40 { - // PR was fast-forwarded, so just use last commit of PR - mergeCommit = commitID[:40] - } - - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) - } - defer gitRepo.Close() - - commit, err := gitRepo.GetCommit(mergeCommit[:40]) - if err != nil { - return nil, fmt.Errorf("GetCommit: %v", err) - } - - return commit, nil -} - // patchConflicts is a list of conflict description from Git. var patchConflicts = []string{ "patch does not apply", @@ -589,6 +489,11 @@ var patchConflicts = []string{ "error:", } +// TestPatch checks if patch can be merged to base repository without conflict. +func (pr *PullRequest) TestPatch() error { + return pr.testPatch(x) +} + // testPatch checks if patch can be merged to base repository without conflict. func (pr *PullRequest) testPatch(e Engine) (err error) { if pr.BaseRepo == nil { @@ -949,32 +854,6 @@ func (pr *PullRequest) PushToBaseRepo() (err error) { return nil } -// AddToTaskQueue adds itself to pull request test task queue. -func (pr *PullRequest) AddToTaskQueue() { - go pullRequestQueue.AddFunc(pr.ID, func() { - pr.Status = PullRequestStatusChecking - if err := pr.UpdateCols("status"); err != nil { - log.Error("AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err) - } - }) -} - -// checkAndUpdateStatus checks if pull request is possible to leaving checking status, -// and set to be either conflict or mergeable. -func (pr *PullRequest) checkAndUpdateStatus() { - // Status is not changed to conflict means mergeable. - if pr.Status == PullRequestStatusChecking { - pr.Status = PullRequestStatusMergeable - } - - // Make sure there is no waiting test to process before leaving the checking status. - if !pullRequestQueue.Exist(pr.ID) { - if err := pr.UpdateCols("status, conflicted_files"); err != nil { - log.Error("Update[%d]: %v", pr.ID, err) - } - } -} - // IsWorkInProgress determine if the Pull Request is a Work In Progress by its title func (pr *PullRequest) IsWorkInProgress() bool { if err := pr.LoadIssue(); err != nil { |