Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

status.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/repofiles"
  11. api "code.gitea.io/gitea/modules/structs"
  12. )
  13. // NewCommitStatus creates a new CommitStatus
  14. func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) {
  15. // swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
  16. // ---
  17. // summary: Create a commit status
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: owner
  22. // in: path
  23. // description: owner of the repo
  24. // type: string
  25. // required: true
  26. // - name: repo
  27. // in: path
  28. // description: name of the repo
  29. // type: string
  30. // required: true
  31. // - name: sha
  32. // in: path
  33. // description: sha of the commit
  34. // type: string
  35. // required: true
  36. // - name: body
  37. // in: body
  38. // schema:
  39. // "$ref": "#/definitions/CreateStatusOption"
  40. // responses:
  41. // "201":
  42. // "$ref": "#/responses/Status"
  43. // "400":
  44. // "$ref": "#/responses/error"
  45. sha := ctx.Params("sha")
  46. if len(sha) == 0 {
  47. ctx.Error(http.StatusBadRequest, "sha not given", nil)
  48. return
  49. }
  50. status := &models.CommitStatus{
  51. State: api.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(http.StatusInternalServerError, "CreateCommitStatus", err)
  58. return
  59. }
  60. ctx.JSON(http.StatusCreated, 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. // "400":
  106. // "$ref": "#/responses/error"
  107. getCommitStatuses(ctx, ctx.Params("sha"))
  108. }
  109. // GetCommitStatusesByRef returns all statuses for any given commit ref
  110. func GetCommitStatusesByRef(ctx *context.APIContext) {
  111. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  112. // ---
  113. // summary: Get a commit's statuses, by branch/tag/commit reference
  114. // produces:
  115. // - application/json
  116. // parameters:
  117. // - name: owner
  118. // in: path
  119. // description: owner of the repo
  120. // type: string
  121. // required: true
  122. // - name: repo
  123. // in: path
  124. // description: name of the repo
  125. // type: string
  126. // required: true
  127. // - name: ref
  128. // in: path
  129. // description: name of branch/tag/commit
  130. // type: string
  131. // required: true
  132. // - name: page
  133. // in: query
  134. // description: page number of results
  135. // type: integer
  136. // required: false
  137. // - name: sort
  138. // in: query
  139. // description: type of sort
  140. // type: string
  141. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  142. // required: false
  143. // - name: state
  144. // in: query
  145. // description: type of state
  146. // type: string
  147. // enum: [pending, success, error, failure, warning]
  148. // required: false
  149. // responses:
  150. // "200":
  151. // "$ref": "#/responses/StatusList"
  152. // "400":
  153. // "$ref": "#/responses/error"
  154. filter := ctx.Params("ref")
  155. if len(filter) == 0 {
  156. ctx.Error(http.StatusBadRequest, "ref not given", nil)
  157. return
  158. }
  159. for _, reftype := range []string{"heads", "tags"} { //Search branches and tags
  160. refSHA, lastMethodName, err := searchRefCommitByType(ctx, reftype, filter)
  161. if err != nil {
  162. ctx.Error(http.StatusInternalServerError, lastMethodName, err)
  163. return
  164. }
  165. if refSHA != "" {
  166. filter = refSHA
  167. break
  168. }
  169. }
  170. getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
  171. }
  172. func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
  173. refs, lastMethodName, err := getGitRefs(ctx, refType+"/"+filter) //Search by type
  174. if err != nil {
  175. return "", lastMethodName, err
  176. }
  177. if len(refs) > 0 {
  178. return refs[0].Object.String(), "", nil //Return found SHA
  179. }
  180. return "", "", nil
  181. }
  182. func getCommitStatuses(ctx *context.APIContext, sha string) {
  183. if len(sha) == 0 {
  184. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  185. return
  186. }
  187. repo := ctx.Repo.Repository
  188. statuses, _, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{
  189. Page: ctx.QueryInt("page"),
  190. SortType: ctx.QueryTrim("sort"),
  191. State: ctx.QueryTrim("state"),
  192. })
  193. if err != nil {
  194. ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, ctx.QueryInt("page"), err))
  195. return
  196. }
  197. apiStatuses := make([]*api.Status, 0, len(statuses))
  198. for _, status := range statuses {
  199. apiStatuses = append(apiStatuses, status.APIFormat())
  200. }
  201. ctx.JSON(http.StatusOK, apiStatuses)
  202. }
  203. type combinedCommitStatus struct {
  204. State api.CommitStatusState `json:"state"`
  205. SHA string `json:"sha"`
  206. TotalCount int `json:"total_count"`
  207. Statuses []*api.Status `json:"statuses"`
  208. Repo *api.Repository `json:"repository"`
  209. CommitURL string `json:"commit_url"`
  210. URL string `json:"url"`
  211. }
  212. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  213. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  214. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef
  215. // ---
  216. // summary: Get a commit's combined status, by branch/tag/commit reference
  217. // produces:
  218. // - application/json
  219. // parameters:
  220. // - name: owner
  221. // in: path
  222. // description: owner of the repo
  223. // type: string
  224. // required: true
  225. // - name: repo
  226. // in: path
  227. // description: name of the repo
  228. // type: string
  229. // required: true
  230. // - name: ref
  231. // in: path
  232. // description: name of branch/tag/commit
  233. // type: string
  234. // required: true
  235. // - name: page
  236. // in: query
  237. // description: page number of results
  238. // type: integer
  239. // required: false
  240. // responses:
  241. // "200":
  242. // "$ref": "#/responses/Status"
  243. // "400":
  244. // "$ref": "#/responses/error"
  245. sha := ctx.Params("ref")
  246. if len(sha) == 0 {
  247. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  248. return
  249. }
  250. repo := ctx.Repo.Repository
  251. page := ctx.QueryInt("page")
  252. statuses, err := models.GetLatestCommitStatus(repo, sha, page)
  253. if err != nil {
  254. ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  255. return
  256. }
  257. if len(statuses) == 0 {
  258. ctx.Status(http.StatusOK)
  259. return
  260. }
  261. retStatus := &combinedCommitStatus{
  262. SHA: sha,
  263. TotalCount: len(statuses),
  264. Repo: repo.APIFormat(ctx.Repo.AccessMode),
  265. URL: "",
  266. }
  267. retStatus.Statuses = make([]*api.Status, 0, len(statuses))
  268. for _, status := range statuses {
  269. retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
  270. if status.State.NoBetterThan(retStatus.State) {
  271. retStatus.State = status.State
  272. }
  273. }
  274. ctx.JSON(http.StatusOK, retStatus)
  275. }