2019-12-07 10:44:10 +08:00
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
2019-12-15 17:51:28 +08:00
|
|
|
"context"
|
2019-12-07 10:44:10 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-02-03 07:19:58 +08:00
|
|
|
"strconv"
|
2019-12-07 10:44:10 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-12-07 10:44:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-12-15 17:51:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-12-07 10:44:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-12-16 05:57:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2022-01-20 07:26:57 +08:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-02-03 07:19:58 +08:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-12-07 10:44:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-08-12 04:05:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-07 10:44:10 +08:00
|
|
|
)
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// prQueue represents a queue to handle update pull request tests
|
|
|
|
var prQueue queue.UniqueQueue
|
2019-12-07 10:44:10 +08:00
|
|
|
|
|
|
|
// AddToTaskQueue adds itself to pull request test task queue.
|
|
|
|
func AddToTaskQueue(pr *models.PullRequest) {
|
2021-06-10 03:52:55 +08:00
|
|
|
err := prQueue.PushFunc(strconv.FormatInt(pr.ID, 10), func() error {
|
|
|
|
pr.Status = models.PullRequestStatusChecking
|
|
|
|
err := pr.UpdateColsIfNotMerged("status")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err)
|
|
|
|
} else {
|
|
|
|
log.Trace("Adding PR ID: %d to the test pull requests queue", pr.ID)
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
2021-06-10 03:52:55 +08:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil && err != queue.ErrAlreadyInQueue {
|
|
|
|
log.Error("Error adding prID %d to the test pull requests queue: %v", pr.ID, err)
|
|
|
|
}
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkAndUpdateStatus checks if pull request is possible to leaving checking status,
|
|
|
|
// and set to be either conflict or mergeable.
|
|
|
|
func checkAndUpdateStatus(pr *models.PullRequest) {
|
|
|
|
// Status is not changed to conflict means mergeable.
|
|
|
|
if pr.Status == models.PullRequestStatusChecking {
|
|
|
|
pr.Status = models.PullRequestStatusMergeable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is no waiting test to process before leaving the checking status.
|
2020-02-03 07:19:58 +08:00
|
|
|
has, err := prQueue.Has(strconv.FormatInt(pr.ID, 10))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if the queue is waiting to reprocess pr.ID %d. Error: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
2020-10-14 02:50:57 +08:00
|
|
|
if err := pr.UpdateColsIfNotMerged("merge_base", "status", "conflicted_files", "changed_protected_files"); err != nil {
|
2019-12-07 10:44:10 +08:00
|
|
|
log.Error("Update[%d]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMergeCommit checks if a pull request got merged
|
|
|
|
// Returns the git.Commit of the pull request if merged
|
2022-01-20 07:26:57 +08:00
|
|
|
func getMergeCommit(ctx context.Context, pr *models.PullRequest) (*git.Commit, error) {
|
2019-12-07 10:44:10 +08:00
|
|
|
if pr.BaseRepo == nil {
|
|
|
|
var err error
|
2021-12-10 09:27:50 +08:00
|
|
|
pr.BaseRepo, err = repo_model.GetRepositoryByID(pr.BaseRepoID)
|
2019-12-07 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetRepositoryByID: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:38:34 +08:00
|
|
|
indexTmpPath, err := os.MkdirTemp(os.TempDir(), "gitea-"+pr.BaseRepo.Name)
|
2019-12-16 13:17:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create temp dir for repository %s: %v", pr.BaseRepo.RepoPath(), err)
|
|
|
|
}
|
2020-08-12 04:05:34 +08:00
|
|
|
defer func() {
|
|
|
|
if err := util.RemoveAll(indexTmpPath); err != nil {
|
|
|
|
log.Warn("Unable to remove temporary index path: %s: Error: %v", indexTmpPath, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-07 10:44:10 +08:00
|
|
|
|
|
|
|
headFile := pr.GetGitRefName()
|
|
|
|
|
|
|
|
// Check if a pull request is merged into BaseBranch
|
2022-02-07 03:01:47 +08:00
|
|
|
_, err = git.NewCommand(ctx, "merge-base", "--is-ancestor", headFile, pr.BaseBranch).
|
2020-02-03 07:19:58 +08:00
|
|
|
RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()})
|
2019-12-07 10:44:10 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:38:34 +08:00
|
|
|
commitIDBytes, err := os.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
|
2019-12-07 10:44:10 +08:00
|
|
|
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
|
2022-02-07 03:01:47 +08:00
|
|
|
mergeCommit, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse", cmd).
|
2020-02-03 07:19:58 +08:00
|
|
|
RunInDirWithEnv("", []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()})
|
2019-12-07 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v", err)
|
|
|
|
} else if len(mergeCommit) < 40 {
|
2021-03-04 11:41:23 +08:00
|
|
|
// PR was maybe fast-forwarded, so just use last commit of PR
|
2019-12-07 10:44:10 +08:00
|
|
|
mergeCommit = commitID[:40]
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2019-12-07 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("OpenRepository: %v", err)
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetCommit(mergeCommit[:40])
|
|
|
|
if err != nil {
|
2021-08-31 09:18:15 +08:00
|
|
|
return nil, fmt.Errorf("GetMergeCommit[%v]: %v", mergeCommit[:40], err)
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// manuallyMerged checks if a pull request got manually merged
|
|
|
|
// When a pull request got manually merged mark the pull request as merged
|
2022-01-20 07:26:57 +08:00
|
|
|
func manuallyMerged(ctx context.Context, pr *models.PullRequest) bool {
|
2021-03-04 11:41:23 +08:00
|
|
|
if err := pr.LoadBaseRepo(); err != nil {
|
|
|
|
log.Error("PullRequest[%d].LoadBaseRepo: %v", pr.ID, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-10 03:57:58 +08:00
|
|
|
if unit, err := pr.BaseRepo.GetUnit(unit.TypePullRequests); err == nil {
|
2021-03-04 11:41:23 +08:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
if !config.AutodetectManualMerge {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-10 03:57:58 +08:00
|
|
|
log.Error("PullRequest[%d].BaseRepo.GetUnit(unit.TypePullRequests): %v", pr.ID, err)
|
2021-03-04 11:41:23 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
commit, err := getMergeCommit(ctx, pr)
|
2019-12-07 10:44:10 +08:00
|
|
|
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 = models.PullRequestStatusManuallyMerged
|
2021-11-24 17:49:20 +08:00
|
|
|
merger, _ := user_model.GetUserByEmail(commit.Author.Email)
|
2019-12-07 10:44:10 +08:00
|
|
|
|
|
|
|
// When the commit author is unknown set the BaseRepo owner as merger
|
|
|
|
if merger == nil {
|
|
|
|
if pr.BaseRepo.Owner == nil {
|
2022-03-22 23:22:54 +08:00
|
|
|
if err = pr.BaseRepo.GetOwner(ctx); err != nil {
|
2019-12-07 10:44:10 +08:00
|
|
|
log.Error("BaseRepo.GetOwner[%d]: %v", pr.ID, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
merger = pr.BaseRepo.Owner
|
|
|
|
}
|
|
|
|
pr.Merger = merger
|
|
|
|
pr.MergerID = merger.ID
|
|
|
|
|
2020-02-10 07:09:31 +08:00
|
|
|
if merged, err := pr.SetMerged(); err != nil {
|
2019-12-07 10:44:10 +08:00
|
|
|
log.Error("PullRequest[%d].setMerged : %v", pr.ID, err)
|
|
|
|
return false
|
2020-02-10 07:09:31 +08:00
|
|
|
} else if !merged {
|
|
|
|
return false
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
2019-12-16 05:57:34 +08:00
|
|
|
|
2020-01-17 00:24:20 +08:00
|
|
|
notification.NotifyMergePullRequest(pr, merger)
|
2019-12-16 05:57:34 +08:00
|
|
|
|
2019-12-07 10:44:10 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// InitializePullRequests checks and tests untested patches of pull requests.
|
|
|
|
func InitializePullRequests(ctx context.Context) {
|
|
|
|
prs, err := models.GetPullRequestIDsByCheckStatus(models.PullRequestStatusChecking)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Find Checking PRs: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, prID := range prs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-12-15 17:51:28 +08:00
|
|
|
return
|
2020-02-03 07:19:58 +08:00
|
|
|
default:
|
|
|
|
if err := prQueue.PushFunc(strconv.FormatInt(prID, 10), func() error {
|
|
|
|
log.Trace("Adding PR ID: %d to the pull requests patch checking queue", prID)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("Error adding prID: %s to the pull requests patch checking queue %v", prID, err)
|
2019-12-15 17:51:28 +08:00
|
|
|
}
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
2020-02-03 07:19:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-07 10:44:10 +08:00
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// handle passed PR IDs and test the PRs
|
2022-01-23 05:22:14 +08:00
|
|
|
func handle(data ...queue.Data) []queue.Data {
|
2020-02-03 07:19:58 +08:00
|
|
|
for _, datum := range data {
|
2020-12-25 17:59:32 +08:00
|
|
|
id, _ := strconv.ParseInt(datum.(string), 10, 64)
|
2019-12-15 17:51:28 +08:00
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
testPR(id)
|
|
|
|
}
|
2022-01-23 05:22:14 +08:00
|
|
|
return nil
|
2022-01-20 07:26:57 +08:00
|
|
|
}
|
2019-12-15 17:51:28 +08:00
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
func testPR(id int64) {
|
|
|
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Test PR[%d] from patch checking queue", id))
|
|
|
|
defer finished()
|
|
|
|
|
|
|
|
pr, err := models.GetPullRequestByID(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetPullRequestByID[%d]: %v", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.HasMerged {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if manuallyMerged(ctx, pr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := TestPatch(pr); err != nil {
|
|
|
|
log.Error("testPatch[%d]: %v", pr.ID, err)
|
|
|
|
pr.Status = models.PullRequestStatusError
|
|
|
|
if err := pr.UpdateCols("status"); err != nil {
|
|
|
|
log.Error("update pr [%d] status to PullRequestStatusError failed: %v", pr.ID, err)
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
return
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
checkAndUpdateStatus(pr)
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:50:57 +08:00
|
|
|
// CheckPrsForBaseBranch check all pulls with bseBrannch
|
2021-12-10 09:27:50 +08:00
|
|
|
func CheckPrsForBaseBranch(baseRepo *repo_model.Repository, baseBranchName string) error {
|
2020-10-14 02:50:57 +08:00
|
|
|
prs, err := models.GetUnmergedPullRequestsByBaseInfo(baseRepo.ID, baseBranchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
AddToTaskQueue(pr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-07 10:44:10 +08:00
|
|
|
// Init runs the task queue to test all the checking status pull requests
|
2020-02-03 07:19:58 +08:00
|
|
|
func Init() error {
|
2021-10-18 03:47:12 +08:00
|
|
|
prQueue = queue.CreateUniqueQueue("pr_patch_checker", handle, "")
|
2020-02-03 07:19:58 +08:00
|
|
|
|
|
|
|
if prQueue == nil {
|
|
|
|
return fmt.Errorf("Unable to create pr_patch_checker Queue")
|
|
|
|
}
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(prQueue.Run)
|
|
|
|
go graceful.GetManager().RunWithShutdownContext(InitializePullRequests)
|
|
|
|
return nil
|
2019-12-07 10:44:10 +08:00
|
|
|
}
|