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.

224 lines
5.8 KiB

  1. // Copyright 2017 Gitea. 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. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // NewCommitStatus creates a new CommitStatus
  12. func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) {
  13. // swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
  14. // ---
  15. // summary: Create a commit status
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // - name: sha
  30. // in: path
  31. // description: sha of the commit
  32. // type: string
  33. // required: true
  34. // - name: body
  35. // in: body
  36. // schema:
  37. // "$ref": "#/definitions/CreateStatusOption"
  38. // responses:
  39. // "200":
  40. // "$ref": "#/responses/StatusList"
  41. sha := ctx.Params("sha")
  42. if len(sha) == 0 {
  43. sha = ctx.Params("ref")
  44. }
  45. if len(sha) == 0 {
  46. ctx.Error(400, "ref/sha not given", nil)
  47. return
  48. }
  49. status := &models.CommitStatus{
  50. State: models.CommitStatusState(form.State),
  51. TargetURL: form.TargetURL,
  52. Description: form.Description,
  53. Context: form.Context,
  54. }
  55. if err := models.NewCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
  56. ctx.Error(500, "NewCommitStatus", err)
  57. return
  58. }
  59. newStatus, err := models.GetCommitStatus(ctx.Repo.Repository, sha, status)
  60. if err != nil {
  61. ctx.Error(500, "GetCommitStatus", err)
  62. return
  63. }
  64. ctx.JSON(201, newStatus.APIFormat())
  65. }
  66. // GetCommitStatuses returns all statuses for any given commit hash
  67. func GetCommitStatuses(ctx *context.APIContext) {
  68. // swagger:operation GET /repos/{owner}/{repo}/statuses/{sha} repository repoListStatuses
  69. // ---
  70. // summary: Get a commit's statuses
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: owner
  75. // in: path
  76. // description: owner of the repo
  77. // type: string
  78. // required: true
  79. // - name: repo
  80. // in: path
  81. // description: name of the repo
  82. // type: string
  83. // required: true
  84. // - name: sha
  85. // in: path
  86. // description: sha of the commit
  87. // type: string
  88. // required: true
  89. // responses:
  90. // "200":
  91. // "$ref": "#/responses/StatusList"
  92. getCommitStatuses(ctx, ctx.Params("sha"))
  93. }
  94. // GetCommitStatusesByRef returns all statuses for any given commit ref
  95. func GetCommitStatusesByRef(ctx *context.APIContext) {
  96. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  97. // ---
  98. // summary: Get a commit's statuses, by branch/tag/commit reference
  99. // produces:
  100. // - application/json
  101. // parameters:
  102. // - name: owner
  103. // in: path
  104. // description: owner of the repo
  105. // type: string
  106. // required: true
  107. // - name: repo
  108. // in: path
  109. // description: name of the repo
  110. // type: string
  111. // required: true
  112. // - name: ref
  113. // in: path
  114. // description: name of branch/tag/commit
  115. // type: string
  116. // required: true
  117. // responses:
  118. // "200":
  119. // "$ref": "#/responses/StatusList"
  120. getCommitStatuses(ctx, ctx.Params("ref"))
  121. }
  122. func getCommitStatuses(ctx *context.APIContext, sha string) {
  123. if len(sha) == 0 {
  124. ctx.Error(400, "ref/sha not given", nil)
  125. return
  126. }
  127. repo := ctx.Repo.Repository
  128. page := ctx.ParamsInt("page")
  129. statuses, err := models.GetCommitStatuses(repo, sha, page)
  130. if err != nil {
  131. ctx.Error(500, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  132. }
  133. apiStatuses := make([]*api.Status, 0, len(statuses))
  134. for _, status := range statuses {
  135. apiStatuses = append(apiStatuses, status.APIFormat())
  136. }
  137. ctx.JSON(200, apiStatuses)
  138. }
  139. type combinedCommitStatus struct {
  140. State models.CommitStatusState `json:"state"`
  141. SHA string `json:"sha"`
  142. TotalCount int `json:"total_count"`
  143. Statuses []*api.Status `json:"statuses"`
  144. Repo *api.Repository `json:"repository"`
  145. CommitURL string `json:"commit_url"`
  146. URL string `json:"url"`
  147. }
  148. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  149. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  150. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef
  151. // ---
  152. // summary: Get a commit's combined status, by branch/tag/commit reference
  153. // produces:
  154. // - application/json
  155. // parameters:
  156. // - name: owner
  157. // in: path
  158. // description: owner of the repo
  159. // type: string
  160. // required: true
  161. // - name: repo
  162. // in: path
  163. // description: name of the repo
  164. // type: string
  165. // required: true
  166. // - name: ref
  167. // in: path
  168. // description: name of branch/tag/commit
  169. // type: string
  170. // required: true
  171. // responses:
  172. // "200":
  173. // "$ref": "#/responses/Status"
  174. sha := ctx.Params("ref")
  175. if len(sha) == 0 {
  176. ctx.Error(400, "ref/sha not given", nil)
  177. return
  178. }
  179. repo := ctx.Repo.Repository
  180. page := ctx.ParamsInt("page")
  181. statuses, err := models.GetLatestCommitStatus(repo, sha, page)
  182. if err != nil {
  183. ctx.Error(500, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  184. return
  185. }
  186. if len(statuses) == 0 {
  187. ctx.Status(200)
  188. return
  189. }
  190. retStatus := &combinedCommitStatus{
  191. SHA: sha,
  192. TotalCount: len(statuses),
  193. Repo: repo.APIFormat(ctx.Repo.AccessMode),
  194. URL: "",
  195. }
  196. retStatus.Statuses = make([]*api.Status, 0, len(statuses))
  197. for _, status := range statuses {
  198. retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
  199. if status.State.IsWorseThan(retStatus.State) {
  200. retStatus.State = status.State
  201. }
  202. }
  203. ctx.JSON(200, retStatus)
  204. }