From 5eb4c6386709259a9280c5aad6e0488f381144c5 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 18 Apr 2023 01:49:47 +0800 Subject: [PATCH] Support triggering workflows by wiki related events (#24119) This PR is to support triggering workflows by wiki related events like creating, editing or deleting wiki pages. In GitHub, this event is called [gollum](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum) --- modules/actions/github.go | 5 +++++ modules/actions/workflows.go | 2 -- modules/actions/workflows_test.go | 7 +++++++ services/actions/notifier.go | 35 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index 1148554139..f3cb335da9 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -21,6 +21,7 @@ const ( githubEventIssueComment = "issue_comment" githubEventRelease = "release" githubEventPullRequestComment = "pull_request_comment" + githubEventGollum = "gollum" ) // canGithubEventMatch check if the input Github event can match any Gitea event. @@ -29,6 +30,10 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent 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 githubEventIssues: switch triggedEvent { case webhook_module.HookEventIssues, diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index d21dc1d809..f37f4f2878 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -119,8 +119,6 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventCreate, webhook_module.HookEventDelete, webhook_module.HookEventFork, - // FIXME: `wiki` event should match `gollum` event - // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum webhook_module.HookEventWiki: if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) diff --git a/modules/actions/workflows_test.go b/modules/actions/workflows_test.go index 6724abafd8..6ef5d59942 100644 --- a/modules/actions/workflows_test.go +++ b/modules/actions/workflows_test.go @@ -92,6 +92,13 @@ func TestDetectMatched(t *testing.T) { yamlOn: "on:\n registry_package:\n types: [updated]", expected: false, }, + { + desc: "HookEventWiki(wiki) matches githubEventGollum(gollum)", + triggedEvent: webhook_module.HookEventWiki, + payload: nil, + yamlOn: "on: gollum", + expected: true, + }, } for _, tc := range testCases { diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 6956c25cee..4ac77276ff 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -526,3 +526,38 @@ func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex WithPullRequest(pr). Notify(ctx) } + +func (n *actionsNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { + ctx = withMethod(ctx, "NotifyNewWikiPage") + + newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{ + Action: api.HookWikiCreated, + Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), + Sender: convert.ToUser(ctx, doer, nil), + Page: page, + Comment: comment, + }).Notify(ctx) +} + +func (n *actionsNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { + ctx = withMethod(ctx, "NotifyEditWikiPage") + + newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{ + Action: api.HookWikiEdited, + Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), + Sender: convert.ToUser(ctx, doer, nil), + Page: page, + Comment: comment, + }).Notify(ctx) +} + +func (n *actionsNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) { + ctx = withMethod(ctx, "NotifyDeleteWikiPage") + + newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{ + Action: api.HookWikiDeleted, + Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), + Sender: convert.ToUser(ctx, doer, nil), + Page: page, + }).Notify(ctx) +}