1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 00:54:28 +00:00

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

@ -47,6 +47,7 @@ const (
HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected" HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected"
HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment" HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment"
HookEventPullRequestSync HookEventType = "pull_request_sync" HookEventPullRequestSync HookEventType = "pull_request_sync"
HookEventWiki HookEventType = "wiki"
HookEventRepository HookEventType = "repository" HookEventRepository HookEventType = "repository"
HookEventRelease HookEventType = "release" HookEventRelease HookEventType = "release"
HookEventPackage HookEventType = "package" HookEventPackage HookEventType = "package"
@ -76,6 +77,8 @@ func (h HookEventType) Event() string {
return "pull_request_rejected" return "pull_request_rejected"
case HookEventPullRequestReviewComment: case HookEventPullRequestReviewComment:
return "pull_request_comment" return "pull_request_comment"
case HookEventWiki:
return "wiki"
case HookEventRepository: case HookEventRepository:
return "repository" return "repository"
case HookEventRelease: case HookEventRelease:

View File

@ -132,6 +132,7 @@ type HookEvents struct {
PullRequestComment bool `json:"pull_request_comment"` PullRequestComment bool `json:"pull_request_comment"`
PullRequestReview bool `json:"pull_request_review"` PullRequestReview bool `json:"pull_request_review"`
PullRequestSync bool `json:"pull_request_sync"` PullRequestSync bool `json:"pull_request_sync"`
Wiki bool `json:"wiki"`
Repository bool `json:"repository"` Repository bool `json:"repository"`
Release bool `json:"release"` Release bool `json:"release"`
Package bool `json:"package"` Package bool `json:"package"`
@ -328,6 +329,12 @@ func (w *Webhook) HasPullRequestSyncEvent() bool {
(w.ChooseEvents && w.HookEvents.PullRequestSync) (w.ChooseEvents && w.HookEvents.PullRequestSync)
} }
// HasWikiEvent returns true if hook enabled wiki event.
func (w *Webhook) HasWikiEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvent.Wiki)
}
// HasReleaseEvent returns if hook enabled release event. // HasReleaseEvent returns if hook enabled release event.
func (w *Webhook) HasReleaseEvent() bool { func (w *Webhook) HasReleaseEvent() bool {
return w.SendEverything || return w.SendEverything ||
@ -373,6 +380,7 @@ func (w *Webhook) EventCheckers() []struct {
{w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected}, {w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected},
{w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment}, {w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment},
{w.HasPullRequestSyncEvent, HookEventPullRequestSync}, {w.HasPullRequestSyncEvent, HookEventPullRequestSync},
{w.HasWikiEvent, HookEventWiki},
{w.HasRepositoryEvent, HookEventRepository}, {w.HasRepositoryEvent, HookEventRepository},
{w.HasReleaseEvent, HookEventRelease}, {w.HasReleaseEvent, HookEventRelease},
{w.HasPackageEvent, HookEventPackage}, {w.HasPackageEvent, HookEventPackage},

View File

@ -71,7 +71,7 @@ func TestWebhook_EventsArray(t *testing.T) {
"issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment", "issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment",
"pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone", "pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
"pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected", "pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
"pull_request_review_comment", "pull_request_sync", "repository", "release", "pull_request_review_comment", "pull_request_sync", "wiki", "repository", "release",
"package", "package",
}, },
(&Webhook{ (&Webhook{

View File

@ -45,6 +45,9 @@ type Notifier interface {
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User) issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User)
NotifyUpdateComment(*user_model.User, *issues_model.Comment, string) NotifyUpdateComment(*user_model.User, *issues_model.Comment, string)
NotifyDeleteComment(*user_model.User, *issues_model.Comment) 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) NotifyNewRelease(rel *repo_model.Release)
NotifyUpdateRelease(doer *user_model.User, rel *repo_model.Release) NotifyUpdateRelease(doer *user_model.User, rel *repo_model.Release)
NotifyDeleteRelease(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) { 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 // NotifyNewRelease places a place holder function
func (*NullNotifier) NotifyNewRelease(rel *repo_model.Release) { func (*NullNotifier) NotifyNewRelease(rel *repo_model.Release) {
} }

View File

@ -40,6 +40,27 @@ func NewContext() {
RegisterNotifier(mirror.NewNotifier()) 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 // NotifyCreateIssueComment notifies issue comment related message to notifiers
func NotifyCreateIssueComment(doer *user_model.User, repo *repo_model.Repository, func NotifyCreateIssueComment(doer *user_model.User, repo *repo_model.Repository,
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, 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, func (m *webhookNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *issues_model.Issue,
addedLabels, removedLabels []*issues_model.Label, addedLabels, removedLabels []*issues_model.Label,
) { ) {

View File

@ -397,6 +397,39 @@ type ReviewPayload struct {
Content string `json:"content"` 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, "", " ")
}
//__________ .__ __ //__________ .__ __
//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__. //\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | | // | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |

View File

@ -1956,6 +1956,8 @@ settings.event_delete = Delete
settings.event_delete_desc = Branch or tag deleted. settings.event_delete_desc = Branch or tag deleted.
settings.event_fork = Fork settings.event_fork = Fork
settings.event_fork_desc = Repository forked. settings.event_fork_desc = Repository forked.
settings.event_wiki = Wiki
settings.event_wiki_desc = Wiki page created, renamed, edited or deleted.
settings.event_release = Release settings.event_release = Release
settings.event_release_desc = Release published, updated or deleted in a repository. settings.event_release_desc = Release published, updated or deleted in a repository.
settings.event_push = Push settings.event_push = Push

View File

@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -85,6 +86,7 @@ func NewWikiPage(ctx *context.APIContext) {
wikiPage := getWikiPage(ctx, wikiName) wikiPage := getWikiPage(ctx, wikiName)
if !ctx.Written() { if !ctx.Written() {
notification.NotifyNewWikiPage(ctx.Doer, ctx.Repo.Repository, wikiName, form.Message)
ctx.JSON(http.StatusCreated, wikiPage) ctx.JSON(http.StatusCreated, wikiPage)
} }
} }
@ -152,6 +154,7 @@ func EditWikiPage(ctx *context.APIContext) {
wikiPage := getWikiPage(ctx, newWikiName) wikiPage := getWikiPage(ctx, newWikiName)
if !ctx.Written() { if !ctx.Written() {
notification.NotifyEditWikiPage(ctx.Doer, ctx.Repo.Repository, newWikiName, form.Message)
ctx.JSON(http.StatusOK, wikiPage) ctx.JSON(http.StatusOK, wikiPage)
} }
} }
@ -242,6 +245,8 @@ func DeleteWikiPage(ctx *context.APIContext) {
return return
} }
notification.NotifyDeleteWikiPage(ctx.Doer, ctx.Repo.Repository, wikiName)
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }

View File

@ -126,6 +126,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
PullRequestComment: pullHook(form.Events, string(webhook.HookEventPullRequestComment)), PullRequestComment: pullHook(form.Events, string(webhook.HookEventPullRequestComment)),
PullRequestReview: pullHook(form.Events, "pull_request_review"), PullRequestReview: pullHook(form.Events, "pull_request_review"),
PullRequestSync: pullHook(form.Events, string(webhook.HookEventPullRequestSync)), PullRequestSync: pullHook(form.Events, string(webhook.HookEventPullRequestSync)),
Wiki: util.IsStringInSlice(string(webhook.HookEventWiki), form.Events, true),
Repository: util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true), Repository: util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true),
Release: util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true), Release: util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true),
}, },
@ -249,6 +250,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
w.Delete = util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true) w.Delete = util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true)
w.Fork = util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true) w.Fork = util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true)
w.Repository = util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true) w.Repository = util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true)
w.Wiki = util.IsStringInSlice(string(webhook.HookEventWiki), form.Events, true)
w.Release = util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true) w.Release = util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true)
w.BranchFilter = form.BranchFilter w.BranchFilter = form.BranchFilter

