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.

issue_label.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "code.gitea.io/gitea/models/db"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. "code.gitea.io/gitea/models/organization"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/label"
  12. "code.gitea.io/gitea/modules/log"
  13. repo_module "code.gitea.io/gitea/modules/repository"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "code.gitea.io/gitea/modules/web"
  16. "code.gitea.io/gitea/services/forms"
  17. issue_service "code.gitea.io/gitea/services/issue"
  18. )
  19. const (
  20. tplLabels base.TplName = "repo/issue/labels"
  21. )
  22. // Labels render issue's labels page
  23. func Labels(ctx *context.Context) {
  24. ctx.Data["Title"] = ctx.Tr("repo.labels")
  25. ctx.Data["PageIsIssueList"] = true
  26. ctx.Data["PageIsLabels"] = true
  27. ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
  28. ctx.HTML(http.StatusOK, tplLabels)
  29. }
  30. // InitializeLabels init labels for a repository
  31. func InitializeLabels(ctx *context.Context) {
  32. form := web.GetForm(ctx).(*forms.InitializeLabelsForm)
  33. if ctx.HasError() {
  34. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  35. return
  36. }
  37. if err := repo_module.InitializeLabels(ctx, ctx.Repo.Repository.ID, form.TemplateName, false); err != nil {
  38. if label.IsErrTemplateLoad(err) {
  39. originalErr := err.(label.ErrTemplateLoad).OriginalError
  40. ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
  41. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  42. return
  43. }
  44. ctx.ServerError("InitializeLabels", err)
  45. return
  46. }
  47. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  48. }
  49. // RetrieveLabels find all the labels of a repository and organization
  50. func RetrieveLabels(ctx *context.Context) {
  51. labels, err := issues_model.GetLabelsByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormString("sort"), db.ListOptions{})
  52. if err != nil {
  53. ctx.ServerError("RetrieveLabels.GetLabels", err)
  54. return
  55. }
  56. for _, l := range labels {
  57. l.CalOpenIssues()
  58. }
  59. ctx.Data["Labels"] = labels
  60. if ctx.Repo.Owner.IsOrganization() {
  61. orgLabels, err := issues_model.GetLabelsByOrgID(ctx, ctx.Repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{})
  62. if err != nil {
  63. ctx.ServerError("GetLabelsByOrgID", err)
  64. return
  65. }
  66. for _, l := range orgLabels {
  67. l.CalOpenOrgIssues(ctx, ctx.Repo.Repository.ID, l.ID)
  68. }
  69. ctx.Data["OrgLabels"] = orgLabels
  70. org, err := organization.GetOrgByName(ctx, ctx.Repo.Owner.LowerName)
  71. if err != nil {
  72. ctx.ServerError("GetOrgByName", err)
  73. return
  74. }
  75. if ctx.Doer != nil {
  76. ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.Doer.ID)
  77. if err != nil {
  78. ctx.ServerError("org.IsOwnedBy", err)
  79. return
  80. }
  81. ctx.Org.OrgLink = org.AsUser().OrganisationLink()
  82. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  83. ctx.Data["OrganizationLink"] = ctx.Org.OrgLink
  84. }
  85. }
  86. ctx.Data["NumLabels"] = len(labels)
  87. ctx.Data["SortType"] = ctx.FormString("sort")
  88. }
  89. // NewLabel create new label for repository
  90. func NewLabel(ctx *context.Context) {
  91. form := web.GetForm(ctx).(*forms.CreateLabelForm)
  92. ctx.Data["Title"] = ctx.Tr("repo.labels")
  93. ctx.Data["PageIsLabels"] = true
  94. if ctx.HasError() {
  95. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  96. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  97. return
  98. }
  99. l := &issues_model.Label{
  100. RepoID: ctx.Repo.Repository.ID,
  101. Name: form.Title,
  102. Exclusive: form.Exclusive,
  103. Description: form.Description,
  104. Color: form.Color,
  105. ArchivedUnix: timeutil.TimeStamp(0),
  106. }
  107. if err := issues_model.NewLabel(ctx, l); err != nil {
  108. ctx.ServerError("NewLabel", err)
  109. return
  110. }
  111. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  112. }
  113. // UpdateLabel update a label's name and color
  114. func UpdateLabel(ctx *context.Context) {
  115. form := web.GetForm(ctx).(*forms.CreateLabelForm)
  116. l, err := issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, form.ID)
  117. if err != nil {
  118. switch {
  119. case issues_model.IsErrRepoLabelNotExist(err):
  120. ctx.Error(http.StatusNotFound)
  121. default:
  122. ctx.ServerError("UpdateLabel", err)
  123. }
  124. return
  125. }
  126. l.Name = form.Title
  127. l.Exclusive = form.Exclusive
  128. l.Description = form.Description
  129. l.Color = form.Color
  130. l.SetArchived(form.IsArchived)
  131. if err := issues_model.UpdateLabel(ctx, l); err != nil {
  132. ctx.ServerError("UpdateLabel", err)
  133. return
  134. }
  135. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  136. }
  137. // DeleteLabel delete a label
  138. func DeleteLabel(ctx *context.Context) {
  139. if err := issues_model.DeleteLabel(ctx, ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
  140. ctx.Flash.Error("DeleteLabel: " + err.Error())
  141. } else {
  142. ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
  143. }
  144. ctx.JSONRedirect(ctx.Repo.RepoLink + "/labels")
  145. }
  146. // UpdateIssueLabel change issue's labels
  147. func UpdateIssueLabel(ctx *context.Context) {
  148. issues := getActionIssues(ctx)
  149. if ctx.Written() {
  150. return
  151. }
  152. switch action := ctx.FormString("action"); action {
  153. case "clear":
  154. for _, issue := range issues {
  155. if err := issue_service.ClearLabels(ctx, issue, ctx.Doer); err != nil {
  156. ctx.ServerError("ClearLabels", err)
  157. return
  158. }
  159. }
  160. case "attach", "detach", "toggle", "toggle-alt":
  161. label, err := issues_model.GetLabelByID(ctx, ctx.FormInt64("id"))
  162. if err != nil {
  163. if issues_model.IsErrRepoLabelNotExist(err) {
  164. ctx.Error(http.StatusNotFound, "GetLabelByID")
  165. } else {
  166. ctx.ServerError("GetLabelByID", err)
  167. }
  168. return
  169. }
  170. if action == "toggle" {
  171. // detach if any issues already have label, otherwise attach
  172. action = "attach"
  173. if label.ExclusiveScope() == "" {
  174. for _, issue := range issues {
  175. if issues_model.HasIssueLabel(ctx, issue.ID, label.ID) {
  176. action = "detach"
  177. break
  178. }
  179. }
  180. }
  181. } else if action == "toggle-alt" {
  182. // always detach with alt key pressed, to be able to remove
  183. // scoped labels
  184. action = "detach"
  185. }
  186. if action == "attach" {
  187. for _, issue := range issues {
  188. if err = issue_service.AddLabel(ctx, issue, ctx.Doer, label); err != nil {
  189. ctx.ServerError("AddLabel", err)
  190. return
  191. }
  192. }
  193. } else {
  194. for _, issue := range issues {
  195. if err = issue_service.RemoveLabel(ctx, issue, ctx.Doer, label); err != nil {
  196. ctx.ServerError("RemoveLabel", err)
  197. return
  198. }
  199. }
  200. }
  201. default:
  202. log.Warn("Unrecognized action: %s", action)
  203. ctx.Error(http.StatusInternalServerError)
  204. return
  205. }
  206. ctx.JSONOK()
  207. }