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.

pull_list.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "xorm.io/xorm"
  11. )
  12. // PullRequestsOptions holds the options for PRs
  13. type PullRequestsOptions struct {
  14. Page int
  15. State string
  16. SortType string
  17. Labels []string
  18. MilestoneID int64
  19. }
  20. func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
  21. sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
  22. sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
  23. switch opts.State {
  24. case "closed", "open":
  25. sess.And("issue.is_closed=?", opts.State == "closed")
  26. }
  27. if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
  28. return nil, err
  29. } else if len(labelIDs) > 0 {
  30. sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
  31. In("issue_label.label_id", labelIDs)
  32. }
  33. if opts.MilestoneID > 0 {
  34. sess.And("issue.milestone_id=?", opts.MilestoneID)
  35. }
  36. return sess, nil
  37. }
  38. // GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
  39. // by given head information (repo and branch).
  40. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  41. prs := make([]*PullRequest, 0, 2)
  42. return prs, x.
  43. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
  44. repoID, branch, false, false).
  45. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  46. Find(&prs)
  47. }
  48. // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
  49. // by given base information (repo and branch).
  50. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  51. prs := make([]*PullRequest, 0, 2)
  52. return prs, x.
  53. Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  54. repoID, branch, false, false).
  55. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  56. Find(&prs)
  57. }
  58. // GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
  59. func GetPullRequestIDsByCheckStatus(status PullRequestStatus) ([]int64, error) {
  60. prs := make([]int64, 0, 10)
  61. return prs, x.Table("pull_request").
  62. Where("status=?", status).
  63. Cols("pull_request.id").
  64. Find(&prs)
  65. }
  66. // PullRequests returns all pull requests for a base Repo by the given conditions
  67. func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
  68. if opts.Page <= 0 {
  69. opts.Page = 1
  70. }
  71. countSession, err := listPullRequestStatement(baseRepoID, opts)
  72. if err != nil {
  73. log.Error("listPullRequestStatement: %v", err)
  74. return nil, 0, err
  75. }
  76. maxResults, err := countSession.Count(new(PullRequest))
  77. if err != nil {
  78. log.Error("Count PRs: %v", err)
  79. return nil, maxResults, err
  80. }
  81. prs := make([]*PullRequest, 0, ItemsPerPage)
  82. findSession, err := listPullRequestStatement(baseRepoID, opts)
  83. sortIssuesSession(findSession, opts.SortType, 0)
  84. if err != nil {
  85. log.Error("listPullRequestStatement: %v", err)
  86. return nil, maxResults, err
  87. }
  88. findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
  89. return prs, maxResults, findSession.Find(&prs)
  90. }
  91. // PullRequestList defines a list of pull requests
  92. type PullRequestList []*PullRequest
  93. func (prs PullRequestList) loadAttributes(e Engine) error {
  94. if len(prs) == 0 {
  95. return nil
  96. }
  97. // Load issues.
  98. issueIDs := prs.getIssueIDs()
  99. issues := make([]*Issue, 0, len(issueIDs))
  100. if err := e.
  101. Where("id > 0").
  102. In("id", issueIDs).
  103. Find(&issues); err != nil {
  104. return fmt.Errorf("find issues: %v", err)
  105. }
  106. set := make(map[int64]*Issue)
  107. for i := range issues {
  108. set[issues[i].ID] = issues[i]
  109. }
  110. for i := range prs {
  111. prs[i].Issue = set[prs[i].IssueID]
  112. }
  113. return nil
  114. }
  115. func (prs PullRequestList) getIssueIDs() []int64 {
  116. issueIDs := make([]int64, 0, len(prs))
  117. for i := range prs {
  118. issueIDs = append(issueIDs, prs[i].IssueID)
  119. }
  120. return issueIDs
  121. }
  122. // LoadAttributes load all the prs attributes
  123. func (prs PullRequestList) LoadAttributes() error {
  124. return prs.loadAttributes(x)
  125. }
  126. func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
  127. if len(prs) == 0 {
  128. return nil
  129. }
  130. issueIDs := prs.getIssueIDs()
  131. var codeComments []*Comment
  132. if err := e.
  133. Where("type = ? and invalidated = ?", CommentTypeCode, false).
  134. In("issue_id", issueIDs).
  135. Find(&codeComments); err != nil {
  136. return fmt.Errorf("find code comments: %v", err)
  137. }
  138. for _, comment := range codeComments {
  139. if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
  146. func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
  147. return prs.invalidateCodeComments(x, doer, repo, branch)
  148. }