View File

@ -178,6 +178,7 @@ func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent {
PullRequestComment: form.PullRequestComment, PullRequestComment: form.PullRequestComment,
PullRequestReview: form.PullRequestReview, PullRequestReview: form.PullRequestReview,
PullRequestSync: form.PullRequestSync, PullRequestSync: form.PullRequestSync,
Wiki: form.Wiki,
Repository: form.Repository, Repository: form.Repository,
Package: form.Package, Package: form.Package,
}, },

View File

@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -705,6 +706,8 @@ func NewWikiPost(ctx *context.Context) {
return return
} }
notification.NotifyNewWikiPage(ctx.Doer, ctx.Repo.Repository, wikiName, form.Message)
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.NameToSubURL(wikiName)) ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.NameToSubURL(wikiName))
} }
@ -747,6 +750,8 @@ func EditWikiPost(ctx *context.Context) {
return return
} }
notification.NotifyEditWikiPage(ctx.Doer, ctx.Repo.Repository, newWikiName, form.Message)
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.NameToSubURL(newWikiName)) ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + wiki_service.NameToSubURL(newWikiName))
} }
@ -762,6 +767,8 @@ func DeleteWikiPagePost(ctx *context.Context) {
return return
} }
notification.NotifyDeleteWikiPage(ctx.Doer, ctx.Repo.Repository, wikiName)
ctx.JSON(http.StatusOK, map[string]interface{}{ ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": ctx.Repo.RepoLink + "/wiki/", "redirect": ctx.Repo.RepoLink + "/wiki/",
}) })

