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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2017 Gitea. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "net/http"
  7. git_model "code.gitea.io/gitea/models/git"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/modules/web"
  11. "code.gitea.io/gitea/routers/api/v1/utils"
  12. "code.gitea.io/gitea/services/convert"
  13. files_service "code.gitea.io/gitea/services/repository/files"
  14. )
  15. // NewCommitStatus creates a new CommitStatus
  16. func NewCommitStatus(ctx *context.APIContext) {
  17. // swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
  18. // ---
  19. // summary: Create a commit status
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: owner
  24. // in: path
  25. // description: owner of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: name of the repo
  31. // type: string
  32. // required: true
  33. // - name: sha
  34. // in: path
  35. // description: sha of the commit
  36. // type: string
  37. // required: true
  38. // - name: body
  39. // in: body
  40. // schema:
  41. // "$ref": "#/definitions/CreateStatusOption"
  42. // responses:
  43. // "201":
  44. // "$ref": "#/responses/CommitStatus"
  45. // "400":
  46. // "$ref": "#/responses/error"
  47. form := web.GetForm(ctx).(*api.CreateStatusOption)
  48. sha := ctx.Params("sha")
  49. if len(sha) == 0 {
  50. ctx.Error(http.StatusBadRequest, "sha not given", nil)
  51. return
  52. }
  53. status := &git_model.CommitStatus{
  54. State: form.State,
  55. TargetURL: form.TargetURL,
  56. Description: form.Description,
  57. Context: form.Context,
  58. }
  59. if err := files_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.Doer, sha, status); err != nil {
  60. ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
  61. return
  62. }
  63. ctx.JSON(http.StatusCreated, convert.ToCommitStatus(ctx, status))
  64. }
  65. // GetCommitStatuses returns all statuses for any given commit hash
  66. func GetCommitStatuses(ctx *context.APIContext) {
  67. // swagger:operation GET /repos/{owner}/{repo}/statuses/{sha} repository repoListStatuses
  68. // ---
  69. // summary: Get a commit's statuses
  70. // produces:
  71. // - application/json
  72. // parameters:
  73. // - name: owner
  74. // in: path
  75. // description: owner of the repo
  76. // type: string
  77. // required: true
  78. // - name: repo
  79. // in: path
  80. // description: name of the repo
  81. // type: string
  82. // required: true
  83. // - name: sha
  84. // in: path
  85. // description: sha of the commit
  86. // type: string
  87. // required: true
  88. // - name: sort
  89. // in: query
  90. // description: type of sort
  91. // type: string
  92. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  93. // required: false
  94. // - name: state
  95. // in: query
  96. // description: type of state
  97. // type: string
  98. // enum: [pending, success, error, failure, warning]
  99. // required: false
  100. // - name: page
  101. // in: query
  102. // description: page number of results to return (1-based)
  103. // type: integer
  104. // - name: limit
  105. // in: query
  106. // description: page size of results
  107. // type: integer
  108. // responses:
  109. // "200":
  110. // "$ref": "#/responses/CommitStatusList"
  111. // "400":
  112. // "$ref": "#/responses/error"
  113. getCommitStatuses(ctx, ctx.Params("sha"))
  114. }
  115. // GetCommitStatusesByRef returns all statuses for any given commit ref
  116. func GetCommitStatusesByRef(ctx *context.APIContext) {
  117. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  118. // ---
  119. // summary: Get a commit's statuses, by branch/tag/commit reference
  120. // produces:
  121. // - application/json
  122. // parameters:
  123. // - name: owner
  124. // in: path
  125. // description: owner of the repo
  126. // type: string
  127. // required: true
  128. // - name: repo
  129. // in: path
  130. // description: name of the repo
  131. // type: string
  132. // required: true
  133. // - name: ref
  134. // in: path
  135. // description: name of branch/tag/commit
  136. // type: string
  137. // required: true
  138. // - name: sort
  139. // in: query
  140. // description: type of sort
  141. // type: string
  142. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  143. // required: false
  144. // - name: state
  145. // in: query
  146. // description: type of state
  147. // type: string
  148. // enum: [pending, success, error, failure, warning]
  149. // required: false
  150. // - name: page
  151. // in: query
  152. // description: page number of results to return (1-based)
  153. // type: integer
  154. // - name: limit
  155. // in: query
  156. // description: page size of results
  157. // type: integer
  158. // responses:
  159. // "200":
  160. // "$ref": "#/responses/CommitStatusList"
  161. // "400":
  162. // "$ref": "#/responses/error"
  163. filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
  164. if ctx.Written() {
  165. return
  166. }
  167. getCommitStatuses(ctx, filter) // By default filter is maybe the raw SHA
  168. }
  169. func getCommitStatuses(ctx *context.APIContext, sha string) {
  170. if len(sha) == 0 {
  171. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  172. return
  173. }
  174. sha = utils.MustConvertToSHA1(ctx.Base, ctx.Repo, sha)
  175. repo := ctx.Repo.Repository
  176. listOptions := utils.GetListOptions(ctx)
  177. statuses, maxResults, err := git_model.GetCommitStatuses(ctx, repo, sha, &git_model.CommitStatusOptions{
  178. ListOptions: listOptions,
  179. SortType: ctx.FormTrim("sort"),
  180. State: ctx.FormTrim("state"),
  181. })
  182. if err != nil {
  183. ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), sha, ctx.FormInt("page"), err))
  184. return
  185. }
  186. apiStatuses := make([]*api.CommitStatus, 0, len(statuses))
  187. for _, status := range statuses {
  188. apiStatuses = append(apiStatuses, convert.ToCommitStatus(ctx, status))
  189. }
  190. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  191. ctx.SetTotalCountHeader(maxResults)
  192. ctx.JSON(http.StatusOK, apiStatuses)
  193. }
  194. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  195. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  196. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/status repository repoGetCombinedStatusByRef
  197. // ---
  198. // summary: Get a commit's combined status, by branch/tag/commit reference
  199. // produces:
  200. // - application/json
  201. // parameters:
  202. // - name: owner
  203. // in: path
  204. // description: owner of the repo
  205. // type: string
  206. // required: true
  207. // - name: repo
  208. // in: path
  209. // description: name of the repo
  210. // type: string
  211. // required: true
  212. // - name: ref
  213. // in: path
  214. // description: name of branch/tag/commit
  215. // type: string
  216. // required: true
  217. // - name: page
  218. // in: query
  219. // description: page number of results to return (1-based)
  220. // type: integer
  221. // - name: limit
  222. // in: query
  223. // description: page size of results
  224. // type: integer
  225. // responses:
  226. // "200":
  227. // "$ref": "#/responses/CombinedStatus"
  228. // "400":
  229. // "$ref": "#/responses/error"
  230. sha := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
  231. if ctx.Written() {
  232. return
  233. }
  234. repo := ctx.Repo.Repository
  235. statuses, count, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, utils.GetListOptions(ctx))
  236. if err != nil {
  237. ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %w", repo.FullName(), sha, err))
  238. return
  239. }
  240. if len(statuses) == 0 {
  241. ctx.JSON(http.StatusOK, &api.CombinedStatus{})
  242. return
  243. }
  244. combiStatus := convert.ToCombinedStatus(ctx, statuses, convert.ToRepo(ctx, repo, ctx.Repo.Permission))
  245. ctx.SetTotalCountHeader(count)
  246. ctx.JSON(http.StatusOK, combiStatus)
  247. }