code.gitea.io/gitea@v1.19.3/modules/actions/workflows_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package actions 5 6 import ( 7 "bytes" 8 "testing" 9 10 "code.gitea.io/gitea/modules/git" 11 api "code.gitea.io/gitea/modules/structs" 12 webhook_module "code.gitea.io/gitea/modules/webhook" 13 14 "github.com/nektos/act/pkg/jobparser" 15 "github.com/nektos/act/pkg/model" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestDetectMatched(t *testing.T) { 20 testCases := []struct { 21 desc string 22 commit *git.Commit 23 triggedEvent webhook_module.HookEventType 24 payload api.Payloader 25 yamlOn string 26 expected bool 27 }{ 28 { 29 desc: "HookEventCreate(create) matches githubEventCreate(create)", 30 triggedEvent: webhook_module.HookEventCreate, 31 payload: nil, 32 yamlOn: "on: create", 33 expected: true, 34 }, 35 { 36 desc: "HookEventIssues(issues) `opened` action matches githubEventIssues(issues)", 37 triggedEvent: webhook_module.HookEventIssues, 38 payload: &api.IssuePayload{Action: api.HookIssueOpened}, 39 yamlOn: "on: issues", 40 expected: true, 41 }, 42 { 43 desc: "HookEventIssues(issues) `milestoned` action matches githubEventIssues(issues)", 44 triggedEvent: webhook_module.HookEventIssues, 45 payload: &api.IssuePayload{Action: api.HookIssueMilestoned}, 46 yamlOn: "on: issues", 47 expected: true, 48 }, 49 { 50 desc: "HookEventPullRequestSync(pull_request_sync) matches githubEventPullRequest(pull_request)", 51 triggedEvent: webhook_module.HookEventPullRequestSync, 52 payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized}, 53 yamlOn: "on: pull_request", 54 expected: true, 55 }, 56 { 57 desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match githubEventPullRequest(pull_request) with no activity type", 58 triggedEvent: webhook_module.HookEventPullRequest, 59 payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, 60 yamlOn: "on: pull_request", 61 expected: false, 62 }, 63 { 64 desc: "HookEventPullRequest(pull_request) `label_updated` action matches githubEventPullRequest(pull_request) with `label` activity type", 65 triggedEvent: webhook_module.HookEventPullRequest, 66 payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, 67 yamlOn: "on:\n pull_request:\n types: [labeled]", 68 expected: true, 69 }, 70 { 71 desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches githubEventPullRequestReviewComment(pull_request_review_comment)", 72 triggedEvent: webhook_module.HookEventPullRequestReviewComment, 73 payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, 74 yamlOn: "on:\n pull_request_review_comment:\n types: [created]", 75 expected: true, 76 }, 77 { 78 desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match githubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)", 79 triggedEvent: webhook_module.HookEventPullRequestReviewRejected, 80 payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, 81 yamlOn: "on:\n pull_request_review:\n types: [dismissed]", 82 expected: false, 83 }, 84 { 85 desc: "HookEventRelease(release) `published` action matches githubEventRelease(release) with `published` activity type", 86 triggedEvent: webhook_module.HookEventRelease, 87 payload: &api.ReleasePayload{Action: api.HookReleasePublished}, 88 yamlOn: "on:\n release:\n types: [published]", 89 expected: true, 90 }, 91 { 92 desc: "HookEventPackage(package) `created` action doesn't match githubEventRegistryPackage(registry_package) with `updated` activity type", 93 triggedEvent: webhook_module.HookEventPackage, 94 payload: &api.PackagePayload{Action: api.HookPackageCreated}, 95 yamlOn: "on:\n registry_package:\n types: [updated]", 96 expected: false, 97 }, 98 } 99 100 for _, tc := range testCases { 101 t.Run(tc.desc, func(t *testing.T) { 102 workflow, err := model.ReadWorkflow(bytes.NewReader([]byte(tc.yamlOn))) 103 assert.NoError(t, err) 104 evts, err := jobparser.ParseRawOn(&workflow.RawOn) 105 assert.NoError(t, err) 106 assert.NoError(t, err) 107 assert.Len(t, evts, 1) 108 assert.Equal(t, tc.expected, detectMatched(tc.commit, tc.triggedEvent, tc.payload, evts[0])) 109 }) 110 } 111 }