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.

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