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.

324 lines
8.0 KiB

Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
  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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/setting"
  9. api "code.gitea.io/gitea/modules/structs"
  10. releaseservice "code.gitea.io/gitea/services/release"
  11. )
  12. // GetRelease get a single release of a repository
  13. func GetRelease(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/releases/{id} repository repoGetRelease
  15. // ---
  16. // summary: Get a release
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: id
  31. // in: path
  32. // description: id of the release to get
  33. // type: integer
  34. // format: int64
  35. // required: true
  36. // responses:
  37. // "200":
  38. // "$ref": "#/responses/Release"
  39. id := ctx.ParamsInt64(":id")
  40. release, err := models.GetReleaseByID(id)
  41. if err != nil {
  42. ctx.Error(500, "GetReleaseByID", err)
  43. return
  44. }
  45. if release.RepoID != ctx.Repo.Repository.ID {
  46. ctx.NotFound()
  47. return
  48. }
  49. if err := release.LoadAttributes(); err != nil {
  50. ctx.Error(500, "LoadAttributes", err)
  51. return
  52. }
  53. ctx.JSON(200, release.APIFormat())
  54. }
  55. func getPagesInfo(ctx *context.APIContext) (int, int) {
  56. page := ctx.QueryInt("page")
  57. if page == 0 {
  58. page = 1
  59. }
  60. perPage := ctx.QueryInt("per_page")
  61. if perPage == 0 {
  62. perPage = setting.API.DefaultPagingNum
  63. } else if perPage > setting.API.MaxResponseItems {
  64. perPage = setting.API.MaxResponseItems
  65. }
  66. return page, perPage
  67. }
  68. // ListReleases list a repository's releases
  69. func ListReleases(ctx *context.APIContext) {
  70. // swagger:operation GET /repos/{owner}/{repo}/releases repository repoListReleases
  71. // ---
  72. // summary: List a repo's releases
  73. // produces:
  74. // - application/json
  75. // parameters:
  76. // - name: owner
  77. // in: path
  78. // description: owner of the repo
  79. // type: string
  80. // required: true
  81. // - name: repo
  82. // in: path
  83. // description: name of the repo
  84. // type: string
  85. // required: true
  86. // - name: page
  87. // in: query
  88. // description: page wants to load
  89. // type: integer
  90. // - name: per_page
  91. // in: query
  92. // description: items count every page wants to load
  93. // type: integer
  94. // responses:
  95. // "200":
  96. // "$ref": "#/responses/ReleaseList"
  97. page, limit := getPagesInfo(ctx)
  98. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
  99. IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite,
  100. IncludeTags: false,
  101. }, page, limit)
  102. if err != nil {
  103. ctx.Error(500, "GetReleasesByRepoID", err)
  104. return
  105. }
  106. rels := make([]*api.Release, len(releases))
  107. for i, release := range releases {
  108. if err := release.LoadAttributes(); err != nil {
  109. ctx.Error(500, "LoadAttributes", err)
  110. return
  111. }
  112. rels[i] = release.APIFormat()
  113. }
  114. ctx.JSON(200, rels)
  115. }
  116. // CreateRelease create a release
  117. func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
  118. // swagger:operation POST /repos/{owner}/{repo}/releases repository repoCreateRelease
  119. // ---
  120. // summary: Create a release
  121. // consumes:
  122. // - application/json
  123. // produces:
  124. // - application/json
  125. // parameters:
  126. // - name: owner
  127. // in: path
  128. // description: owner of the repo
  129. // type: string
  130. // required: true
  131. // - name: repo
  132. // in: path
  133. // description: name of the repo
  134. // type: string
  135. // required: true
  136. // - name: body
  137. // in: body
  138. // schema:
  139. // "$ref": "#/definitions/CreateReleaseOption"
  140. // responses:
  141. // "201":
  142. // "$ref": "#/responses/Release"
  143. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  144. if err != nil {
  145. if !models.IsErrReleaseNotExist(err) {
  146. ctx.ServerError("GetRelease", err)
  147. return
  148. }
  149. // If target is not provided use default branch
  150. if len(form.Target) == 0 {
  151. form.Target = ctx.Repo.Repository.DefaultBranch
  152. }
  153. rel = &models.Release{
  154. RepoID: ctx.Repo.Repository.ID,
  155. PublisherID: ctx.User.ID,
  156. Publisher: ctx.User,
  157. TagName: form.TagName,
  158. Target: form.Target,
  159. Title: form.Title,
  160. Note: form.Note,
  161. IsDraft: form.IsDraft,
  162. IsPrerelease: form.IsPrerelease,
  163. IsTag: false,
  164. Repo: ctx.Repo.Repository,
  165. }
  166. if err := releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  167. if models.IsErrReleaseAlreadyExist(err) {
  168. ctx.Status(409)
  169. } else {
  170. ctx.Error(500, "CreateRelease", err)
  171. }
  172. return
  173. }
  174. } else {
  175. if !rel.IsTag {
  176. ctx.Status(409)
  177. return
  178. }
  179. rel.Title = form.Title
  180. rel.Note = form.Note
  181. rel.IsDraft = form.IsDraft
  182. rel.IsPrerelease = form.IsPrerelease
  183. rel.PublisherID = ctx.User.ID
  184. rel.IsTag = false
  185. rel.Repo = ctx.Repo.Repository
  186. rel.Publisher = ctx.User
  187. if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, nil); err != nil {
  188. ctx.ServerError("UpdateRelease", err)
  189. return
  190. }
  191. }
  192. ctx.JSON(201, rel.APIFormat())
  193. }
  194. // EditRelease edit a release
  195. func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) {
  196. // swagger:operation PATCH /repos/{owner}/{repo}/releases/{id} repository repoEditRelease
  197. // ---
  198. // summary: Update a release
  199. // consumes:
  200. // - application/json
  201. // produces:
  202. // - application/json
  203. // parameters:
  204. // - name: owner
  205. // in: path
  206. // description: owner of the repo
  207. // type: string
  208. // required: true
  209. // - name: repo
  210. // in: path
  211. // description: name of the repo
  212. // type: string
  213. // required: true
  214. // - name: id
  215. // in: path
  216. // description: id of the release to edit
  217. // type: integer
  218. // format: int64
  219. // required: true
  220. // - name: body
  221. // in: body
  222. // schema:
  223. // "$ref": "#/definitions/EditReleaseOption"
  224. // responses:
  225. // "200":
  226. // "$ref": "#/responses/Release"
  227. id := ctx.ParamsInt64(":id")
  228. rel, err := models.GetReleaseByID(id)
  229. if err != nil && !models.IsErrReleaseNotExist(err) {
  230. ctx.Error(500, "GetReleaseByID", err)
  231. return
  232. }
  233. if err != nil && models.IsErrReleaseNotExist(err) ||
  234. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  235. ctx.NotFound()
  236. return
  237. }
  238. if len(form.TagName) > 0 {
  239. rel.TagName = form.TagName
  240. }
  241. if len(form.Target) > 0 {
  242. rel.Target = form.Target
  243. }
  244. if len(form.Title) > 0 {
  245. rel.Title = form.Title
  246. }
  247. if len(form.Note) > 0 {
  248. rel.Note = form.Note
  249. }
  250. if form.IsDraft != nil {
  251. rel.IsDraft = *form.IsDraft
  252. }
  253. if form.IsPrerelease != nil {
  254. rel.IsPrerelease = *form.IsPrerelease
  255. }
  256. if err := releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, nil); err != nil {
  257. ctx.Error(500, "UpdateRelease", err)
  258. return
  259. }
  260. rel, err = models.GetReleaseByID(id)
  261. if err != nil {
  262. ctx.Error(500, "GetReleaseByID", err)
  263. return
  264. }
  265. if err := rel.LoadAttributes(); err != nil {
  266. ctx.Error(500, "LoadAttributes", err)
  267. return
  268. }
  269. ctx.JSON(200, rel.APIFormat())
  270. }
  271. // DeleteRelease delete a release from a repository
  272. func DeleteRelease(ctx *context.APIContext) {
  273. // swagger:operation DELETE /repos/{owner}/{repo}/releases/{id} repository repoDeleteRelease
  274. // ---
  275. // summary: Delete a release
  276. // parameters:
  277. // - name: owner
  278. // in: path
  279. // description: owner of the repo
  280. // type: string
  281. // required: true
  282. // - name: repo
  283. // in: path
  284. // description: name of the repo
  285. // type: string
  286. // required: true
  287. // - name: id
  288. // in: path
  289. // description: id of the release to delete
  290. // type: integer
  291. // format: int64
  292. // required: true
  293. // responses:
  294. // "204":
  295. // "$ref": "#/responses/empty"
  296. id := ctx.ParamsInt64(":id")
  297. rel, err := models.GetReleaseByID(id)
  298. if err != nil && !models.IsErrReleaseNotExist(err) {
  299. ctx.Error(500, "GetReleaseByID", err)
  300. return
  301. }
  302. if err != nil && models.IsErrReleaseNotExist(err) ||
  303. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  304. ctx.NotFound()
  305. return
  306. }
  307. if err := releaseservice.DeleteReleaseByID(id, ctx.User, false); err != nil {
  308. ctx.Error(500, "DeleteReleaseByID", err)
  309. return
  310. }
  311. ctx.Status(204)
  312. }