Skip to content

Commit 462b1f5

Browse files
committed
Fix edithook api can not update package, status and workflow_job events
* the origin of this problem is duplicated code
1 parent b6c0667 commit 462b1f5

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

routers/api/v1/utils/hook.go

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,41 @@ func pullHook(events []string, event string) bool {
154154
return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true)
155155
}
156156

157+
func updateHookEvents(events []string) webhook_module.HookEvents {
158+
if len(events) == 0 {
159+
events = []string{"push"}
160+
}
161+
hookEvents := make(webhook_module.HookEvents)
162+
hookEvents[webhook_module.HookEventCreate] = util.SliceContainsString(events, string(webhook_module.HookEventCreate), true)
163+
hookEvents[webhook_module.HookEventPush] = util.SliceContainsString(events, string(webhook_module.HookEventPush), true)
164+
hookEvents[webhook_module.HookEventDelete] = util.SliceContainsString(events, string(webhook_module.HookEventDelete), true)
165+
hookEvents[webhook_module.HookEventFork] = util.SliceContainsString(events, string(webhook_module.HookEventFork), true)
166+
hookEvents[webhook_module.HookEventRepository] = util.SliceContainsString(events, string(webhook_module.HookEventRepository), true)
167+
hookEvents[webhook_module.HookEventWiki] = util.SliceContainsString(events, string(webhook_module.HookEventWiki), true)
168+
hookEvents[webhook_module.HookEventRelease] = util.SliceContainsString(events, string(webhook_module.HookEventRelease), true)
169+
hookEvents[webhook_module.HookEventPackage] = util.SliceContainsString(events, string(webhook_module.HookEventPackage), true)
170+
hookEvents[webhook_module.HookEventStatus] = util.SliceContainsString(events, string(webhook_module.HookEventStatus), true)
171+
hookEvents[webhook_module.HookEventWorkflowJob] = util.SliceContainsString(events, string(webhook_module.HookEventWorkflowJob), true)
172+
173+
// Issues
174+
hookEvents[webhook_module.HookEventIssues] = issuesHook(events, "issues_only")
175+
hookEvents[webhook_module.HookEventIssueAssign] = issuesHook(events, string(webhook_module.HookEventIssueAssign))
176+
hookEvents[webhook_module.HookEventIssueLabel] = issuesHook(events, string(webhook_module.HookEventIssueLabel))
177+
hookEvents[webhook_module.HookEventIssueMilestone] = issuesHook(events, string(webhook_module.HookEventIssueMilestone))
178+
hookEvents[webhook_module.HookEventIssueComment] = issuesHook(events, string(webhook_module.HookEventIssueComment))
179+
180+
// Pull requests
181+
hookEvents[webhook_module.HookEventPullRequest] = pullHook(events, "pull_request_only")
182+
hookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(events, string(webhook_module.HookEventPullRequestAssign))
183+
hookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(events, string(webhook_module.HookEventPullRequestLabel))
184+
hookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(events, string(webhook_module.HookEventPullRequestMilestone))
185+
hookEvents[webhook_module.HookEventPullRequestComment] = pullHook(events, string(webhook_module.HookEventPullRequestComment))
186+
hookEvents[webhook_module.HookEventPullRequestReview] = pullHook(events, "pull_request_review")
187+
hookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(events, string(webhook_module.HookEventPullRequestReviewRequest))
188+
hookEvents[webhook_module.HookEventPullRequestSync] = pullHook(events, string(webhook_module.HookEventPullRequestSync))
189+
return hookEvents
190+
}
191+
157192
// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is
158193
// an error, write to `ctx` accordingly. Return (webhook, ok)
159194
func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
@@ -162,9 +197,6 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
162197
return nil, false
163198
}
164199

