mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-26 10:47:04 -06:00
Adds a merge queue for protected branches, modeled on GitHub's. Merging
into a branch that requires it queues the pull request, tests it with
the ones ahead of it in `merge_group` runs and fast-forwards the base
once the required checks pass. Failing or conflicting pull requests are
removed.
Rules configure merge method, group sizes, grouping and timeouts. Each
queue has a page at `/{owner}/{repo}/queue/{branch}`. The API can queue,
jump, look up and remove a pull request and list or clear a queue, and
webhooks and Actions get the `merge_group` event and
`enqueued`/`dequeued` actions. The merge box's command line hint now
sits beside the merge buttons like on GitHub, also outside of merge
queues.
One notable difference from GitHub is that Gitea allows merge queue to
be enabled in any repo, as opposed to GitHub which only allows it in
organization repos.
| View | Screenshot |
| --- | --- |
| Merge box, ready | <img width="440" alt="Merge box, ready"
src="https://github.com/user-attachments/assets/7d2ac984-90c4-4744-8ac8-5daf37cd96e9"
/> |
| Merge box, queued | <img width="440" alt="Merge box, queued"
src="https://github.com/user-attachments/assets/33b85cab-dfd6-405f-8ffd-283cd920e59f"
/> |
| Pull request header, queued | <img width="600" alt="Pull request
header, queued"
src="https://github.com/user-attachments/assets/d61c635b-8552-4b47-a900-e493912e8c16"
/> |
| Timeline, removal after failed checks | <img width="440"
alt="Timeline, removal after failed checks"
src="https://github.com/user-attachments/assets/bee95e79-4c76-4592-ad35-82109d9da4ed"
/> |
| Queue page | <img width="600" alt="Queue page"
src="https://github.com/user-attachments/assets/847a595a-1e13-4099-aa37-7f44018dc188"
/> |
| Pull request list, queued | <img width="600" alt="Pull request list,
queued"
src="https://github.com/user-attachments/assets/a382a143-a67f-4298-aa11-594e281d54db"
/> |
| Merge queue settings | <img width="600" alt="Merge queue settings"
src="https://github.com/user-attachments/assets/cdaf4aa5-350e-4570-a095-c91c41d03cde"
/> |
Fixes: https://github.com/go-gitea/gitea/issues/36392
Docs: https://gitea.com/gitea/docs/pulls/655
Assisted-by: Claude Code:claude-opus-5-5
---------
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
457 lines
15 KiB
Go
457 lines
15 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/setting"
|
|
api "gitea.dev/modules/structs"
|
|
"gitea.dev/modules/test"
|
|
webhook_module "gitea.dev/modules/webhook"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func fullWorkflowContent(part string) []byte {
|
|
return []byte(`
|
|
name: test
|
|
` + part + `
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: echo hello
|
|
`)
|
|
}
|
|
|
|
func TestReadWorkflowEventsStaticErrors(t *testing.T) {
|
|
for content, static := range map[string]bool{
|
|
"on: push\njobs: {}": true,
|
|
"on: push\njobs: {test: {runs-on: x, needs: absent}}": true,
|
|
"on: push\njobs: {one: {runs-on: x, needs: two}, two: {runs-on: x, needs: one}}": true,
|
|
"on: push\njobs: {test: {runs-on: x, strategy: {matrix: {os: []}}}}": true,
|
|
"on: push\nrun-name: ${{ secrets.TOKEN }}\njobs: {test: {runs-on: x}}": true,
|
|
"on: push\njobs: {test: {steps: [{run: echo}]}}": true,
|
|
"on: push\nrun-name: ${{ fromJSON(inputs.x) }}\njobs: {test: {runs-on: x, steps: [{run: echo}]}}": false,
|
|
} {
|
|
_, gotStatic, err := readWorkflowEvents([]byte(content))
|
|
require.Error(t, err, content)
|
|
assert.Equal(t, static, gotStatic, content)
|
|
}
|
|
for _, content := range []string{
|
|
"on: push\njobs: {test: {runs-on: x, steps: [{run: echo}]}}",
|
|
"on: push\nrun-name: ${{ github.ref }}\njobs: {test: {runs-on: x}}",
|
|
"on: push\njobs: {call: {uses: ./.gitea/workflows/called.yml}}",
|
|
} {
|
|
_, _, err := readWorkflowEvents([]byte(content))
|
|
assert.NoError(t, err, content)
|
|
}
|
|
}
|
|
|
|
func TestIsWorkflow(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.Actions.WorkflowDirs)()
|
|
|
|
tests := []struct {
|
|
name string
|
|
dirs []string
|
|
path string
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "default with yml extension",
|
|
dirs: []string{".gitea/workflows", ".github/workflows"},
|
|
path: ".gitea/workflows/test.yml",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "default with yaml extension",
|
|
dirs: []string{".gitea/workflows", ".github/workflows"},
|
|
path: ".github/workflows/test.yaml",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "only gitea configured, github path rejected",
|
|
dirs: []string{".gitea/workflows"},
|
|
path: ".github/workflows/test.yml",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "only github configured, gitea path rejected",
|
|
dirs: []string{".github/workflows"},
|
|
path: ".gitea/workflows/test.yml",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "custom workflow dir",
|
|
dirs: []string{".custom/workflows"},
|
|
path: ".custom/workflows/deploy.yml",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "non-workflow file",
|
|
dirs: []string{".gitea/workflows", ".github/workflows"},
|
|
path: ".gitea/workflows/readme.md",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "directory boundary",
|
|
dirs: []string{".gitea/workflows"},
|
|
path: ".gitea/workflows2/test.yml",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "unrelated path",
|
|
dirs: []string{".gitea/workflows", ".github/workflows"},
|
|
path: "src/main.go",
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
setting.Actions.WorkflowDirs = tt.dirs
|
|
assert.Equal(t, tt.expected, IsWorkflow(tt.path))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDetectMatched(t *testing.T) {
|
|
testCases := []struct {
|
|
desc string
|
|
commit *git.Commit
|
|
triggedEvent webhook_module.HookEventType
|
|
payload api.Payloader
|
|
yamlOn string
|
|
expected detectResult
|
|
}{
|
|
{
|
|
desc: "HookEventCreate(create) matches GithubEventCreate(create)",
|
|
triggedEvent: webhook_module.HookEventCreate,
|
|
payload: nil,
|
|
yamlOn: "on: create",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
|
|
triggedEvent: webhook_module.HookEventIssues,
|
|
payload: &api.IssuePayload{Action: api.HookIssueOpened},
|
|
yamlOn: "on: issues",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
|
|
triggedEvent: webhook_module.HookEventIssues,
|
|
payload: &api.IssuePayload{Action: api.HookIssueMilestoned},
|
|
yamlOn: "on: issues",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
|
|
triggedEvent: webhook_module.HookEventPullRequestSync,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized},
|
|
yamlOn: "on: pull_request",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
|
|
triggedEvent: webhook_module.HookEventPullRequest,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
|
|
yamlOn: "on: pull_request",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
|
|
triggedEvent: webhook_module.HookEventPullRequest,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
|
|
yamlOn: "on: pull_request",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
|
|
triggedEvent: webhook_module.HookEventPullRequest,
|
|
payload: &api.PullRequestPayload{
|
|
Action: api.HookIssueClosed,
|
|
PullRequest: &api.PullRequest{
|
|
Base: &api.PRBranchInfo{},
|
|
},
|
|
},
|
|
yamlOn: "on:\n pull_request:\n branches: [main]",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
|
|
triggedEvent: webhook_module.HookEventPullRequest,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated, Changes: &api.ChangesPayload{AddedLabels: []*api.Label{{ID: 1, Name: "label"}}}},
|
|
yamlOn: "on:\n pull_request:\n types: [labeled]",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequestLabel(pull_request_label) label removal doesn't match GithubEventPullRequest(pull_request) with `labeled` activity type",
|
|
triggedEvent: webhook_module.HookEventPullRequestLabel,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated, Changes: &api.ChangesPayload{RemovedLabels: []*api.Label{{ID: 1}}}},
|
|
yamlOn: "on:\n pull_request:\n types: [labeled]",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
|
|
triggedEvent: webhook_module.HookEventPullRequestReviewComment,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
|
|
yamlOn: "on:\n pull_request_review_comment:\n types: [created]",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
|
|
triggedEvent: webhook_module.HookEventPullRequestReviewRejected,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
|
|
yamlOn: "on:\n pull_request_review:\n types: [dismissed]",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
|
|
triggedEvent: webhook_module.HookEventRelease,
|
|
payload: &api.ReleasePayload{Action: api.HookReleasePublished},
|
|
yamlOn: "on:\n release:\n types: [published]",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventRelease(release) doesn't match an invalid activity type pattern",
|
|
triggedEvent: webhook_module.HookEventRelease,
|
|
payload: &api.ReleasePayload{Action: api.HookReleasePublished},
|
|
yamlOn: "on:\n release:\n types: [\"[\"]",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
|
|
triggedEvent: webhook_module.HookEventPackage,
|
|
payload: &api.PackagePayload{Action: api.HookPackageCreated},
|
|
yamlOn: "on:\n registry_package:\n types: [updated]",
|
|
expected: detectNotApplicable,
|
|
},
|
|
{
|
|
desc: "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
|
|
triggedEvent: webhook_module.HookEventWiki,
|
|
payload: nil,
|
|
yamlOn: "on: gollum",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "HookEventSchedule(schedule) matches GithubEventSchedule(schedule)",
|
|
triggedEvent: webhook_module.HookEventSchedule,
|
|
payload: nil,
|
|
yamlOn: "on: schedule",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "push to tag matches workflow with paths condition (should skip paths check)",
|
|
triggedEvent: webhook_module.HookEventPush,
|
|
payload: &api.PushPayload{
|
|
Ref: "refs/tags/v1.0.0",
|
|
Before: "0000000",
|
|
Commits: []*api.PayloadCommit{
|
|
{
|
|
ID: "abcdef123456",
|
|
Added: []string{"src/main.go"},
|
|
Message: "Release v1.0.0",
|
|
},
|
|
},
|
|
},
|
|
commit: nil,
|
|
yamlOn: "on:\n push:\n paths:\n - src/**",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "push branch filter excludes -> filtered out",
|
|
triggedEvent: webhook_module.HookEventPush,
|
|
payload: &api.PushPayload{
|
|
Ref: "refs/heads/feature/x",
|
|
Before: "0000000",
|
|
Commits: []*api.PayloadCommit{{ID: "abc", Added: []string{"a.go"}, Message: "x"}},
|
|
},
|
|
commit: nil,
|
|
yamlOn: "on:\n push:\n branches: [main]",
|
|
expected: detectFilteredOut,
|
|
},
|
|
{
|
|
desc: "merge_group checks_requested matches the base branch with default activity types",
|
|
triggedEvent: webhook_module.HookEventMergeGroup,
|
|
payload: &api.MergeGroupPayload{Action: api.HookMergeGroupChecksRequested, MergeGroup: &api.MergeGroup{BaseRef: "refs/heads/main"}},
|
|
yamlOn: "on: {merge_group: {branches: [main]}}",
|
|
expected: detectMatched,
|
|
},
|
|
{
|
|
desc: "merge_group matches the activity type but ignores the base branch",
|
|
triggedEvent: webhook_module.HookEventMergeGroup,
|
|
payload: &api.MergeGroupPayload{Action: api.HookMergeGroupChecksRequested, MergeGroup: &api.MergeGroup{BaseRef: "refs/heads/main"}},
|
|
yamlOn: "on: {merge_group: {types: [checks_requested], branches-ignore: [main]}}",
|
|
expected: detectFilteredOut,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
evts, err := GetEventsFromContent(fullWorkflowContent(tc.yamlOn))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, evts, 1)
|
|
assert.Equal(t, tc.expected, detectWorkflowMatch(t.Context(), nil, tc.commit, tc.triggedEvent, tc.payload, evts[0]))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMatchIssuesEvent(t *testing.T) {
|
|
labelChangesPayload := func() *api.IssuePayload {
|
|
return &api.IssuePayload{
|
|
Action: api.HookIssueLabelUpdated,
|
|
Issue: &api.Issue{
|
|
Labels: []*api.Label{
|
|
{ID: 789, Name: "new-label"},
|
|
},
|
|
},
|
|
Changes: &api.ChangesPayload{
|
|
AddedLabels: []*api.Label{
|
|
{ID: 789, Name: "new-label"},
|
|
},
|
|
RemovedLabels: []*api.Label{
|
|
{ID: 123, Name: "deleted-label"},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
testCases := []struct {
|
|
desc string
|
|
payload *api.IssuePayload
|
|
yamlOn string
|
|
expected bool
|
|
eventType string
|
|
}{
|
|
{
|
|
desc: "Label deletion should trigger unlabeled event",
|
|
payload: &api.IssuePayload{
|
|
Action: api.HookIssueLabelUpdated,
|
|
Issue: &api.Issue{
|
|
Labels: []*api.Label{},
|
|
},
|
|
Changes: &api.ChangesPayload{
|
|
RemovedLabels: []*api.Label{
|
|
{ID: 123, Name: "deleted-label"},
|
|
},
|
|
},
|
|
},
|
|
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
|
expected: true,
|
|
eventType: "unlabeled",
|
|
},
|
|
{
|
|
desc: "Label deletion with existing labels should trigger unlabeled event",
|
|
payload: &api.IssuePayload{
|
|
Action: api.HookIssueLabelUpdated,
|
|
Issue: &api.Issue{
|
|
Labels: []*api.Label{
|
|
{ID: 456, Name: "existing-label"},
|
|
},
|
|
},
|
|
Changes: &api.ChangesPayload{
|
|
AddedLabels: nil,
|
|
RemovedLabels: []*api.Label{
|
|
{ID: 123, Name: "deleted-label"},
|
|
},
|
|
},
|
|
},
|
|
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
|
expected: true,
|
|
eventType: "unlabeled",
|
|
},
|
|
{
|
|
desc: "Label addition should trigger labeled event",
|
|
payload: &api.IssuePayload{
|
|
Action: api.HookIssueLabelUpdated,
|
|
Issue: &api.Issue{
|
|
Labels: []*api.Label{
|
|
{ID: 123, Name: "new-label"},
|
|
},
|
|
},
|
|
Changes: &api.ChangesPayload{
|
|
AddedLabels: []*api.Label{
|
|
{ID: 123, Name: "new-label"},
|
|
},
|
|
RemovedLabels: []*api.Label{}, // Empty array, no labels removed
|
|
},
|
|
},
|
|
yamlOn: "on:\n issues:\n types: [labeled]",
|
|
expected: true,
|
|
eventType: "labeled",
|
|
},
|
|
{
|
|
desc: "Label clear should trigger unlabeled event",
|
|
payload: &api.IssuePayload{
|
|
Action: api.HookIssueLabelCleared,
|
|
Issue: &api.Issue{
|
|
Labels: []*api.Label{},
|
|
},
|
|
},
|
|
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
|
expected: true,
|
|
eventType: "unlabeled",
|
|
},
|
|
{
|
|
desc: "Both adding and removing labels should trigger labeled event",
|
|
payload: labelChangesPayload(),
|
|
yamlOn: "on:\n issues:\n types: [labeled]",
|
|
expected: true,
|
|
eventType: "labeled",
|
|
},
|
|
{
|
|
desc: "Both adding and removing labels should trigger unlabeled event",
|
|
payload: labelChangesPayload(),
|
|
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
|
expected: true,
|
|
eventType: "unlabeled",
|
|
},
|
|
{
|
|
desc: "Both adding and removing labels should trigger both events",
|
|
payload: labelChangesPayload(),
|
|
yamlOn: "on:\n issues:\n types: [labeled, unlabeled]",
|
|
expected: true,
|
|
eventType: "multiple",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
evts, err := GetEventsFromContent(fullWorkflowContent(tc.yamlOn))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, evts, 1)
|
|
|
|
// Test if the event matches as expected
|
|
assert.Equal(t, tc.expected, matchIssuesEvent(tc.payload, evts[0]))
|
|
|
|
// For extra validation, check that action mapping works correctly
|
|
if tc.eventType == "multiple" {
|
|
// Skip direct action mapping validation for multiple events case
|
|
// as one action can map to multiple event types
|
|
return
|
|
}
|
|
|
|
// Determine expected action for single event case
|
|
var expectedAction string
|
|
switch tc.payload.Action {
|
|
case api.HookIssueLabelUpdated:
|
|
if tc.eventType == "labeled" {
|
|
expectedAction = "labeled"
|
|
} else if tc.eventType == "unlabeled" {
|
|
expectedAction = "unlabeled"
|
|
}
|
|
case api.HookIssueLabelCleared:
|
|
expectedAction = "unlabeled"
|
|
default:
|
|
expectedAction = string(tc.payload.Action)
|
|
}
|
|
|
|
assert.Equal(t, expectedAction, tc.eventType, "Event type should match expected")
|
|
})
|
|
}
|
|
}
|