mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
@ -21,6 +21,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/notification"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/queue"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
@ -69,7 +70,7 @@ func checkAndUpdateStatus(pr *models.PullRequest) {
|
||||
|
||||
// getMergeCommit checks if a pull request got merged
|
||||
// Returns the git.Commit of the pull request if merged
|
||||
func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
|
||||
func getMergeCommit(ctx context.Context, pr *models.PullRequest) (*git.Commit, error) {
|
||||
if pr.BaseRepo == nil {
|
||||
var err error
|
||||
pr.BaseRepo, err = repo_model.GetRepositoryByID(pr.BaseRepoID)
|
||||
@ -91,7 +92,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
|
||||
headFile := pr.GetGitRefName()
|
||||
|
||||
// Check if a pull request is merged into BaseBranch
|
||||
_, err = git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch).
|
||||
_, err = git.NewCommandContext(ctx, "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
|
||||
@ -112,7 +113,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
|
||||
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).
|
||||
mergeCommit, err := git.NewCommandContext(ctx, "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)
|
||||
@ -121,7 +122,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
|
||||
mergeCommit = commitID[:40]
|
||||
}
|
||||
|
||||
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
|
||||
gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("OpenRepository: %v", err)
|
||||
}
|
||||
@ -137,7 +138,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
|
||||
|
||||
// manuallyMerged checks if a pull request got manually merged
|
||||
// When a pull request got manually merged mark the pull request as merged
|
||||
func manuallyMerged(pr *models.PullRequest) bool {
|
||||
func manuallyMerged(ctx context.Context, pr *models.PullRequest) bool {
|
||||
if err := pr.LoadBaseRepo(); err != nil {
|
||||
log.Error("PullRequest[%d].LoadBaseRepo: %v", pr.ID, err)
|
||||
return false
|
||||
@ -153,7 +154,7 @@ func manuallyMerged(pr *models.PullRequest) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
commit, err := getMergeCommit(pr)
|
||||
commit, err := getMergeCommit(ctx, pr)
|
||||
if err != nil {
|
||||
log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err)
|
||||
return false
|
||||
@ -219,28 +220,39 @@ func handle(data ...queue.Data) {
|
||||
for _, datum := range data {
|
||||
id, _ := strconv.ParseInt(datum.(string), 10, 64)
|
||||
|
||||
log.Trace("Testing PR ID %d from the pull requests patch checking queue", id)
|
||||
|
||||
pr, err := models.GetPullRequestByID(id)
|
||||
if err != nil {
|
||||
log.Error("GetPullRequestByID[%s]: %v", datum, err)
|
||||
continue
|
||||
} else if pr.HasMerged {
|
||||
continue
|
||||
} else if manuallyMerged(pr) {
|
||||
continue
|
||||
} else 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)
|
||||
}
|
||||
continue
|
||||
}
|
||||
checkAndUpdateStatus(pr)
|
||||
testPR(id)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
return
|
||||
}
|
||||
checkAndUpdateStatus(pr)
|
||||
}
|
||||
|
||||
// CheckPrsForBaseBranch check all pulls with bseBrannch
|
||||
func CheckPrsForBaseBranch(baseRepo *repo_model.Repository, baseBranchName string) error {
|
||||
prs, err := models.GetUnmergedPullRequestsByBaseInfo(baseRepo.ID, baseBranchName)
|
||||
|
Reference in New Issue
Block a user