View File

@ -215,12 +215,12 @@ func (f *ProtectBranchForm) Validate(req *http.Request, errs binding.Errors) bin
return middleware.Validate(errs, ctx.Data, f, ctx.Locale) return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
} }
// __ __ ___. .__ .__ __ // __ __ ___. .__ __
// / \ / \ ____\_ |__ | |__ | |__ ____ | | __ // / \ / \ ____\_ |__ | |__ ____ ____ | | __
// \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ / // \ \/\/ // __ \| __ \| | \ / _ \ / _ \| |/ /
// \ /\ ___/| \_\ \ Y \ Y ( <_> ) < // \ /\ ___/| \_\ \ Y ( <_> | <_> ) <
// \__/\ / \___ >___ /___| /___| /\____/|__|_ \ // \__/\ / \___ >___ /___| /\____/ \____/|__|_ \
// \/ \/ \/ \/ \/ \/ // \/ \/ \/ \/ \/
// WebhookForm form for changing web hook // WebhookForm form for changing web hook
type WebhookForm struct { type WebhookForm struct {
@ -242,6 +242,7 @@ type WebhookForm struct {
PullRequestComment bool PullRequestComment bool
PullRequestReview bool PullRequestReview bool
PullRequestSync bool PullRequestSync bool
Wiki bool
Repository bool Repository bool
Package bool Package bool
Active bool Active bool

View File

@ -107,6 +107,14 @@ func (d *DingtalkPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view issue", p.Issue.HTMLURL), nil return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view issue", p.Issue.HTMLURL), nil
} }
// Wiki implements PayloadConvertor Wiki method
func (d *DingtalkPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
url := p.Repository.HTMLURL + "/wiki/" + url.PathEscape(p.Page)
return createDingtalkPayload(text, text, "view wiki", url), nil
}
// IssueComment implements PayloadConvertor IssueComment method // IssueComment implements PayloadConvertor IssueComment method
func (d *DingtalkPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { func (d *DingtalkPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)

View File

@ -189,6 +189,44 @@ func TestDingTalkPayload(t *testing.T) {
assert.Equal(t, "http://localhost:3000/test/repo", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL)) assert.Equal(t, "http://localhost:3000/test/repo", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(DingtalkPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DingtalkPayload{}, pl)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Text)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Title)
assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DingtalkPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Text)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Title)
assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DingtalkPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*DingtalkPayload).ActionCard.Text)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*DingtalkPayload).ActionCard.Title)
assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -7,6 +7,7 @@ package webhook
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"strings" "strings"
@ -235,6 +236,19 @@ func (d *DiscordPayload) Repository(p *api.RepositoryPayload) (api.Payloader, er
return d.createPayload(p.Sender, title, "", url, color), nil return d.createPayload(p.Sender, title, "", url, color), nil
} }
// Wiki implements PayloadConvertor Wiki method
func (d *DiscordPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, color, _ := getWikiPayloadInfo(p, noneLinkFormatter, false)
htmlLink := p.Repository.HTMLURL + "/wiki/" + url.PathEscape(p.Page)
var description string
if p.Action != api.HookWikiDeleted {
description = p.Comment
}
return d.createPayload(p.Sender, text, description, htmlLink, color), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (d *DiscordPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (d *DiscordPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, color := getReleasePayloadInfo(p, noneLinkFormatter, false) text, color := getReleasePayloadInfo(p, noneLinkFormatter, false)

View File

@ -212,6 +212,53 @@ func TestDiscordPayload(t *testing.T) {
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL) assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(DiscordPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DiscordPayload{}, pl)
assert.Len(t, pl.(*DiscordPayload).Embeds, 1)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*DiscordPayload).Embeds[0].Title)
assert.Equal(t, "Wiki change comment", pl.(*DiscordPayload).Embeds[0].Description)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL)
assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name)
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL)
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DiscordPayload{}, pl)
assert.Len(t, pl.(*DiscordPayload).Embeds, 1)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*DiscordPayload).Embeds[0].Title)
assert.Equal(t, "Wiki change comment", pl.(*DiscordPayload).Embeds[0].Description)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL)
assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name)
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL)
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DiscordPayload{}, pl)
assert.Len(t, pl.(*DiscordPayload).Embeds, 1)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*DiscordPayload).Embeds[0].Title)
assert.Empty(t, pl.(*DiscordPayload).Embeds[0].Description)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL)
assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name)
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL)
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -145,6 +145,13 @@ func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err
return nil, nil return nil, nil
} }
// Wiki implements PayloadConvertor Wiki method
func (f *FeishuPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
return newFeishuTextPayload(text), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)

