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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/repofiles"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // NewCommitStatus creates a new CommitStatus
  17. func NewCommitStatus(ctx *context.APIContext) {
  18. // swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
  19. // ---
  20. // summary: Create a commit status
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: sha
  35. // in: path
  36. // description: sha of the commit
  37. // type: string
  38. // required: true
  39. // - name: body
  40. // in: body
  41. // schema:
  42. // "$ref": "#/definitions/CreateStatusOption"
  43. // responses:
  44. // "201":
  45. // "$ref": "#/responses/CommitStatus"
  46. // "400":
  47. // "$ref": "#/responses/error"
  48. form := web.GetForm(ctx).(*api.CreateStatusOption)
  49. sha := ctx.Params("sha")
  50. if len(sha) == 0 {
  51. ctx.Error(http.StatusBadRequest, "sha not given", nil)
  52. return
  53. }
  54. status := &models.CommitStatus{
  55. State: api.CommitStatusState(form.State),
  56. TargetURL: form.TargetURL,
  57. Description: form.Description,
  58. Context: form.Context,
  59. }
  60. if err := repofiles.CreateCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
  61. ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
  62. return
  63. }
  64. ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status))
  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. // - name: sort
  90. // in: query
  91. // description: type of sort
  92. // type: string
  93. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  94. // required: false
  95. // - name: state
  96. // in: query
  97. // description: type of state
  98. // type: string
  99. // enum: [pending, success, error, failure, warning]
  100. // required: false
  101. // - name: page
  102. // in: query
  103. // description: page number of results to return (1-based)
  104. // type: integer
  105. // - name: limit
  106. // in: query
  107. // description: page size of results
  108. // type: integer
  109. // responses:
  110. // "200":
  111. // "$ref": "#/responses/CommitStatusList"
  112. // "400":
  113. // "$ref": "#/responses/error"
  114. getCommitStatuses(ctx, ctx.Params("sha"))
  115. }
  116. // GetCommitStatusesByRef returns all statuses for any given commit ref
  117. func GetCommitStatusesByRef(ctx *context.APIContext) {
  118. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  119. // ---
  120. // summary: Get a commit's statuses, by branch/tag/commit reference
  121. // produces:
  122. // - application/json
  123. // parameters:
  124. // - name: owner
  125. // in: path
  126. // description: owner of the repo
  127. // type: string
  128. // required: true
  129. // - name: repo
  130. // in: path
  131. // description: name of the repo
  132. // type: string
  133. // required: true
  134. // - name: ref
  135. // in: path
  136. // description: name of branch/tag/commit
  137. // type: string
  138. // required: true
  139. // - name: sort
  140. // in: query
  141. // description: type of sort
  142. // type: string
  143. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  144. // required: false
  145. // - name: state
  146. // in: query
  147. // description: type of state
  148. // type: string
  149. // enum: [pending, success, error, failure, warning]
  150. // required: false
  151. // - name: page
  152. // in: query
  153. // description: page number of results to return (1-based)
  154. // type: integer
  155. // - name: limit
  156. // in: query
  157. // description: page size of results
  158. // type: integer
  159. // responses:
  160. // "200":
  161. // "$ref": "#/responses/CommitStatusList"
  162. // "400":
  163. // "$ref": "#/responses/error"
  164. filter := utils.ResolveRefOrSha(ctx, ctx.Params("ref"))
  165. if ctx.Written() {
  166. return
  167. }
  168. getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
  169. }
  170. func getCommitStatuses(ctx *context.APIContext, sha string) {
  171. if len(sha) == 0 {
  172. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  173. return
  174. }
  175. repo := ctx.Repo.Repository
  176. listOptions := utils.GetListOptions(ctx)
  177. statuses, maxResults, err := models.GetCommitStatuses(repo, sha, &models.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]: %v", 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(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, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx))
  236. if err != nil {
  237. ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %v", 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(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode))
  245. // TODO: ctx.SetTotalCountHeader(count)
  246. ctx.JSON(http.StatusOK, combiStatus)
  247. }