mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-25 14:00:02 +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>
106 lines
3.4 KiB
Go
106 lines
3.4 KiB
Go
// Copyright 2022 Gitea. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
repo_model "forgejo.org/models/repo"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/timeutil"
|
|
)
|
|
|
|
// AutoMerge represents a pull request scheduled for merging when checks succeed
|
|
type AutoMerge struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
PullID int64 `xorm:"UNIQUE"`
|
|
DoerID int64 `xorm:"INDEX NOT NULL"`
|
|
Doer *user_model.User `xorm:"-"`
|
|
MergeStyle repo_model.MergeStyle `xorm:"varchar(30)"`
|
|
Message string `xorm:"LONGTEXT"`
|
|
DeleteBranchAfterMerge bool `xorm:"NOT NULL DEFAULT false"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
}
|
|
|
|
// TableName return database table name for xorm
|
|
func (AutoMerge) TableName() string {
|
|
return "pull_auto_merge"
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(AutoMerge))
|
|
}
|
|
|
|
// ErrAlreadyScheduledToAutoMerge represents a "PullRequestHasMerged"-error
|
|
type ErrAlreadyScheduledToAutoMerge struct {
|
|
PullID int64
|
|
}
|
|
|
|
func (err ErrAlreadyScheduledToAutoMerge) Error() string {
|
|
return fmt.Sprintf("pull request is already scheduled to auto merge when checks succeed [pull_id: %d]", err.PullID)
|
|
}
|
|
|
|
// IsErrAlreadyScheduledToAutoMerge checks if an error is a ErrAlreadyScheduledToAutoMerge.
|
|
func IsErrAlreadyScheduledToAutoMerge(err error) bool {
|
|
_, ok := err.(ErrAlreadyScheduledToAutoMerge)
|
|
return ok
|
|
}
|
|
|
|
// ScheduleAutoMerge schedules a pull request to be merged when all checks succeed
|
|
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pullID int64, style repo_model.MergeStyle, message string, deleteBranch bool) error {
|
|
// Check if we already have a merge scheduled for that pull request
|
|
if exists, _, err := GetScheduledMergeByPullID(ctx, pullID); err != nil {
|
|
return err
|
|
} else if exists {
|
|
return ErrAlreadyScheduledToAutoMerge{PullID: pullID}
|
|
}
|
|
|
|
scheduledPRM, err := db.GetEngine(ctx).Insert(&AutoMerge{
|
|
DoerID: doer.ID,
|
|
PullID: pullID,
|
|
MergeStyle: style,
|
|
Message: message,
|
|
DeleteBranchAfterMerge: deleteBranch,
|
|
})
|
|
log.Trace("ScheduleAutoMerge %+v for PR %d", scheduledPRM, pullID)
|
|
|
|
return err
|
|
}
|
|
|
|
// GetScheduledMergeByPullID gets a scheduled pull request merge by pull request id
|
|
func GetScheduledMergeByPullID(ctx context.Context, pullID int64) (bool, *AutoMerge, error) {
|
|
scheduledPRM := &AutoMerge{}
|
|
exists, err := db.GetEngine(ctx).Where("pull_id = ?", pullID).Get(scheduledPRM)
|
|
if err != nil || !exists {
|
|
return false, nil, err
|
|
}
|
|
|
|
doer, err := user_model.GetPossibleUserByID(ctx, scheduledPRM.DoerID)
|
|
if err != nil {
|
|
return false, nil, err
|
|
}
|
|
|
|
log.Trace("GetScheduledMergeByPullID found %+v for PR %d", scheduledPRM, pullID)
|
|
|
|
scheduledPRM.Doer = doer
|
|
return true, scheduledPRM, nil
|
|
}
|
|
|
|
// DeleteScheduledAutoMerge delete a scheduled pull request
|
|
func DeleteScheduledAutoMerge(ctx context.Context, pullID int64) error {
|
|
exist, scheduledPRM, err := GetScheduledMergeByPullID(ctx, pullID)
|
|
if err != nil {
|
|
return err
|
|
} else if !exist {
|
|
return db.ErrNotExist{Resource: "auto_merge", ID: pullID}
|
|
}
|
|
|
|
log.Trace("DeleteScheduledAutoMerge %+v for PR %d", scheduledPRM, pullID)
|
|
|
|
_, err = db.GetEngine(ctx).ID(scheduledPRM.ID).Delete(&AutoMerge{})
|
|
return err
|
|
}
|