Skip to content

Improve workflow event triggers #23613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add canGithubEventMatch
  • Loading branch information
Zettat123 committed Mar 21, 2023
commit ca58ecd8f4414da9f987c0eff737d77c3b73cadd
124 changes: 124 additions & 0 deletions modules/actions/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package actions

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

"github.com/nektos/act/pkg/jobparser"
Expand All @@ -25,6 +26,25 @@ const (
githubEventPullRequestComment = "pull_request_comment"
)

const (
githubActivityTypeOpened = "opened"
githubActivityTypeClosed = "closed"
githubActivityTypeReopened = "reopened"
githubActivityTypeEdited = "edited"
githubActivityTypeAssigned = "assigned"
githubActivityTypeUnassigned = "unassigned"
githubActivityTypeLabeled = "labeled"
githubActivityTypeUnlabeled = "unlabeled"
githubActivityTypeMilestoned = "milestoned"
githubActivityTypeDemilestoned = "demilestoned"

githubActivityTypeSynchronize = "synchronize"

githubActivityTypePublished = "published"
githubActivityTypeCreated = "created"
githubActivityTypeDeleted = "deleted"
)

func convertFromGithubEvent(evt *jobparser.Event) string {
switch evt.Name {
case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview,
Expand All @@ -39,3 +59,107 @@ func convertFromGithubEvent(evt *jobparser.Event) string {
return evt.Name
}
}

// canGithubEventMatch check if the input Github event can match any Gitea event.
func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookEventType) bool {
switch evt.Name {
case githubEventCreate:
return triggedEvent == webhook_module.HookEventCreate
case githubEventDelete:
return triggedEvent == webhook_module.HookEventDelete
case githubEventFork:
return triggedEvent == webhook_module.HookEventFork
case githubEventPush:
return triggedEvent == webhook_module.HookEventPush
case githubEventRelease:
return triggedEvent == webhook_module.HookEventRelease

case githubEventIssues:
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
if isEventActsTypesEmpty(evt.Acts) {
return util.SliceContains([]webhook_module.HookEventType{
webhook_module.HookEventIssues,
webhook_module.HookEventIssueAssign,
webhook_module.HookEventIssueLabel,
webhook_module.HookEventIssueMilestone,
}, triggedEvent)
}
switch triggedEvent {
case webhook_module.HookEventIssues:
return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened)
case webhook_module.HookEventIssueAssign:
return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned)
case webhook_module.HookEventIssueLabel:
return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled)
case webhook_module.HookEventIssueMilestone:
return matchByActivityTypes(evt.Acts, githubActivityTypeMilestoned, githubActivityTypeDemilestoned)
}
return false

case githubEventIssueComment:
return triggedEvent == webhook_module.HookEventIssueComment

case githubEventPullRequest:
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
if isEventActsTypesEmpty(evt.Acts) {
return util.SliceContains([]webhook_module.HookEventType{
webhook_module.HookEventPullRequest,
webhook_module.HookEventPullRequestSync,
}, triggedEvent)
}
switch triggedEvent {
case webhook_module.HookEventPullRequest:
return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened)
case webhook_module.HookEventPullRequestAssign:
return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned)
case webhook_module.HookEventPullRequestLabel:
return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled)
case webhook_module.HookEventPullRequestSync:
return matchByActivityTypes(evt.Acts, githubActivityTypeSynchronize)
}
return false

case githubEventPullRequestComment:
return triggedEvent == webhook_module.HookEventPullRequestComment

case githubEventPullRequestReview:
return util.SliceContains([]webhook_module.HookEventType{
webhook_module.HookEventPullRequestReviewApproved,
webhook_module.HookEventPullRequestReviewComment,
webhook_module.HookEventPullRequestReviewRejected,
}, triggedEvent)

case githubEventPullRequestReviewComment:
// TODO

case githubEventPullRequestTarget:
// TODO

default:
return false
}

return false
}

func isEventActsTypesEmpty(evtActs map[string][]string) bool {
if len(evtActs) == 0 || len(evtActs["types"]) == 0 {
return true
}
return false
}

func matchByActivityTypes(evtActs map[string][]string, actTypes ...string) bool {
if isEventActsTypesEmpty(evtActs) {
return false
}

evtActTypes := evtActs["types"]
for _, actType := range actTypes {
if util.SliceContainsString(evtActTypes, actType) {
return true
}
}

return false
}
2 changes: 1 addition & 1 deletion modules/actions/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
}

func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool {
if convertFromGithubEvent(evt) != string(triggedEvent) {
if !canGithubEventMatch(evt, triggedEvent) {
return false
}

Expand Down