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.

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