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.

195 lines
4.7 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. "code.gitea.io/gitea/modules/log"
  11. )
  12. const (
  13. tplLabels base.TplName = "repo/issue/labels"
  14. )
  15. // Labels render issue's labels page
  16. func Labels(ctx *context.Context) {
  17. ctx.Data["Title"] = ctx.Tr("repo.labels")
  18. ctx.Data["PageIsIssueList"] = true
  19. ctx.Data["PageIsLabels"] = true
  20. ctx.Data["RequireMinicolors"] = true
  21. ctx.Data["LabelTemplates"] = models.LabelTemplates
  22. ctx.HTML(200, tplLabels)
  23. }
  24. // InitializeLabels init labels for a repository
  25. func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
  26. if ctx.HasError() {
  27. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  28. return
  29. }
  30. list, err := models.GetLabelTemplateFile(form.TemplateName)
  31. if err != nil {
  32. ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, err))
  33. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  34. return
  35. }
  36. labels := make([]*models.Label, len(list))
  37. for i := 0; i < len(list); i++ {
  38. labels[i] = &models.Label{
  39. RepoID: ctx.Repo.Repository.ID,
  40. Name: list[i][0],
  41. Description: list[i][2],
  42. Color: list[i][1],
  43. }
  44. }
  45. if err := models.NewLabels(labels...); err != nil {
  46. ctx.ServerError("NewLabels", err)
  47. return
  48. }
  49. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  50. }
  51. // RetrieveLabels find all the labels of a repository
  52. func RetrieveLabels(ctx *context.Context) {
  53. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
  54. if err != nil {
  55. ctx.ServerError("RetrieveLabels.GetLabels", err)
  56. return
  57. }
  58. for _, l := range labels {
  59. l.CalOpenIssues()
  60. }
  61. ctx.Data["Labels"] = labels
  62. ctx.Data["NumLabels"] = len(labels)
  63. ctx.Data["SortType"] = ctx.Query("sort")
  64. }
  65. // NewLabel create new label for repository
  66. func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
  67. ctx.Data["Title"] = ctx.Tr("repo.labels")
  68. ctx.Data["PageIsLabels"] = true
  69. if ctx.HasError() {
  70. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  71. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  72. return
  73. }
  74. l := &models.Label{
  75. RepoID: ctx.Repo.Repository.ID,
  76. Name: form.Title,
  77. Description: form.Description,
  78. Color: form.Color,
  79. }
  80. if err := models.NewLabel(l); err != nil {
  81. ctx.ServerError("NewLabel", err)
  82. return
  83. }
  84. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  85. }
  86. // UpdateLabel update a label's name and color
  87. func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
  88. l, err := models.GetLabelByID(form.ID)
  89. if err != nil {
  90. switch {
  91. case models.IsErrLabelNotExist(err):
  92. ctx.Error(404)
  93. default:
  94. ctx.ServerError("UpdateLabel", err)
  95. }
  96. return
  97. }
  98. l.Name = form.Title
  99. l.Description = form.Description
  100. l.Color = form.Color
  101. if err := models.UpdateLabel(l); err != nil {
  102. ctx.ServerError("UpdateLabel", err)
  103. return
  104. }
  105. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  106. }
  107. // DeleteLabel delete a label
  108. func DeleteLabel(ctx *context.Context) {
  109. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  110. ctx.Flash.Error("DeleteLabel: " + err.Error())
  111. } else {
  112. ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
  113. }
  114. ctx.JSON(200, map[string]interface{}{
  115. "redirect": ctx.Repo.RepoLink + "/labels",
  116. })
  117. return
  118. }
  119. // UpdateIssueLabel change issue's labels
  120. func UpdateIssueLabel(ctx *context.Context) {
  121. issues := getActionIssues(ctx)
  122. if ctx.Written() {
  123. return
  124. }
  125. switch action := ctx.Query("action"); action {
  126. case "clear":
  127. for _, issue := range issues {
  128. if err := issue.ClearLabels(ctx.User); err != nil {
  129. ctx.ServerError("ClearLabels", err)
  130. return
  131. }
  132. }
  133. case "attach", "detach", "toggle":
  134. label, err := models.GetLabelByID(ctx.QueryInt64("id"))
  135. if err != nil {
  136. if models.IsErrLabelNotExist(err) {
  137. ctx.Error(404, "GetLabelByID")
  138. } else {
  139. ctx.ServerError("GetLabelByID", err)
  140. }
  141. return
  142. }
  143. if action == "toggle" {
  144. // detach if any issues already have label, otherwise attach
  145. action = "attach"
  146. for _, issue := range issues {
  147. if issue.HasLabel(label.ID) {
  148. action = "detach"
  149. break
  150. }
  151. }
  152. }
  153. if action == "attach" {
  154. for _, issue := range issues {
  155. if err = issue.AddLabel(ctx.User, label); err != nil {
  156. ctx.ServerError("AddLabel", err)
  157. return
  158. }
  159. }
  160. } else {
  161. for _, issue := range issues {
  162. if err = issue.RemoveLabel(ctx.User, label); err != nil {
  163. ctx.ServerError("RemoveLabel", err)
  164. return
  165. }
  166. }
  167. }
  168. default:
  169. log.Warn("Unrecognized action: %s", action)
  170. ctx.Error(500)
  171. return
  172. }
  173. ctx.JSON(200, map[string]interface{}{
  174. "ok": true,
  175. })
  176. }