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.

status.go 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // - name: page
  86. // in: query
  87. // description: page number of results
  88. // type: integer
  89. // required: false
  90. // - name: sort
  91. // in: query
  92. // description: type of sort
  93. // type: string
  94. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  95. // required: false
  96. // - name: state
  97. // in: query
  98. // description: type of state
  99. // type: string
  100. // enum: [pending, success, error, failure, warning]
  101. // required: false
  102. // responses:
  103. // "200":
  104. // "$ref": "#/responses/StatusList"
  105. getCommitStatuses(ctx, ctx.Params("sha"))
  106. }
  107. // GetCommitStatusesByRef returns all statuses for any given commit ref
  108. func GetCommitStatusesByRef(ctx *context.APIContext) {
  109. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  110. // ---
  111. // summary: Get a commit's statuses, by branch/tag/commit reference
  112. // produces:
  113. // - application/json
  114. // parameters:
  115. // - name: owner
  116. // in: path
  117. // description: owner of the repo
  118. // type: string
  119. // required: true
  120. // - name: repo
  121. // in: path
  122. // description: name of the repo
  123. // type: string
  124. // required: true
  125. // - name: ref
  126. // in: path
  127. // description: name of branch/tag/commit
  128. // type: string
  129. // required: true
  130. // - name: page
  131. // in: query
  132. // description: page number of results
  133. // type: integer
  134. // required: false
  135. // - name: sort
  136. // in: query
  137. // description: type of sort
  138. // type: string
  139. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  140. // required: false
  141. // - name: state
  142. // in: query
  143. // description: type of state
  144. // type: string
  145. // enum: [pending, success, error, failure, warning]
  146. // required: false
  147. // responses:
  148. // "200":
  149. // "$ref": "#/responses/StatusList"
  150. getCommitStatuses(ctx, ctx.Params("ref"))
  151. }
  152. func getCommitStatuses(ctx *context.APIContext, sha string) {
  153. if len(sha) == 0 {
  154. ctx.Error(400, "ref/sha not given", nil)
  155. return
  156. }
  157. repo := ctx.Repo.Repository
  158. statuses, _, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{
  159. Page: ctx.QueryInt("page"),
  160. SortType: ctx.QueryTrim("sort"),
  161. State: ctx.QueryTrim("state"),
  162. })
  163. if err != nil {
  164. ctx.Error(500, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, ctx.QueryInt("page"), err))
  165. return
  166. }
  167. apiStatuses := make([]*api.Status, 0, len(statuses))
  168. for _, status := range statuses {
  169. apiStatuses = append(apiStatuses, status.APIFormat())
  170. }
  171. ctx.JSON(200, apiStatuses)
  172. }
  173. type combinedCommitStatus struct {
  174. State models.CommitStatusState `json:"state"`
  175. SHA string `json:"sha"`
  176. TotalCount int `json:"total_count"`
  177. Statuses []*api.Status `json:"statuses"`
  178. Repo *api.Repository `json:"repository"`
  179. CommitURL string `json:"commit_url"`
  180. URL string `json:"url"`
  181. }
  182. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  183. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  184. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef
  185. // ---
  186. // summary: Get a commit's combined status, by branch/tag/commit reference
  187. // produces:
  188. // - application/json
  189. // parameters:
  190. // - name: owner
  191. // in: path
  192. // description: owner of the repo
  193. // type: string
  194. // required: true
  195. // - name: repo
  196. // in: path
  197. // description: name of the repo
  198. // type: string
  199. // required: true
  200. // - name: ref
  201. // in: path
  202. // description: name of branch/tag/commit
  203. // type: string
  204. // required: true
  205. // - name: page
  206. // in: query
  207. // description: page number of results
  208. // type: integer
  209. // required: false
  210. // responses:
  211. // "200":
  212. // "$ref": "#/responses/Status"
  213. sha := ctx.Params("ref")
  214. if len(sha) == 0 {
  215. ctx.Error(400, "ref/sha not given", nil)
  216. return
  217. }
  218. repo := ctx.Repo.Repository
  219. page := ctx.QueryInt("page")
  220. statuses, err := models.GetLatestCommitStatus(repo, sha, page)
  221. if err != nil {
  222. ctx.Error(500, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  223. return
  224. }
  225. if len(statuses) == 0 {
  226. ctx.Status(200)
  227. return
  228. }
  229. retStatus := &combinedCommitStatus{
  230. SHA: sha,
  231. TotalCount: len(statuses),
  232. Repo: repo.APIFormat(ctx.Repo.AccessMode),
  233. URL: "",
  234. }
  235. retStatus.Statuses = make([]*api.Status, 0, len(statuses))
  236. for _, status := range statuses {
  237. retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
  238. if status.State.IsWorseThan(retStatus.State) {
  239. retStatus.State = status.State
  240. }
  241. }
  242. ctx.JSON(200, retStatus)
  243. }