Skip to content

Commit 39b507e

Browse files
authored
Trigger webhooks on issue label-change via API too (#10421)
* trigger webhooks with api too * fix comment * notify report old too * CI restart * restart CI again * remove duplicated code
1 parent 062f351 commit 39b507e

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

routers/api/v1/repo/issue_label.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,8 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
102102
// "403":
103103
// "$ref": "#/responses/forbidden"
104104

105-
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
105+
issue, labels, err := prepareForReplaceOrAdd(ctx, form)
106106
if err != nil {
107-
if models.IsErrIssueNotExist(err) {
108-
ctx.NotFound()
109-
} else {
110-
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
111-
}
112-
return
113-
}
114-
115-
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
116-
ctx.Status(http.StatusForbidden)
117-
return
118-
}
119-
120-
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
121-
if err != nil {
122-
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
123107
return
124108
}
125109

@@ -204,7 +188,7 @@ func DeleteIssueLabel(ctx *context.APIContext) {
204188
return
205189
}
206190

207-
if err := models.DeleteIssueLabel(issue, label, ctx.User); err != nil {
191+
if err := issue_service.RemoveLabel(issue, ctx.User, label); err != nil {
208192
ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err)
209193
return
210194
}
@@ -248,28 +232,12 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
248232
// "403":
249233
// "$ref": "#/responses/forbidden"
250234

251-
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
235+
issue, labels, err := prepareForReplaceOrAdd(ctx, form)
252236
if err != nil {
253-
if models.IsErrIssueNotExist(err) {
254-
ctx.NotFound()
255-
} else {
256-
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
257-
}
258-
return
259-
}
260-
261-
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
262-
ctx.Status(http.StatusForbidden)
263237
return
264238
}
265239

266-
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
267-
if err != nil {
268-
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
269-
return
270-
}
271-
272-
if err := issue.ReplaceLabels(labels, ctx.User); err != nil {
240+
if err := issue_service.ReplaceLabels(issue, ctx.User, labels); err != nil {
273241
ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err)
274242
return
275243
}
@@ -339,3 +307,28 @@ func ClearIssueLabels(ctx *context.APIContext) {
339307

340308
ctx.Status(http.StatusNoContent)
341309
}
310+
311+
func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) (issue *models.Issue, labels []*models.Label, err error) {
312+
issue, err = models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
313+
if err != nil {
314+
if models.IsErrIssueNotExist(err) {
315+
ctx.NotFound()
316+
} else {
317+
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
318+
}
319+
return
320+
}
321+
322+
labels, err = models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
323+
if err != nil {
324+
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
325+
return
326+
}
327+
328+
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
329+
ctx.Status(http.StatusForbidden)
330+
return
331+
}
332+
333+
return
334+
}

services/issue/label.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,18 @@ func RemoveLabel(issue *models.Issue, doer *models.User, label *models.Label) er
6161
notification.NotifyIssueChangeLabels(doer, issue, nil, []*models.Label{label})
6262
return nil
6363
}
64+
65+
// ReplaceLabels removes all current labels and add new labels to the issue.
66+
func ReplaceLabels(issue *models.Issue, doer *models.User, labels []*models.Label) error {
67+
old, err := models.GetLabelsByIssueID(issue.ID)
68+
if err != nil {
69+
return err
70+
}
71+
72+
if err := issue.ReplaceLabels(labels, doer); err != nil {
73+
return err
74+
}
75+
76+
notification.NotifyIssueChangeLabels(doer, issue, labels, old)
77+
return nil
78+
}

0 commit comments

Comments
 (0)