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.

topic.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright 2019 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. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/log"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // ListTopics returns list of current topics for repo
  17. func ListTopics(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/topics repository repoListTopics
  19. // ---
  20. // summary: Get list of topics that a repository has
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: page
  35. // in: query
  36. // description: page number of results to return (1-based)
  37. // type: integer
  38. // - name: limit
  39. // in: query
  40. // description: page size of results
  41. // type: integer
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/TopicNames"
  45. opts := &models.FindTopicOptions{
  46. ListOptions: utils.GetListOptions(ctx),
  47. RepoID: ctx.Repo.Repository.ID,
  48. }
  49. topics, total, err := models.FindTopics(opts)
  50. if err != nil {
  51. ctx.InternalServerError(err)
  52. return
  53. }
  54. topicNames := make([]string, len(topics))
  55. for i, topic := range topics {
  56. topicNames[i] = topic.Name
  57. }
  58. ctx.SetTotalCountHeader(total)
  59. ctx.JSON(http.StatusOK, map[string]interface{}{
  60. "topics": topicNames,
  61. })
  62. }
  63. // UpdateTopics updates repo with a new set of topics
  64. func UpdateTopics(ctx *context.APIContext) {
  65. // swagger:operation PUT /repos/{owner}/{repo}/topics repository repoUpdateTopics
  66. // ---
  67. // summary: Replace list of topics for a repository
  68. // produces:
  69. // - application/json
  70. // parameters:
  71. // - name: owner
  72. // in: path
  73. // description: owner of the repo
  74. // type: string
  75. // required: true
  76. // - name: repo
  77. // in: path
  78. // description: name of the repo
  79. // type: string
  80. // required: true
  81. // - name: body
  82. // in: body
  83. // schema:
  84. // "$ref": "#/definitions/RepoTopicOptions"
  85. // responses:
  86. // "204":
  87. // "$ref": "#/responses/empty"
  88. // "422":
  89. // "$ref": "#/responses/invalidTopicsError"
  90. form := web.GetForm(ctx).(*api.RepoTopicOptions)
  91. topicNames := form.Topics
  92. validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
  93. if len(validTopics) > 25 {
  94. ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
  95. "invalidTopics": nil,
  96. "message": "Exceeding maximum number of topics per repo",
  97. })
  98. return
  99. }
  100. if len(invalidTopics) > 0 {
  101. ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
  102. "invalidTopics": invalidTopics,
  103. "message": "Topic names are invalid",
  104. })
  105. return
  106. }
  107. err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
  108. if err != nil {
  109. log.Error("SaveTopics failed: %v", err)
  110. ctx.InternalServerError(err)
  111. return
  112. }
  113. ctx.Status(http.StatusNoContent)
  114. }
  115. // AddTopic adds a topic name to a repo
  116. func AddTopic(ctx *context.APIContext) {
  117. // swagger:operation PUT /repos/{owner}/{repo}/topics/{topic} repository repoAddTopic
  118. // ---
  119. // summary: Add a topic to a repository
  120. // produces:
  121. // - application/json
  122. // parameters:
  123. // - name: owner
  124. // in: path
  125. // description: owner of the repo
  126. // type: string
  127. // required: true
  128. // - name: repo
  129. // in: path
  130. // description: name of the repo
  131. // type: string
  132. // required: true
  133. // - name: topic
  134. // in: path
  135. // description: name of the topic to add
  136. // type: string
  137. // required: true
  138. // responses:
  139. // "204":
  140. // "$ref": "#/responses/empty"
  141. // "422":
  142. // "$ref": "#/responses/invalidTopicsError"
  143. topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
  144. if !models.ValidateTopic(topicName) {
  145. ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
  146. "invalidTopics": topicName,
  147. "message": "Topic name is invalid",
  148. })
  149. return
  150. }
  151. // Prevent adding more topics than allowed to repo
  152. count, err := models.CountTopics(&models.FindTopicOptions{
  153. RepoID: ctx.Repo.Repository.ID,
  154. })
  155. if err != nil {
  156. log.Error("CountTopics failed: %v", err)
  157. ctx.InternalServerError(err)
  158. return
  159. }
  160. if count >= 25 {
  161. ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
  162. "message": "Exceeding maximum allowed topics per repo.",
  163. })
  164. return
  165. }
  166. _, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
  167. if err != nil {
  168. log.Error("AddTopic failed: %v", err)
  169. ctx.InternalServerError(err)
  170. return
  171. }
  172. ctx.Status(http.StatusNoContent)
  173. }
  174. // DeleteTopic removes topic name from repo
  175. func DeleteTopic(ctx *context.APIContext) {
  176. // swagger:operation DELETE /repos/{owner}/{repo}/topics/{topic} repository repoDeleteTopic
  177. // ---
  178. // summary: Delete a topic from a repository
  179. // produces:
  180. // - application/json
  181. // parameters:
  182. // - name: owner
  183. // in: path
  184. // description: owner of the repo
  185. // type: string
  186. // required: true
  187. // - name: repo
  188. // in: path
  189. // description: name of the repo
  190. // type: string
  191. // required: true
  192. // - name: topic
  193. // in: path
  194. // description: name of the topic to delete
  195. // type: string
  196. // required: true
  197. // responses:
  198. // "204":
  199. // "$ref": "#/responses/empty"
  200. // "422":
  201. // "$ref": "#/responses/invalidTopicsError"
  202. topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
  203. if !models.ValidateTopic(topicName) {
  204. ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
  205. "invalidTopics": topicName,
  206. "message": "Topic name is invalid",
  207. })
  208. return
  209. }
  210. topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName)
  211. if err != nil {
  212. log.Error("DeleteTopic failed: %v", err)
  213. ctx.InternalServerError(err)
  214. return
  215. }
  216. if topic == nil {
  217. ctx.NotFound()
  218. }
  219. ctx.Status(http.StatusNoContent)
  220. }
  221. // TopicSearch search for creating topic
  222. func TopicSearch(ctx *context.APIContext) {
  223. // swagger:operation GET /topics/search repository topicSearch
  224. // ---
  225. // summary: search topics via keyword
  226. // produces:
  227. // - application/json
  228. // parameters:
  229. // - name: q
  230. // in: query
  231. // description: keywords to search
  232. // required: true
  233. // type: string
  234. // - name: page
  235. // in: query
  236. // description: page number of results to return (1-based)
  237. // type: integer
  238. // - name: limit
  239. // in: query
  240. // description: page size of results
  241. // type: integer
  242. // responses:
  243. // "200":
  244. // "$ref": "#/responses/TopicListResponse"
  245. // "403":
  246. // "$ref": "#/responses/forbidden"
  247. opts := &models.FindTopicOptions{
  248. Keyword: ctx.FormString("q"),
  249. ListOptions: utils.GetListOptions(ctx),
  250. }
  251. topics, total, err := models.FindTopics(opts)
  252. if err != nil {
  253. ctx.InternalServerError(err)
  254. return
  255. }
  256. topicResponses := make([]*api.TopicResponse, len(topics))
  257. for i, topic := range topics {
  258. topicResponses[i] = convert.ToTopicResponse(topic)
  259. }
  260. ctx.SetTotalCountHeader(total)
  261. ctx.JSON(http.StatusOK, map[string]interface{}{
  262. "topics": topicResponses,
  263. })
  264. }