165-
if len(form.Events) == 0 {
166-
form.Events = []string{"push"}
167-
}
168200
if form.Config["is_system_webhook"] != "" {
169201
sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
170202
if err != nil {
@@ -183,31 +215,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
183215
IsSystemWebhook: isSystemWebhook,
184216
HookEvent: &webhook_module.HookEvent{
185217
ChooseEvents: true,
186-
HookEvents: webhook_module.HookEvents{
187-
webhook_module.HookEventCreate: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true),
188-
webhook_module.HookEventDelete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true),
189-
webhook_module.HookEventFork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true),
190-
webhook_module.HookEventIssues: issuesHook(form.Events, "issues_only"),
191-
webhook_module.HookEventIssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)),
192-
webhook_module.HookEventIssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)),
193-
webhook_module.HookEventIssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)),
194-
webhook_module.HookEventIssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)),
195-
webhook_module.HookEventPush: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true),
196-
webhook_module.HookEventPullRequest: pullHook(form.Events, "pull_request_only"),
197-
webhook_module.HookEventPullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)),
198-
webhook_module.HookEventPullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)),
199-
webhook_module.HookEventPullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)),
200-
webhook_module.HookEventPullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)),
201-
webhook_module.HookEventPullRequestReview: pullHook(form.Events, "pull_request_review"),
202-
webhook_module.HookEventPullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)),
203-
webhook_module.HookEventPullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)),
204-
webhook_module.HookEventWiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
205-
webhook_module.HookEventRepository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
206-
webhook_module.HookEventRelease: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
207-
webhook_module.HookEventPackage: util.SliceContainsString(form.Events, string(webhook_module.HookEventPackage), true),
208-
webhook_module.HookEventStatus: util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true),
209-
webhook_module.HookEventWorkflowJob: util.SliceContainsString(form.Events, string(webhook_module.HookEventWorkflowJob), true),
210-
},
218+
HookEvents: updateHookEvents(form.Events),
211219
BranchFilter: form.BranchFilter,
212220
},
213221
IsActive: form.Active,
@@ -352,19 +360,10 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
352360
}
353361

354362
// Update events
355-
if len(form.Events) == 0 {
356-
form.Events = []string{"push"}
357-
}
363+
w.HookEvents = updateHookEvents(form.Events)
358364
w.PushOnly = false
359365
w.SendEverything = false
360366
w.ChooseEvents = true
361-
w.HookEvents[webhook_module.HookEventCreate] = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
362-
w.HookEvents[webhook_module.HookEventPush] = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true)
363-
w.HookEvents[webhook_module.HookEventDelete] = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true)
364-
w.HookEvents[webhook_module.HookEventFork] = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true)
365-
w.HookEvents[webhook_module.HookEventRepository] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true)
366-
w.HookEvents[webhook_module.HookEventWiki] = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true)
367-
w.HookEvents[webhook_module.HookEventRelease] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true)
368367
w.BranchFilter = form.BranchFilter
369368

370369
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
@@ -373,23 +372,6 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
373372
return false
374373
}
375374

376-
// Issues
377-
w.HookEvents[webhook_module.HookEventIssues] = issuesHook(form.Events, "issues_only")
378-
w.HookEvents[webhook_module.HookEventIssueAssign] = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign))
379-
w.HookEvents[webhook_module.HookEventIssueLabel] = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel))
380-
w.HookEvents[webhook_module.HookEventIssueMilestone] = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone))
381-
w.HookEvents[webhook_module.HookEventIssueComment] = issuesHook(form.Events, string(webhook_module.HookEventIssueComment))
382-
383-
// Pull requests
384-
w.HookEvents[webhook_module.HookEventPullRequest] = pullHook(form.Events, "pull_request_only")
385-
w.HookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
386-
w.HookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
387-
w.HookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
388-
w.HookEvents[webhook_module.HookEventPullRequestComment] = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment))
389-
w.HookEvents[webhook_module.HookEventPullRequestReview] = pullHook(form.Events, "pull_request_review")
390-
w.HookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest))
391-
w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
392-
393375
if err := w.UpdateEvent(); err != nil {
394376
ctx.APIErrorInternal(err)
395377
return false

0 commit comments

Comments
 (0)