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.

tag.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/modules/context"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/web"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. "code.gitea.io/gitea/services/convert"
  15. releaseservice "code.gitea.io/gitea/services/release"
  16. )
  17. // ListTags list all the tags of a repository
  18. func ListTags(ctx *context.APIContext) {
  19. // swagger:operation GET /repos/{owner}/{repo}/tags repository repoListTags
  20. // ---
  21. // summary: List a repository's tags
  22. // produces:
  23. // - application/json
  24. // parameters:
  25. // - name: owner
  26. // in: path
  27. // description: owner of the repo
  28. // type: string
  29. // required: true
  30. // - name: repo
  31. // in: path
  32. // description: name of the repo
  33. // type: string
  34. // required: true
  35. // - name: page
  36. // in: query
  37. // description: page number of results to return (1-based)
  38. // type: integer
  39. // - name: limit
  40. // in: query
  41. // description: page size of results, default maximum page size is 50
  42. // type: integer
  43. // responses:
  44. // "200":
  45. // "$ref": "#/responses/TagList"
  46. // "404":
  47. // "$ref": "#/responses/notFound"
  48. listOpts := utils.GetListOptions(ctx)
  49. tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
  50. if err != nil {
  51. ctx.Error(http.StatusInternalServerError, "GetTags", err)
  52. return
  53. }
  54. apiTags := make([]*api.Tag, len(tags))
  55. for i := range tags {
  56. apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
  57. }
  58. ctx.SetTotalCountHeader(int64(total))
  59. ctx.JSON(http.StatusOK, &apiTags)
  60. }
  61. // GetAnnotatedTag get the tag of a repository.
  62. func GetAnnotatedTag(ctx *context.APIContext) {
  63. // swagger:operation GET /repos/{owner}/{repo}/git/tags/{sha} repository GetAnnotatedTag
  64. // ---
  65. // summary: Gets the tag object of an annotated tag (not lightweight tags)
  66. // produces:
  67. // - application/json
  68. // parameters:
  69. // - name: owner
  70. // in: path
  71. // description: owner of the repo
  72. // type: string
  73. // required: true
  74. // - name: repo
  75. // in: path
  76. // description: name of the repo
  77. // type: string
  78. // required: true
  79. // - name: sha
  80. // in: path
  81. // description: sha of the tag. The Git tags API only supports annotated tag objects, not lightweight tags.
  82. // type: string
  83. // required: true
  84. // responses:
  85. // "200":
  86. // "$ref": "#/responses/AnnotatedTag"
  87. // "400":
  88. // "$ref": "#/responses/error"
  89. // "404":
  90. // "$ref": "#/responses/notFound"
  91. sha := ctx.Params("sha")
  92. if len(sha) == 0 {
  93. ctx.Error(http.StatusBadRequest, "", "SHA not provided")
  94. return
  95. }
  96. if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
  97. ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
  98. } else {
  99. commit, err := tag.Commit(ctx.Repo.GitRepo)
  100. if err != nil {
  101. ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
  102. }
  103. ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx, ctx.Repo.Repository, tag, commit))
  104. }
  105. }
  106. // GetTag get the tag of a repository
  107. func GetTag(ctx *context.APIContext) {
  108. // swagger:operation GET /repos/{owner}/{repo}/tags/{tag} repository repoGetTag
  109. // ---
  110. // summary: Get the tag of a repository by tag name
  111. // produces:
  112. // - application/json
  113. // parameters:
  114. // - name: owner
  115. // in: path
  116. // description: owner of the repo
  117. // type: string
  118. // required: true
  119. // - name: repo
  120. // in: path
  121. // description: name of the repo
  122. // type: string
  123. // required: true
  124. // - name: tag
  125. // in: path
  126. // description: name of tag
  127. // type: string
  128. // required: true
  129. // responses:
  130. // "200":
  131. // "$ref": "#/responses/Tag"
  132. // "404":
  133. // "$ref": "#/responses/notFound"
  134. tagName := ctx.Params("*")
  135. tag, err := ctx.Repo.GitRepo.GetTag(tagName)
  136. if err != nil {
  137. ctx.NotFound(tagName)
  138. return
  139. }
  140. ctx.JSON(http.StatusOK, convert.ToTag(ctx.Repo.Repository, tag))
  141. }
  142. // CreateTag create a new git tag in a repository
  143. func CreateTag(ctx *context.APIContext) {
  144. // swagger:operation POST /repos/{owner}/{repo}/tags repository repoCreateTag
  145. // ---
  146. // summary: Create a new git tag in a repository
  147. // produces:
  148. // - application/json
  149. // parameters:
  150. // - name: owner
  151. // in: path
  152. // description: owner of the repo
  153. // type: string
  154. // required: true
  155. // - name: repo
  156. // in: path
  157. // description: name of the repo
  158. // type: string
  159. // required: true
  160. // - name: body
  161. // in: body
  162. // schema:
  163. // "$ref": "#/definitions/CreateTagOption"
  164. // responses:
  165. // "200":
  166. // "$ref": "#/responses/Tag"
  167. // "404":
  168. // "$ref": "#/responses/notFound"
  169. // "405":
  170. // "$ref": "#/responses/empty"
  171. // "409":
  172. // "$ref": "#/responses/conflict"
  173. form := web.GetForm(ctx).(*api.CreateTagOption)
  174. // If target is not provided use default branch
  175. if len(form.Target) == 0 {
  176. form.Target = ctx.Repo.Repository.DefaultBranch
  177. }
  178. commit, err := ctx.Repo.GitRepo.GetCommit(form.Target)
  179. if err != nil {
  180. ctx.Error(http.StatusNotFound, "target not found", fmt.Errorf("target not found: %w", err))
  181. return
  182. }
  183. if err := releaseservice.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil {
  184. if models.IsErrTagAlreadyExists(err) {
  185. ctx.Error(http.StatusConflict, "tag exist", err)
  186. return
  187. }
  188. if models.IsErrProtectedTagName(err) {
  189. ctx.Error(http.StatusMethodNotAllowed, "CreateNewTag", "user not allowed to create protected tag")
  190. return
  191. }
  192. ctx.InternalServerError(err)
  193. return
  194. }
  195. tag, err := ctx.Repo.GitRepo.GetTag(form.TagName)
  196. if err != nil {
  197. ctx.InternalServerError(err)
  198. return
  199. }
  200. ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag))
  201. }
  202. // DeleteTag delete a specific tag of in a repository by name
  203. func DeleteTag(ctx *context.APIContext) {
  204. // swagger:operation DELETE /repos/{owner}/{repo}/tags/{tag} repository repoDeleteTag
  205. // ---
  206. // summary: Delete a repository's tag by name
  207. // produces:
  208. // - application/json
  209. // parameters:
  210. // - name: owner
  211. // in: path
  212. // description: owner of the repo
  213. // type: string
  214. // required: true
  215. // - name: repo
  216. // in: path
  217. // description: name of the repo
  218. // type: string
  219. // required: true
  220. // - name: tag
  221. // in: path
  222. // description: name of tag to delete
  223. // type: string
  224. // required: true
  225. // responses:
  226. // "204":
  227. // "$ref": "#/responses/empty"
  228. // "404":
  229. // "$ref": "#/responses/notFound"
  230. // "405":
  231. // "$ref": "#/responses/empty"
  232. // "409":
  233. // "$ref": "#/responses/conflict"
  234. tagName := ctx.Params("*")
  235. tag, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, tagName)
  236. if err != nil {
  237. if repo_model.IsErrReleaseNotExist(err) {
  238. ctx.NotFound()
  239. return
  240. }
  241. ctx.Error(http.StatusInternalServerError, "GetRelease", err)
  242. return
  243. }
  244. if !tag.IsTag {
  245. ctx.Error(http.StatusConflict, "IsTag", errors.New("a tag attached to a release cannot be deleted directly"))
  246. return
  247. }
  248. if err = releaseservice.DeleteReleaseByID(ctx, tag.ID, ctx.Doer, true); err != nil {
  249. if models.IsErrProtectedTagName(err) {
  250. ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
  251. return
  252. }
  253. ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
  254. return
  255. }
  256. ctx.Status(http.StatusNoContent)
  257. }