code.gitea.io/gitea@v1.22.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  	"testing"
     8  
     9  	"code.gitea.io/gitea/modules/git"
    10  	api "code.gitea.io/gitea/modules/structs"
    11  	webhook_module "code.gitea.io/gitea/modules/webhook"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestDetectMatched(t *testing.T) {
    17  	testCases := []struct {
    18  		desc         string
    19  		commit       *git.Commit
    20  		triggedEvent webhook_module.HookEventType
    21  		payload      api.Payloader
    22  		yamlOn       string
    23  		expected     bool
    24  	}{
    25  		{
    26  			desc:         "HookEventCreate(create) matches GithubEventCreate(create)",
    27  			triggedEvent: webhook_module.HookEventCreate,
    28  			payload:      nil,
    29  			yamlOn:       "on: create",
    30  			expected:     true,
    31  		},
    32  		{
    33  			desc:         "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
    34  			triggedEvent: webhook_module.HookEventIssues,
    35  			payload:      &api.IssuePayload{Action: api.HookIssueOpened},
    36  			yamlOn:       "on: issues",
    37  			expected:     true,
    38  		},
    39  		{
    40  			desc:         "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
    41  			triggedEvent: webhook_module.HookEventIssues,
    42  			payload:      &api.IssuePayload{Action: api.HookIssueMilestoned},
    43  			yamlOn:       "on: issues",
    44  			expected:     true,
    45  		},
    46  		{
    47  			desc:         "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
    48  			triggedEvent: webhook_module.HookEventPullRequestSync,
    49  			payload:      &api.PullRequestPayload{Action: api.HookIssueSynchronized},
    50  			yamlOn:       "on: pull_request",
    51  			expected:     true,
    52  		},
    53  		{
    54  			desc:         "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
    55  			triggedEvent: webhook_module.HookEventPullRequest,
    56  			payload:      &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
    57  			yamlOn:       "on: pull_request",
    58  			expected:     false,
    59  		},
    60  		{
    61  			desc:         "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
    62  			triggedEvent: webhook_module.HookEventPullRequest,
    63  			payload:      &api.PullRequestPayload{Action: api.HookIssueClosed},
    64  			yamlOn:       "on: pull_request",
    65  			expected:     false,
    66  		},
    67  		{
    68  			desc:         "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
    69  			triggedEvent: webhook_module.HookEventPullRequest,
    70  			payload: &api.PullRequestPayload{
    71  				Action: api.HookIssueClosed,
    72  				PullRequest: &api.PullRequest{
    73  					Base: &api.PRBranchInfo{},
    74  				},
    75  			},
    76  			yamlOn:   "on:\n  pull_request:\n    branches: [main]",
    77  			expected: false,
    78  		},
    79  		{
    80  			desc:         "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
    81  			triggedEvent: webhook_module.HookEventPullRequest,
    82  			payload:      &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
    83  			yamlOn:       "on:\n  pull_request:\n    types: [labeled]",
    84  			expected:     true,
    85  		},
    86  		{
    87  			desc:         "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
    88  			triggedEvent: webhook_module.HookEventPullRequestReviewComment,
    89  			payload:      &api.PullRequestPayload{Action: api.HookIssueReviewed},
    90  			yamlOn:       "on:\n  pull_request_review_comment:\n    types: [created]",
    91  			expected:     true,
    92  		},
    93  		{
    94  			desc:         "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
    95  			triggedEvent: webhook_module.HookEventPullRequestReviewRejected,
    96  			payload:      &api.PullRequestPayload{Action: api.HookIssueReviewed},
    97  			yamlOn:       "on:\n  pull_request_review:\n    types: [dismissed]",
    98  			expected:     false,
    99  		},
   100  		{
   101  			desc:         "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
   102  			triggedEvent: webhook_module.HookEventRelease,
   103  			payload:      &api.ReleasePayload{Action: api.HookReleasePublished},
   104  			yamlOn:       "on:\n  release:\n    types: [published]",
   105  			expected:     true,
   106  		},
   107  		{
   108  			desc:         "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
   109  			triggedEvent: webhook_module.HookEventPackage,
   110  			payload:      &api.PackagePayload{Action: api.HookPackageCreated},
   111  			yamlOn:       "on:\n  registry_package:\n    types: [updated]",
   112  			expected:     false,
   113  		},
   114  		{
   115  			desc:         "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
   116  			triggedEvent: webhook_module.HookEventWiki,
   117  			payload:      nil,
   118  			yamlOn:       "on: gollum",
   119  			expected:     true,
   120  		},
   121  		{
   122  			desc:         "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)",
   123  			triggedEvent: webhook_module.HookEventSchedule,
   124  			payload:      nil,
   125  			yamlOn:       "on: schedule",
   126  			expected:     true,
   127  		},
   128  	}
   129  
   130  	for _, tc := range testCases {
   131  		t.Run(tc.desc, func(t *testing.T) {
   132  			evts, err := GetEventsFromContent([]byte(tc.yamlOn))
   133  			assert.NoError(t, err)
   134  			assert.Len(t, evts, 1)
   135  			assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggedEvent, tc.payload, evts[0]))
   136  		})
   137  	}
   138  }