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.

pull_list.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2019 The Gitea Authors. 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. "fmt"
  7. "code.gitea.io/gitea/modules/base"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. "github.com/unknwon/com"
  11. "xorm.io/xorm"
  12. )
  13. // PullRequestsOptions holds the options for PRs
  14. type PullRequestsOptions struct {
  15. Page int
  16. State string
  17. SortType string
  18. Labels []string
  19. MilestoneID int64
  20. }
  21. func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
  22. sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
  23. sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
  24. switch opts.State {
  25. case "closed", "open":
  26. sess.And("issue.is_closed=?", opts.State == "closed")
  27. }
  28. if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
  29. return nil, err
  30. } else if len(labelIDs) > 0 {
  31. sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
  32. In("issue_label.label_id", labelIDs)
  33. }
  34. if opts.MilestoneID > 0 {
  35. sess.And("issue.milestone_id=?", opts.MilestoneID)
  36. }
  37. return sess, nil
  38. }
  39. // GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
  40. // by given head information (repo and branch).
  41. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  42. prs := make([]*PullRequest, 0, 2)
  43. return prs, x.
  44. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
  45. repoID, branch, false, false).
  46. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  47. Find(&prs)
  48. }
  49. // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
  50. // by given base information (repo and branch).
  51. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  52. prs := make([]*PullRequest, 0, 2)
  53. return prs, x.
  54. Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  55. repoID, branch, false, false).
  56. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  57. Find(&prs)
  58. }
  59. // PullRequests returns all pull requests for a base Repo by the given conditions
  60. func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
  61. if opts.Page <= 0 {
  62. opts.Page = 1
  63. }
  64. countSession, err := listPullRequestStatement(baseRepoID, opts)
  65. if err != nil {
  66. log.Error("listPullRequestStatement: %v", err)
  67. return nil, 0, err
  68. }
  69. maxResults, err := countSession.Count(new(PullRequest))
  70. if err != nil {
  71. log.Error("Count PRs: %v", err)
  72. return nil, maxResults, err
  73. }
  74. prs := make([]*PullRequest, 0, ItemsPerPage)
  75. findSession, err := listPullRequestStatement(baseRepoID, opts)
  76. sortIssuesSession(findSession, opts.SortType, 0)
  77. if err != nil {
  78. log.Error("listPullRequestStatement: %v", err)
  79. return nil, maxResults, err
  80. }
  81. findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
  82. return prs, maxResults, findSession.Find(&prs)
  83. }
  84. // PullRequestList defines a list of pull requests
  85. type PullRequestList []*PullRequest
  86. func (prs PullRequestList) loadAttributes(e Engine) error {
  87. if len(prs) == 0 {
  88. return nil
  89. }
  90. // Load issues.
  91. issueIDs := prs.getIssueIDs()
  92. issues := make([]*Issue, 0, len(issueIDs))
  93. if err := e.
  94. Where("id > 0").
  95. In("id", issueIDs).
  96. Find(&issues); err != nil {
  97. return fmt.Errorf("find issues: %v", err)
  98. }
  99. set := make(map[int64]*Issue)
  100. for i := range issues {
  101. set[issues[i].ID] = issues[i]
  102. }
  103. for i := range prs {
  104. prs[i].Issue = set[prs[i].IssueID]
  105. }
  106. return nil
  107. }
  108. func (prs PullRequestList) getIssueIDs() []int64 {
  109. issueIDs := make([]int64, 0, len(prs))
  110. for i := range prs {
  111. issueIDs = append(issueIDs, prs[i].IssueID)
  112. }
  113. return issueIDs
  114. }
  115. // LoadAttributes load all the prs attributes
  116. func (prs PullRequestList) LoadAttributes() error {
  117. return prs.loadAttributes(x)
  118. }
  119. func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
  120. if len(prs) == 0 {
  121. return nil
  122. }
  123. issueIDs := prs.getIssueIDs()
  124. var codeComments []*Comment
  125. if err := e.
  126. Where("type = ? and invalidated = ?", CommentTypeCode, false).
  127. In("issue_id", issueIDs).
  128. Find(&codeComments); err != nil {
  129. return fmt.Errorf("find code comments: %v", err)
  130. }
  131. for _, comment := range codeComments {
  132. if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
  133. return err
  134. }
  135. }
  136. return nil
  137. }
  138. // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
  139. func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
  140. return prs.invalidateCodeComments(x, doer, repo, branch)
  141. }
  142. // TestPullRequests checks and tests untested patches of pull requests.
  143. // TODO: test more pull requests at same time.
  144. func TestPullRequests() {
  145. prs := make([]*PullRequest, 0, 10)
  146. err := x.Where("status = ?", PullRequestStatusChecking).Find(&prs)
  147. if err != nil {
  148. log.Error("Find Checking PRs: %v", err)
  149. return
  150. }
  151. var checkedPRs = make(map[int64]struct{})
  152. // Update pull request status.
  153. for _, pr := range prs {
  154. checkedPRs[pr.ID] = struct{}{}
  155. if err := pr.GetBaseRepo(); err != nil {
  156. log.Error("GetBaseRepo: %v", err)
  157. continue
  158. }
  159. if pr.manuallyMerged() {
  160. continue
  161. }
  162. if err := pr.testPatch(x); err != nil {
  163. log.Error("testPatch: %v", err)
  164. continue
  165. }
  166. pr.checkAndUpdateStatus()
  167. }
  168. // Start listening on new test requests.
  169. for prID := range pullRequestQueue.Queue() {
  170. log.Trace("TestPullRequests[%v]: processing test task", prID)
  171. pullRequestQueue.Remove(prID)
  172. id := com.StrTo(prID).MustInt64()
  173. if _, ok := checkedPRs[id]; ok {
  174. continue
  175. }
  176. pr, err := GetPullRequestByID(id)
  177. if err != nil {
  178. log.Error("GetPullRequestByID[%s]: %v", prID, err)
  179. continue
  180. } else if pr.manuallyMerged() {
  181. continue
  182. } else if err = pr.testPatch(x); err != nil {
  183. log.Error("testPatch[%d]: %v", pr.ID, err)
  184. continue
  185. }
  186. pr.checkAndUpdateStatus()
  187. }
  188. }
  189. // InitTestPullRequests runs the task to test all the checking status pull requests
  190. func InitTestPullRequests() {
  191. go TestPullRequests()
  192. }