mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-07-07 07:00:01 +02:00

Automerge can be ignored when the following race happens: * Conflict check is happening on a repository and `pr.Status = issues_model.PullRequestStatusChecking` for all open pull requests (this happens every time a pull request is merged). * While the conflict check is ongoing, an event (Forgejo Actions being successful for instance) happens and and `StartPRCheckAndAutoMerge*` is called. * Because `pr.CanAutoMerge()` is false, the pull request is not selected and not added to the automerge queue. * When the conflict check completes and `pr.CanAutoMerge()` becomes true, there no longer is a task in the auto merge queue and the auto merge does not happen. This is fixed by adding a task to the auto merge queue when the conflict check for a pull request completes. This is done when the mutx protecting the conflict check task is released to prevent a deadlock when a synchronous queues are used in the following situation: * the conflict check task finds the pull request is mergeable * it schedules the auto merge tasks that finds it must be merged * merging concludes with scheduling a conflict check task Avoid an extra loop where a conflict check task queues an auto merge task that will schedule a conflict check task if the pull request can be merged. The auto merge row is removed from the database before merging. It would otherwise be removed after the merge commit is received via the git hook which happens asynchronously and can lead to a race. StartPRCheckAndAutoMerge is modified to re-use HeadCommitID when available, such as when called after a pull request conflict check. --- A note on tests: they cover the new behavior, i.e. automerge being triggered by a successful conflict check. This is also on the critical paths for every test that involve creating, merging or updating a pull request. - `tests/integration/git_test.go` - `tests/integration/actions_commit_status_test.go` - `tests/integration/api_helper_for_declarative_test.go` - `tests/integration/patch_status_test.go` - `tests/integration/pull_merge_test.go` The [missing fixture file](https://codeberg.org/forgejo/forgejo/pulls/8189/files#diff-b86fdd79108b3ba3cb2e56ffcfd1be2a7b32f46c) for the auto merge table can be verified to be necessary simply by removing it an observing that the integration tests fail. The [scheduling of the auto merge task](https://codeberg.org/forgejo/forgejo/pulls/8189/files#diff-9489262e93967f6bb2db41837f37c06f4e70d978) in `testPR` can be verified to be required by moving it in the `testPRProtected` function and observing that the tests hang forever because of the deadlock. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/8189): <!--number 8189 --><!--line 0 --><!--description ZG8gbm90IGlnbm9yZSBhdXRvbWVyZ2Ugd2hpbGUgYSBQUiBpcyBjaGVja2luZyBmb3IgY29uZmxpY3Rz-->do not ignore automerge while a PR is checking for conflicts<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8189 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Lucas <sclu1034@noreply.codeberg.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
138 lines
3.6 KiB
Go
138 lines
3.6 KiB
Go
// Copyright 2021 Gitea. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package automerge
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
issues_model "forgejo.org/models/issues"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/modules/git"
|
|
"forgejo.org/modules/gitrepo"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/queue"
|
|
)
|
|
|
|
// PRAutoMergeQueue represents a queue to handle update pull request tests
|
|
var PRAutoMergeQueue *queue.WorkerPoolQueue[string]
|
|
|
|
func addToQueue(pr *issues_model.PullRequest, sha string) {
|
|
log.Trace("Adding pullID: %d to the automerge queue with sha %s", pr.ID, sha)
|
|
if err := PRAutoMergeQueue.Push(fmt.Sprintf("%d_%s", pr.ID, sha)); err != nil {
|
|
log.Error("Error adding pullID: %d to the automerge queue %v", pr.ID, err)
|
|
}
|
|
}
|
|
|
|
// StartPRCheckAndAutoMergeBySHA start an automerge check and auto merge task for all pull requests of repository and SHA
|
|
func StartPRCheckAndAutoMergeBySHA(ctx context.Context, sha string, repo *repo_model.Repository) error {
|
|
pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool {
|
|
return !pr.HasMerged && pr.CanAutoMerge()
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pr := range pulls {
|
|
addToQueue(pr, sha)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func StartPRCheckAndAutoMerge(ctx context.Context, pull *issues_model.PullRequest) {
|
|
if pull == nil || pull.HasMerged || !pull.CanAutoMerge() {
|
|
return
|
|
}
|
|
|
|
commitID := pull.HeadCommitID
|
|
if commitID == "" {
|
|
commitID = getCommitIDFromRefName(ctx, pull)
|
|
}
|
|
|
|
if commitID == "" {
|
|
return
|
|
}
|
|
|
|
addToQueue(pull, commitID)
|
|
}
|
|
|
|
var AddToQueueIfMergeable = func(ctx context.Context, pull *issues_model.PullRequest) {
|
|
if pull.Status == issues_model.PullRequestStatusMergeable {
|
|
StartPRCheckAndAutoMerge(ctx, pull)
|
|
}
|
|
}
|
|
|
|
func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) {
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
refs, err := gitRepo.GetRefsBySha(sha, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pulls := make(map[int64]*issues_model.PullRequest)
|
|
|
|
for _, ref := range refs {
|
|
// Each pull branch starts with refs/pull/ we then go from there to find the index of the pr and then
|
|
// use that to get the pr.
|
|
if strings.HasPrefix(ref, git.PullPrefix) {
|
|
parts := strings.Split(ref[len(git.PullPrefix):], "/")
|
|
|
|
// e.g. 'refs/pull/1/head' would be []string{"1", "head"}
|
|
if len(parts) != 2 {
|
|
log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo)
|
|
continue
|
|
}
|
|
|
|
prIndex, err := strconv.ParseInt(parts[0], 10, 64)
|
|
if err != nil {
|
|
log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo)
|
|
continue
|
|
}
|
|
|
|
p, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, prIndex)
|
|
if err != nil {
|
|
// If there is no pull request for this branch, we don't try to merge it.
|
|
if issues_model.IsErrPullRequestNotExist(err) {
|
|
continue
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if filter(p) {
|
|
pulls[p.ID] = p
|
|
}
|
|
}
|
|
}
|
|
|
|
return pulls, nil
|
|
}
|
|
|
|
func getCommitIDFromRefName(ctx context.Context, pull *issues_model.PullRequest) string {
|
|
if err := pull.LoadBaseRepo(ctx); err != nil {
|
|
log.Error("LoadBaseRepo: %v", err)
|
|
return ""
|
|
}
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo)
|
|
if err != nil {
|
|
log.Error("OpenRepository: %v", err)
|
|
return ""
|
|
}
|
|
defer gitRepo.Close()
|
|
commitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
|
|
if err != nil {
|
|
log.Error("GetRefCommitID: %v", err)
|
|
return ""
|
|
}
|
|
|
|
return commitID
|
|
}
|