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.4KB

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