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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "context"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. access_model "code.gitea.io/gitea/models/perm/access"
  10. "code.gitea.io/gitea/models/unit"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "xorm.io/xorm"
  16. )
  17. // PullRequestsOptions holds the options for PRs
  18. type PullRequestsOptions struct {
  19. db.ListOptions
  20. State string
  21. SortType string
  22. Labels []string
  23. MilestoneID int64
  24. }
  25. func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
  26. sess := db.GetEngine(db.DefaultContext).Where("pull_request.base_repo_id=?", baseRepoID)
  27. sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
  28. switch opts.State {
  29. case "closed", "open":
  30. sess.And("issue.is_closed=?", opts.State == "closed")
  31. }
  32. if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
  33. return nil, err
  34. } else if len(labelIDs) > 0 {
  35. sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
  36. In("issue_label.label_id", labelIDs)
  37. }
  38. if opts.MilestoneID > 0 {
  39. sess.And("issue.milestone_id=?", opts.MilestoneID)
  40. }
  41. return sess, nil
  42. }
  43. // GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
  44. // by given head information (repo and branch).
  45. func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
  46. prs := make([]*PullRequest, 0, 2)
  47. return prs, db.GetEngine(db.DefaultContext).
  48. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
  49. repoID, branch, false, false, PullRequestFlowGithub).
  50. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  51. Find(&prs)
  52. }
  53. // CanMaintainerWriteToBranch check whether user is a matainer and could write to the branch
  54. func CanMaintainerWriteToBranch(p access_model.Permission, branch string, user *user_model.User) bool {
  55. if p.CanWrite(unit.TypeCode) {
  56. return true
  57. }
  58. if len(p.Units) < 1 {
  59. return false
  60. }
  61. prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch)
  62. if err != nil {
  63. return false
  64. }
  65. for _, pr := range prs {
  66. if pr.AllowMaintainerEdit {
  67. err = pr.LoadBaseRepo()
  68. if err != nil {
  69. continue
  70. }
  71. prPerm, err := access_model.GetUserRepoPermission(db.DefaultContext, pr.BaseRepo, user)
  72. if err != nil {
  73. continue
  74. }
  75. if prPerm.CanWrite(unit.TypeCode) {
  76. return true
  77. }
  78. }
  79. }
  80. return false
  81. }
  82. // HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
  83. // by given head information (repo and branch)
  84. func HasUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) (bool, error) {
  85. return db.GetEngine(ctx).
  86. Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
  87. repoID, branch, false, false, PullRequestFlowGithub).
  88. Join("INNER", "issue", "issue.id = pull_request.issue_id").
  89. Exist(&PullRequest{})
  90. }
  91. // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
  92. // by given base information (repo and branch).
  93. func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
  94. prs := make([]*PullRequest, 0, 2)
  95. return prs, db.GetEngine(db.DefaultContext).
  96. Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  97. repoID, branch, false, false).
  98. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  99. Find(&prs)
  100. }
  101. // GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
  102. func GetPullRequestIDsByCheckStatus(status PullRequestStatus) ([]int64, error) {
  103. prs := make([]int64, 0, 10)
  104. return prs, db.GetEngine(db.DefaultContext).Table("pull_request").
  105. Where("status=?", status).
  106. Cols("pull_request.id").
  107. Find(&prs)
  108. }
  109. // PullRequests returns all pull requests for a base Repo by the given conditions
  110. func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
  111. if opts.Page <= 0 {
  112. opts.Page = 1
  113. }
  114. countSession, err := listPullRequestStatement(baseRepoID, opts)
  115. if err != nil {
  116. log.Error("listPullRequestStatement: %v", err)
  117. return nil, 0, err
  118. }
  119. maxResults, err := countSession.Count(new(PullRequest))
  120. if err != nil {
  121. log.Error("Count PRs: %v", err)
  122. return nil, maxResults, err
  123. }
  124. findSession, err := listPullRequestStatement(baseRepoID, opts)
  125. sortIssuesSession(findSession, opts.SortType, 0)
  126. if err != nil {
  127. log.Error("listPullRequestStatement: %v", err)
  128. return nil, maxResults, err
  129. }
  130. findSession = db.SetSessionPagination(findSession, opts)
  131. prs := make([]*PullRequest, 0, opts.PageSize)
  132. return prs, maxResults, findSession.Find(&prs)
  133. }
  134. // PullRequestList defines a list of pull requests
  135. type PullRequestList []*PullRequest
  136. func (prs PullRequestList) loadAttributes(ctx context.Context) error {
  137. if len(prs) == 0 {
  138. return nil
  139. }
  140. // Load issues.
  141. issueIDs := prs.getIssueIDs()
  142. issues := make([]*Issue, 0, len(issueIDs))
  143. if err := db.GetEngine(ctx).
  144. Where("id > 0").
  145. In("id", issueIDs).
  146. Find(&issues); err != nil {
  147. return fmt.Errorf("find issues: %v", err)
  148. }
  149. set := make(map[int64]*Issue)
  150. for i := range issues {
  151. set[issues[i].ID] = issues[i]
  152. }
  153. for i := range prs {
  154. prs[i].Issue = set[prs[i].IssueID]
  155. }
  156. return nil
  157. }
  158. func (prs PullRequestList) getIssueIDs() []int64 {
  159. issueIDs := make([]int64, 0, len(prs))
  160. for i := range prs {
  161. issueIDs = append(issueIDs, prs[i].IssueID)
  162. }
  163. return issueIDs
  164. }
  165. // LoadAttributes load all the prs attributes
  166. func (prs PullRequestList) LoadAttributes() error {
  167. return prs.loadAttributes(db.DefaultContext)
  168. }
  169. // InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
  170. func (prs PullRequestList) InvalidateCodeComments(ctx context.Context, doer *user_model.User, repo *git.Repository, branch string) error {
  171. if len(prs) == 0 {
  172. return nil
  173. }
  174. issueIDs := prs.getIssueIDs()
  175. var codeComments []*Comment
  176. if err := db.GetEngine(ctx).
  177. Where("type = ? and invalidated = ?", CommentTypeCode, false).
  178. In("issue_id", issueIDs).
  179. Find(&codeComments); err != nil {
  180. return fmt.Errorf("find code comments: %v", err)
  181. }
  182. for _, comment := range codeComments {
  183. if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
  184. return err
  185. }
  186. }
  187. return nil
  188. }