mirror of
https://github.com/go-gitea/gitea
synced 2024-12-27 23:04:30 +01:00
2d91afaa92
Some workflow trigger events can have multiple activity types, such as `issues` and `pull_request`, and user can specify which types can trigger the workflow. See GitHub documentation: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows Now some hook events cannot match the workflow trigger events correctly because we don't check the activity types. For example, `pull_request_label` is an individual hook event. But there isn't a `pull_request_label` workflow trigger event, we can only use `pull_request` event's `label` activity type. If we don't check the activity types, the workflows without the `label` activity type may be triggered by the `pull_request_label` event by mistake. We need to improve the match logic. - [x] [`issues` ](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues) - [x] [`issue_comment`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment) - [x] [`pull_request`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request) - [x] [`pull_request_review`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review) - [x] [`pull_request_review_comment`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment) - [x] [`release`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#release) - [x] [`registry_package`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#registry_package)
105 lines
4.1 KiB
Go
105 lines
4.1 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDetectMatched(t *testing.T) {
|
|
testCases := []struct {
|
|
desc string
|
|
commit *git.Commit
|
|
triggedEvent webhook_module.HookEventType
|
|
payload api.Payloader
|
|
yamlOn string
|
|
expected bool
|
|
}{
|
|
{
|
|
desc: "HookEventCreate(create) matches githubEventCreate(create)",
|
|
triggedEvent: webhook_module.HookEventCreate,
|
|
payload: nil,
|
|
yamlOn: "on: create",
|
|
expected: true,
|
|
},
|
|
{
|
|
desc: "HookEventIssues(issues) `opened` action matches githubEventIssues(issues)",
|
|
triggedEvent: webhook_module.HookEventIssues,
|
|
payload: &api.IssuePayload{Action: api.HookIssueOpened},
|
|
yamlOn: "on: issues",
|
|
expected: true,
|
|
},
|
|
{
|
|
desc: "HookEventIssues(issues) `milestoned` action matches githubEventIssues(issues)",
|
|
triggedEvent: webhook_module.HookEventIssues,
|
|
payload: &api.IssuePayload{Action: api.HookIssueMilestoned},
|
|
yamlOn: "on: issues",
|
|
expected: true,
|
|
},
|
|
{
|
|
desc: "HookEventPullRequestSync(pull_request_sync) matches githubEventPullRequest(pull_request)",
|
|
triggedEvent: webhook_module.HookEventPullRequestSync,
|
|
payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized},
|
|
yamlOn: "on: pull_request",
|
|
expected: true,
|
|
},
|
|
{
|
|
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: false,
|
|
},
|
|
{
|
|
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},
|
|
yamlOn: "on:\n pull_request:\n types: [labeled]",
|
|
expected: true,
|
|
},
|
|
{
|
|
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: true,
|
|
},
|
|
{
|
|
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: false,
|
|
},
|
|
{
|
|
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: true,
|
|
},
|
|
{
|
|
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: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
evts, err := GetEventsFromContent([]byte(tc.yamlOn))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, evts, 1)
|
|
assert.Equal(t, tc.expected, detectMatched(tc.commit, tc.triggedEvent, tc.payload, evts[0]))
|
|
})
|
|
}
|
|
}
|