From 09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Tue, 14 Mar 2017 21:10:35 -0400 Subject: Batch updates for issues (#926) --- routers/repo/issue_label.go | 56 +++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'routers/repo/issue_label.go') 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{}{ -- cgit v1.2.3