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.

commit_status.go 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 models
  5. import (
  6. "container/list"
  7. "crypto/sha1"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. // CommitStatusState holds the state of a Status
  16. // It can be "pending", "success", "error", "failure", and "warning"
  17. type CommitStatusState string
  18. // IsWorseThan returns true if this State is worse than the given State
  19. func (css CommitStatusState) IsWorseThan(css2 CommitStatusState) bool {
  20. switch css {
  21. case CommitStatusError:
  22. return true
  23. case CommitStatusFailure:
  24. return css2 != CommitStatusError
  25. case CommitStatusWarning:
  26. return css2 != CommitStatusError && css2 != CommitStatusFailure
  27. case CommitStatusSuccess:
  28. return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning
  29. default:
  30. return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning && css2 != CommitStatusSuccess
  31. }
  32. }
  33. const (
  34. // CommitStatusPending is for when the Status is Pending
  35. CommitStatusPending CommitStatusState = "pending"
  36. // CommitStatusSuccess is for when the Status is Success
  37. CommitStatusSuccess CommitStatusState = "success"
  38. // CommitStatusError is for when the Status is Error
  39. CommitStatusError CommitStatusState = "error"
  40. // CommitStatusFailure is for when the Status is Failure
  41. CommitStatusFailure CommitStatusState = "failure"
  42. // CommitStatusWarning is for when the Status is Warning
  43. CommitStatusWarning CommitStatusState = "warning"
  44. )
  45. // CommitStatus holds a single Status of a single Commit
  46. type CommitStatus struct {
  47. ID int64 `xorm:"pk autoincr"`
  48. Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  49. RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  50. Repo *Repository `xorm:"-"`
  51. State CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
  52. SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
  53. TargetURL string `xorm:"TEXT"`
  54. Description string `xorm:"TEXT"`
  55. ContextHash string `xorm:"char(40) index"`
  56. Context string `xorm:"TEXT"`
  57. Creator *User `xorm:"-"`
  58. CreatorID int64
  59. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  60. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  61. }
  62. func (status *CommitStatus) loadRepo(e Engine) (err error) {
  63. if status.Repo == nil {
  64. status.Repo, err = getRepositoryByID(e, status.RepoID)
  65. if err != nil {
  66. return fmt.Errorf("getRepositoryByID [%d]: %v", status.RepoID, err)
  67. }
  68. }
  69. if status.Creator == nil && status.CreatorID > 0 {
  70. status.Creator, err = getUserByID(e, status.CreatorID)
  71. if err != nil {
  72. return fmt.Errorf("getUserByID [%d]: %v", status.CreatorID, err)
  73. }
  74. }
  75. return nil
  76. }
  77. // APIURL returns the absolute APIURL to this commit-status.
  78. func (status *CommitStatus) APIURL() string {
  79. _ = status.loadRepo(x)
  80. return fmt.Sprintf("%sapi/v1/repos/%s/statuses/%s",
  81. setting.AppURL, status.Repo.FullName(), status.SHA)
  82. }
  83. // APIFormat assumes some fields assigned with values:
  84. // Required - Repo, Creator
  85. func (status *CommitStatus) APIFormat() *api.Status {
  86. _ = status.loadRepo(x)
  87. apiStatus := &api.Status{
  88. Created: status.CreatedUnix.AsTime(),
  89. Updated: status.CreatedUnix.AsTime(),
  90. State: api.StatusState(status.State),
  91. TargetURL: status.TargetURL,
  92. Description: status.Description,
  93. ID: status.Index,
  94. URL: status.APIURL(),
  95. Context: status.Context,
  96. }
  97. if status.Creator != nil {
  98. apiStatus.Creator = status.Creator.APIFormat()
  99. }
  100. return apiStatus
  101. }
  102. // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
  103. func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
  104. var lastStatus *CommitStatus
  105. var state CommitStatusState
  106. for _, status := range statuses {
  107. if status.State.IsWorseThan(state) {
  108. state = status.State
  109. lastStatus = status
  110. }
  111. }
  112. if lastStatus == nil {
  113. if len(statuses) > 0 {
  114. lastStatus = statuses[0]
  115. } else {
  116. lastStatus = &CommitStatus{}
  117. }
  118. }
  119. return lastStatus
  120. }
  121. // GetCommitStatuses returns all statuses for a given commit.
  122. func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
  123. statuses := make([]*CommitStatus, 0, 10)
  124. return statuses, x.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
  125. }
  126. // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
  127. func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
  128. ids := make([]int64, 0, 10)
  129. err := x.Limit(10, page*10).
  130. Table(&CommitStatus{}).
  131. Where("repo_id = ?", repo.ID).And("sha = ?", sha).
  132. Select("max( id ) as id").
  133. GroupBy("context_hash").OrderBy("max( id ) desc").Find(&ids)
  134. if err != nil {
  135. return nil, err
  136. }
  137. statuses := make([]*CommitStatus, 0, len(ids))
  138. if len(ids) == 0 {
  139. return statuses, nil
  140. }
  141. return statuses, x.In("id", ids).Find(&statuses)
  142. }
  143. // NewCommitStatusOptions holds options for creating a CommitStatus
  144. type NewCommitStatusOptions struct {
  145. Repo *Repository
  146. Creator *User
  147. SHA string
  148. CommitStatus *CommitStatus
  149. }
  150. // NewCommitStatus save commit statuses into database
  151. func NewCommitStatus(opts NewCommitStatusOptions) error {
  152. if opts.Repo == nil {
  153. return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
  154. }
  155. repoPath := opts.Repo.RepoPath()
  156. if opts.Creator == nil {
  157. return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
  158. }
  159. sess := x.NewSession()
  160. defer sess.Close()
  161. if err := sess.Begin(); err != nil {
  162. return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
  163. }
  164. opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
  165. opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
  166. opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
  167. opts.CommitStatus.SHA = opts.SHA
  168. opts.CommitStatus.CreatorID = opts.Creator.ID
  169. opts.CommitStatus.RepoID = opts.Repo.ID
  170. // Get the next Status Index
  171. var nextIndex int64
  172. lastCommitStatus := &CommitStatus{
  173. SHA: opts.SHA,
  174. RepoID: opts.Repo.ID,
  175. }
  176. has, err := sess.Desc("index").Limit(1).Get(lastCommitStatus)
  177. if err != nil {
  178. if err := sess.Rollback(); err != nil {
  179. log.Error("NewCommitStatus: sess.Rollback: %v", err)
  180. }
  181. return fmt.Errorf("NewCommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  182. }
  183. if has {
  184. log.Debug("NewCommitStatus[%s, %s]: found", repoPath, opts.SHA)
  185. nextIndex = lastCommitStatus.Index
  186. }
  187. opts.CommitStatus.Index = nextIndex + 1
  188. log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index)
  189. opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
  190. // Insert new CommitStatus
  191. if _, err = sess.Insert(opts.CommitStatus); err != nil {
  192. if err := sess.Rollback(); err != nil {
  193. log.Error("Insert CommitStatus: sess.Rollback: %v", err)
  194. }
  195. return fmt.Errorf("Insert CommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  196. }
  197. return sess.Commit()
  198. }
  199. // SignCommitWithStatuses represents a commit with validation of signature and status state.
  200. type SignCommitWithStatuses struct {
  201. Status *CommitStatus
  202. *SignCommit
  203. }
  204. // ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
  205. func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
  206. var (
  207. newCommits = list.New()
  208. e = oldCommits.Front()
  209. )
  210. for e != nil {
  211. c := e.Value.(SignCommit)
  212. commit := SignCommitWithStatuses{
  213. SignCommit: &c,
  214. }
  215. statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
  216. if err != nil {
  217. log.Error("GetLatestCommitStatus: %v", err)
  218. } else {
  219. commit.Status = CalcCommitStatus(statuses)
  220. }
  221. newCommits.PushBack(commit)
  222. e = e.Next()
  223. }
  224. return newCommits
  225. }
  226. // hashCommitStatusContext hash context
  227. func hashCommitStatusContext(context string) string {
  228. return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
  229. }