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.

127 lines
3.4 KiB

  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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // NewCommitStatus creates a new CommitStatus
  12. func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) {
  13. sha := ctx.Params("sha")
  14. if len(sha) == 0 {
  15. sha = ctx.Params("ref")
  16. }
  17. if len(sha) == 0 {
  18. ctx.Error(400, "ref/sha not given", nil)
  19. return
  20. }
  21. status := &models.CommitStatus{
  22. State: models.CommitStatusState(form.State),
  23. TargetURL: form.TargetURL,
  24. Description: form.Description,
  25. Context: form.Context,
  26. }
  27. if err := models.NewCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
  28. ctx.Error(500, "NewCommitStatus", err)
  29. return
  30. }
  31. newStatus, err := models.GetCommitStatus(ctx.Repo.Repository, sha, status)
  32. if err != nil {
  33. ctx.Error(500, "GetCommitStatus", err)
  34. return
  35. }
  36. ctx.JSON(201, newStatus.APIFormat())
  37. }
  38. // GetCommitStatuses returns all statuses for any given commit hash
  39. func GetCommitStatuses(ctx *context.APIContext) {
  40. sha := ctx.Params("sha")
  41. if len(sha) == 0 {
  42. sha = ctx.Params("ref")
  43. }
  44. if len(sha) == 0 {
  45. ctx.Error(400, "ref/sha not given", nil)
  46. return
  47. }
  48. repo := ctx.Repo.Repository
  49. page := ctx.ParamsInt("page")
  50. statuses, err := models.GetCommitStatuses(repo, sha, page)
  51. if err != nil {
  52. ctx.Error(500, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  53. }
  54. apiStatuses := make([]*api.Status, 0, len(statuses))
  55. for _, status := range statuses {
  56. apiStatuses = append(apiStatuses, status.APIFormat())
  57. }
  58. ctx.JSON(200, apiStatuses)
  59. }
  60. type combinedCommitStatus struct {
  61. State models.CommitStatusState `json:"state"`
  62. SHA string `json:"sha"`
  63. TotalCount int `json:"total_count"`
  64. Statuses []*api.Status `json:"statuses"`
  65. Repo *api.Repository `json:"repository"`
  66. CommitURL string `json:"commit_url"`
  67. URL string `json:"url"`
  68. }
  69. // GetCombinedCommitStatus returns the combined status for any given commit hash
  70. func GetCombinedCommitStatus(ctx *context.APIContext) {
  71. sha := ctx.Params("sha")
  72. if len(sha) == 0 {
  73. sha = ctx.Params("ref")
  74. }
  75. if len(sha) == 0 {
  76. ctx.Error(400, "ref/sha not given", nil)
  77. return
  78. }
  79. repo := ctx.Repo.Repository
  80. page := ctx.ParamsInt("page")
  81. statuses, err := models.GetLatestCommitStatus(repo, sha, page)
  82. if err != nil {
  83. ctx.Error(500, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err))
  84. return
  85. }
  86. if len(statuses) == 0 {
  87. ctx.Status(200)
  88. return
  89. }
  90. acl, err := models.AccessLevel(ctx.User.ID, repo)
  91. if err != nil {
  92. ctx.Error(500, "AccessLevel", fmt.Errorf("AccessLevel[%d, %s]: %v", ctx.User.ID, repo.FullName(), err))
  93. return
  94. }
  95. retStatus := &combinedCommitStatus{
  96. SHA: sha,
  97. TotalCount: len(statuses),
  98. Repo: repo.APIFormat(acl),
  99. URL: "",
  100. }
  101. retStatus.Statuses = make([]*api.Status, 0, len(statuses))
  102. for _, status := range statuses {
  103. retStatus.Statuses = append(retStatus.Statuses, status.APIFormat())
  104. if status.State.IsWorseThan(retStatus.State) {
  105. retStatus.State = status.State
  106. }
  107. }
  108. ctx.JSON(200, retStatus)
  109. }