Skip to content

Commit 23971a7

Browse files
authored
Add tests for webhook and fix some webhook bugs (#33396) (#33442)
This PR created a mock webhook server in the tests and added integration tests for generic webhooks. It also fixes bugs in package webhooks and pull request comment webhooks. This also corrected an error on the package webhook. The previous implementation uses a `User` struct as an organization, now it has been corrected but it will not be consistent with the previous implementation, some fields which not belong to the organization have been removed. Backport #33396 Backport part of #33337
1 parent ebac324 commit 23971a7

26 files changed

+682
-105
lines changed

models/webhook/webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ func (w *Webhook) HasPackageEvent() bool {
299299
(w.ChooseEvents && w.HookEvents.Package)
300300
}
301301

302+
func (w *Webhook) HasStatusEvent() bool {
303+
return w.SendEverything ||
304+
(w.ChooseEvents && w.HookEvents.Status)
305+
}
306+
302307
// HasPullRequestReviewRequestEvent returns true if hook enabled pull request review request event.
303308
func (w *Webhook) HasPullRequestReviewRequestEvent() bool {
304309
return w.SendEverything ||
@@ -337,6 +342,7 @@ func (w *Webhook) EventCheckers() []struct {
337342
{w.HasReleaseEvent, webhook_module.HookEventRelease},
338343
{w.HasPackageEvent, webhook_module.HookEventPackage},
339344
{w.HasPullRequestReviewRequestEvent, webhook_module.HookEventPullRequestReviewRequest},
345+
{w.HasStatusEvent, webhook_module.HookEventStatus},
340346
}
341347
}
342348

models/webhook/webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestWebhook_EventsArray(t *testing.T) {
7474
"pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
7575
"pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
7676
"pull_request_review_comment", "pull_request_sync", "wiki", "repository", "release",
77-
"package", "pull_request_review_request",
77+
"package", "pull_request_review_request", "status",
7878
},
7979
(&Webhook{
8080
HookEvent: &webhook_module.HookEvent{SendEverything: true},

modules/structs/hook.go

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,7 @@ var (
116116
_ Payloader = &PackagePayload{}
117117
)
118118

119-
// _________ __
120-
// \_ ___ \_______ ____ _____ _/ |_ ____
121-
// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
122-
// \ \____| | \/\ ___/ / __ \| | \ ___/
123-
// \______ /|__| \___ >____ /__| \___ >
124-
// \/ \/ \/ \/
125-
126-
// CreatePayload FIXME
119+
// CreatePayload represents a payload information of create event.
127120
type CreatePayload struct {
128121
Sha string `json:"sha"`
129122
Ref string `json:"ref"`
@@ -157,13 +150,6 @@ func ParseCreateHook(raw []byte) (*CreatePayload, error) {
157150
return hook, nil
158151
}
159152

160-
// ________ .__ __
161-
// \______ \ ____ | | _____/ |_ ____
162-
// | | \_/ __ \| | _/ __ \ __\/ __ \
163-
// | ` \ ___/| |_\ ___/| | \ ___/
164-
// /_______ /\___ >____/\___ >__| \___ >
165-
// \/ \/ \/ \/
166-
167153
// PusherType define the type to push
168154
type PusherType string
169155

@@ -186,13 +172,6 @@ func (p *DeletePayload) JSONPayload() ([]byte, error) {
186172
return json.MarshalIndent(p, "", " ")
187173
}
188174

189-
// ___________ __
190-
// \_ _____/__________| | __
191-
// | __)/ _ \_ __ \ |/ /
192-
// | \( <_> ) | \/ <
193-
// \___ / \____/|__| |__|_ \
194-
// \/ \/
195-
196175
// ForkPayload represents fork payload
197176
type ForkPayload struct {
198177
Forkee *Repository `json:"forkee"`
@@ -232,13 +211,6 @@ func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
232211
return json.MarshalIndent(p, "", " ")
233212
}
234213

235-
// __________ .__
236-
// \______ \ ____ | | ____ _____ ______ ____
237-
// | _// __ \| | _/ __ \\__ \ / ___// __ \
238-
// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
239-
// |____|_ /\___ >____/\___ >____ /____ >\___ >
240-
// \/ \/ \/ \/ \/ \/
241-
242214
// HookReleaseAction defines hook release action type
243215
type HookReleaseAction string
244216

@@ -302,13 +274,6 @@ func (p *PushPayload) Branch() string {
302274
return strings.ReplaceAll(p.Ref, "refs/heads/", "")
303275
}
304276

305-
// .___
306-
// | | ______ ________ __ ____
307-
// | |/ ___// ___/ | \_/ __ \
308-
// | |\___ \ \___ \| | /\ ___/
309-
// |___/____ >____ >____/ \___ >
310-
// \/ \/ \/
311-
312277
// HookIssueAction FIXME
313278
type HookIssueAction string
314279

@@ -371,13 +336,6 @@ type ChangesPayload struct {
371336
Ref *ChangesFromPayload `json:"ref,omitempty"`
372337
}
373338

374-
// __________ .__ .__ __________ __
375-
// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
376-
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
377-
// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
378-
// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
379-
// \/ \/ |__| \/ \/
380-
381339
// PullRequestPayload represents a payload information of pull request event.
382340
type PullRequestPayload struct {
383341
Action HookIssueAction `json:"action"`
@@ -402,13 +360,6 @@ type ReviewPayload struct {
402360
Content string `json:"content"`
403361
}
404362

405-
// __ __.__ __ .__
406-
// / \ / \__| | _|__|
407-
// \ \/\/ / | |/ / |
408-
// \ /| | <| |
409-
// \__/\ / |__|__|_ \__|
410-
// \/ \/
411-
412363
// HookWikiAction an action that happens to a wiki page
413364
type HookWikiAction string
414365

@@ -435,13 +386,6 @@ func (p *WikiPayload) JSONPayload() ([]byte, error) {
435386
return json.MarshalIndent(p, "", " ")
436387
}
437388

438-
//__________ .__ __
439-
//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
440-
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
441-
// | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
442-
// |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
443-
// \/ \/|__| \/ \/
444-
445389
// HookRepoAction an action that happens to a repo
446390
type HookRepoAction string
447391

@@ -480,7 +424,7 @@ type PackagePayload struct {
480424
Action HookPackageAction `json:"action"`
481425
Repository *Repository `json:"repository"`
482426
Package *Package `json:"package"`
483-
Organization *User `json:"organization"`
427+
Organization *Organization `json:"organization"`
484428
Sender *User `json:"sender"`
485429
}
486430

modules/webhook/structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type HookEvents struct {
2626
Repository bool `json:"repository"`
2727
Release bool `json:"release"`
2828
Package bool `json:"package"`
29+
Status bool `json:"status"`
2930
}
3031

3132
// HookEvent represents events that will delivery hook.

modules/webhook/type.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ const (
3838
// Event returns the HookEventType as an event string
3939
func (h HookEventType) Event() string {
4040
switch h {
41-
case HookEventCreate:
42-
return "create"
43-
case HookEventDelete:
44-
return "delete"
45-
case HookEventFork:
46-
return "fork"
47-
case HookEventPush:
48-
return "push"
4941
case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone:
5042
return "issues"
5143
case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone,
@@ -59,14 +51,9 @@ func (h HookEventType) Event() string {
5951
return "pull_request_rejected"
6052
case HookEventPullRequestReviewComment:
6153
return "pull_request_comment"
62-
case HookEventWiki:
63-
return "wiki"
64-
case HookEventRepository:
65-
return "repository"
66-
case HookEventRelease:
67-
return "release"
54+
default:
55+
return string(h)
6856
}
69-
return ""
7057
}
7158

7259
// HookType is the type of a webhook

routers/api/v1/utils/hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
205205
Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
206206
Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
207207
Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
208+
Package: util.SliceContainsString(form.Events, string(webhook_module.HookEventPackage), true),
209+
Status: util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true),
208210
},
209211
BranchFilter: form.BranchFilter,
210212
},

services/webhook/dingtalk.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,7 @@ func newDingtalkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_
190190
var pc payloadConvertor[DingtalkPayload] = dingtalkConvertor{}
191191
return newJSONRequest(pc, w, t, true)
192192
}
193+
194+
func init() {
195+
RegisterWebhookRequester(webhook_module.DINGTALK, newDingtalkRequest)
196+
}

services/webhook/discord.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_m
277277
return newJSONRequest(pc, w, t, true)
278278
}
279279

280+
func init() {
281+
RegisterWebhookRequester(webhook_module.DISCORD, newDiscordRequest)
282+
}
283+
280284
func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) {
281285
switch event {
282286
case webhook_module.HookEventPullRequestReviewApproved:

services/webhook/feishu.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,7 @@ func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
170170
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
171171
return newJSONRequest(pc, w, t, true)
172172
}
173+
174+
func init() {
175+
RegisterWebhookRequester(webhook_module.FEISHU, newFeishuRequest)
176+
}

services/webhook/general_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ func packageTestPayload() *api.PackagePayload {
319319
AvatarURL: "http://localhost:3000/user1/avatar",
320320
},
321321
Repository: nil,
322-
Organization: &api.User{
323-
UserName: "org1",
322+
Organization: &api.Organization{
323+
Name: "org1",
324324
AvatarURL: "http://localhost:3000/org1/avatar",
325325
},
326326
Package: &api.Package{

0 commit comments

Comments
 (0)