summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-03-14 21:10:35 -0400
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2017-03-15 02:10:35 +0100
commit09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b (patch)
treed7c595f4b004e2bfe70fc363a8258b0b26cae41b /routers
parent021904e4e65804baa67b38e193e15aa37a391c60 (diff)
downloadgitea-09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b.tar.gz
gitea-09fe4a2ae9dfa8b3bc8a5039d0feab1e1a34d07b.zip
Batch updates for issues (#926)
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/issue.go93
-rw-r--r--routers/repo/issue_label.go56
2 files changed, 112 insertions, 37 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index a06f21b859..0a723d755b 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -10,6 +10,7 @@ import (
"fmt"
"io"
"io/ioutil"
+ "strconv"
"strings"
"time"
@@ -644,6 +645,28 @@ func getActionIssue(ctx *context.Context) *models.Issue {
return issue
}
+func getActionIssues(ctx *context.Context) []*models.Issue {
+ commaSeparatedIssueIDs := ctx.Query("issue_ids")
+ if len(commaSeparatedIssueIDs) == 0 {
+ return nil
+ }
+ issueIDs := make([]int64, 0, 10)
+ for _, stringIssueID := range strings.Split(commaSeparatedIssueIDs, ",") {
+ issueID, err := strconv.ParseInt(stringIssueID, 10, 64)
+ if err != nil {
+ ctx.Handle(500, "ParseInt", err)
+ return nil
+ }
+ issueIDs = append(issueIDs, issueID)
+ }
+ issues, err := models.GetIssuesByIDs(issueIDs)
+ if err != nil {
+ ctx.Handle(500, "GetIssuesByIDs", err)
+ return nil
+ }
+ return issues
+}
+
// UpdateIssueTitle change issue's title
func UpdateIssueTitle(ctx *context.Context) {
issue := getActionIssue(ctx)
@@ -697,25 +720,22 @@ func UpdateIssueContent(ctx *context.Context) {
// UpdateIssueMilestone change issue's milestone
func UpdateIssueMilestone(ctx *context.Context) {
- issue := getActionIssue(ctx)
+ issues := getActionIssues(ctx)
if ctx.Written() {
return
}
- oldMilestoneID := issue.MilestoneID
milestoneID := ctx.QueryInt64("id")
- if oldMilestoneID == milestoneID {
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- })
- return
- }
-
- // Not check for invalid milestone id and give responsibility to owners.
- issue.MilestoneID = milestoneID
- if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
- ctx.Handle(500, "ChangeMilestoneAssign", err)
- return
+ for _, issue := range issues {
+ oldMilestoneID := issue.MilestoneID
+ if oldMilestoneID == milestoneID {
+ continue
+ }
+ issue.MilestoneID = milestoneID
+ if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
+ ctx.Handle(500, "ChangeMilestoneAssign", err)
+ return
+ }
}
ctx.JSON(200, map[string]interface{}{
@@ -725,24 +745,53 @@ func UpdateIssueMilestone(ctx *context.Context) {
// UpdateIssueAssignee change issue's assignee
func UpdateIssueAssignee(ctx *context.Context) {
- issue := getActionIssue(ctx)
+ issues := getActionIssues(ctx)
if ctx.Written() {
return
}
assigneeID := ctx.QueryInt64("id")
- if issue.AssigneeID == assigneeID {
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- })
- return
+ for _, issue := range issues {
+ if issue.AssigneeID == assigneeID {
+ continue
+ }
+ if err := issue.ChangeAssignee(ctx.User, assigneeID); err != nil {
+ ctx.Handle(500, "ChangeAssignee", err)
+ return
+ }
}
+ ctx.JSON(200, map[string]interface{}{
+ "ok": true,
+ })
+}
- if err := issue.ChangeAssignee(ctx.User, assigneeID); err != nil {
- ctx.Handle(500, "ChangeAssignee", err)
+// UpdateIssueStatus change issue's status
+func UpdateIssueStatus(ctx *context.Context) {
+ issues := getActionIssues(ctx)
+ if ctx.Written() {
return
}
+ var isClosed bool
+ switch action := ctx.Query("action"); action {
+ case "open":
+ isClosed = false
+ case "close":
+ isClosed = true
+ default:
+ log.Warn("Unrecognized action: %s", action)
+ }
+
+ if _, err := models.IssueList(issues).LoadRepositories(); err != nil {
+ ctx.Handle(500, "LoadRepositories", err)
+ return
+ }
+ for _, issue := range issues {
+ if err := issue.ChangeStatus(ctx.User, issue.Repo, isClosed); err != nil {
+ ctx.Handle(500, "ChangeStatus", err)
+ return
+ }
+ }
ctx.JSON(200, map[string]interface{}{
"ok": true,
})
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{}{