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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "errors"
  7. "fmt"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  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. 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. listOpts := utils.GetListOptions(ctx)
  47. tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
  48. if err != nil {
  49. ctx.Error(http.StatusInternalServerError, "GetTags", err)
  50. return
  51. }
  52. apiTags := make([]*api.Tag, len(tags))
  53. for i := range tags {
  54. apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
  55. }
  56. ctx.SetTotalCountHeader(int64(total))
  57. ctx.JSON(http.StatusOK, &apiTags)
  58. }
  59. // GetAnnotatedTag get the tag of a repository.
  60. func GetAnnotatedTag(ctx *context.APIContext) {
  61. // swagger:operation GET /repos/{owner}/{repo}/git/tags/{sha} repository GetAnnotatedTag
  62. // ---
  63. // summary: Gets the tag object of an annotated tag (not lightweight tags)
  64. // produces:
  65. // - application/json
  66. // parameters:
  67. // - name: owner
  68. // in: path
  69. // description: owner of the repo
  70. // type: string
  71. // required: true
  72. // - name: repo
  73. // in: path
  74. // description: name of the repo
  75. // type: string
  76. // required: true
  77. // - name: sha
  78. // in: path
  79. // description: sha of the tag. The Git tags API only supports annotated tag objects, not lightweight tags.
  80. // type: string
  81. // required: true
  82. // responses:
  83. // "200":
  84. // "$ref": "#/responses/AnnotatedTag"
  85. // "400":
  86. // "$ref": "#/responses/error"
  87. sha := ctx.Params("sha")
  88. if len(sha) == 0 {
  89. ctx.Error(http.StatusBadRequest, "", "SHA not provided")
  90. return
  91. }
  92. if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
  93. ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
  94. } else {
  95. commit, err := tag.Commit()
  96. if err != nil {
  97. ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
  98. }
  99. ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx.Repo.Repository, tag, commit))
  100. }
  101. }
  102. // GetTag get the tag of a repository
  103. func GetTag(ctx *context.APIContext) {
  104. // swagger:operation GET /repos/{owner}/{repo}/tags/{tag} repository repoGetTag
  105. // ---
  106. // summary: Get the tag of a repository by tag name
  107. // produces:
  108. // - application/json
  109. // parameters:
  110. // - name: owner
  111. // in: path
  112. // description: owner of the repo
  113. // type: string
  114. // required: true
  115. // - name: repo
  116. // in: path
  117. // description: name of the repo
  118. // type: string
  119. // required: true
  120. // - name: tag
  121. // in: path
  122. // description: name of tag
  123. // type: string
  124. // required: true
  125. // responses:
  126. // "200":
  127. // "$ref": "#/responses/Tag"
  128. // "404":
  129. // "$ref": "#/responses/notFound"
  130. tagName := ctx.Params("*")
  131. tag, err := ctx.Repo.GitRepo.GetTag(tagName)
  132. if err != nil {
  133. ctx.NotFound(tagName)
  134. return
  135. }
  136. ctx.JSON(http.StatusOK, convert.ToTag(ctx.Repo.Repository, tag))
  137. }
  138. // CreateTag create a new git tag in a repository
  139. func CreateTag(ctx *context.APIContext) {
  140. // swagger:operation POST /repos/{owner}/{repo}/tags repository repoCreateTag
  141. // ---
  142. // summary: Create a new git tag in a repository
  143. // produces:
  144. // - application/json
  145. // parameters:
  146. // - name: owner
  147. // in: path
  148. // description: owner of the repo
  149. // type: string
  150. // required: true
  151. // - name: repo
  152. // in: path
  153. // description: name of the repo
  154. // type: string
  155. // required: true
  156. // - name: body
  157. // in: body
  158. // schema:
  159. // "$ref": "#/definitions/CreateTagOption"
  160. // responses:
  161. // "200":
  162. // "$ref": "#/responses/Tag"
  163. // "404":
  164. // "$ref": "#/responses/notFound"
  165. // "409":
  166. // "$ref": "#/responses/conflict"
  167. form := web.GetForm(ctx).(*api.CreateTagOption)
  168. // If target is not provided use default branch
  169. if len(form.Target) == 0 {
  170. form.Target = ctx.Repo.Repository.DefaultBranch
  171. }
  172. commit, err := ctx.Repo.GitRepo.GetCommit(form.Target)
  173. if err != nil {
  174. ctx.Error(http.StatusNotFound, "target not found", fmt.Errorf("target not found: %v", err))
  175. return
  176. }
  177. if err := releaseservice.CreateNewTag(ctx.User, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil {
  178. if models.IsErrTagAlreadyExists(err) {
  179. ctx.Error(http.StatusConflict, "tag exist", err)
  180. return
  181. }
  182. ctx.InternalServerError(err)
  183. return
  184. }
  185. tag, err := ctx.Repo.GitRepo.GetTag(form.TagName)
  186. if err != nil {
  187. ctx.InternalServerError(err)
  188. return
  189. }
  190. ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag))
  191. }
  192. // DeleteTag delete a specific tag of in a repository by name
  193. func DeleteTag(ctx *context.APIContext) {
  194. // swagger:operation DELETE /repos/{owner}/{repo}/tags/{tag} repository repoDeleteTag
  195. // ---
  196. // summary: Delete a repository's tag by name
  197. // produces:
  198. // - application/json
  199. // parameters:
  200. // - name: owner
  201. // in: path
  202. // description: owner of the repo
  203. // type: string
  204. // required: true
  205. // - name: repo
  206. // in: path
  207. // description: name of the repo
  208. // type: string
  209. // required: true
  210. // - name: tag
  211. // in: path
  212. // description: name of tag to delete
  213. // type: string
  214. // required: true
  215. // responses:
  216. // "204":
  217. // "$ref": "#/responses/empty"
  218. // "404":
  219. // "$ref": "#/responses/notFound"
  220. // "409":
  221. // "$ref": "#/responses/conflict"
  222. tagName := ctx.Params("*")
  223. tag, err := models.GetRelease(ctx.Repo.Repository.ID, tagName)
  224. if err != nil {
  225. if models.IsErrReleaseNotExist(err) {
  226. ctx.NotFound()
  227. return
  228. }
  229. ctx.Error(http.StatusInternalServerError, "GetRelease", err)
  230. return
  231. }
  232. if !tag.IsTag {
  233. ctx.Error(http.StatusConflict, "IsTag", errors.New("a tag attached to a release cannot be deleted directly"))
  234. return
  235. }
  236. if err = releaseservice.DeleteReleaseByID(tag.ID, ctx.User, true); err != nil {
  237. ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
  238. }
  239. ctx.Status(http.StatusNoContent)
  240. }