summaryrefslogtreecommitdiffstats
path: root/modules/actions/github.go
blob: c27d4edf539e371b9cdd5d6af53e70cceeb61e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
	webhook_module "code.gitea.io/gitea/modules/webhook"
)

const (
	GithubEventPullRequest              = "pull_request"
	GithubEventPullRequestTarget        = "pull_request_target"
	GithubEventPullRequestReviewComment = "pull_request_review_comment"
	GithubEventPullRequestReview        = "pull_request_review"
	GithubEventRegistryPackage          = "registry_package"
	GithubEventCreate                   = "create"
	GithubEventDelete                   = "delete"
	GithubEventFork                     = "fork"
	GithubEventPush                     = "push"
	GithubEventIssues                   = "issues"
	GithubEventIssueComment             = "issue_comment"
	GithubEventRelease                  = "release"
	GithubEventPullRequestComment       = "pull_request_comment"
	GithubEventGollum                   = "gollum"
	GithubEventSchedule                 = "schedule"
	GithubEventWorkflowDispatch         = "workflow_dispatch"
)

// IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch
func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
	switch triggedEvent {
	case webhook_module.HookEventDelete:
		// GitHub "delete" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete
		return true
	case webhook_module.HookEventFork:
		// GitHub "fork" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#fork
		return true
	case webhook_module.HookEventIssueComment:
		// GitHub "issue_comment" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
		return true
	case webhook_module.HookEventPullRequestComment:
		// GitHub "pull_request_comment" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
		return true
	case webhook_module.HookEventWiki:
		// GitHub "gollum" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
		return true
	case webhook_module.HookEventSchedule:
		// GitHub "schedule" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
		return true
	case webhook_module.HookEventWorkflowDispatch:
		// GitHub "workflow_dispatch" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
		return true
	case webhook_module.HookEventIssues,
		webhook_module.HookEventIssueAssign,
		webhook_module.HookEventIssueLabel,
		webhook_module.HookEventIssueMilestone:
		// Github "issues" event
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
		return true
	}

	return false
}

// canGithubEventMatch check if the input Github event can match any Gitea event.
func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
	switch eventName {
	case GithubEventRegistryPackage:
		return triggedEvent == webhook_module.HookEventPackage

	// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
	case GithubEventGollum:
		return triggedEvent == webhook_module.HookEventWiki

	case GithubEventWorkflowDispatch:
		return triggedEvent == webhook_module.HookEventWorkflowDispatch

	case GithubEventIssues:
		switch triggedEvent {
		case webhook_module.HookEventIssues,
			webhook_module.HookEventIssueAssign,
			webhook_module.HookEventIssueLabel,
			webhook_module.HookEventIssueMilestone:
			return true

		default:
			return false
		}

	case GithubEventPullRequest, GithubEventPullRequestTarget:
		switch triggedEvent {
		case webhook_module.HookEventPullRequest,
			webhook_module.HookEventPullRequestSync,
			webhook_module.HookEventPullRequestAssign,
			webhook_module.HookEventPullRequestLabel,
			webhook_module.HookEventPullRequestReviewRequest,
			webhook_module.HookEventPullRequestMilestone:
			return true

		default:
			return false
		}

	case GithubEventPullRequestReview:
		switch triggedEvent {
		case webhook_module.HookEventPullRequestReviewApproved,
			webhook_module.HookEventPullRequestReviewComment,
			webhook_module.HookEventPullRequestReviewRejected:
			return true

		default:
			return false
		}

	case GithubEventIssueComment:
		// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
		return triggedEvent == webhook_module.HookEventIssueComment ||
			triggedEvent == webhook_module.HookEventPullRequestComment

	case GithubEventSchedule:
		return triggedEvent == webhook_module.HookEventSchedule

	default:
		return eventName == string(triggedEvent)
	}
}