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.

237 lines
6.0 KiB

Add Organization Wide Labels (#10814) * Add organization wide labels Implement organization wide labels similar to organization wide webhooks. This lets you create individual labels for organizations that can be used for all repos under that organization (so being able to reuse the same label across multiple repos). This makes it possible for small organizations with many repos to use labels effectively. Fixes #7406 * Add migration * remove comments * fix tests * Update options/locale/locale_en-US.ini Removed unused translation string * show org labels in issue search label filter * Use more clear var name * rename migration after merge from master * comment typo * update migration again after rebase with master * check for orgID <=0 per guillep2k review * fmt * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * remove unused code * Make sure RepoID is 0 when searching orgID per code review * more changes/code review requests * More descriptive translation var per code review * func description/delete comment when issue label deleted instead of hiding it * remove comment * only use issues in that repo when calculating number of open issues for org label on repo label page * Add integration test for IssuesSearch API with labels * remove unused function * Update models/issue_label.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Use subquery in GetLabelIDsInReposByNames * Fix tests to use correct orgID * fix more tests * IssuesSearch api now uses new BuildLabelNamesIssueIDsCondition. Add a few more tests as well * update comment for clarity * Revert previous code change now that we can use the new BuildLabelNamesIssueIDsCondition * Don't sort repos by date in IssuesSearch API After much debugging I've found a strange issue where in some cases MySQL will return a different result than other enigines if a query is sorted by a null collumn. For example with our integration test data where we don't set updated_unix in repository fixtures: SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` ORDER BY updated_unix DESC LIMIT 15 OFFSET 45 Returns different results for MySQL than other engines. However, the similar query: SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` ORDER BY updated_unix DESC LIMIT 15 OFFSET 30 Returns the same results. This causes integration tests to fail on MySQL in certain cases but would never show up in a real installation. Since this API call always returns issues based on the optionally provided repo_priority_id or the issueID itself, there is no change to results by changing the repo sorting method used to get ids earlier in the function. * linter is back! * code review * remove now unused option * Fix newline at end of files * more unused code * update to master * check for matching ids before query * Update models/issue_label.go Co-Authored-By: 6543 <6543@obermui.de> * Update models/issue_label.go * update comments * Update routers/org/setting.go Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: 6543 <6543@obermui.de>
4 years ago
  1. // Copyright 2020 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 org
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // ListLabels list all the labels of an organization
  17. func ListLabels(ctx *context.APIContext) {
  18. // swagger:operation GET /orgs/{org}/labels organization orgListLabels
  19. // ---
  20. // summary: List an organization's labels
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: org
  25. // in: path
  26. // description: name of the organization
  27. // type: string
  28. // required: true
  29. // - name: page
  30. // in: query
  31. // description: page number of results to return (1-based)
  32. // type: integer
  33. // - name: limit
  34. // in: query
  35. // description: page size of results, maximum page size is 50
  36. // type: integer
  37. // responses:
  38. // "200":
  39. // "$ref": "#/responses/LabelList"
  40. labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
  41. if err != nil {
  42. ctx.Error(http.StatusInternalServerError, "GetLabelsByOrgID", err)
  43. return
  44. }
  45. ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
  46. }
  47. // CreateLabel create a label for a repository
  48. func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
  49. // swagger:operation POST /orgs/{org}/labels organization orgCreateLabel
  50. // ---
  51. // summary: Create a label for an organization
  52. // consumes:
  53. // - application/json
  54. // produces:
  55. // - application/json
  56. // parameters:
  57. // - name: org
  58. // in: path
  59. // description: name of the organization
  60. // type: string
  61. // required: true
  62. // - name: body
  63. // in: body
  64. // schema:
  65. // "$ref": "#/definitions/CreateLabelOption"
  66. // responses:
  67. // "201":
  68. // "$ref": "#/responses/Label"
  69. // "422":
  70. // "$ref": "#/responses/validationError"
  71. form.Color = strings.Trim(form.Color, " ")
  72. if len(form.Color) == 6 {
  73. form.Color = "#" + form.Color
  74. }
  75. if !models.LabelColorPattern.MatchString(form.Color) {
  76. ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", form.Color))
  77. return
  78. }
  79. label := &models.Label{
  80. Name: form.Name,
  81. Color: form.Color,
  82. OrgID: ctx.Org.Organization.ID,
  83. Description: form.Description,
  84. }
  85. if err := models.NewLabel(label); err != nil {
  86. ctx.Error(http.StatusInternalServerError, "NewLabel", err)
  87. return
  88. }
  89. ctx.JSON(http.StatusCreated, convert.ToLabel(label))
  90. }
  91. // GetLabel get label by organization and label id
  92. func GetLabel(ctx *context.APIContext) {
  93. // swagger:operation GET /orgs/{org}/labels/{id} organization orgGetLabel
  94. // ---
  95. // summary: Get a single label
  96. // produces:
  97. // - application/json
  98. // parameters:
  99. // - name: org
  100. // in: path
  101. // description: name of the organization
  102. // type: string
  103. // required: true
  104. // - name: id
  105. // in: path
  106. // description: id of the label to get
  107. // type: integer
  108. // format: int64
  109. // required: true
  110. // responses:
  111. // "200":
  112. // "$ref": "#/responses/Label"
  113. var (
  114. label *models.Label
  115. err error
  116. )
  117. strID := ctx.Params(":id")
  118. if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
  119. label, err = models.GetLabelInOrgByName(ctx.Org.Organization.ID, strID)
  120. } else {
  121. label, err = models.GetLabelInOrgByID(ctx.Org.Organization.ID, intID)
  122. }
  123. if err != nil {
  124. if models.IsErrOrgLabelNotExist(err) {
  125. ctx.NotFound()
  126. } else {
  127. ctx.Error(http.StatusInternalServerError, "GetLabelByOrgID", err)
  128. }
  129. return
  130. }
  131. ctx.JSON(http.StatusOK, convert.ToLabel(label))
  132. }
  133. // EditLabel modify a label for an Organization
  134. func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
  135. // swagger:operation PATCH /orgs/{org}/labels/{id} organization orgEditLabel
  136. // ---
  137. // summary: Update a label
  138. // consumes:
  139. // - application/json
  140. // produces:
  141. // - application/json
  142. // parameters:
  143. // - name: org
  144. // in: path
  145. // description: name of the organization
  146. // type: string
  147. // required: true
  148. // - name: id
  149. // in: path
  150. // description: id of the label to edit
  151. // type: integer
  152. // format: int64
  153. // required: true
  154. // - name: body
  155. // in: body
  156. // schema:
  157. // "$ref": "#/definitions/EditLabelOption"
  158. // responses:
  159. // "200":
  160. // "$ref": "#/responses/Label"
  161. // "422":
  162. // "$ref": "#/responses/validationError"
  163. label, err := models.GetLabelInOrgByID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  164. if err != nil {
  165. if models.IsErrOrgLabelNotExist(err) {
  166. ctx.NotFound()
  167. } else {
  168. ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
  169. }
  170. return
  171. }
  172. if form.Name != nil {
  173. label.Name = *form.Name
  174. }
  175. if form.Color != nil {
  176. label.Color = strings.Trim(*form.Color, " ")
  177. if len(label.Color) == 6 {
  178. label.Color = "#" + label.Color
  179. }
  180. if !models.LabelColorPattern.MatchString(label.Color) {
  181. ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", label.Color))
  182. return
  183. }
  184. }
  185. if form.Description != nil {
  186. label.Description = *form.Description
  187. }
  188. if err := models.UpdateLabel(label); err != nil {
  189. ctx.ServerError("UpdateLabel", err)
  190. return
  191. }
  192. ctx.JSON(http.StatusOK, convert.ToLabel(label))
  193. }
  194. // DeleteLabel delete a label for an organization
  195. func DeleteLabel(ctx *context.APIContext) {
  196. // swagger:operation DELETE /orgs/{org}/labels/{id} organization orgDeleteLabel
  197. // ---
  198. // summary: Delete a label
  199. // parameters:
  200. // - name: org
  201. // in: path
  202. // description: name of the organization
  203. // type: string
  204. // required: true
  205. // - name: id
  206. // in: path
  207. // description: id of the label to delete
  208. // type: integer
  209. // format: int64
  210. // required: true
  211. // responses:
  212. // "204":
  213. // "$ref": "#/responses/empty"
  214. if err := models.DeleteLabel(ctx.Org.Organization.ID, ctx.ParamsInt64(":id")); err != nil {
  215. ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
  216. return
  217. }
  218. ctx.Status(http.StatusNoContent)
  219. }