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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "xorm.io/xorm"
  16. )
  17. // CommitStatus holds a single Status of a single Commit
  18. type CommitStatus struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  21. RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  22. Repo *Repository `xorm:"-"`
  23. State api.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
  24. SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
  25. TargetURL string `xorm:"TEXT"`
  26. Description string `xorm:"TEXT"`
  27. ContextHash string `xorm:"char(40) index"`
  28. Context string `xorm:"TEXT"`
  29. Creator *User `xorm:"-"`
  30. CreatorID int64
  31. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. }
  34. func (status *CommitStatus) loadAttributes(e Engine) (err error) {
  35. if status.Repo == nil {
  36. status.Repo, err = getRepositoryByID(e, status.RepoID)
  37. if err != nil {
  38. return fmt.Errorf("getRepositoryByID [%d]: %v", status.RepoID, err)
  39. }
  40. }
  41. if status.Creator == nil && status.CreatorID > 0 {
  42. status.Creator, err = getUserByID(e, status.CreatorID)
  43. if err != nil {
  44. return fmt.Errorf("getUserByID [%d]: %v", status.CreatorID, err)
  45. }
  46. }
  47. return nil
  48. }
  49. // APIURL returns the absolute APIURL to this commit-status.
  50. func (status *CommitStatus) APIURL() string {
  51. _ = status.loadAttributes(x)
  52. return fmt.Sprintf("%sapi/v1/repos/%s/statuses/%s",
  53. setting.AppURL, status.Repo.FullName(), status.SHA)
  54. }
  55. // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
  56. func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
  57. var lastStatus *CommitStatus
  58. var state api.CommitStatusState
  59. for _, status := range statuses {
  60. if status.State.NoBetterThan(state) {
  61. state = status.State
  62. lastStatus = status
  63. }
  64. }
  65. if lastStatus == nil {
  66. if len(statuses) > 0 {
  67. lastStatus = statuses[0]
  68. } else {
  69. lastStatus = &CommitStatus{}
  70. }
  71. }
  72. return lastStatus
  73. }
  74. // CommitStatusOptions holds the options for query commit statuses
  75. type CommitStatusOptions struct {
  76. ListOptions
  77. State string
  78. SortType string
  79. }
  80. // GetCommitStatuses returns all statuses for a given commit.
  81. func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
  82. if opts.Page <= 0 {
  83. opts.Page = 1
  84. }
  85. if opts.PageSize <= 0 {
  86. opts.Page = ItemsPerPage
  87. }
  88. countSession := listCommitStatusesStatement(repo, sha, opts)
  89. countSession = opts.setSessionPagination(countSession)
  90. maxResults, err := countSession.Count(new(CommitStatus))
  91. if err != nil {
  92. log.Error("Count PRs: %v", err)
  93. return nil, maxResults, err
  94. }
  95. statuses := make([]*CommitStatus, 0, opts.PageSize)
  96. findSession := listCommitStatusesStatement(repo, sha, opts)
  97. findSession = opts.setSessionPagination(findSession)
  98. sortCommitStatusesSession(findSession, opts.SortType)
  99. return statuses, maxResults, findSession.Find(&statuses)
  100. }
  101. func listCommitStatusesStatement(repo *Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
  102. sess := x.Where("repo_id = ?", repo.ID).And("sha = ?", sha)
  103. switch opts.State {
  104. case "pending", "success", "error", "failure", "warning":
  105. sess.And("state = ?", opts.State)
  106. }
  107. return sess
  108. }
  109. func sortCommitStatusesSession(sess *xorm.Session, sortType string) {
  110. switch sortType {
  111. case "oldest":
  112. sess.Asc("created_unix")
  113. case "recentupdate":
  114. sess.Desc("updated_unix")
  115. case "leastupdate":
  116. sess.Asc("updated_unix")
  117. case "leastindex":
  118. sess.Desc("index")
  119. case "highestindex":
  120. sess.Asc("index")
  121. default:
  122. sess.Desc("created_unix")
  123. }
  124. }
  125. // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
  126. func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) {
  127. return getLatestCommitStatus(x, repoID, sha, listOptions)
  128. }
  129. func getLatestCommitStatus(e Engine, repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) {
  130. ids := make([]int64, 0, 10)
  131. sess := e.Table(&CommitStatus{}).
  132. Where("repo_id = ?", repoID).And("sha = ?", sha).
  133. Select("max( id ) as id").
  134. GroupBy("context_hash").OrderBy("max( id ) desc")
  135. sess = listOptions.setSessionPagination(sess)
  136. err := sess.Find(&ids)
  137. if err != nil {
  138. return nil, err
  139. }
  140. statuses := make([]*CommitStatus, 0, len(ids))
  141. if len(ids) == 0 {
  142. return statuses, nil
  143. }
  144. return statuses, x.In("id", ids).Find(&statuses)
  145. }
  146. // FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
  147. func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]string, error) {
  148. start := timeutil.TimeStampNow().AddDuration(-before)
  149. ids := make([]int64, 0, 10)
  150. if err := x.Table("commit_status").
  151. Where("repo_id = ?", repoID).
  152. And("updated_unix >= ?", start).
  153. Select("max( id ) as id").
  154. GroupBy("context_hash").OrderBy("max( id ) desc").
  155. Find(&ids); err != nil {
  156. return nil, err
  157. }
  158. contexts := make([]string, 0, len(ids))
  159. if len(ids) == 0 {
  160. return contexts, nil
  161. }
  162. return contexts, x.Select("context").Table("commit_status").In("id", ids).Find(&contexts)
  163. }
  164. // NewCommitStatusOptions holds options for creating a CommitStatus
  165. type NewCommitStatusOptions struct {
  166. Repo *Repository
  167. Creator *User
  168. SHA string
  169. CommitStatus *CommitStatus
  170. }
  171. // NewCommitStatus save commit statuses into database
  172. func NewCommitStatus(opts NewCommitStatusOptions) error {
  173. if opts.Repo == nil {
  174. return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
  175. }
  176. repoPath := opts.Repo.RepoPath()
  177. if opts.Creator == nil {
  178. return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
  179. }
  180. sess := x.NewSession()
  181. defer sess.Close()
  182. if err := sess.Begin(); err != nil {
  183. return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
  184. }
  185. opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
  186. opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
  187. opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
  188. opts.CommitStatus.SHA = opts.SHA
  189. opts.CommitStatus.CreatorID = opts.Creator.ID
  190. opts.CommitStatus.RepoID = opts.Repo.ID
  191. // Get the next Status Index
  192. var nextIndex int64
  193. lastCommitStatus := &CommitStatus{
  194. SHA: opts.SHA,
  195. RepoID: opts.Repo.ID,
  196. }
  197. has, err := sess.Desc("index").Limit(1).Get(lastCommitStatus)
  198. if err != nil {
  199. if err := sess.Rollback(); err != nil {
  200. log.Error("NewCommitStatus: sess.Rollback: %v", err)
  201. }
  202. return fmt.Errorf("NewCommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  203. }
  204. if has {
  205. log.Debug("NewCommitStatus[%s, %s]: found", repoPath, opts.SHA)
  206. nextIndex = lastCommitStatus.Index
  207. }
  208. opts.CommitStatus.Index = nextIndex + 1
  209. log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index)
  210. opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
  211. // Insert new CommitStatus
  212. if _, err = sess.Insert(opts.CommitStatus); err != nil {
  213. if err := sess.Rollback(); err != nil {
  214. log.Error("Insert CommitStatus: sess.Rollback: %v", err)
  215. }
  216. return fmt.Errorf("Insert CommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  217. }
  218. return sess.Commit()
  219. }
  220. // SignCommitWithStatuses represents a commit with validation of signature and status state.
  221. type SignCommitWithStatuses struct {
  222. Status *CommitStatus
  223. Statuses []*CommitStatus
  224. *SignCommit
  225. }
  226. // ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
  227. func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
  228. var (
  229. newCommits = list.New()
  230. e = oldCommits.Front()
  231. )
  232. for e != nil {
  233. c := e.Value.(SignCommit)
  234. commit := SignCommitWithStatuses{
  235. SignCommit: &c,
  236. }
  237. statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
  238. if err != nil {
  239. log.Error("GetLatestCommitStatus: %v", err)
  240. } else {
  241. commit.Statuses = statuses
  242. commit.Status = CalcCommitStatus(statuses)
  243. }
  244. newCommits.PushBack(commit)
  245. e = e.Next()
  246. }
  247. return newCommits
  248. }
  249. // hashCommitStatusContext hash context
  250. func hashCommitStatusContext(context string) string {
  251. return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
  252. }