You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

release.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. id := ctx.ParamsInt64(":id")
  13. release, err := models.GetReleaseByID(id)
  14. if err != nil {
  15. ctx.Error(500, "GetReleaseByID", err)
  16. return
  17. }
  18. if release.RepoID != ctx.Repo.Repository.ID {
  19. ctx.Status(404)
  20. return
  21. }
  22. if err := release.LoadAttributes(); err != nil {
  23. ctx.Error(500, "LoadAttributes", err)
  24. return
  25. }
  26. ctx.JSON(200, release.APIFormat())
  27. }
  28. // ListReleases list a repository's releases
  29. func ListReleases(ctx *context.APIContext) {
  30. releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
  31. IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite,
  32. IncludeTags: false,
  33. }, 1, 2147483647)
  34. if err != nil {
  35. ctx.Error(500, "GetReleasesByRepoID", err)
  36. return
  37. }
  38. rels := make([]*api.Release, len(releases))
  39. for i, release := range releases {
  40. if err := release.LoadAttributes(); err != nil {
  41. ctx.Error(500, "LoadAttributes", err)
  42. return
  43. }
  44. rels[i] = release.APIFormat()
  45. }
  46. ctx.JSON(200, rels)
  47. }
  48. // CreateRelease create a release
  49. func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
  50. if ctx.Repo.AccessMode < models.AccessModeWrite {
  51. ctx.Status(403)
  52. return
  53. }
  54. if !ctx.Repo.GitRepo.IsTagExist(form.TagName) {
  55. ctx.Status(404)
  56. return
  57. }
  58. rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
  59. if err != nil {
  60. if !models.IsErrReleaseNotExist(err) {
  61. ctx.Handle(500, "GetRelease", err)
  62. return
  63. }
  64. rel = &models.Release{
  65. RepoID: ctx.Repo.Repository.ID,
  66. PublisherID: ctx.User.ID,
  67. Publisher: ctx.User,
  68. TagName: form.TagName,
  69. Target: form.Target,
  70. Title: form.Title,
  71. Note: form.Note,
  72. IsDraft: form.IsDraft,
  73. IsPrerelease: form.IsPrerelease,
  74. IsTag: false,
  75. }
  76. if err := models.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  77. if models.IsErrReleaseAlreadyExist(err) {
  78. ctx.Status(409)
  79. } else {
  80. ctx.Error(500, "CreateRelease", err)
  81. }
  82. return
  83. }
  84. } else {
  85. if !rel.IsTag {
  86. ctx.Status(409)
  87. return
  88. }
  89. rel.Title = form.Title
  90. rel.Note = form.Note
  91. rel.IsDraft = form.IsDraft
  92. rel.IsPrerelease = form.IsPrerelease
  93. rel.PublisherID = ctx.User.ID
  94. rel.IsTag = false
  95. if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  96. ctx.Handle(500, "UpdateRelease", err)
  97. return
  98. }
  99. }
  100. ctx.JSON(201, rel.APIFormat())
  101. }
  102. // EditRelease edit a release
  103. func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) {
  104. if ctx.Repo.AccessMode < models.AccessModeWrite {
  105. ctx.Status(403)
  106. return
  107. }
  108. id := ctx.ParamsInt64(":id")
  109. rel, err := models.GetReleaseByID(id)
  110. if err != nil && !models.IsErrReleaseNotExist(err) {
  111. ctx.Error(500, "GetReleaseByID", err)
  112. return
  113. }
  114. if err != nil && models.IsErrReleaseNotExist(err) ||
  115. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  116. ctx.Status(404)
  117. return
  118. }
  119. if len(form.TagName) > 0 {
  120. rel.TagName = form.TagName
  121. }
  122. if len(form.Target) > 0 {
  123. rel.Target = form.Target
  124. }
  125. if len(form.Title) > 0 {
  126. rel.Title = form.Title
  127. }
  128. if len(form.Note) > 0 {
  129. rel.Note = form.Note
  130. }
  131. if form.IsDraft != nil {
  132. rel.IsDraft = *form.IsDraft
  133. }
  134. if form.IsPrerelease != nil {
  135. rel.IsPrerelease = *form.IsPrerelease
  136. }
  137. if err := models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
  138. ctx.Error(500, "UpdateRelease", err)
  139. return
  140. }
  141. rel, err = models.GetReleaseByID(id)
  142. if err != nil {
  143. ctx.Error(500, "GetReleaseByID", err)
  144. return
  145. }
  146. if err := rel.LoadAttributes(); err != nil {
  147. ctx.Error(500, "LoadAttributes", err)
  148. return
  149. }
  150. ctx.JSON(200, rel.APIFormat())
  151. }
  152. // DeleteRelease delete a release from a repository
  153. func DeleteRelease(ctx *context.APIContext) {
  154. if ctx.Repo.AccessMode < models.AccessModeWrite {
  155. ctx.Status(403)
  156. return
  157. }
  158. id := ctx.ParamsInt64(":id")
  159. rel, err := models.GetReleaseByID(id)
  160. if err != nil && !models.IsErrReleaseNotExist(err) {
  161. ctx.Error(500, "GetReleaseByID", err)
  162. return
  163. }
  164. if err != nil && models.IsErrReleaseNotExist(err) ||
  165. rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID {
  166. ctx.Status(404)
  167. return
  168. }
  169. if err := models.DeleteReleaseByID(id, ctx.User, false); err != nil {
  170. ctx.Error(500, "DeleteReleaseByID", err)
  171. return
  172. }
  173. ctx.Status(204)
  174. }