code.gitea.io/gitea@v1.19.3/modules/actions/github_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  	webhook_module "code.gitea.io/gitea/modules/webhook"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCanGithubEventMatch(t *testing.T) {
    15  	testCases := []struct {
    16  		desc           string
    17  		eventName      string
    18  		triggeredEvent webhook_module.HookEventType
    19  		expected       bool
    20  	}{
    21  		// registry_package event
    22  		{
    23  			"registry_package matches",
    24  			githubEventRegistryPackage,
    25  			webhook_module.HookEventPackage,
    26  			true,
    27  		},
    28  		{
    29  			"registry_package cannot match",
    30  			githubEventRegistryPackage,
    31  			webhook_module.HookEventPush,
    32  			false,
    33  		},
    34  		// issues event
    35  		{
    36  			"issue matches",
    37  			githubEventIssues,
    38  			webhook_module.HookEventIssueLabel,
    39  			true,
    40  		},
    41  		{
    42  			"issue cannot match",
    43  			githubEventIssues,
    44  			webhook_module.HookEventIssueComment,
    45  			false,
    46  		},
    47  		// issue_comment event
    48  		{
    49  			"issue_comment matches",
    50  			githubEventIssueComment,
    51  			webhook_module.HookEventIssueComment,
    52  			true,
    53  		},
    54  		{
    55  			"issue_comment cannot match",
    56  			githubEventIssueComment,
    57  			webhook_module.HookEventIssues,
    58  			false,
    59  		},
    60  		// pull_request event
    61  		{
    62  			"pull_request matches",
    63  			githubEventPullRequest,
    64  			webhook_module.HookEventPullRequestSync,
    65  			true,
    66  		},
    67  		{
    68  			"pull_request cannot match",
    69  			githubEventPullRequest,
    70  			webhook_module.HookEventPullRequestComment,
    71  			false,
    72  		},
    73  		// pull_request_target event
    74  		{
    75  			"pull_request_target matches",
    76  			githubEventPullRequest,
    77  			webhook_module.HookEventPullRequest,
    78  			true,
    79  		},
    80  		{
    81  			"pull_request_target cannot match",
    82  			githubEventPullRequest,
    83  			webhook_module.HookEventPullRequestComment,
    84  			false,
    85  		},
    86  		// pull_request_review event
    87  		{
    88  			"pull_request_review matches",
    89  			githubEventPullRequestReview,
    90  			webhook_module.HookEventPullRequestReviewComment,
    91  			true,
    92  		},
    93  		{
    94  			"pull_request_review cannot match",
    95  			githubEventPullRequestReview,
    96  			webhook_module.HookEventPullRequestComment,
    97  			false,
    98  		},
    99  		// other events
   100  		{
   101  			"create event",
   102  			githubEventCreate,
   103  			webhook_module.HookEventCreate,
   104  			true,
   105  		},
   106  	}
   107  
   108  	for _, tc := range testCases {
   109  		t.Run(tc.desc, func(t *testing.T) {
   110  			assert.Equalf(t, tc.expected, canGithubEventMatch(tc.eventName, tc.triggeredEvent), "canGithubEventMatch(%v, %v)", tc.eventName, tc.triggeredEvent)
   111  		})
   112  	}
   113  }