gitea源码

status.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2017 Gitea. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "net/http"
  7. "code.gitea.io/gitea/models/db"
  8. git_model "code.gitea.io/gitea/models/git"
  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/context"
  13. "code.gitea.io/gitea/services/convert"
  14. commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
  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. // "404":
  49. // "$ref": "#/responses/notFound"
  50. form := web.GetForm(ctx).(*api.CreateStatusOption)
  51. sha := ctx.PathParam("sha")
  52. if len(sha) == 0 {
  53. ctx.APIError(http.StatusBadRequest, nil)
  54. return
  55. }
  56. status := &git_model.CommitStatus{
  57. State: form.State,
  58. TargetURL: form.TargetURL,
  59. Description: form.Description,
  60. Context: form.Context,
  61. }
  62. if err := commitstatus_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.Doer, sha, status); err != nil {
  63. ctx.APIErrorInternal(err)
  64. return
  65. }
  66. ctx.JSON(http.StatusCreated, convert.ToCommitStatus(ctx, status))
  67. }
  68. // GetCommitStatuses returns all statuses for any given commit hash
  69. func GetCommitStatuses(ctx *context.APIContext) {
  70. // swagger:operation GET /repos/{owner}/{repo}/statuses/{sha} repository repoListStatuses
  71. // ---
  72. // summary: Get a commit's statuses
  73. // produces:
  74. // - application/json
  75. // parameters:
  76. // - name: owner
  77. // in: path
  78. // description: owner of the repo
  79. // type: string
  80. // required: true
  81. // - name: repo
  82. // in: path
  83. // description: name of the repo
  84. // type: string
  85. // required: true
  86. // - name: sha
  87. // in: path
  88. // description: sha of the commit
  89. // type: string
  90. // required: true
  91. // - name: sort
  92. // in: query
  93. // description: type of sort
  94. // type: string
  95. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  96. // required: false
  97. // - name: state
  98. // in: query
  99. // description: type of state
  100. // type: string
  101. // enum: [pending, success, error, failure, warning]
  102. // required: false
  103. // - name: page
  104. // in: query
  105. // description: page number of results to return (1-based)
  106. // type: integer
  107. // - name: limit
  108. // in: query
  109. // description: page size of results
  110. // type: integer
  111. // responses:
  112. // "200":
  113. // "$ref": "#/responses/CommitStatusList"
  114. // "400":
  115. // "$ref": "#/responses/error"
  116. // "404":
  117. // "$ref": "#/responses/notFound"
  118. getCommitStatuses(ctx, ctx.PathParam("sha"))
  119. }
  120. // GetCommitStatusesByRef returns all statuses for any given commit ref
  121. func GetCommitStatusesByRef(ctx *context.APIContext) {
  122. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  123. // ---
  124. // summary: Get a commit's statuses, by branch/tag/commit reference
  125. // produces:
  126. // - application/json
  127. // parameters:
  128. // - name: owner
  129. // in: path
  130. // description: owner of the repo
  131. // type: string
  132. // required: true
  133. // - name: repo
  134. // in: path
  135. // description: name of the repo
  136. // type: string
  137. // required: true
  138. // - name: ref
  139. // in: path
  140. // description: name of branch/tag/commit
  141. // type: string
  142. // required: true
  143. // - name: sort
  144. // in: query
  145. // description: type of sort
  146. // type: string
  147. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  148. // required: false
  149. // - name: state
  150. // in: query
  151. // description: type of state
  152. // type: string
  153. // enum: [pending, success, error, failure, warning]
  154. // required: false
  155. // - name: page
  156. // in: query
  157. // description: page number of results to return (1-based)
  158. // type: integer
  159. // - name: limit
  160. // in: query
  161. // description: page size of results
  162. // type: integer
  163. // responses:
  164. // "200":
  165. // "$ref": "#/responses/CommitStatusList"
  166. // "400":
  167. // "$ref": "#/responses/error"
  168. // "404":
  169. // "$ref": "#/responses/notFound"
  170. refCommit := resolveRefCommit(ctx, ctx.PathParam("ref"), 7)
  171. if ctx.Written() {
  172. return
  173. }
  174. getCommitStatuses(ctx, refCommit.CommitID)
  175. }
  176. func getCommitStatuses(ctx *context.APIContext, commitID string) {
  177. repo := ctx.Repo.Repository
  178. listOptions := utils.GetListOptions(ctx)
  179. statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](ctx, &git_model.CommitStatusOptions{
  180. ListOptions: listOptions,
  181. RepoID: repo.ID,
  182. SHA: commitID,
  183. SortType: ctx.FormTrim("sort"),
  184. State: ctx.FormTrim("state"),
  185. })
  186. if err != nil {
  187. ctx.APIErrorInternal(fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), commitID, ctx.FormInt("page"), err))
  188. return
  189. }
  190. apiStatuses := make([]*api.CommitStatus, 0, len(statuses))
  191. for _, status := range statuses {
  192. apiStatuses = append(apiStatuses, convert.ToCommitStatus(ctx, status))
  193. }
  194. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  195. ctx.SetTotalCountHeader(maxResults)
  196. ctx.JSON(http.StatusOK, apiStatuses)
  197. }
  198. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  199. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  200. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/status repository repoGetCombinedStatusByRef
  201. // ---
  202. // summary: Get a commit's combined status, by branch/tag/commit reference
  203. // produces:
  204. // - application/json
  205. // parameters:
  206. // - name: owner
  207. // in: path
  208. // description: owner of the repo
  209. // type: string
  210. // required: true
  211. // - name: repo
  212. // in: path
  213. // description: name of the repo
  214. // type: string
  215. // required: true
  216. // - name: ref
  217. // in: path
  218. // description: name of branch/tag/commit
  219. // type: string
  220. // required: true
  221. // - name: page
  222. // in: query
  223. // description: page number of results to return (1-based)
  224. // type: integer
  225. // - name: limit
  226. // in: query
  227. // description: page size of results
  228. // type: integer
  229. // responses:
  230. // "200":
  231. // "$ref": "#/responses/CombinedStatus"
  232. // "400":
  233. // "$ref": "#/responses/error"
  234. // "404":
  235. // "$ref": "#/responses/notFound"
  236. refCommit := resolveRefCommit(ctx, ctx.PathParam("ref"), 7)
  237. if ctx.Written() {
  238. return
  239. }
  240. repo := ctx.Repo.Repository
  241. statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, refCommit.Commit.ID.String(), utils.GetListOptions(ctx))
  242. if err != nil {
  243. ctx.APIErrorInternal(fmt.Errorf("GetLatestCommitStatus[%s, %s]: %w", repo.FullName(), refCommit.CommitID, err))
  244. return
  245. }
  246. count, err := git_model.CountLatestCommitStatus(ctx, repo.ID, refCommit.Commit.ID.String())
  247. if err != nil {
  248. ctx.APIErrorInternal(fmt.Errorf("CountLatestCommitStatus[%s, %s]: %w", repo.FullName(), refCommit.CommitID, err))
  249. return
  250. }
  251. ctx.SetTotalCountHeader(count)
  252. combiStatus := convert.ToCombinedStatus(ctx, refCommit.Commit.ID.String(), statuses,
  253. convert.ToRepo(ctx, repo, ctx.Repo.Permission))
  254. ctx.JSON(http.StatusOK, combiStatus)
  255. }