View File

@ -145,6 +145,35 @@ func TestFeishuPayload(t *testing.T) {
assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text) assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(FeishuPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &FeishuPayload{}, pl)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &FeishuPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &FeishuPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*FeishuPayload).Content.Text)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -161,6 +161,35 @@ func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, w
return text, color return text, color
} }
func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) {
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page)
var text string
color := greenColor
switch p.Action {
case api.HookWikiCreated:
text = fmt.Sprintf("[%s] New wiki page '%s'", repoLink, pageLink)
case api.HookWikiEdited:
text = fmt.Sprintf("[%s] Wiki page '%s' edited", repoLink, pageLink)
color = yellowColor
case api.HookWikiDeleted:
text = fmt.Sprintf("[%s] Wiki page '%s' deleted", repoLink, pageLink)
color = redColor
}
if p.Action != api.HookWikiDeleted && p.Comment != "" {
text += fmt.Sprintf(" (%s)", p.Comment)
}
if withSender {
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
}
return text, color, pageLink
}
func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) { func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) {
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title) issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)

View File

@ -195,6 +195,22 @@ func pullRequestCommentTestPayload() *api.IssueCommentPayload {
} }
} }
func wikiTestPayload() *api.WikiPayload {
return &api.WikiPayload{
Repository: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
Sender: &api.User{
UserName: "user1",
AvatarURL: "http://localhost:3000/user1/avatar",
},
Page: "index",
Comment: "Wiki change comment",
}
}
func pullReleaseTestPayload() *api.ReleasePayload { func pullReleaseTestPayload() *api.ReleasePayload {
return &api.ReleasePayload{ return &api.ReleasePayload{
Action: api.HookReleasePublished, Action: api.HookReleasePublished,
@ -469,6 +485,44 @@ func TestGetPullRequestPayloadInfo(t *testing.T) {
} }
} }
func TestGetWikiPayloadInfo(t *testing.T) {
p := wikiTestPayload()
cases := []struct {
action api.HookWikiAction
text string
color int
link string
}{
{
api.HookWikiCreated,
"[test/repo] New wiki page 'index' (Wiki change comment) by user1",
greenColor,
"index",
},
{
api.HookWikiEdited,
"[test/repo] Wiki page 'index' edited (Wiki change comment) by user1",
yellowColor,
"index",
},
{
api.HookWikiDeleted,
"[test/repo] Wiki page 'index' deleted by user1",
redColor,
"index",
},
}
for i, c := range cases {
p.Action = c.action
text, color, link := getWikiPayloadInfo(p, noneLinkFormatter, true)
assert.Equal(t, c.text, text, "case %d", i)
assert.Equal(t, c.color, color, "case %d", i)
assert.Equal(t, c.link, link, "case %d", i)
}
}
func TestGetReleasePayloadInfo(t *testing.T) { func TestGetReleasePayloadInfo(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -143,6 +143,13 @@ func (m *MatrixPayloadUnsafe) IssueComment(p *api.IssueCommentPayload) (api.Payl
return getMatrixPayloadUnsafe(text, nil, m.AccessToken, m.MsgType), nil return getMatrixPayloadUnsafe(text, nil, m.AccessToken, m.MsgType), nil
} }
// Wiki implements PayloadConvertor Wiki method
func (m *MatrixPayloadUnsafe) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, MatrixLinkFormatter, true)
return getMatrixPayloadUnsafe(text, nil, m.AccessToken, m.MsgType), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, error) { func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, MatrixLinkFormatter, true) text, _ := getReleasePayloadInfo(p, MatrixLinkFormatter, true)

