func UpdateLabel(ctx *middleware.Context, form auth.CreateLabelForm) { l, err := models.GetLabelByID(form.ID) if err != nil { switch { case models.IsErrLabelNotExist(err): ctx.Error(404) default: ctx.Handle(500, "UpdateLabel", err) } return } l.Name = form.Title l.Color = form.Color if err := models.UpdateLabel(l); err != nil { ctx.Handle(500, "UpdateLabel", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/labels") }
func UpdateIssueLabel(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } if ctx.Query("action") == "clear" { if err := issue.ClearLabels(); err != nil { ctx.Handle(500, "ClearLabels", err) return } } else { isAttach := ctx.Query("action") == "attach" label, err := models.GetLabelByID(ctx.QueryInt64("id")) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Error(404, "GetLabelByID") } else { ctx.Handle(500, "GetLabelByID", err) } return } if isAttach && !issue.HasLabel(label.ID) { if err = issue.AddLabel(label); err != nil { ctx.Handle(500, "AddLabel", err) return } } else if !isAttach && issue.HasLabel(label.ID) { if err = issue.RemoveLabel(label); err != nil { ctx.Handle(500, "RemoveLabel", err) return } } } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func UpdateIssueLabel(ctx *middleware.Context) { if !ctx.Repo.IsOwner() { ctx.Error(403) return } idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx <= 0 { ctx.Error(404) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, idx) if err != nil { if err == models.ErrIssueNotExist { ctx.Handle(404, "issue.UpdateIssueLabel(GetIssueByIndex)", err) } else { ctx.Handle(500, "issue.UpdateIssueLabel(GetIssueByIndex)", err) } return } isAttach := ctx.Query("action") == "attach" labelStrId := ctx.Query("id") labelID := com.StrTo(labelStrId).MustInt64() label, err := models.GetLabelByID(labelID) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Handle(404, "issue.UpdateIssueLabel(GetLabelById)", err) } else { ctx.Handle(500, "issue.UpdateIssueLabel(GetLabelById)", err) } return } isNeedUpdate := false if isAttach { if !issue.HasLabel(labelID) { if err = issue.AddLabel(labelID); err != nil { ctx.Handle(500, "AddLabel", err) return } isNeedUpdate = true } } else { if issue.HasLabel(labelID) { if err = issue.RemoveLabel(labelID); err != nil { ctx.Handle(500, "RemoveLabel", err) return } isNeedUpdate = true } } if isNeedUpdate { if err = models.UpdateIssue(issue); err != nil { ctx.Handle(500, "issue.UpdateIssueLabel(UpdateIssue)", err) return } if isAttach { label.NumIssues++ if issue.IsClosed { label.NumClosedIssues++ } } else { label.NumIssues-- if issue.IsClosed { label.NumClosedIssues-- } } if err = models.UpdateLabel(label); err != nil { ctx.Handle(500, "issue.UpdateIssueLabel(UpdateLabel)", err) return } } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }