2023-04-12 16:16:47 +00:00
// 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"
2024-07-30 19:41:10 +00:00
"github.com/stretchr/testify/require"
2023-04-12 16:16:47 +00:00
)
func TestDetectMatched ( t * testing . T ) {
testCases := [ ] struct {
2024-07-30 22:15:22 +00:00
desc string
commit * git . Commit
triggeredEvent webhook_module . HookEventType
payload api . Payloader
yamlOn string
expected bool
2023-04-12 16:16:47 +00:00
} {
{
2024-07-30 22:15:22 +00:00
desc : "HookEventCreate(create) matches GithubEventCreate(create)" ,
triggeredEvent : webhook_module . HookEventCreate ,
payload : nil ,
yamlOn : "on: create" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)" ,
triggeredEvent : webhook_module . HookEventIssues ,
payload : & api . IssuePayload { Action : api . HookIssueOpened } ,
yamlOn : "on: issues" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventIssueComment(issue_comment) `created` action matches GithubEventIssueComment(issue_comment)" ,
triggeredEvent : webhook_module . HookEventIssueComment ,
payload : & api . IssueCommentPayload { Action : api . HookIssueCommentCreated } ,
yamlOn : "on:\n issue_comment:\n types: [created]" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
2024-07-30 22:15:22 +00:00
{
desc : "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)" ,
triggeredEvent : webhook_module . HookEventIssues ,
payload : & api . IssuePayload { Action : api . HookIssueMilestoned } ,
yamlOn : "on: issues" ,
expected : true ,
} ,
2023-04-12 16:16:47 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)" ,
triggeredEvent : webhook_module . HookEventPullRequestSync ,
payload : & api . PullRequestPayload { Action : api . HookIssueSynchronized } ,
yamlOn : "on: pull_request" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type" ,
triggeredEvent : webhook_module . HookEventPullRequest ,
payload : & api . PullRequestPayload { Action : api . HookIssueLabelUpdated } ,
yamlOn : "on: pull_request" ,
expected : false ,
2023-04-12 16:16:47 +00:00
} ,
2023-07-07 16:30:07 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type" ,
triggeredEvent : webhook_module . HookEventPullRequest ,
payload : & api . PullRequestPayload { Action : api . HookIssueClosed } ,
yamlOn : "on: pull_request" ,
expected : false ,
2023-07-07 16:30:07 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches" ,
triggeredEvent : webhook_module . HookEventPullRequest ,
2023-07-07 16:30:07 +00:00
payload : & api . PullRequestPayload {
Action : api . HookIssueClosed ,
PullRequest : & api . PullRequest {
Base : & api . PRBranchInfo { } ,
} ,
} ,
yamlOn : "on:\n pull_request:\n branches: [main]" ,
expected : false ,
} ,
2023-04-12 16:16:47 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type" ,
triggeredEvent : webhook_module . HookEventPullRequest ,
payload : & api . PullRequestPayload { Action : api . HookIssueLabelUpdated } ,
yamlOn : "on:\n pull_request:\n types: [labeled]" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)" ,
triggeredEvent : webhook_module . HookEventPullRequestReviewComment ,
payload : & api . PullRequestPayload { Action : api . HookIssueReviewed } ,
yamlOn : "on:\n pull_request_review_comment:\n types: [created]" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)" ,
triggeredEvent : webhook_module . HookEventPullRequestReviewRejected ,
payload : & api . PullRequestPayload { Action : api . HookIssueReviewed } ,
yamlOn : "on:\n pull_request_review:\n types: [dismissed]" ,
expected : false ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type" ,
triggeredEvent : webhook_module . HookEventRelease ,
payload : & api . ReleasePayload { Action : api . HookReleasePublished } ,
yamlOn : "on:\n release:\n types: [published]" ,
expected : true ,
2023-04-12 16:16:47 +00:00
} ,
{
2024-07-30 22:15:22 +00:00
desc : "HookEventRelease(updated) `updated` action matches GithubEventRelease(edited) with `edited` activity type" ,
triggeredEvent : webhook_module . HookEventRelease ,
payload : & api . ReleasePayload { Action : api . HookReleaseUpdated } ,
yamlOn : "on:\n release:\n types: [edited]" ,
expected : true ,
} ,
{
desc : "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type" ,
triggeredEvent : webhook_module . HookEventPackage ,
payload : & api . PackagePayload { Action : api . HookPackageCreated } ,
yamlOn : "on:\n registry_package:\n types: [updated]" ,
expected : false ,
2023-04-12 16:16:47 +00:00
} ,
2023-04-17 17:49:47 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventWiki(wiki) matches GithubEventGollum(gollum)" ,
triggeredEvent : webhook_module . HookEventWiki ,
payload : nil ,
yamlOn : "on: gollum" ,
expected : true ,
2023-04-17 17:49:47 +00:00
} ,
2024-01-12 21:50:38 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventSchedule(schedule) matches GithubEventSchedule(schedule)" ,
triggeredEvent : webhook_module . HookEventSchedule ,
payload : nil ,
yamlOn : "on: schedule" ,
expected : true ,
2024-01-12 21:50:38 +00:00
} ,
2024-06-28 05:17:11 +00:00
{
2024-07-30 22:15:22 +00:00
desc : "HookEventWorkflowDispatch(workflow_dispatch) matches GithubEventWorkflowDispatch(workflow_dispatch)" ,
triggeredEvent : webhook_module . HookEventWorkflowDispatch ,
payload : nil ,
yamlOn : "on: workflow_dispatch" ,
expected : true ,
2024-06-28 05:17:11 +00:00
} ,
2023-04-12 16:16:47 +00:00
}
for _ , tc := range testCases {
t . Run ( tc . desc , func ( t * testing . T ) {
evts , err := GetEventsFromContent ( [ ] byte ( tc . yamlOn ) )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2023-04-12 16:16:47 +00:00
assert . Len ( t , evts , 1 )
2024-07-30 22:15:22 +00:00
assert . Equal ( t , tc . expected , detectMatched ( nil , tc . commit , tc . triggeredEvent , tc . payload , evts [ 0 ] ) )
2023-04-12 16:16:47 +00:00
} )
}
}