View File

@ -156,6 +156,38 @@ func TestMatrixPayload(t *testing.T) {
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody) assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(MatrixPayloadUnsafe)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MatrixPayloadUnsafe{}, pl)
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -6,6 +6,7 @@ package webhook
import ( import (
"fmt" "fmt"
"net/url"
"strings" "strings"
webhook_model "code.gitea.io/gitea/models/webhook" webhook_model "code.gitea.io/gitea/models/webhook"
@ -266,6 +267,21 @@ func (m *MSTeamsPayload) Repository(p *api.RepositoryPayload) (api.Payloader, er
), nil ), nil
} }
// Wiki implements PayloadConvertor Wiki method
func (m *MSTeamsPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
title, color, _ := getWikiPayloadInfo(p, noneLinkFormatter, false)
return createMSTeamsPayload(
p.Repository,
p.Sender,
title,
"",
p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page),
color,
&MSTeamsFact{"Repository:", p.Repository.FullName},
), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (m *MSTeamsPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (m *MSTeamsPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
title, color := getReleasePayloadInfo(p, noneLinkFormatter, false) title, color := getReleasePayloadInfo(p, noneLinkFormatter, false)

View File

@ -330,6 +330,80 @@ func TestMSTeamsPayload(t *testing.T) {
assert.Equal(t, "http://localhost:3000/test/repo", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI) assert.Equal(t, "http://localhost:3000/test/repo", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(MSTeamsPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MSTeamsPayload{}, pl)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*MSTeamsPayload).Title)
assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*MSTeamsPayload).Summary)
assert.Len(t, pl.(*MSTeamsPayload).Sections, 1)
assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle)
assert.Equal(t, "", pl.(*MSTeamsPayload).Sections[0].Text)
assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2)
for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts {
if fact.Name == "Repository:" {
assert.Equal(t, p.Repository.FullName, fact.Value)
} else {
t.Fail()
}
}
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1)
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MSTeamsPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*MSTeamsPayload).Title)
assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*MSTeamsPayload).Summary)
assert.Len(t, pl.(*MSTeamsPayload).Sections, 1)
assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle)
assert.Equal(t, "", pl.(*MSTeamsPayload).Sections[0].Text)
assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2)
for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts {
if fact.Name == "Repository:" {
assert.Equal(t, p.Repository.FullName, fact.Value)
} else {
t.Fail()
}
}
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1)
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MSTeamsPayload{}, pl)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*MSTeamsPayload).Title)
assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*MSTeamsPayload).Summary)
assert.Len(t, pl.(*MSTeamsPayload).Sections, 1)
assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle)
assert.Empty(t, pl.(*MSTeamsPayload).Sections[0].Text)
assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2)
for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts {
if fact.Name == "Repository:" {
assert.Equal(t, p.Repository.FullName, fact.Value)
} else {
t.Fail()
}
}
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1)
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1)
assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -94,6 +94,11 @@ func (f *PackagistPayload) Repository(p *api.RepositoryPayload) (api.Payloader,
return nil, nil return nil, nil
} }
// Wiki implements PayloadConvertor Wiki method
func (f *PackagistPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
return nil, nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (f *PackagistPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (f *PackagistPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
return nil, nil return nil, nil

View File

@ -116,6 +116,26 @@ func TestPackagistPayload(t *testing.T) {
require.Nil(t, pl) require.Nil(t, pl)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(PackagistPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.Nil(t, pl)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.Nil(t, pl)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.Nil(t, pl)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -22,6 +22,7 @@ type PayloadConvertor interface {
Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error) Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error)
Repository(*api.RepositoryPayload) (api.Payloader, error) Repository(*api.RepositoryPayload) (api.Payloader, error)
Release(*api.ReleasePayload) (api.Payloader, error) Release(*api.ReleasePayload) (api.Payloader, error)
Wiki(*api.WikiPayload) (api.Payloader, error)
} }
func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) { func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) {
@ -51,6 +52,8 @@ func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.H
return s.Repository(p.(*api.RepositoryPayload)) return s.Repository(p.(*api.RepositoryPayload))
case webhook_model.HookEventRelease: case webhook_model.HookEventRelease:
return s.Release(p.(*api.ReleasePayload)) return s.Release(p.(*api.ReleasePayload))
case webhook_model.HookEventWiki:
return s.Wiki(p.(*api.WikiPayload))
} }
return s, nil return s, nil
} }

