2019-12-07 08:14:10 +05:30
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
// All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-07 08:14:10 +05:30
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
2019-12-15 15:21:28 +05:30
|
|
|
"context"
|
2022-03-31 20:23:08 +05:30
|
|
|
"errors"
|
2019-12-07 08:14:10 +05:30
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-02-03 04:49:58 +05:30
|
|
|
"strconv"
|
2019-12-07 08:14:10 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-05-04 01:16:28 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-01-16 13:30:22 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-12-07 08:14:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-12-15 15:21:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-12-07 08:14:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-12-16 03:27:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2022-01-20 04:56:57 +05:30
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-02-03 04:49:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-12-07 08:14:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-31 20:23:08 +05:30
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2019-12-07 08:14:10 +05:30
|
|
|
)
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
// prPatchCheckerQueue represents a queue to handle update pull request tests
|
|
|
|
var prPatchCheckerQueue queue.UniqueQueue
|
2019-12-07 08:14:10 +05:30
|
|
|
|
2022-03-31 20:23:08 +05:30
|
|
|
var (
|
2022-05-02 05:24:44 +05:30
|
|
|
ErrIsClosed = errors.New("pull is closed")
|
|
|
|
ErrUserNotAllowedToMerge = models.ErrDisallowedToMerge{}
|
2022-03-31 20:23:08 +05:30
|
|
|
ErrHasMerged = errors.New("has already been merged")
|
|
|
|
ErrIsWorkInProgress = errors.New("work in progress PRs cannot be merged")
|
2022-04-20 20:13:15 +05:30
|
|
|
ErrIsChecking = errors.New("cannot merge while conflict checking is in progress")
|
2022-03-31 20:23:08 +05:30
|
|
|
ErrNotMergableState = errors.New("not in mergeable state")
|
|
|
|
ErrDependenciesLeft = errors.New("is blocked by an open dependency")
|
|
|
|
)
|
|
|
|
|
2019-12-07 08:14:10 +05:30
|
|
|
// AddToTaskQueue adds itself to pull request test task queue.
|
2022-06-13 15:07:59 +05:30
|
|
|
func AddToTaskQueue(pr *issues_model.PullRequest) {
|
2022-05-02 05:24:44 +05:30
|
|
|
err := prPatchCheckerQueue.PushFunc(strconv.FormatInt(pr.ID, 10), func() error {
|
2022-06-13 15:07:59 +05:30
|
|
|
pr.Status = issues_model.PullRequestStatusChecking
|
2022-11-19 13:42:33 +05:30
|
|
|
err := pr.UpdateColsIfNotMerged(db.DefaultContext, "status")
|
2021-06-10 01:22:55 +05:30
|
|
|
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 08:14:10 +05:30
|
|
|
}
|
2021-06-10 01:22:55 +05:30
|
|
|
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 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
2022-03-31 20:23:08 +05:30
|
|
|
// CheckPullMergable check if the pull mergable based on all conditions (branch protection, merge options, ...)
|
2022-06-13 15:07:59 +05:30
|
|
|
func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *issues_model.PullRequest, manuallMerge, force bool) error {
|
2022-11-13 01:48:50 +05:30
|
|
|
return db.WithTx(stdCtx, func(ctx context.Context) error {
|
2022-05-04 01:16:28 +05:30
|
|
|
if pr.HasMerged {
|
|
|
|
return ErrHasMerged
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
2022-05-04 01:16:28 +05:30
|
|
|
return err
|
|
|
|
} else if pr.Issue.IsClosed {
|
|
|
|
return ErrIsClosed
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if allowedMerge, err := IsUserAllowedToMerge(ctx, pr, *perm, doer); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !allowedMerge {
|
|
|
|
return ErrUserNotAllowedToMerge
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if manuallMerge {
|
|
|
|
// don't check rules to "auto merge", doer is going to mark this pull as merged manually
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if pr.IsWorkInProgress() {
|
|
|
|
return ErrIsWorkInProgress
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-07-13 13:52:51 +05:30
|
|
|
if !pr.CanAutoMerge() && !pr.IsEmpty() {
|
2022-05-04 01:16:28 +05:30
|
|
|
return ErrNotMergableState
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if pr.IsChecking() {
|
|
|
|
return ErrIsChecking
|
|
|
|
}
|
2022-04-20 20:13:15 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if err := CheckPullBranchProtections(ctx, pr, false); err != nil {
|
|
|
|
if models.IsErrDisallowedToMerge(err) {
|
|
|
|
if force {
|
2022-05-20 19:38:52 +05:30
|
|
|
if isRepoAdmin, err2 := access_model.IsUserRepoAdmin(ctx, pr.BaseRepo, doer); err2 != nil {
|
2022-05-04 01:16:28 +05:30
|
|
|
return err2
|
|
|
|
} else if !isRepoAdmin {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
}
|
2022-05-04 01:16:28 +05:30
|
|
|
} else {
|
|
|
|
return err
|
2022-03-31 20:23:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if _, err := isSignedIfRequired(ctx, pr, doer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if noDeps, err := issues_model.IssueNoDependenciesLeft(ctx, pr.Issue); err != nil {
|
2022-05-04 01:16:28 +05:30
|
|
|
return err
|
|
|
|
} else if !noDeps {
|
|
|
|
return ErrDependenciesLeft
|
|
|
|
}
|
2022-03-31 20:23:08 +05:30
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
return nil
|
2022-11-13 01:48:50 +05:30
|
|
|
})
|
2022-03-31 20:23:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// isSignedIfRequired check if merge will be signed if required
|
2022-06-13 15:07:59 +05:30
|
|
|
func isSignedIfRequired(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) (bool, error) {
|
2023-01-16 13:30:22 +05:30
|
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
|
|
|
|
if err != nil {
|
2022-03-31 20:23:08 +05:30
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
if pb == nil || !pb.RequireSignedCommits {
|
2022-03-31 20:23:08 +05:30
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sign, _, _, err := asymkey_service.SignMerge(ctx, pr, doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName())
|
|
|
|
|
|
|
|
return sign, err
|
|
|
|
}
|
|
|
|
|
2019-12-07 08:14:10 +05:30
|
|
|
// checkAndUpdateStatus checks if pull request is possible to leaving checking status,
|
|
|
|
// and set to be either conflict or mergeable.
|
2022-11-19 13:42:33 +05:30
|
|
|
func checkAndUpdateStatus(ctx context.Context, pr *issues_model.PullRequest) {
|
2019-12-07 08:14:10 +05:30
|
|
|
// Status is not changed to conflict means mergeable.
|
2022-06-13 15:07:59 +05:30
|
|
|
if pr.Status == issues_model.PullRequestStatusChecking {
|
|
|
|
pr.Status = issues_model.PullRequestStatusMergeable
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is no waiting test to process before leaving the checking status.
|
2022-05-02 05:24:44 +05:30
|
|
|
has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
2020-02-03 04:49:58 +05:30
|
|
|
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 {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.UpdateColsIfNotMerged(ctx, "merge_base", "status", "conflicted_files", "changed_protected_files"); err != nil {
|
2019-12-07 08:14:10 +05:30
|
|
|
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-06-13 15:07:59 +05:30
|
|
|
func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Commit, error) {
|
2019-12-07 08:14:10 +05:30
|
|
|
if pr.BaseRepo == nil {
|
|
|
|
var err error
|
2022-12-03 08:18:26 +05:30
|
|
|
pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID)
|
2019-12-07 08:14:10 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("GetRepositoryByID: %w", err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
indexTmpPath, err := os.MkdirTemp(os.TempDir(), "gitea-"+pr.BaseRepo.Name)
|
2019-12-16 10:47:55 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to create temp dir for repository %s: %w", pr.BaseRepo.RepoPath(), err)
|
2019-12-16 10:47:55 +05:30
|
|
|
}
|
2020-08-12 01:35:34 +05:30
|
|
|
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 08:14:10 +05:30
|
|
|
|
|
|
|
headFile := pr.GetGitRefName()
|
|
|
|
|
|
|
|
// Check if a pull request is merged into BaseBranch
|
2022-10-23 20:14:45 +05:30
|
|
|
_, _, err = git.NewCommand(ctx, "merge-base", "--is-ancestor").AddDynamicArguments(headFile, pr.BaseBranch).
|
2022-04-01 08:25:30 +05:30
|
|
|
RunStdString(&git.RunOpts{Dir: pr.BaseRepo.RepoPath(), Env: []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}})
|
2019-12-07 08:14:10 +05:30
|
|
|
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
|
|
|
|
}
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("git merge-base --is-ancestor: %w", err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
commitIDBytes, err := os.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
|
2019-12-07 08:14:10 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("ReadFile(%s): %w", headFile, err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
commitID := string(commitIDBytes)
|
2022-12-27 18:42:49 +05:30
|
|
|
if len(commitID) < git.SHAFullLength {
|
2019-12-07 08:14:10 +05:30
|
|
|
return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID)
|
|
|
|
}
|
2022-12-27 18:42:49 +05:30
|
|
|
cmd := commitID[:git.SHAFullLength] + ".." + pr.BaseBranch
|
2019-12-07 08:14:10 +05:30
|
|
|
|
|
|
|
// Get the commit from BaseBranch where the pull request got merged
|
2022-10-23 20:14:45 +05:30
|
|
|
mergeCommit, _, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse").AddDynamicArguments(cmd).
|
2022-04-01 08:25:30 +05:30
|
|
|
RunStdString(&git.RunOpts{Dir: "", Env: []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}})
|
2019-12-07 08:14:10 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err)
|
2022-12-27 18:42:49 +05:30
|
|
|
} else if len(mergeCommit) < git.SHAFullLength {
|
2021-03-04 09:11:23 +05:30
|
|
|
// PR was maybe fast-forwarded, so just use last commit of PR
|
2022-12-27 18:42:49 +05:30
|
|
|
mergeCommit = commitID[:git.SHAFullLength]
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2019-12-07 08:14:10 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("OpenRepository: %w", err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2022-12-27 18:42:49 +05:30
|
|
|
commit, err := gitRepo.GetCommit(mergeCommit[:git.SHAFullLength])
|
2019-12-07 08:14:10 +05:30
|
|
|
if err != nil {
|
2022-12-27 18:42:49 +05:30
|
|
|
return nil, fmt.Errorf("GetMergeCommit[%v]: %w", mergeCommit[:git.SHAFullLength], err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-06-13 15:07:59 +05:30
|
|
|
func manuallyMerged(ctx context.Context, pr *issues_model.PullRequest) bool {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
2021-03-04 09:11:23 +05:30
|
|
|
log.Error("PullRequest[%d].LoadBaseRepo: %v", pr.ID, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-12-10 08:16:31 +05:30
|
|
|
if unit, err := pr.BaseRepo.GetUnit(ctx, unit.TypePullRequests); err == nil {
|
2021-03-04 09:11:23 +05:30
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
if !config.AutodetectManualMerge {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-10 01:27:58 +05:30
|
|
|
log.Error("PullRequest[%d].BaseRepo.GetUnit(unit.TypePullRequests): %v", pr.ID, err)
|
2021-03-04 09:11:23 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
commit, err := getMergeCommit(ctx, pr)
|
2019-12-07 08:14:10 +05:30
|
|
|
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())
|
2022-06-13 15:07:59 +05:30
|
|
|
pr.Status = issues_model.PullRequestStatusManuallyMerged
|
2021-11-24 15:19:20 +05:30
|
|
|
merger, _ := user_model.GetUserByEmail(commit.Author.Email)
|
2019-12-07 08:14:10 +05:30
|
|
|
|
|
|
|
// When the commit author is unknown set the BaseRepo owner as merger
|
|
|
|
if merger == nil {
|
|
|
|
if pr.BaseRepo.Owner == nil {
|
2022-03-22 20:52:54 +05:30
|
|
|
if err = pr.BaseRepo.GetOwner(ctx); err != nil {
|
2019-12-07 08:14:10 +05:30
|
|
|
log.Error("BaseRepo.GetOwner[%d]: %v", pr.ID, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
merger = pr.BaseRepo.Owner
|
|
|
|
}
|
|
|
|
pr.Merger = merger
|
|
|
|
pr.MergerID = merger.ID
|
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
if merged, err := pr.SetMerged(ctx); err != nil {
|
2019-12-07 08:14:10 +05:30
|
|
|
log.Error("PullRequest[%d].setMerged : %v", pr.ID, err)
|
|
|
|
return false
|
2020-02-10 04:39:31 +05:30
|
|
|
} else if !merged {
|
|
|
|
return false
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
2019-12-16 03:27:34 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyMergePullRequest(ctx, merger, pr)
|
2019-12-16 03:27:34 +05:30
|
|
|
|
2019-12-07 08:14:10 +05:30
|
|
|
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 04:49:58 +05:30
|
|
|
// InitializePullRequests checks and tests untested patches of pull requests.
|
|
|
|
func InitializePullRequests(ctx context.Context) {
|
2022-06-13 15:07:59 +05:30
|
|
|
prs, err := issues_model.GetPullRequestIDsByCheckStatus(issues_model.PullRequestStatusChecking)
|
2020-02-03 04:49:58 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Find Checking PRs: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, prID := range prs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-12-15 15:21:28 +05:30
|
|
|
return
|
2020-02-03 04:49:58 +05:30
|
|
|
default:
|
2022-05-02 05:24:44 +05:30
|
|
|
if err := prPatchCheckerQueue.PushFunc(strconv.FormatInt(prID, 10), func() error {
|
2020-02-03 04:49:58 +05:30
|
|
|
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 15:21:28 +05:30
|
|
|
}
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
2020-02-03 04:49:58 +05:30
|
|
|
}
|
|
|
|
}
|
2019-12-07 08:14:10 +05:30
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// handle passed PR IDs and test the PRs
|
2022-01-23 02:52:14 +05:30
|
|
|
func handle(data ...queue.Data) []queue.Data {
|
2020-02-03 04:49:58 +05:30
|
|
|
for _, datum := range data {
|
2020-12-25 15:29:32 +05:30
|
|
|
id, _ := strconv.ParseInt(datum.(string), 10, 64)
|
2019-12-15 15:21:28 +05:30
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
testPR(id)
|
|
|
|
}
|
2022-01-23 02:52:14 +05:30
|
|
|
return nil
|
2022-01-20 04:56:57 +05:30
|
|
|
}
|
2019-12-15 15:21:28 +05:30
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
func testPR(id int64) {
|
2022-05-04 21:36:23 +05:30
|
|
|
pullWorkingPool.CheckIn(fmt.Sprint(id))
|
|
|
|
defer pullWorkingPool.CheckOut(fmt.Sprint(id))
|
2022-01-20 04:56:57 +05:30
|
|
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Test PR[%d] from patch checking queue", id))
|
|
|
|
defer finished()
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
pr, err := issues_model.GetPullRequestByID(ctx, id)
|
2022-01-20 04:56:57 +05:30
|
|
|
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)
|
2022-06-13 15:07:59 +05:30
|
|
|
pr.Status = issues_model.PullRequestStatusError
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := pr.UpdateCols("status"); err != nil {
|
|
|
|
log.Error("update pr [%d] status to PullRequestStatusError failed: %v", pr.ID, err)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
return
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
checkAndUpdateStatus(ctx, pr)
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|
|
|
|
|
2023-01-16 13:30:22 +05:30
|
|
|
// CheckPRsForBaseBranch check all pulls with baseBrannch
|
|
|
|
func CheckPRsForBaseBranch(baseRepo *repo_model.Repository, baseBranchName string) error {
|
2022-06-13 15:07:59 +05:30
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(baseRepo.ID, baseBranchName)
|
2020-10-14 00:20:57 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
AddToTaskQueue(pr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-07 08:14:10 +05:30
|
|
|
// Init runs the task queue to test all the checking status pull requests
|
2020-02-03 04:49:58 +05:30
|
|
|
func Init() error {
|
2022-05-02 05:24:44 +05:30
|
|
|
prPatchCheckerQueue = queue.CreateUniqueQueue("pr_patch_checker", handle, "")
|
2020-02-03 04:49:58 +05:30
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
if prPatchCheckerQueue == nil {
|
2020-02-03 04:49:58 +05:30
|
|
|
return fmt.Errorf("Unable to create pr_patch_checker Queue")
|
|
|
|
}
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
go graceful.GetManager().RunWithShutdownFns(prPatchCheckerQueue.Run)
|
2020-02-03 04:49:58 +05:30
|
|
|
go graceful.GetManager().RunWithShutdownContext(InitializePullRequests)
|
|
|
|
return nil
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|