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
This commit is contained in:
6543 2020-02-23 23:53:08 +01:00 committed by GitHub
parent 062f35109d
commit 39b507e3c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 36 deletions

View file

@ -102,24 +102,8 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) issue, labels, err := prepareForReplaceOrAdd(ctx, form)
if err != nil { if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
}
return
}
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
ctx.Status(http.StatusForbidden)
return
}
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
return return
} }
@ -204,7 +188,7 @@ func DeleteIssueLabel(ctx *context.APIContext) {
return return
} }
if err := models.DeleteIssueLabel(issue, label, ctx.User); err != nil { if err := issue_service.RemoveLabel(issue, ctx.User, label); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err) ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err)
return return
} }
@ -248,28 +232,12 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) issue, labels, err := prepareForReplaceOrAdd(ctx, form)
if err != nil { if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
}
return return
} }
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) { if err := issue_service.ReplaceLabels(issue, ctx.User, labels); err != nil {
ctx.Status(http.StatusForbidden)
return
}
labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
return
}
if err := issue.ReplaceLabels(labels, ctx.User); err != nil {
ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err) ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err)
return return
} }
@ -339,3 +307,28 @@ func ClearIssueLabels(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }
func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) (issue *models.Issue, labels []*models.Label, err error) {
issue, err = models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
}
return
}
labels, err = models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
return
}
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
ctx.Status(http.StatusForbidden)
return
}
return
}

View file

@ -61,3 +61,18 @@ func RemoveLabel(issue *models.Issue, doer *models.User, label *models.Label) er
notification.NotifyIssueChangeLabels(doer, issue, nil, []*models.Label{label}) notification.NotifyIssueChangeLabels(doer, issue, nil, []*models.Label{label})
return nil return nil
} }
// ReplaceLabels removes all current labels and add new labels to the issue.
func ReplaceLabels(issue *models.Issue, doer *models.User, labels []*models.Label) error {
old, err := models.GetLabelsByIssueID(issue.ID)
if err != nil {
return err
}
if err := issue.ReplaceLabels(labels, doer); err != nil {
return err
}
notification.NotifyIssueChangeLabels(doer, issue, labels, old)
return nil
}