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.

status.go 5.8KB

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