diff options
author | Aaron F <mail@aaron-fischer.net> | 2022-09-04 21:54:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-04 20:54:23 +0100 |
commit | 3963625b6ef6f4d4f16959466c077593a1f908db (patch) | |
tree | f509433e0ca198759452c54a65b943c28bd6e2c3 /modules/notification | |
parent | 8b0aaa5f86e40a4699f278d09d116add63f8e4a0 (diff) | |
download | gitea-3963625b6ef6f4d4f16959466c077593a1f908db.tar.gz gitea-3963625b6ef6f4d4f16959466c077593a1f908db.zip |
Webhook for Wiki changes (#20219)
Add support for triggering webhook notifications on wiki changes.
This PR contains frontend and backend for webhook notifications on wiki actions (create a new page, rename a page, edit a page and delete a page). The frontend got a new checkbox under the Custom Event -> Repository Events section. There is only one checkbox for create/edit/rename/delete actions, because it makes no sense to separate it and others like releases or packages follow the same schema.
![image](https://user-images.githubusercontent.com/121972/177018803-26851196-831f-4fde-9a4c-9e639b0e0d6b.png)
The actions itself are separated, so that different notifications will be executed (with the "action" field). All the webhook receivers implement the new interface method (Wiki) and the corresponding tests.
When implementing this, I encounter a little bug on editing a wiki page. Creating and editing a wiki page is technically the same action and will be handled by the ```updateWikiPage``` function. But the function need to know if it is a new wiki page or just a change. This distinction is done by the ```action``` parameter, but this will not be sent by the frontend (on form submit). This PR will fix this by adding the ```action``` parameter with the values ```_new``` or ```_edit```, which will be used by the ```updateWikiPage``` function.
I've done integration tests with matrix and gitea (http).
![image](https://user-images.githubusercontent.com/121972/177018795-eb5cdc01-9ba3-483e-a6b7-ed0e313a71fb.png)
Fix #16457
Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
Diffstat (limited to 'modules/notification')
-rw-r--r-- | modules/notification/base/notifier.go | 3 | ||||
-rw-r--r-- | modules/notification/base/null.go | 12 | ||||
-rw-r--r-- | modules/notification/notification.go | 21 | ||||
-rw-r--r-- | modules/notification/webhook/webhook.go | 38 |
4 files changed, 74 insertions, 0 deletions
diff --git a/modules/notification/base/notifier.go b/modules/notification/base/notifier.go index 541731ddbf..d6cec92e19 100644 --- a/modules/notification/base/notifier.go +++ b/modules/notification/base/notifier.go @@ -45,6 +45,9 @@ type Notifier interface { issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User) NotifyUpdateComment(*user_model.User, *issues_model.Comment, string) NotifyDeleteComment(*user_model.User, *issues_model.Comment) + NotifyNewWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) + NotifyEditWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) + NotifyDeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, page string) NotifyNewRelease(rel *repo_model.Release) NotifyUpdateRelease(doer *user_model.User, rel *repo_model.Release) NotifyDeleteRelease(doer *user_model.User, rel *repo_model.Release) diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go index 4f382b3d01..b113ae79e1 100644 --- a/modules/notification/base/null.go +++ b/modules/notification/base/null.go @@ -78,6 +78,18 @@ func (*NullNotifier) NotifyUpdateComment(doer *user_model.User, c *issues_model. func (*NullNotifier) NotifyDeleteComment(doer *user_model.User, c *issues_model.Comment) { } +// NotifyNewWikiPage places a place holder function +func (*NullNotifier) NotifyNewWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { +} + +// NotifyEditWikiPage places a place holder function +func (*NullNotifier) NotifyEditWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { +} + +// NotifyDeleteWikiPage places a place holder function +func (*NullNotifier) NotifyDeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, page string) { +} + // NotifyNewRelease places a place holder function func (*NullNotifier) NotifyNewRelease(rel *repo_model.Release) { } diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 713cc72aac..693c7f5c22 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -40,6 +40,27 @@ func NewContext() { RegisterNotifier(mirror.NewNotifier()) } +// NotifyNewWikiPage notifies creating new wiki pages to notifiers +func NotifyNewWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { + for _, notifier := range notifiers { + notifier.NotifyNewWikiPage(doer, repo, page, comment) + } +} + +// NotifyEditWikiPage notifies editing or renaming wiki pages to notifiers +func NotifyEditWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { + for _, notifier := range notifiers { + notifier.NotifyEditWikiPage(doer, repo, page, comment) + } +} + +// NotifyDeleteWikiPage notifies deleting wiki pages to notifiers +func NotifyDeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, page string) { + for _, notifier := range notifiers { + notifier.NotifyDeleteWikiPage(doer, repo, page) + } +} + // NotifyCreateIssueComment notifies issue comment related message to notifiers func NotifyCreateIssueComment(doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index ed3b53e3e7..0eb2099a20 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -501,6 +501,44 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *user_model.User, comment *is } } +func (m *webhookNotifier) NotifyNewWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { + // Add to hook queue for created wiki page. + if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventWiki, &api.WikiPayload{ + Action: api.HookWikiCreated, + Repository: convert.ToRepo(repo, perm.AccessModeOwner), + Sender: convert.ToUser(doer, nil), + Page: page, + Comment: comment, + }); err != nil { + log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) + } +} + +func (m *webhookNotifier) NotifyEditWikiPage(doer *user_model.User, repo *repo_model.Repository, page, comment string) { + // Add to hook queue for edit wiki page. + if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventWiki, &api.WikiPayload{ + Action: api.HookWikiEdited, + Repository: convert.ToRepo(repo, perm.AccessModeOwner), + Sender: convert.ToUser(doer, nil), + Page: page, + Comment: comment, + }); err != nil { + log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) + } +} + +func (m *webhookNotifier) NotifyDeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, page string) { + // Add to hook queue for edit wiki page. + if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventWiki, &api.WikiPayload{ + Action: api.HookWikiDeleted, + Repository: convert.ToRepo(repo, perm.AccessModeOwner), + Sender: convert.ToUser(doer, nil), + Page: page, + }); err != nil { + log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) + } +} + func (m *webhookNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *issues_model.Issue, addedLabels, removedLabels []*issues_model.Label, ) { |