You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.2 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/auth"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. const (
  12. tplLabels base.TplName = "repo/issue/labels"
  13. )
  14. // Labels render issue's labels page
  15. func Labels(ctx *context.Context) {
  16. ctx.Data["Title"] = ctx.Tr("repo.labels")
  17. ctx.Data["PageIsIssueList"] = true
  18. ctx.Data["PageIsLabels"] = true
  19. ctx.Data["RequireMinicolors"] = true
  20. ctx.Data["LabelTemplates"] = models.LabelTemplates
  21. ctx.HTML(200, tplLabels)
  22. }
  23. // InitializeLabels init labels for a repository
  24. func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
  25. if ctx.HasError() {
  26. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  27. return
  28. }
  29. list, err := models.GetLabelTemplateFile(form.TemplateName)
  30. if err != nil {
  31. ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, err))
  32. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  33. return
  34. }
  35. labels := make([]*models.Label, len(list))
  36. for i := 0; i < len(list); i++ {
  37. labels[i] = &models.Label{
  38. RepoID: ctx.Repo.Repository.ID,
  39. Name: list[i][0],
  40. Color: list[i][1],
  41. }
  42. }
  43. if err := models.NewLabels(labels...); err != nil {
  44. ctx.Handle(500, "NewLabels", err)
  45. return
  46. }
  47. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  48. }
  49. // RetrieveLabels find all the labels of a repository
  50. func RetrieveLabels(ctx *context.Context) {
  51. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
  52. if err != nil {
  53. ctx.Handle(500, "RetrieveLabels.GetLabels", err)
  54. return
  55. }
  56. for _, l := range labels {
  57. l.CalOpenIssues()
  58. }
  59. ctx.Data["Labels"] = labels
  60. ctx.Data["NumLabels"] = len(labels)
  61. ctx.Data["SortType"] = ctx.Query("sort")
  62. }
  63. // NewLabel create new label for repository
  64. func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
  65. ctx.Data["Title"] = ctx.Tr("repo.labels")
  66. ctx.Data["PageIsLabels"] = true
  67. if ctx.HasError() {
  68. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  69. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  70. return
  71. }
  72. l := &models.Label{
  73. RepoID: ctx.Repo.Repository.ID,
  74. Name: form.Title,
  75. Color: form.Color,
  76. }
  77. if err := models.NewLabels(l); err != nil {
  78. ctx.Handle(500, "NewLabel", err)
  79. return
  80. }
  81. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  82. }
  83. // UpdateLabel update a label's name and color
  84. func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
  85. l, err := models.GetLabelByID(form.ID)
  86. if err != nil {
  87. switch {
  88. case models.IsErrLabelNotExist(err):
  89. ctx.Error(404)
  90. default:
  91. ctx.Handle(500, "UpdateLabel", err)
  92. }
  93. return
  94. }
  95. l.Name = form.Title
  96. l.Color = form.Color
  97. if err := models.UpdateLabel(l); err != nil {
  98. ctx.Handle(500, "UpdateLabel", err)
  99. return
  100. }
  101. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  102. }
  103. // DeleteLabel delete a label
  104. func DeleteLabel(ctx *context.Context) {
  105. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  106. ctx.Flash.Error("DeleteLabel: " + err.Error())
  107. } else {
  108. ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
  109. }
  110. ctx.JSON(200, map[string]interface{}{
  111. "redirect": ctx.Repo.RepoLink + "/labels",
  112. })
  113. return
  114. }
  115. // UpdateIssueLabel change issue's labels
  116. func UpdateIssueLabel(ctx *context.Context) {
  117. issue := getActionIssue(ctx)
  118. if ctx.Written() {
  119. return
  120. }
  121. if ctx.Query("action") == "clear" {
  122. if err := issue.ClearLabels(ctx.User); err != nil {
  123. ctx.Handle(500, "ClearLabels", err)
  124. return
  125. }
  126. } else {
  127. isAttach := ctx.Query("action") == "attach"
  128. label, err := models.GetLabelByID(ctx.QueryInt64("id"))
  129. if err != nil {
  130. if models.IsErrLabelNotExist(err) {
  131. ctx.Error(404, "GetLabelByID")
  132. } else {
  133. ctx.Handle(500, "GetLabelByID", err)
  134. }
  135. return
  136. }
  137. if isAttach && !issue.HasLabel(label.ID) {
  138. if err = issue.AddLabel(ctx.User, label); err != nil {
  139. ctx.Handle(500, "AddLabel", err)
  140. return
  141. }
  142. } else if !isAttach && issue.HasLabel(label.ID) {
  143. if err = issue.RemoveLabel(ctx.User, label); err != nil {
  144. ctx.Handle(500, "RemoveLabel", err)
  145. return
  146. }
  147. }
  148. }
  149. ctx.JSON(200, map[string]interface{}{
  150. "ok": true,
  151. })
  152. }