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.

306 lines
7.3 KiB

  1. // Copyright 2016 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. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. )
  10. // GetRelease get a single release of a repository
  11. func GetRelease(ctx *context.APIContext) {
  12. // swagger:operation GET /repos/{owner}/{repo}/releases repository repoGetRelease
  13. // ---
  14. // summary: Get a release
  15. // produces:
  16. // - application/json
  17. // parameters:
  18. // - name: owner
  19. // in: path
  20. // description: owner of the repo
  21. // type: string
  22. // required: true
  23. // - name: repo
  24. // in: path
  25. // description: name of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: id of the release to get
  31. // type: integer
  32. // required: true
  33. // responses:
  34. // "200":
  35. // "$ref": "#/responses/Release"
  36. id := ctx.ParamsInt64(":id")
  37. release, err := models.GetReleaseByID(id)
  38. if err != nil {
  39. ctx.Error(500, "GetReleaseByID", err)
  40. return
  41. }
  42. if release.RepoID != ctx.Repo.Repository.ID {
  43. ctx.Status(404)
  44. return
  45. }
  46. if err := release.LoadAttributes(); err != nil {
  47. ctx.Error(500, "LoadAttributes", err)
  48. return
  49. }
  50. ctx.JSON(200, release.APIFormat())
  51. }
  52. // ListReleases list a repository's releases
  53. func ListReleases(ctx *context.APIContext) {
  54. // swagger:operation GET /repos/{owner}/{repo}/releases repository repoListReleases
  55. // ---
  56. // summary: List a repo's releases
  57. // produces:
  58. // - application/json
  59. // parameters:
  60. // - name: owner
  61. // in: path
  62. // description: owner of the repo
  63. // type: string
  64. // required: true
  65. // - name: repo
  66. // in: path
  67. // description: name of the repo
  68. // type: string
  69. // required: true
  70. // responses:
  71. // "200":
  72. // "$ref": "#/responses/ReleaseList"
  73. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
  74. IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite,
  75. IncludeTags: false,
  76. }, 1, 2147483647)
  77. if err != nil {
  78. ctx.Error(500, "GetReleasesByRepoID", err)
  79. return
  80. }
  81. rels := make([]*api.Release, len(releases))
  82. for i, release := range releases {
  83. if err := release.LoadAttributes(); err != nil {
  84. ctx.Error(500, "LoadAttributes", err)
  85. return
  86. }
  87. rels[i] = release.APIFormat()
  88. }
  89. ctx.JSON(200, rels)
  90. }
  91. // CreateRelease create a release
  92. func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
  93. // swagger:operation GET /repos/{owner}/{repo}/releases repository repoCreateRelease
  94. // ---
  95. // summary: Create a release
  96. // consumes:
  97. // - application/json
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: owner
  102. // in: path
  103. // description: owner of the repo
  104. // type: string
  105. // required: true
  106. // - name: repo
  107. // in: path
  108. // description: name of the repo
  109. // type: string
  110. // required: true
  111. // - name: body
  112. // in: body
  113. // schema:
  114. // "$ref": "#/definitions/CreateReleaseOption"
  115. // responses:
  116. // "201":
  117. // "$ref": "#/responses/Release"
  118. if ctx.Repo.AccessMode < models.AccessModeWrite {
  119. ctx.Status(403)
  120. return
  121. }
  122. if !ctx.Repo.GitRepo.IsTagExist(form.TagName) {
  123. ctx.Status(404)
  124. return
  125. }
  126. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  127. if err != nil {
  128. if !models.IsErrReleaseNotExist(err) {
  129. ctx.ServerError("GetRelease", err)
  130. return
  131. }
  132. rel = &models.Release{
  133. RepoID: ctx.Repo.Repository.ID,
  134. PublisherID: ctx.User.ID,
  135. Publisher: ctx.User,
  136. TagName: form.TagName,
  137. Target: form.Target,
  138. Title: form.Title,
  139. Note: form.Note,
  140. IsDraft: form.IsDraft,
  141. IsPrerelease: form.IsPrerelease,
  142. IsTag: false,
  143. }
  144. if err := models.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  145. if models.IsErrReleaseAlreadyExist(err) {
  146. ctx.Status(409)
  147. } else {
  148. ctx.Error(500, "CreateRelease", err)
  149. }
  150. return
  151. }
  152. } else {
  153. if !rel.IsTag {
  154. ctx.Status(409)
  155. return
  156. }
  157. rel.Title = form.Title
  158. rel.Note = form.Note
  159. rel.IsDraft = form.IsDraft
  160. rel.IsPrerelease = form.IsPrerelease
  161. rel.PublisherID = ctx.User.ID
  162. rel.IsTag = false
  163. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  164. ctx.ServerError("UpdateRelease", err)
  165. return
  166. }
  167. }
  168. ctx.JSON(201, rel.APIFormat())
  169. }
  170. // EditRelease edit a release
  171. func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) {
  172. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id} repository repoEditRelease
  173. // ---
  174. // summary: Update a release
  175. // consumes:
  176. // - application/json
  177. // produces:
  178. // - application/json
  179. // parameters:
  180. // - name: owner
  181. // in: path
  182. // description: owner of the repo
  183. // type: string
  184. // required: true
  185. // - name: repo
  186. // in: path
  187. // description: name of the repo
  188. // type: string
  189. // required: true
  190. // - name: id
  191. // in: path
  192. // description: id of the release to edit
  193. // type: integer
  194. // required: true
  195. // - name: body
  196. // in: body
  197. // schema:
  198. // "$ref": "#/definitions/EditReleaseOption"
  199. // responses:
  200. // "200":
  201. // "$ref": "#/responses/Release"
  202. if ctx.Repo.AccessMode < models.AccessModeWrite {
  203. ctx.Status(403)
  204. return
  205. }
  206. id := ctx.ParamsInt64(":id")
  207. rel, err := models.GetReleaseByID(id)
  208. if err != nil && !models.IsErrReleaseNotExist(err) {
  209. ctx.Error(500, "GetReleaseByID", err)
  210. return
  211. }
  212. if err != nil && models.IsErrReleaseNotExist(err) ||
  213. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  214. ctx.Status(404)
  215. return
  216. }
  217. if len(form.TagName) > 0 {
  218. rel.TagName = form.TagName
  219. }
  220. if len(form.Target) > 0 {
  221. rel.Target = form.Target
  222. }
  223. if len(form.Title) > 0 {
  224. rel.Title = form.Title
  225. }
  226. if len(form.Note) > 0 {
  227. rel.Note = form.Note
  228. }
  229. if form.IsDraft != nil {
  230. rel.IsDraft = *form.IsDraft
  231. }
  232. if form.IsPrerelease != nil {
  233. rel.IsPrerelease = *form.IsPrerelease
  234. }
  235. if err := models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  236. ctx.Error(500, "UpdateRelease", err)
  237. return
  238. }
  239. rel, err = models.GetReleaseByID(id)
  240. if err != nil {
  241. ctx.Error(500, "GetReleaseByID", err)
  242. return
  243. }
  244. if err := rel.LoadAttributes(); err != nil {
  245. ctx.Error(500, "LoadAttributes", err)
  246. return
  247. }
  248. ctx.JSON(200, rel.APIFormat())
  249. }
  250. // DeleteRelease delete a release from a repository
  251. func DeleteRelease(ctx *context.APIContext) {
  252. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id} repository repoDeleteRelease
  253. // ---
  254. // summary: Delete a release
  255. // parameters:
  256. // - name: owner
  257. // in: path
  258. // description: owner of the repo
  259. // type: string
  260. // required: true
  261. // - name: repo
  262. // in: path
  263. // description: name of the repo
  264. // type: string
  265. // required: true
  266. // - name: id
  267. // in: path
  268. // description: id of the release to delete
  269. // type: integer
  270. // required: true
  271. // responses:
  272. // "204":
  273. // "$ref": "#/responses/empty"
  274. if ctx.Repo.AccessMode < models.AccessModeWrite {
  275. ctx.Status(403)
  276. return
  277. }
  278. id := ctx.ParamsInt64(":id")
  279. rel, err := models.GetReleaseByID(id)
  280. if err != nil && !models.IsErrReleaseNotExist(err) {
  281. ctx.Error(500, "GetReleaseByID", err)
  282. return
  283. }
  284. if err != nil && models.IsErrReleaseNotExist(err) ||
  285. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  286. ctx.Status(404)
  287. return
  288. }
  289. if err := models.DeleteReleaseByID(id, ctx.User, false); err != nil {
  290. ctx.Error(500, "DeleteReleaseByID", err)
  291. return
  292. }
  293. ctx.Status(204)
  294. }