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.

309 lines
8.3 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  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. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. // NewCommitStatus creates a new CommitStatus
  15. func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) {
  16. // swagger:operation POST /repos/{owner}/{repo}/statuses/{sha} repository repoCreateStatus
  17. // ---
  18. // summary: Create a commit status
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: sha
  33. // in: path
  34. // description: sha of the commit
  35. // type: string
  36. // required: true
  37. // - name: body
  38. // in: body
  39. // schema:
  40. // "$ref": "#/definitions/CreateStatusOption"
  41. // responses:
  42. // "201":
  43. // "$ref": "#/responses/Status"
  44. // "400":
  45. // "$ref": "#/responses/error"
  46. sha := ctx.Params("sha")
  47. if len(sha) == 0 {
  48. ctx.Error(http.StatusBadRequest, "sha not given", nil)
  49. return
  50. }
  51. status := &models.CommitStatus{
  52. State: api.CommitStatusState(form.State),
  53. TargetURL: form.TargetURL,
  54. Description: form.Description,
  55. Context: form.Context,
  56. }
  57. if err := repofiles.CreateCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
  58. ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
  59. return
  60. }
  61. ctx.JSON(http.StatusCreated, status.APIFormat())
  62. }
  63. // GetCommitStatuses returns all statuses for any given commit hash
  64. func GetCommitStatuses(ctx *context.APIContext) {
  65. // swagger:operation GET /repos/{owner}/{repo}/statuses/{sha} repository repoListStatuses
  66. // ---
  67. // summary: Get a commit's statuses
  68. // produces:
  69. // - application/json
  70. // parameters:
  71. // - name: owner
  72. // in: path
  73. // description: owner of the repo
  74. // type: string
  75. // required: true
  76. // - name: repo
  77. // in: path
  78. // description: name of the repo
  79. // type: string
  80. // required: true
  81. // - name: sha
  82. // in: path
  83. // description: sha of the commit
  84. // type: string
  85. // required: true
  86. // - name: sort
  87. // in: query
  88. // description: type of sort
  89. // type: string
  90. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  91. // required: false
  92. // - name: state
  93. // in: query
  94. // description: type of state
  95. // type: string
  96. // enum: [pending, success, error, failure, warning]
  97. // required: false
  98. // - name: page
  99. // in: query
  100. // description: page number of results to return (1-based)
  101. // type: integer
  102. // - name: limit
  103. // in: query
  104. // description: page size of results, maximum page size is 50
  105. // type: integer
  106. // responses:
  107. // "200":
  108. // "$ref": "#/responses/StatusList"
  109. // "400":
  110. // "$ref": "#/responses/error"
  111. getCommitStatuses(ctx, ctx.Params("sha"))
  112. }
  113. // GetCommitStatusesByRef returns all statuses for any given commit ref
  114. func GetCommitStatusesByRef(ctx *context.APIContext) {
  115. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoListStatusesByRef
  116. // ---
  117. // summary: Get a commit's statuses, by branch/tag/commit reference
  118. // produces:
  119. // - application/json
  120. // parameters:
  121. // - name: owner
  122. // in: path
  123. // description: owner of the repo
  124. // type: string
  125. // required: true
  126. // - name: repo
  127. // in: path
  128. // description: name of the repo
  129. // type: string
  130. // required: true
  131. // - name: ref
  132. // in: path
  133. // description: name of branch/tag/commit
  134. // type: string
  135. // required: true
  136. // - name: sort
  137. // in: query
  138. // description: type of sort
  139. // type: string
  140. // enum: [oldest, recentupdate, leastupdate, leastindex, highestindex]
  141. // required: false
  142. // - name: state
  143. // in: query
  144. // description: type of state
  145. // type: string
  146. // enum: [pending, success, error, failure, warning]
  147. // required: false
  148. // - name: page
  149. // in: query
  150. // description: page number of results to return (1-based)
  151. // type: integer
  152. // - name: limit
  153. // in: query
  154. // description: page size of results, maximum page size is 50
  155. // type: integer
  156. // responses:
  157. // "200":
  158. // "$ref": "#/responses/StatusList"
  159. // "400":
  160. // "$ref": "#/responses/error"
  161. filter := ctx.Params("ref")
  162. if len(filter) == 0 {
  163. ctx.Error(http.StatusBadRequest, "ref not given", nil)
  164. return
  165. }
  166. for _, reftype := range []string{"heads", "tags"} { //Search branches and tags
  167. refSHA, lastMethodName, err := searchRefCommitByType(ctx, reftype, filter)
  168. if err != nil {
  169. ctx.Error(http.StatusInternalServerError, lastMethodName, err)
  170. return
  171. }
  172. if refSHA != "" {
  173. filter = refSHA
  174. break
  175. }
  176. }
  177. getCommitStatuses(ctx, filter) //By default filter is maybe the raw SHA
  178. }
  179. func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
  180. refs, lastMethodName, err := getGitRefs(ctx, refType+"/"+filter) //Search by type
  181. if err != nil {
  182. return "", lastMethodName, err
  183. }
  184. if len(refs) > 0 {
  185. return refs[0].Object.String(), "", nil //Return found SHA
  186. }
  187. return "", "", nil
  188. }
  189. func getCommitStatuses(ctx *context.APIContext, sha string) {
  190. if len(sha) == 0 {
  191. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  192. return
  193. }
  194. repo := ctx.Repo.Repository
  195. statuses, _, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{
  196. ListOptions: utils.GetListOptions(ctx),
  197. SortType: ctx.QueryTrim("sort"),
  198. State: ctx.QueryTrim("state"),
  199. })
  200. if err != nil {
  201. ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, ctx.QueryInt("page"), err))
  202. return
  203. }
  204. apiStatuses := make([]*api.Status, 0, len(statuses))
  205. for _, status := range statuses {
  206. apiStatuses = append(apiStatuses, status.APIFormat())
  207. }
  208. ctx.JSON(http.StatusOK, apiStatuses)
  209. }
  210. type combinedCommitStatus struct {
  211. State api.CommitStatusState `json:"state"`
  212. SHA string `json:"sha"`
  213. TotalCount int `json:"total_count"`
  214. Statuses []*api.Status `json:"statuses"`
  215. Repo *api.Repository `json:"repository"`
  216. CommitURL string `json:"commit_url"`
  217. URL string `json:"url"`
  218. }
  219. // GetCombinedCommitStatusByRef returns the combined status for any given commit hash
  220. func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
  221. // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef
  222. // ---
  223. // summary: Get a commit's combined status, by branch/tag/commit reference
  224. // produces:
  225. // - application/json
  226. // parameters:
  227. // - name: owner
  228. // in: path
  229. // description: owner of the repo
  230. // type: string
  231. // required: true
  232. // - name: repo
  233. // in: path
  234. // description: name of the repo
  235. // type: string
  236. // required: true
  237. // - name: ref
  238. // in: path
  239. // description: name of branch/tag/commit
  240. // type: string
  241. // required: true
  242. // - name: page
  243. // in: query
  244. // description: page number of results
  245. // type: integer
  246. // required: false
  247. // responses:
  248. // "200":
  249. // "$ref": "#/responses/Status"
  250. // "400":
  251. // "$ref": "#/responses/error"
  252. sha := ctx.Params("ref")
  253. if len(sha) == 0 {
  254. ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
  255. return
  256. }
  257. repo := ctx.Repo.Repository
  258. page := ctx.QueryInt("page")
  259. statuses, err := models.GetLatestCommitStatus(repo, sha, page)
  260. if err != nil {
  261. ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  262. return
  263. }
  264. if len(statuses) == 0 {
  265. ctx.Status(http.StatusOK)
  266. return
  267. }
  268. retStatus := &combinedCommitStatus{
  269. SHA: sha,
  270. TotalCount: len(statuses),
  271. Repo: repo.APIFormat(ctx.Repo.AccessMode),
  272. URL: "",
  273. }
  274. retStatus.Statuses = make([]*api.Status, 0, len(statuses))
  275. for _, status := range statuses {
  276. retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
  277. if status.State.NoBetterThan(retStatus.State) {
  278. retStatus.State = status.State
  279. }
  280. }
  281. ctx.JSON(http.StatusOK, retStatus)
  282. }