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>
This commit is contained in:
Aaron F 2022-09-04 21:54:23 +02:00 committed by GitHub
parent 8b0aaa5f86
commit 3963625b6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 618 additions and 9 deletions

View file

@ -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)

View file

@ -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) {
}

View file

@ -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,

View file

@ -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,
) {

View file

@ -397,6 +397,39 @@ type ReviewPayload struct {
Content string `json:"content"`
}
// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |
// \ /| | <| |
// \__/\ / |__|__|_ \__|
// \/ \/
// HookWikiAction an action that happens to a wiki page
type HookWikiAction string
const (
// HookWikiCreated created
HookWikiCreated HookWikiAction = "created"
// HookWikiEdited edited
HookWikiEdited HookWikiAction = "edited"
// HookWikiDeleted deleted
HookWikiDeleted HookWikiAction = "deleted"
)
// WikiPayload payload for repository webhooks
type WikiPayload struct {
Action HookWikiAction `json:"action"`
Repository *Repository `json:"repository"`
Sender *User `json:"sender"`
Page string `json:"page"`
Comment string `json:"comment"`
}
// JSONPayload JSON representation of the payload
func (p *WikiPayload) JSONPayload() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}
//__________ .__ __
//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |