Skip to content

Commit de2f45f

Browse files
lunnyzeripath
authored andcommitted
Move pull list code to a seperate file (#8748)
1 parent 1286473 commit de2f45f

File tree

2 files changed

+224
-211
lines changed

2 files changed

+224
-211
lines changed

models/pull.go

-211
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"strings"
1717
"time"
1818

19-
"code.gitea.io/gitea/modules/base"
2019
"code.gitea.io/gitea/modules/git"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/process"
@@ -26,7 +25,6 @@ import (
2625
"code.gitea.io/gitea/modules/timeutil"
2726

2827
"github.com/unknwon/com"
29-
"xorm.io/xorm"
3028
)
3129

3230
var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
@@ -753,66 +751,6 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
753751
return nil
754752
}
755753

756-
// PullRequestsOptions holds the options for PRs
757-
type PullRequestsOptions struct {
758-
Page int
759-
State string
760-
SortType string
761-
Labels []string
762-
MilestoneID int64
763-
}
764-
765-
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
766-
sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
767-
768-
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
769-
switch opts.State {
770-
case "closed", "open":
771-
sess.And("issue.is_closed=?", opts.State == "closed")
772-
}
773-
774-
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
775-
return nil, err
776-
} else if len(labelIDs) > 0 {
777-
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
778-
In("issue_label.label_id", labelIDs)
779-
}
780-
781-
if opts.MilestoneID > 0 {
782-
sess.And("issue.milestone_id=?", opts.MilestoneID)
783-
}
784-
785-
return sess, nil
786-
}
787-
788-
// PullRequests returns all pull requests for a base Repo by the given conditions
789-
func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
790-
if opts.Page <= 0 {
791-
opts.Page = 1
792-
}
793-
794-
countSession, err := listPullRequestStatement(baseRepoID, opts)
795-
if err != nil {
796-
log.Error("listPullRequestStatement: %v", err)
797-
return nil, 0, err
798-
}
799-
maxResults, err := countSession.Count(new(PullRequest))
800-
if err != nil {
801-
log.Error("Count PRs: %v", err)
802-
return nil, maxResults, err
803-
}
804-
805-
prs := make([]*PullRequest, 0, ItemsPerPage)
806-
findSession, err := listPullRequestStatement(baseRepoID, opts)
807-
sortIssuesSession(findSession, opts.SortType)
808-
if err != nil {
809-
log.Error("listPullRequestStatement: %v", err)
810-
return nil, maxResults, err
811-
}
812-
findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
813-
return prs, maxResults, findSession.Find(&prs)
814-
}
815-
816754
// GetUnmergedPullRequest returns a pull request that is open and has not been merged
817755
// by given head/base and repo/branch.
818756
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
@@ -831,17 +769,6 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch
831769
return pr, nil
832770
}
833771

834-
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
835-
// by given head information (repo and branch).
836-
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
837-
prs := make([]*PullRequest, 0, 2)
838-
return prs, x.
839-
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
840-
repoID, branch, false, false).
841-
Join("INNER", "issue", "issue.id = pull_request.issue_id").
842-
Find(&prs)
843-
}
844-
845772
// GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status)
846773
// by given head information (repo and branch).
847774
func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
@@ -856,17 +783,6 @@ func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest,
856783
return pr, err
857784
}
858785

859-
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
860-
// by given base information (repo and branch).
861-
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
862-
prs := make([]*PullRequest, 0, 2)
863-
return prs, x.
864-
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
865-
repoID, branch, false, false).
866-
Join("INNER", "issue", "issue.id=pull_request.issue_id").
867-
Find(&prs)
868-
}
869-
870786
// GetPullRequestByIndex returns a pull request by the given index
871787
func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
872788
pr := &PullRequest{
@@ -1035,72 +951,6 @@ func (pr *PullRequest) AddToTaskQueue() {
1035951
})
1036952
}
1037953

1038-
// PullRequestList defines a list of pull requests
1039-
type PullRequestList []*PullRequest
1040-
1041-
func (prs PullRequestList) loadAttributes(e Engine) error {
1042-
if len(prs) == 0 {
1043-
return nil
1044-
}
1045-
1046-
// Load issues.
1047-
issueIDs := prs.getIssueIDs()
1048-
issues := make([]*Issue, 0, len(issueIDs))
1049-
if err := e.
1050-
Where("id > 0").
1051-
In("id", issueIDs).
1052-
Find(&issues); err != nil {
1053-
return fmt.Errorf("find issues: %v", err)
1054-
}
1055-
1056-
set := make(map[int64]*Issue)
1057-
for i := range issues {
1058-
set[issues[i].ID] = issues[i]
1059-
}
1060-
for i := range prs {
1061-
prs[i].Issue = set[prs[i].IssueID]
1062-
}
1063-
return nil
1064-
}
1065-
1066-
func (prs PullRequestList) getIssueIDs() []int64 {
1067-
issueIDs := make([]int64, 0, len(prs))
1068-
for i := range prs {
1069-
issueIDs = append(issueIDs, prs[i].IssueID)
1070-
}
1071-
return issueIDs
1072-
}
1073-
1074-
// LoadAttributes load all the prs attributes
1075-
func (prs PullRequestList) LoadAttributes() error {
1076-
return prs.loadAttributes(x)
1077-
}
1078-
1079-
func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
1080-
if len(prs) == 0 {
1081-
return nil
1082-
}
1083-
issueIDs := prs.getIssueIDs()
1084-
var codeComments []*Comment
1085-
if err := e.
1086-
Where("type = ? and invalidated = ?", CommentTypeCode, false).
1087-
In("issue_id", issueIDs).
1088-
Find(&codeComments); err != nil {
1089-
return fmt.Errorf("find code comments: %v", err)
1090-
}
1091-
for _, comment := range codeComments {
1092-
if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
1093-
return err
1094-
}
1095-
}
1096-
return nil
1097-
}
1098-
1099-
// InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
1100-
func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
1101-
return prs.invalidateCodeComments(x, doer, repo, branch)
1102-
}
1103-
1104954
// checkAndUpdateStatus checks if pull request is possible to leaving checking status,
1105955
// and set to be either conflict or mergeable.
1106956
func (pr *PullRequest) checkAndUpdateStatus() {
@@ -1152,64 +1002,3 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string {
11521002
}
11531003
return ""
11541004
}
1155-
1156-
// TestPullRequests checks and tests untested patches of pull requests.
1157-
// TODO: test more pull requests at same time.
1158-
func TestPullRequests() {
1159-
prs := make([]*PullRequest, 0, 10)
1160-
1161-
err := x.Where("status = ?", PullRequestStatusChecking).Find(&prs)
1162-
if err != nil {
1163-
log.Error("Find Checking PRs: %v", err)
1164-
return
1165-
}
1166-
1167-
var checkedPRs = make(map[int64]struct{})
1168-
1169-
// Update pull request status.
1170-
for _, pr := range prs {
1171-
checkedPRs[pr.ID] = struct{}{}
1172-
if err := pr.GetBaseRepo(); err != nil {
1173-
log.Error("GetBaseRepo: %v", err)
1174-
continue
1175-
}
1176-
if pr.manuallyMerged() {
1177-
continue
1178-
}
1179-
if err := pr.testPatch(x); err != nil {
1180-
log.Error("testPatch: %v", err)
1181-
continue
1182-
}
1183-
1184-
pr.checkAndUpdateStatus()
1185-
}
1186-
1187-
// Start listening on new test requests.
1188-
for prID := range pullRequestQueue.Queue() {
1189-
log.Trace("TestPullRequests[%v]: processing test task", prID)
1190-
pullRequestQueue.Remove(prID)
1191-
1192-
id := com.StrTo(prID).MustInt64()
1193-
if _, ok := checkedPRs[id]; ok {
1194-
continue
1195-
}
1196-
1197-
pr, err := GetPullRequestByID(id)
1198-
if err != nil {
1199-
log.Error("GetPullRequestByID[%s]: %v", prID, err)
1200-
continue
1201-
} else if pr.manuallyMerged() {
1202-
continue
1203-
} else if err = pr.testPatch(x); err != nil {
1204-
log.Error("testPatch[%d]: %v", pr.ID, err)
1205-
continue
1206-
}
1207-
1208-
pr.checkAndUpdateStatus()
1209-
}
1210-
}
1211-
1212-
// InitTestPullRequests runs the task to test all the checking status pull requests
1213-
func InitTestPullRequests() {
1214-
go TestPullRequests()
1215-
}

0 commit comments

Comments
 (0)