diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-03-14 21:10:35 -0400 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-03-15 02:10:35 +0100 |
commit | 09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b (patch) | |
tree | d7c595f4b004e2bfe70fc363a8258b0b26cae41b /routers/repo/issue_label.go | |
parent | 021904e4e65804baa67b38e193e15aa37a391c60 (diff) | |
download | gitea-09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b.tar.gz gitea-09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b.zip |
Batch updates for issues (#926)
Diffstat (limited to 'routers/repo/issue_label.go')
-rw-r--r-- | routers/repo/issue_label.go | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/routers/repo/issue_label.go b/routers/repo/issue_label.go index 6792947669..966c2c1c53 100644 --- a/routers/repo/issue_label.go +++ b/routers/repo/issue_label.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" ) const ( @@ -129,18 +130,20 @@ func DeleteLabel(ctx *context.Context) { // UpdateIssueLabel change issue's labels func UpdateIssueLabel(ctx *context.Context) { - issue := getActionIssue(ctx) + issues := getActionIssues(ctx) if ctx.Written() { return } - if ctx.Query("action") == "clear" { - if err := issue.ClearLabels(ctx.User); err != nil { - ctx.Handle(500, "ClearLabels", err) - return + switch action := ctx.Query("action"); action { + case "clear": + for _, issue := range issues { + if err := issue.ClearLabels(ctx.User); err != nil { + ctx.Handle(500, "ClearLabels", err) + return + } } - } else { - isAttach := ctx.Query("action") == "attach" + case "attach", "detach", "toggle": label, err := models.GetLabelByID(ctx.QueryInt64("id")) if err != nil { if models.IsErrLabelNotExist(err) { @@ -151,17 +154,40 @@ func UpdateIssueLabel(ctx *context.Context) { return } - if isAttach && !issue.HasLabel(label.ID) { - if err = issue.AddLabel(ctx.User, label); err != nil { - ctx.Handle(500, "AddLabel", err) - return + if action == "toggle" { + anyHaveLabel := false + for _, issue := range issues { + if issue.HasLabel(label.ID) { + anyHaveLabel = true + break + } } - } else if !isAttach && issue.HasLabel(label.ID) { - if err = issue.RemoveLabel(ctx.User, label); err != nil { - ctx.Handle(500, "RemoveLabel", err) - return + if anyHaveLabel { + action = "detach" + } else { + action = "attach" + } + } + + if action == "attach" { + for _, issue := range issues { + if err = issue.AddLabel(ctx.User, label); err != nil { + ctx.Handle(500, "AddLabel", err) + return + } + } + } else { + for _, issue := range issues { + if err = issue.RemoveLabel(ctx.User, label); err != nil { + ctx.Handle(500, "RemoveLabel", err) + return + } } } + default: + log.Warn("Unrecognized action: %s", action) + ctx.Error(500) + return } ctx.JSON(200, map[string]interface{}{ |