View File

@ -157,6 +157,13 @@ func (s *SlackPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader,
}}), nil }}), nil
} }
// Wiki implements PayloadConvertor Wiki method
func (s *SlackPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, SlackLinkFormatter, true)
return s.createPayload(text, nil), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true) text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true)

View File

@ -145,6 +145,35 @@ func TestSlackPayload(t *testing.T) {
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text) assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(SlackPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &SlackPayload{}, pl)
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &SlackPayload{}, pl)
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &SlackPayload{}, pl)
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -171,6 +171,13 @@ func (t *TelegramPayload) Repository(p *api.RepositoryPayload) (api.Payloader, e
return nil, nil return nil, nil
} }
// Wiki implements PayloadConvertor Wiki method
func (t *TelegramPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, htmlLinkFormatter, true)
return createTelegramPayload(text), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, htmlLinkFormatter, true) text, _ := getReleasePayloadInfo(p, htmlLinkFormatter, true)

View File

@ -145,6 +145,35 @@ func TestTelegramPayload(t *testing.T) {
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.(*TelegramPayload).Message) assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.(*TelegramPayload).Message)
}) })
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
d := new(TelegramPayload)
p.Action = api.HookWikiCreated
pl, err := d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &TelegramPayload{}, pl)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*TelegramPayload).Message)
p.Action = api.HookWikiEdited
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &TelegramPayload{}, pl)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*TelegramPayload).Message)
p.Action = api.HookWikiDeleted
pl, err = d.Wiki(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &TelegramPayload{}, pl)
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*TelegramPayload).Message)
})
t.Run("Release", func(t *testing.T) { t.Run("Release", func(t *testing.T) {
p := pullReleaseTestPayload() p := pullReleaseTestPayload()

View File

@ -166,6 +166,13 @@ func (f *WechatworkPayload) Repository(p *api.RepositoryPayload) (api.Payloader,
return nil, nil return nil, nil
} }
// Wiki implements PayloadConvertor Wiki method
func (f *WechatworkPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
return newWechatworkMarkdownPayload(text), nil
}
// Release implements PayloadConvertor Release method // Release implements PayloadConvertor Release method
func (f *WechatworkPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { func (f *WechatworkPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)

View File

@ -118,7 +118,7 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string
return foundEscaped, escaped, nil return foundEscaped, escaped, nil
} }
// updateWikiPage adds a new page to the repository wiki. // updateWikiPage adds a new page or edits an existing page in repository wiki.
func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
if err = nameAllowed(newWikiName); err != nil { if err = nameAllowed(newWikiName); err != nil {
return err return err

View File

@ -98,6 +98,17 @@
</div> </div>
</div> </div>
<!-- Wiki -->
<div class="seven wide column">
<div class="field">
<div class="ui checkbox">
<input class="hidden" name="wiki" type="checkbox" tabindex="0" {{if .Webhook.Wiki}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.event_wiki"}}</label>
<span class="help">{{.i18n.Tr "repo.settings.event_wiki_desc"}}</span>
</div>
</div>
</div>
<!-- Issue Events --> <!-- Issue Events -->
<div class="fourteen wide column"> <div class="fourteen wide column">
<label>{{.locale.Tr "repo.settings.event_header_issue"}}</label> <label>{{.locale.Tr "repo.settings.event_header_issue"}}</label>

View File

@ -11,7 +11,7 @@
</div> </div>
{{end}} {{end}}
</div> </div>
<form class="ui form" action="{{.Link}}" method="post"> <form class="ui form" action="{{.Link}}?action={{if .PageIsWikiEdit}}_edit{{else}}_new{{end}}" method="post">
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
<div class="field {{if .Err_Title}}error{{end}}"> <div class="field {{if .Err_Title}}error{{end}}">
<input name="title" value="{{.title}}" autofocus required> <input name="title" value="{{.title}}" autofocus required>