feat: fijnere webhook-events voor issues en reacties
@@ -0,0 +1,44 @@
1
package i18n
2
3
func init() {
4
addCatalog(en, hookEvEN)
5
addCatalog(nl, hookEvNL)
6
addCatalog(de, hookEvDE)
7
addCatalog(fr, hookEvFR)
8
}
9
10
var hookEvEN = map[string]string{
11
"set.ev.issue_opened": "issue opened",
12
"set.ev.issue_closed": "issue closed",
13
"set.ev.issue_reopened": "issue reopened",
14
"set.ev.issue_updated": "issue edited",
15
"set.ev.issue_comment": "comment on issue",
16
"set.ev.merge_request_comment": "comment on merge request",
17
}
18
19
var hookEvNL = map[string]string{
20
"set.ev.issue_opened": "issue geopend",
21
"set.ev.issue_closed": "issue gesloten",
22
"set.ev.issue_reopened": "issue heropend",
23
"set.ev.issue_updated": "issue bewerkt",
24
"set.ev.issue_comment": "reactie op issue",
25
"set.ev.merge_request_comment": "reactie op merge request",
26
}
27
28
var hookEvDE = map[string]string{
29
"set.ev.issue_opened": "Issue eröffnet",
30
"set.ev.issue_closed": "Issue geschlossen",
31
"set.ev.issue_reopened": "Issue wieder geöffnet",
32
"set.ev.issue_updated": "Issue bearbeitet",
33
"set.ev.issue_comment": "Kommentar zu Issue",
34
"set.ev.merge_request_comment": "Kommentar zu Merge Request",
35
}
36
37
var hookEvFR = map[string]string{
38
"set.ev.issue_opened": "ticket ouvert",
39
"set.ev.issue_closed": "ticket fermé",
40
"set.ev.issue_reopened": "ticket rouvert",
41
"set.ev.issue_updated": "ticket modifié",
42
"set.ev.issue_comment": "commentaire sur un ticket",
43
"set.ev.merge_request_comment": "commentaire sur une merge request",
44
}
@@ -0,0 +1,44 @@1
package i18n2
3
func init() {4
addCatalog(en, hookEvEN)5
addCatalog(nl, hookEvNL)6
addCatalog(de, hookEvDE)7
addCatalog(fr, hookEvFR)8
}9
10
var hookEvEN = map[string]string{11
"set.ev.issue_opened": "issue opened",12
"set.ev.issue_closed": "issue closed",13
"set.ev.issue_reopened": "issue reopened",14
"set.ev.issue_updated": "issue edited",15
"set.ev.issue_comment": "comment on issue",16
"set.ev.merge_request_comment": "comment on merge request",17
}18
19
var hookEvNL = map[string]string{20
"set.ev.issue_opened": "issue geopend",21
"set.ev.issue_closed": "issue gesloten",22
"set.ev.issue_reopened": "issue heropend",23
"set.ev.issue_updated": "issue bewerkt",24
"set.ev.issue_comment": "reactie op issue",25
"set.ev.merge_request_comment": "reactie op merge request",26
}27
28
var hookEvDE = map[string]string{29
"set.ev.issue_opened": "Issue eröffnet",30
"set.ev.issue_closed": "Issue geschlossen",31
"set.ev.issue_reopened": "Issue wieder geöffnet",32
"set.ev.issue_updated": "Issue bearbeitet",33
"set.ev.issue_comment": "Kommentar zu Issue",34
"set.ev.merge_request_comment": "Kommentar zu Merge Request",35
}36
37
var hookEvFR = map[string]string{38
"set.ev.issue_opened": "ticket ouvert",39
"set.ev.issue_closed": "ticket fermé",40
"set.ev.issue_reopened": "ticket rouvert",41
"set.ev.issue_updated": "ticket modifié",42
"set.ev.issue_comment": "commentaire sur un ticket",43
"set.ev.merge_request_comment": "commentaire sur une merge request",44
}@@ -283,7 +283,7 @@ func apiSchemas() map[string]any {
283
283
"preset": str("Preset name, or empty for a custom hook."),
284
284
"url": str("URL template."),
285
285
"method": str("HTTP method."),
286
"events": strArr("Events that trigger the hook."),
286
"events": strArr("Events that trigger the hook: push, merge_request, pipeline_success, pipeline_failure, release_published, issue, issue_opened, issue_closed, issue_reopened, issue_updated, issue_comment, merge_request_comment. issue and merge_request also cover the finer issue_* and merge_request_comment events."),
287
287
"branch_filter": str("Branch filter. Empty matches every branch."),
288
288
"active": boolean("Hook is enabled."),
289
289
}),
@@ -330,7 +330,7 @@ func apiSchemas() map[string]any {
330
330
"name": str("Display name."),
331
331
"preset": str("Preset name."),
332
332
"branch_filter": str("Branch filter."),
333
"events": strArr("Events."),
333
"events": strArr("Events, see the list on the webhook schema."),
334
334
"url": str("URL template."),
335
335
"username": str("Username for the preset."),
336
336
"avatar": str("Avatar URL."),
@@ -283,7 +283,7 @@ func apiSchemas() map[string]any {283
"preset": str("Preset name, or empty for a custom hook."),283
"preset": str("Preset name, or empty for a custom hook."),284
"url": str("URL template."),284
"url": str("URL template."),285
"method": str("HTTP method."),285
"method": str("HTTP method."),286
"events": strArr("Events that trigger the hook."),286
"events": strArr("Events that trigger the hook: push, merge_request, pipeline_success, pipeline_failure, release_published, issue, issue_opened, issue_closed, issue_reopened, issue_updated, issue_comment, merge_request_comment. issue and merge_request also cover the finer issue_* and merge_request_comment events."),287
"branch_filter": str("Branch filter. Empty matches every branch."),287
"branch_filter": str("Branch filter. Empty matches every branch."),288
"active": boolean("Hook is enabled."),288
"active": boolean("Hook is enabled."),289
}),289
}),@@ -330,7 +330,7 @@ func apiSchemas() map[string]any {330
"name": str("Display name."),330
"name": str("Display name."),331
"preset": str("Preset name."),331
"preset": str("Preset name."),332
"branch_filter": str("Branch filter."),332
"branch_filter": str("Branch filter."),333
"events": strArr("Events."),333
"events": strArr("Events, see the list on the webhook schema."),334
"url": str("URL template."),334
"url": str("URL template."),335
"username": str("Username for the preset."),335
"username": str("Username for the preset."),336
"avatar": str("Avatar URL."),336
"avatar": str("Avatar URL."),@@ -836,6 +836,14 @@ type HookDraft struct {
836
836
OnFail bool
837
837
OnRel bool
838
838
OnIssue bool
839
Extra []HookEventChoice
840
}
841
842
// HookEventChoice is een fijner event als vinkje in het webhookformulier.
843
type HookEventChoice struct {
844
Name string
845
Key string
846
On bool
839
847
}
840
848
841
849
func repoURL(repo store.Repo) string { return "/" + repo.FullPath() }
@@ -836,6 +836,14 @@ type HookDraft struct {836
OnFail bool836
OnFail bool837
OnRel bool837
OnRel bool838
OnIssue bool838
OnIssue bool839
Extra []HookEventChoice840
}841
842
// HookEventChoice is een fijner event als vinkje in het webhookformulier.843
type HookEventChoice struct {844
Name string845
Key string846
On bool839
}847
}840
848
841
func repoURL(repo store.Repo) string { return "/" + repo.FullPath() }849
func repoURL(repo store.Repo) string { return "/" + repo.FullPath() }@@ -154,6 +154,7 @@ func draftFrom(h store.Webhook) HookDraft {
154
154
d.OnIssue = true
155
155
}
156
156
}
157
d.Extra = hookEventChoices(h.Events)
157
158
if d.Preset == "" {
158
159
d.Preset = webhook.PresetCustom
159
160
}
@@ -666,3 +667,16 @@ func scopeTab(kind, repoTab string) string {
666
667
}
667
668
return "?tab=" + repoTab
668
669
}
670
671
// hookEventChoices zet de fijnere events klaar voor het formulier.
672
func hookEventChoices(selected []string) []HookEventChoice {
673
on := map[string]bool{}
674
for _, e := range selected {
675
on[e] = true
676
}
677
var out []HookEventChoice
678
for _, e := range webhook.SpecificEvents() {
679
out = append(out, HookEventChoice{Name: e, Key: "set.ev." + e, On: on[e]})
680
}
681
return out
682
}
@@ -154,6 +154,7 @@ func draftFrom(h store.Webhook) HookDraft {154
d.OnIssue = true154
d.OnIssue = true155
}155
}156
}156
}157
d.Extra = hookEventChoices(h.Events)157
if d.Preset == "" {158
if d.Preset == "" {158
d.Preset = webhook.PresetCustom159
d.Preset = webhook.PresetCustom159
}160
}@@ -666,3 +667,16 @@ func scopeTab(kind, repoTab string) string {666
}667
}667
return "?tab=" + repoTab668
return "?tab=" + repoTab668
}669
}670
671
// hookEventChoices zet de fijnere events klaar voor het formulier.672
func hookEventChoices(selected []string) []HookEventChoice {673
on := map[string]bool{}674
for _, e := range selected {675
on[e] = true676
}677
var out []HookEventChoice678
for _, e := range webhook.SpecificEvents() {679
out = append(out, HookEventChoice{Name: e, Key: "set.ev." + e, On: on[e]})680
}681
return out682
}@@ -804,6 +804,7 @@
804
804
<label><input type="checkbox" name="event_pipeline_failure" value="1" {{if .HookEdit.OnFail}}checked{{end}}> {{t "set.ev.fail"}}</label>
805
805
<label><input type="checkbox" name="event_release_published" value="1" {{if .HookEdit.OnRel}}checked{{end}}> {{t "set.ev.release"}}</label>
806
806
<label><input type="checkbox" name="event_issue" value="1" {{if .HookEdit.OnIssue}}checked{{end}}> {{t "set.ev.issue"}}</label>
807
{{range .HookEdit.Extra}}<label><input type="checkbox" name="event_{{.Name}}" value="1" {{if .On}}checked{{end}}> {{t .Key}}</label>{{end}}
807
808
</div>
808
809
<div class="acts">
809
810
<button class="fit" type="submit">{{if .HookEdit.ID}}{{t "wh.update"}}{{else}}{{t "set.saveWebhook"}}{{end}}</button>
@@ -804,6 +804,7 @@804
<label><input type="checkbox" name="event_pipeline_failure" value="1" {{if .HookEdit.OnFail}}checked{{end}}> {{t "set.ev.fail"}}</label>804
<label><input type="checkbox" name="event_pipeline_failure" value="1" {{if .HookEdit.OnFail}}checked{{end}}> {{t "set.ev.fail"}}</label>805
<label><input type="checkbox" name="event_release_published" value="1" {{if .HookEdit.OnRel}}checked{{end}}> {{t "set.ev.release"}}</label>805
<label><input type="checkbox" name="event_release_published" value="1" {{if .HookEdit.OnRel}}checked{{end}}> {{t "set.ev.release"}}</label>806
<label><input type="checkbox" name="event_issue" value="1" {{if .HookEdit.OnIssue}}checked{{end}}> {{t "set.ev.issue"}}</label>806
<label><input type="checkbox" name="event_issue" value="1" {{if .HookEdit.OnIssue}}checked{{end}}> {{t "set.ev.issue"}}</label>807
{{range .HookEdit.Extra}}<label><input type="checkbox" name="event_{{.Name}}" value="1" {{if .On}}checked{{end}}> {{t .Key}}</label>{{end}}807
</div>808
</div>808
<div class="acts">809
<div class="acts">809
<button class="fit" type="submit">{{if .HookEdit.ID}}{{t "wh.update"}}{{else}}{{t "set.saveWebhook"}}{{end}}</button>810
<button class="fit" type="submit">{{if .HookEdit.ID}}{{t "wh.update"}}{{else}}{{t "set.saveWebhook"}}{{end}}</button>@@ -271,7 +271,16 @@ func OpenAPI() []byte {
271
271
"actor": str("User who did this action."),
272
272
"merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},
273
273
"comment": str("Comment text. Present only when action is commented."),
274
}, []string{"object_kind", "type", "project", "merge_request"}),
274
"event_name": str("merge_request_comment for a comment, otherwise merge_request."),
275
"note": map[string]any{"$ref": "#/components/schemas/Note"},
276
}, []string{"object_kind", "type", "event_name", "project", "merge_request"}),
277
"Note": obj("A comment. Present only when action is commented.", map[string]any{
278
"body": str("Comment text."),
279
"noteable_type": str("Issue or MergeRequest."),
280
"noteable_id": map[string]any{"type": "integer", "description": "Number of the issue or merge request."},
281
"author": str("User who wrote the comment."),
282
"url": str("Page of the issue or merge request."),
283
}, []string{"body", "noteable_type", "noteable_id", "url"}),
275
284
"MergeRequest": obj("The merge request.", map[string]any{
276
285
"id": map[string]any{"type": "integer", "description": "Number in the project."},
277
286
"title": str("Title."),
@@ -334,7 +343,9 @@ func OpenAPI() []byte {
334
343
"url": str("Issue page."),
335
344
"issue": map[string]any{"$ref": "#/components/schemas/Issue"},
336
345
"comment": str("Comment text. Present only when action is commented."),
337
}, []string{"object_kind", "type", "project", "issue"}),
346
"event_name": str("issue_opened, issue_closed, issue_reopened, issue_updated or issue_comment."),
347
"note": map[string]any{"$ref": "#/components/schemas/Note"},
348
}, []string{"object_kind", "type", "event_name", "project", "issue"}),
338
349
"Issue": obj("The issue this event is about.", map[string]any{
339
350
"id": map[string]any{"type": "integer", "description": "Number in the project."},
340
351
"title": str("Title."),
@@ -271,7 +271,16 @@ func OpenAPI() []byte {271
"actor": str("User who did this action."),271
"actor": str("User who did this action."),272
"merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},272
"merge_request": map[string]any{"$ref": "#/components/schemas/MergeRequest"},273
"comment": str("Comment text. Present only when action is commented."),273
"comment": str("Comment text. Present only when action is commented."),274
}, []string{"object_kind", "type", "project", "merge_request"}),274
"event_name": str("merge_request_comment for a comment, otherwise merge_request."),275
"note": map[string]any{"$ref": "#/components/schemas/Note"},276
}, []string{"object_kind", "type", "event_name", "project", "merge_request"}),277
"Note": obj("A comment. Present only when action is commented.", map[string]any{278
"body": str("Comment text."),279
"noteable_type": str("Issue or MergeRequest."),280
"noteable_id": map[string]any{"type": "integer", "description": "Number of the issue or merge request."},281
"author": str("User who wrote the comment."),282
"url": str("Page of the issue or merge request."),283
}, []string{"body", "noteable_type", "noteable_id", "url"}),275
"MergeRequest": obj("The merge request.", map[string]any{284
"MergeRequest": obj("The merge request.", map[string]any{276
"id": map[string]any{"type": "integer", "description": "Number in the project."},285
"id": map[string]any{"type": "integer", "description": "Number in the project."},277
"title": str("Title."),286
"title": str("Title."),@@ -334,7 +343,9 @@ func OpenAPI() []byte {334
"url": str("Issue page."),343
"url": str("Issue page."),335
"issue": map[string]any{"$ref": "#/components/schemas/Issue"},344
"issue": map[string]any{"$ref": "#/components/schemas/Issue"},336
"comment": str("Comment text. Present only when action is commented."),345
"comment": str("Comment text. Present only when action is commented."),337
}, []string{"object_kind", "type", "project", "issue"}),346
"event_name": str("issue_opened, issue_closed, issue_reopened, issue_updated or issue_comment."),347
"note": map[string]any{"$ref": "#/components/schemas/Note"},348
}, []string{"object_kind", "type", "event_name", "project", "issue"}),338
"Issue": obj("The issue this event is about.", map[string]any{349
"Issue": obj("The issue this event is about.", map[string]any{339
"id": map[string]any{"type": "integer", "description": "Number in the project."},350
"id": map[string]any{"type": "integer", "description": "Number in the project."},340
"title": str("Title."),351
"title": str("Title."),@@ -21,6 +21,7 @@ const MaxPushCommits = 20
21
21
type Document struct {
22
22
ObjectKind string `json:"object_kind"`
23
23
Type string `json:"type"`
24
EventName string `json:"event_name"`
24
25
Project string `json:"project"`
25
26
Group string `json:"group,omitempty"`
26
27
Actor string `json:"actor,omitempty"`
@@ -44,6 +45,16 @@ type Document struct {
44
45
45
46
Release *ReleaseInfo `json:"release,omitempty"`
46
47
Issue *IssueInfo `json:"issue,omitempty"`
48
Note *NoteInfo `json:"note,omitempty"`
49
}
50
51
// NoteInfo is een reactie op een issue of merge request.
52
type NoteInfo struct {
53
Body string `json:"body"`
54
NoteableType string `json:"noteable_type"`
55
NoteableID int64 `json:"noteable_id"`
56
Author string `json:"author"`
57
URL string `json:"url"`
47
58
}
48
59
49
60
// PipelineInfo is de pipeline waar het event over gaat.
@@ -101,6 +112,7 @@ type FileChange struct {
101
112
// DiffStats is de three-dot vergelijking. De difftekst staat op diff_url.
102
113
type MRInfo struct {
103
114
ID int64 `json:"id"`
115
Number int64 `json:"number"`
104
116
Title string `json:"title"`
105
117
Description string `json:"description"`
106
118
SourceBranch string `json:"source_branch"`
@@ -145,6 +157,7 @@ type DiffStats struct {
145
157
// Action is opened, updated, closed, reopened of commented.
146
158
type IssueInfo struct {
147
159
ID int64 `json:"id"`
160
Number int64 `json:"number"`
148
161
Title string `json:"title"`
149
162
Description string `json:"description"`
150
163
Status string `json:"status"`
@@ -195,6 +208,10 @@ func (ev Event) Document() Document {
195
208
Release: ev.Release,
196
209
Issue: ev.Issue,
197
210
}
211
d.EventName = Specific(ev)
212
if d.EventName == "" {
213
d.EventName = ev.Type
214
}
198
215
switch ev.Type {
199
216
case EventPush:
200
217
commits := ev.Commits
@@ -216,6 +233,9 @@ func (ev Event) Document() Document {
216
233
case EventMergeRequest:
217
234
if ev.MergeRequest != nil {
218
235
mr := *ev.MergeRequest
236
if mr.Number == 0 {
237
mr.Number = mr.ID
238
}
219
239
if mr.Reviewers == nil {
220
240
mr.Reviewers = []ReviewerInfo{}
221
241
}
@@ -236,11 +256,15 @@ func (ev Event) Document() Document {
236
256
d.MergeRequest = &mr
237
257
if mr.Action == "commented" {
238
258
d.Comment = ev.Comment
259
d.Note = &NoteInfo{Body: ev.Comment, NoteableType: "MergeRequest", NoteableID: mr.ID, Author: ev.Actor, URL: firstNonEmpty(mr.URL, ev.URL)}
239
260
}
240
261
}
241
262
case EventIssue:
242
263
if ev.Issue != nil {
243
264
iss := *ev.Issue
265
if iss.Number == 0 {
266
iss.Number = iss.ID
267
}
244
268
if iss.Assignees == nil {
245
269
iss.Assignees = []string{}
246
270
}
@@ -250,6 +274,7 @@ func (ev Event) Document() Document {
250
274
d.Issue = &iss
251
275
if iss.Action == "commented" {
252
276
d.Comment = ev.Comment
277
d.Note = &NoteInfo{Body: ev.Comment, NoteableType: "Issue", NoteableID: iss.ID, Author: ev.Actor, URL: firstNonEmpty(iss.URL, ev.URL)}
253
278
}
254
279
}
255
280
}
@@ -415,3 +440,10 @@ func IssueExample() string {
415
440
}
416
441
return string(raw)
417
442
}
443
444
func firstNonEmpty(a, b string) string {
445
if a != "" {
446
return a
447
}
448
return b
449
}
@@ -21,6 +21,7 @@ const MaxPushCommits = 2021
type Document struct {21
type Document struct {22
ObjectKind string `json:"object_kind"`22
ObjectKind string `json:"object_kind"`23
Type string `json:"type"`23
Type string `json:"type"`24
EventName string `json:"event_name"`24
Project string `json:"project"`25
Project string `json:"project"`25
Group string `json:"group,omitempty"`26
Group string `json:"group,omitempty"`26
Actor string `json:"actor,omitempty"`27
Actor string `json:"actor,omitempty"`@@ -44,6 +45,16 @@ type Document struct {44
45
45
Release *ReleaseInfo `json:"release,omitempty"`46
Release *ReleaseInfo `json:"release,omitempty"`46
Issue *IssueInfo `json:"issue,omitempty"`47
Issue *IssueInfo `json:"issue,omitempty"`48
Note *NoteInfo `json:"note,omitempty"`49
}50
51
// NoteInfo is een reactie op een issue of merge request.52
type NoteInfo struct {53
Body string `json:"body"`54
NoteableType string `json:"noteable_type"`55
NoteableID int64 `json:"noteable_id"`56
Author string `json:"author"`57
URL string `json:"url"`47
}58
}48
59
49
// PipelineInfo is de pipeline waar het event over gaat.60
// PipelineInfo is de pipeline waar het event over gaat.@@ -101,6 +112,7 @@ type FileChange struct {101
// DiffStats is de three-dot vergelijking. De difftekst staat op diff_url.112
// DiffStats is de three-dot vergelijking. De difftekst staat op diff_url.102
type MRInfo struct {113
type MRInfo struct {103
ID int64 `json:"id"`114
ID int64 `json:"id"`115
Number int64 `json:"number"`104
Title string `json:"title"`116
Title string `json:"title"`105
Description string `json:"description"`117
Description string `json:"description"`106
SourceBranch string `json:"source_branch"`118
SourceBranch string `json:"source_branch"`@@ -145,6 +157,7 @@ type DiffStats struct {145
// Action is opened, updated, closed, reopened of commented.157
// Action is opened, updated, closed, reopened of commented.146
type IssueInfo struct {158
type IssueInfo struct {147
ID int64 `json:"id"`159
ID int64 `json:"id"`160
Number int64 `json:"number"`148
Title string `json:"title"`161
Title string `json:"title"`149
Description string `json:"description"`162
Description string `json:"description"`150
Status string `json:"status"`163
Status string `json:"status"`@@ -195,6 +208,10 @@ func (ev Event) Document() Document {195
Release: ev.Release,208
Release: ev.Release,196
Issue: ev.Issue,209
Issue: ev.Issue,197
}210
}211
d.EventName = Specific(ev)212
if d.EventName == "" {213
d.EventName = ev.Type214
}198
switch ev.Type {215
switch ev.Type {199
case EventPush:216
case EventPush:200
commits := ev.Commits217
commits := ev.Commits@@ -216,6 +233,9 @@ func (ev Event) Document() Document {216
case EventMergeRequest:233
case EventMergeRequest:217
if ev.MergeRequest != nil {234
if ev.MergeRequest != nil {218
mr := *ev.MergeRequest235
mr := *ev.MergeRequest236
if mr.Number == 0 {237
mr.Number = mr.ID238
}219
if mr.Reviewers == nil {239
if mr.Reviewers == nil {220
mr.Reviewers = []ReviewerInfo{}240
mr.Reviewers = []ReviewerInfo{}221
}241
}@@ -236,11 +256,15 @@ func (ev Event) Document() Document {236
d.MergeRequest = &mr256
d.MergeRequest = &mr237
if mr.Action == "commented" {257
if mr.Action == "commented" {238
d.Comment = ev.Comment258
d.Comment = ev.Comment259
d.Note = &NoteInfo{Body: ev.Comment, NoteableType: "MergeRequest", NoteableID: mr.ID, Author: ev.Actor, URL: firstNonEmpty(mr.URL, ev.URL)}239
}260
}240
}261
}241
case EventIssue:262
case EventIssue:242
if ev.Issue != nil {263
if ev.Issue != nil {243
iss := *ev.Issue264
iss := *ev.Issue265
if iss.Number == 0 {266
iss.Number = iss.ID267
}244
if iss.Assignees == nil {268
if iss.Assignees == nil {245
iss.Assignees = []string{}269
iss.Assignees = []string{}246
}270
}@@ -250,6 +274,7 @@ func (ev Event) Document() Document {250
d.Issue = &iss274
d.Issue = &iss251
if iss.Action == "commented" {275
if iss.Action == "commented" {252
d.Comment = ev.Comment276
d.Comment = ev.Comment277
d.Note = &NoteInfo{Body: ev.Comment, NoteableType: "Issue", NoteableID: iss.ID, Author: ev.Actor, URL: firstNonEmpty(iss.URL, ev.URL)}253
}278
}254
}279
}255
}280
}@@ -415,3 +440,10 @@ func IssueExample() string {415
}440
}416
return string(raw)441
return string(raw)417
}442
}443
444
func firstNonEmpty(a, b string) string {445
if a != "" {446
return a447
}448
return b449
}@@ -0,0 +1,65 @@
1
package webhook
2
3
import (
4
"encoding/json"
5
"testing"
6
)
7
8
func TestSpecificEvents(t *testing.T) {
9
iss := func(action string) Event {
10
return Event{Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoi", URL: "https://git.example/tim/app/-/issues/7",
11
Issue: &IssueInfo{ID: 7, Title: "T", Status: "open", Action: action, URL: "https://git.example/tim/app/-/issues/7"}}
12
}
13
cases := map[string]string{"opened": EventIssueOpened, "closed": EventIssueClosed, "reopened": EventIssueReopened, "updated": EventIssueUpdated, "commented": EventIssueComment}
14
for action, want := range cases {
15
ev := iss(action)
16
if got := Specific(ev); got != want {
17
t.Fatalf("%s: %s", action, got)
18
}
19
if ok, _ := Allows(Hook{Events: []string{want}}, ev); !ok {
20
t.Fatalf("%s niet toegestaan", want)
21
}
22
if ok, _ := Allows(Hook{Events: []string{EventIssue}}, ev); !ok {
23
t.Fatalf("issue dekt %s niet", want)
24
}
25
for _, other := range SpecificEvents() {
26
if other == want {
27
continue
28
}
29
if ok, _ := Allows(Hook{Events: []string{other}}, ev); ok {
30
t.Fatalf("%s krijgt %s", other, want)
31
}
32
}
33
}
34
mr := Event{Type: EventMergeRequest, Project: "tim/app", Actor: "ada", Comment: "lgtm",
35
MergeRequest: &MRInfo{ID: 3, Title: "M", Status: "open", Action: "commented", URL: "https://git.example/tim/app/-/merge-requests/3"}}
36
if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, mr); !ok {
37
t.Fatal("mr comment")
38
}
39
opened := mr
40
info := *mr.MergeRequest
41
info.Action = "opened"
42
opened.MergeRequest = &info
43
if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, opened); ok {
44
t.Fatal("geopende MR is geen comment")
45
}
46
raw, _ := json.Marshal(mr.Document())
47
var d map[string]any
48
_ = json.Unmarshal(raw, &d)
49
note, _ := d["note"].(map[string]any)
50
if d["event_name"] != EventMRComment || note["body"] != "lgtm" || note["noteable_type"] != "MergeRequest" || note["url"] == "" {
51
t.Fatalf("payload %s", raw)
52
}
53
m, _ := d["merge_request"].(map[string]any)
54
if m["number"].(float64) != 3 {
55
t.Fatalf("number %s", raw)
56
}
57
raw, _ = json.Marshal(iss("closed").Document())
58
_ = json.Unmarshal(raw, &d)
59
if d["event_name"] != EventIssueClosed {
60
t.Fatalf("issue %s", raw)
61
}
62
if s, ok := eventField(iss("commented"), "event_name"); !ok || s != EventIssueComment {
63
t.Fatal(s)
64
}
65
}
@@ -0,0 +1,65 @@1
package webhook2
3
import (4
"encoding/json"5
"testing"6
)7
8
func TestSpecificEvents(t *testing.T) {9
iss := func(action string) Event {10
return Event{Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoi", URL: "https://git.example/tim/app/-/issues/7",11
Issue: &IssueInfo{ID: 7, Title: "T", Status: "open", Action: action, URL: "https://git.example/tim/app/-/issues/7"}}12
}13
cases := map[string]string{"opened": EventIssueOpened, "closed": EventIssueClosed, "reopened": EventIssueReopened, "updated": EventIssueUpdated, "commented": EventIssueComment}14
for action, want := range cases {15
ev := iss(action)16
if got := Specific(ev); got != want {17
t.Fatalf("%s: %s", action, got)18
}19
if ok, _ := Allows(Hook{Events: []string{want}}, ev); !ok {20
t.Fatalf("%s niet toegestaan", want)21
}22
if ok, _ := Allows(Hook{Events: []string{EventIssue}}, ev); !ok {23
t.Fatalf("issue dekt %s niet", want)24
}25
for _, other := range SpecificEvents() {26
if other == want {27
continue28
}29
if ok, _ := Allows(Hook{Events: []string{other}}, ev); ok {30
t.Fatalf("%s krijgt %s", other, want)31
}32
}33
}34
mr := Event{Type: EventMergeRequest, Project: "tim/app", Actor: "ada", Comment: "lgtm",35
MergeRequest: &MRInfo{ID: 3, Title: "M", Status: "open", Action: "commented", URL: "https://git.example/tim/app/-/merge-requests/3"}}36
if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, mr); !ok {37
t.Fatal("mr comment")38
}39
opened := mr40
info := *mr.MergeRequest41
info.Action = "opened"42
opened.MergeRequest = &info43
if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, opened); ok {44
t.Fatal("geopende MR is geen comment")45
}46
raw, _ := json.Marshal(mr.Document())47
var d map[string]any48
_ = json.Unmarshal(raw, &d)49
note, _ := d["note"].(map[string]any)50
if d["event_name"] != EventMRComment || note["body"] != "lgtm" || note["noteable_type"] != "MergeRequest" || note["url"] == "" {51
t.Fatalf("payload %s", raw)52
}53
m, _ := d["merge_request"].(map[string]any)54
if m["number"].(float64) != 3 {55
t.Fatalf("number %s", raw)56
}57
raw, _ = json.Marshal(iss("closed").Document())58
_ = json.Unmarshal(raw, &d)59
if d["event_name"] != EventIssueClosed {60
t.Fatalf("issue %s", raw)61
}62
if s, ok := eventField(iss("commented"), "event_name"); !ok || s != EventIssueComment {63
t.Fatal(s)64
}65
}@@ -17,11 +17,52 @@ const (
17
17
EventPipelineFailure = "pipeline_failure"
18
18
EventRelease = "release_published"
19
19
EventIssue = "issue"
20
21
// Fijnere events. Een hook met "issue" of "merge_request" krijgt ze ook.
22
EventIssueOpened = "issue_opened"
23
EventIssueClosed = "issue_closed"
24
EventIssueReopened = "issue_reopened"
25
EventIssueUpdated = "issue_updated"
26
EventIssueComment = "issue_comment"
27
EventMRComment = "merge_request_comment"
20
28
)
21
29
22
30
// AllEvents is de vaste lijst die een webhook mag aanvinken.
23
31
func AllEvents() []string {
24
return []string{EventPush, EventMergeRequest, EventPipelineSuccess, EventPipelineFailure, EventRelease, EventIssue}
32
return []string{EventPush, EventMergeRequest, EventPipelineSuccess, EventPipelineFailure, EventRelease, EventIssue,
33
EventIssueOpened, EventIssueClosed, EventIssueReopened, EventIssueUpdated, EventIssueComment, EventMRComment}
34
}
35
36
// SpecificEvents zijn de fijnere events die bij issue en merge_request horen.
37
func SpecificEvents() []string {
38
return []string{EventIssueOpened, EventIssueClosed, EventIssueReopened, EventIssueUpdated, EventIssueComment, EventMRComment}
39
}
40
41
// Specific geeft de fijnere eventnaam van ev, of "" als die er niet is.
42
func Specific(ev Event) string {
43
switch ev.Type {
44
case EventIssue:
45
if ev.Issue == nil {
46
return ""
47
}
48
switch ev.Issue.Action {
49
case "opened":
50
return EventIssueOpened
51
case "closed":
52
return EventIssueClosed
53
case "reopened":
54
return EventIssueReopened
55
case "updated":
56
return EventIssueUpdated
57
case "commented":
58
return EventIssueComment
59
}
60
case EventMergeRequest:
61
if ev.MergeRequest != nil && ev.MergeRequest.Action == "commented" {
62
return EventMRComment
63
}
64
}
65
return ""
25
66
}
26
67
27
68
// KnownEvent meldt of name een ondersteunde gebeurtenis is.
@@ -84,8 +125,9 @@ func Allows(h Hook, ev Event) (bool, error) {
84
125
return false, i18n.E("err.whEvent", ev.Type)
85
126
}
86
127
found := false
128
specific := Specific(ev)
87
129
for _, e := range h.Events {
88
if e == ev.Type {
130
if e == ev.Type || (specific != "" && e == specific) {
89
131
found = true
90
132
break
91
133
}
@@ -179,6 +221,11 @@ func eventField(ev Event, field string) (string, bool) {
179
221
switch field {
180
222
case "type":
181
223
return ev.Type, true
224
case "event_name":
225
if n := Specific(ev); n != "" {
226
return n, true
227
}
228
return ev.Type, true
182
229
case "project":
183
230
return ev.Project, true
184
231
case "group":
@@ -17,11 +17,52 @@ const (17
EventPipelineFailure = "pipeline_failure"17
EventPipelineFailure = "pipeline_failure"18
EventRelease = "release_published"18
EventRelease = "release_published"19
EventIssue = "issue"19
EventIssue = "issue"20
21
// Fijnere events. Een hook met "issue" of "merge_request" krijgt ze ook.22
EventIssueOpened = "issue_opened"23
EventIssueClosed = "issue_closed"24
EventIssueReopened = "issue_reopened"25
EventIssueUpdated = "issue_updated"26
EventIssueComment = "issue_comment"27
EventMRComment = "merge_request_comment"20
)28
)21
29
22
// AllEvents is de vaste lijst die een webhook mag aanvinken.30
// AllEvents is de vaste lijst die een webhook mag aanvinken.23
func AllEvents() []string {31
func AllEvents() []string {24
return []string{EventPush, EventMergeRequest, EventPipelineSuccess, EventPipelineFailure, EventRelease, EventIssue}32
return []string{EventPush, EventMergeRequest, EventPipelineSuccess, EventPipelineFailure, EventRelease, EventIssue,33
EventIssueOpened, EventIssueClosed, EventIssueReopened, EventIssueUpdated, EventIssueComment, EventMRComment}34
}35
36
// SpecificEvents zijn de fijnere events die bij issue en merge_request horen.37
func SpecificEvents() []string {38
return []string{EventIssueOpened, EventIssueClosed, EventIssueReopened, EventIssueUpdated, EventIssueComment, EventMRComment}39
}40
41
// Specific geeft de fijnere eventnaam van ev, of "" als die er niet is.42
func Specific(ev Event) string {43
switch ev.Type {44
case EventIssue:45
if ev.Issue == nil {46
return ""47
}48
switch ev.Issue.Action {49
case "opened":50
return EventIssueOpened51
case "closed":52
return EventIssueClosed53
case "reopened":54
return EventIssueReopened55
case "updated":56
return EventIssueUpdated57
case "commented":58
return EventIssueComment59
}60
case EventMergeRequest:61
if ev.MergeRequest != nil && ev.MergeRequest.Action == "commented" {62
return EventMRComment63
}64
}65
return ""25
}66
}26
67
27
// KnownEvent meldt of name een ondersteunde gebeurtenis is.68
// KnownEvent meldt of name een ondersteunde gebeurtenis is.@@ -84,8 +125,9 @@ func Allows(h Hook, ev Event) (bool, error) {84
return false, i18n.E("err.whEvent", ev.Type)125
return false, i18n.E("err.whEvent", ev.Type)85
}126
}86
found := false127
found := false128
specific := Specific(ev)87
for _, e := range h.Events {129
for _, e := range h.Events {88
if e == ev.Type {130
if e == ev.Type || (specific != "" && e == specific) {89
found = true131
found = true90
break132
break91
}133
}@@ -179,6 +221,11 @@ func eventField(ev Event, field string) (string, bool) {179
switch field {221
switch field {180
case "type":222
case "type":181
return ev.Type, true223
return ev.Type, true224
case "event_name":225
if n := Specific(ev); n != "" {226
return n, true227
}228
return ev.Type, true182
case "project":229
case "project":183
return ev.Project, true230
return ev.Project, true184
case "group":231
case "group":