2023-03-14 07:50:51 -05:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-06-26 01:33:18 -05:00
|
|
|
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"
|
2024-01-12 16:50:38 -05:00
|
|
|
GithubEventSchedule = "schedule"
|
2023-03-14 07:50:51 -05:00
|
|
|
)
|
|
|
|
|
2024-02-22 09:47:35 -05:00
|
|
|
// 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.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
|
|
|
|
}
|
|
|
|
|
2023-03-22 23:04:15 -05:00
|
|
|
// canGithubEventMatch check if the input Github event can match any Gitea event.
|
|
|
|
func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
|
|
|
|
switch eventName {
|
2023-06-26 01:33:18 -05:00
|
|
|
case GithubEventRegistryPackage:
|
2023-03-22 23:04:15 -05:00
|
|
|
return triggedEvent == webhook_module.HookEventPackage
|
|
|
|
|
2023-04-17 12:49:47 -05:00
|
|
|
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
|
2023-06-26 01:33:18 -05:00
|
|
|
case GithubEventGollum:
|
2023-04-17 12:49:47 -05:00
|
|
|
return triggedEvent == webhook_module.HookEventWiki
|
|
|
|
|
2023-06-26 01:33:18 -05:00
|
|
|
case GithubEventIssues:
|
2023-03-22 23:04:15 -05:00
|
|
|
switch triggedEvent {
|
|
|
|
case webhook_module.HookEventIssues,
|
|
|
|
webhook_module.HookEventIssueAssign,
|
|
|
|
webhook_module.HookEventIssueLabel,
|
|
|
|
webhook_module.HookEventIssueMilestone:
|
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-26 01:33:18 -05:00
|
|
|
case GithubEventPullRequest, GithubEventPullRequestTarget:
|
2023-03-22 23:04:15 -05:00
|
|
|
switch triggedEvent {
|
|
|
|
case webhook_module.HookEventPullRequest,
|
|
|
|
webhook_module.HookEventPullRequestSync,
|
|
|
|
webhook_module.HookEventPullRequestAssign,
|
2024-02-18 05:58:46 -05:00
|
|
|
webhook_module.HookEventPullRequestLabel,
|
|
|
|
webhook_module.HookEventPullRequestReviewRequest,
|
|
|
|
webhook_module.HookEventPullRequestMilestone:
|
2023-03-22 23:04:15 -05:00
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-26 01:33:18 -05:00
|
|
|
case GithubEventPullRequestReview:
|
2023-03-22 23:04:15 -05:00
|
|
|
switch triggedEvent {
|
|
|
|
case webhook_module.HookEventPullRequestReviewApproved,
|
|
|
|
webhook_module.HookEventPullRequestReviewComment,
|
|
|
|
webhook_module.HookEventPullRequestReviewRejected:
|
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-22 09:47:35 -05:00
|
|
|
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
|
|
|
|
|
2024-01-12 16:50:38 -05:00
|
|
|
case GithubEventSchedule:
|
|
|
|
return triggedEvent == webhook_module.HookEventSchedule
|
|
|
|
|
2023-03-14 07:50:51 -05:00
|
|
|
default:
|
2023-03-22 23:04:15 -05:00
|
|
|
return eventName == string(triggedEvent)
|
2023-03-14 07:50:51 -05:00
|
|
|
}
|
|
|
|
}
|