mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
Merge branch 'master' into refactor_issues-subscription
This commit is contained in:
+36
-56
@@ -9,6 +9,7 @@ import (
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
@@ -378,6 +379,12 @@ func (issue *Issue) apiFormat(e Engine) *api.Issue {
|
||||
Updated: issue.UpdatedUnix.AsTime(),
|
||||
}
|
||||
|
||||
apiIssue.Repo = &api.RepositoryMeta{
|
||||
ID: issue.Repo.ID,
|
||||
Name: issue.Repo.Name,
|
||||
FullName: issue.Repo.FullName(),
|
||||
}
|
||||
|
||||
if issue.ClosedUnix != 0 {
|
||||
apiIssue.Closed = issue.ClosedUnix.AsTimePtr()
|
||||
}
|
||||
@@ -750,7 +757,6 @@ func (issue *Issue) UpdateAttachments(uuids []string) (err error) {
|
||||
|
||||
// ChangeContent changes issue content, as the given user.
|
||||
func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
|
||||
oldContent := issue.Content
|
||||
issue.Content = content
|
||||
|
||||
sess := x.NewSession()
|
||||
@@ -769,47 +775,7 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if issue.IsPull {
|
||||
issue.PullRequest.Issue = issue
|
||||
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
|
||||
Action: api.HookIssueEdited,
|
||||
Index: issue.Index,
|
||||
Changes: &api.ChangesPayload{
|
||||
Body: &api.ChangesFromPayload{
|
||||
From: oldContent,
|
||||
},
|
||||
},
|
||||
PullRequest: issue.PullRequest.APIFormat(),
|
||||
Repository: issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
})
|
||||
} else {
|
||||
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
|
||||
Action: api.HookIssueEdited,
|
||||
Index: issue.Index,
|
||||
Changes: &api.ChangesPayload{
|
||||
Body: &api.ChangesFromPayload{
|
||||
From: oldContent,
|
||||
},
|
||||
},
|
||||
Issue: issue.APIFormat(),
|
||||
Repository: issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
||||
} else {
|
||||
go HookQueue.Add(issue.RepoID)
|
||||
}
|
||||
|
||||
return nil
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// GetTasks returns the amount of tasks in the issues content
|
||||
@@ -1088,11 +1054,13 @@ type IssuesOptions struct {
|
||||
LabelIDs []int64
|
||||
SortType string
|
||||
IssueIDs []int64
|
||||
// prioritize issues from this repo
|
||||
PriorityRepoID int64
|
||||
}
|
||||
|
||||
// sortIssuesSession sort an issues-related session based on the provided
|
||||
// sortType string
|
||||
func sortIssuesSession(sess *xorm.Session, sortType string) {
|
||||
func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64) {
|
||||
switch sortType {
|
||||
case "oldest":
|
||||
sess.Asc("issue.created_unix")
|
||||
@@ -1110,6 +1078,8 @@ func sortIssuesSession(sess *xorm.Session, sortType string) {
|
||||
sess.Asc("issue.deadline_unix")
|
||||
case "farduedate":
|
||||
sess.Desc("issue.deadline_unix")
|
||||
case "priorityrepo":
|
||||
sess.OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(priorityRepoID, 10) + " THEN 1 ELSE 2 END, issue.created_unix DESC")
|
||||
default:
|
||||
sess.Desc("issue.created_unix")
|
||||
}
|
||||
@@ -1211,7 +1181,7 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
|
||||
defer sess.Close()
|
||||
|
||||
opts.setupSession(sess)
|
||||
sortIssuesSession(sess, opts.SortType)
|
||||
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
|
||||
|
||||
issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
|
||||
if err := sess.Find(&issues); err != nil {
|
||||
@@ -1517,8 +1487,8 @@ func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen
|
||||
}
|
||||
|
||||
// SearchIssueIDsByKeyword search issues on database
|
||||
func SearchIssueIDsByKeyword(kw string, repoID int64, limit, start int) (int64, []int64, error) {
|
||||
var repoCond = builder.Eq{"repo_id": repoID}
|
||||
func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int64, []int64, error) {
|
||||
var repoCond = builder.In("repo_id", repoIDs)
|
||||
var subQuery = builder.Select("id").From("issue").Where(repoCond)
|
||||
var cond = builder.And(
|
||||
repoCond,
|
||||
@@ -1607,33 +1577,43 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *Us
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// DependencyInfo represents high level information about an issue which is a dependency of another issue.
|
||||
type DependencyInfo struct {
|
||||
Issue `xorm:"extends"`
|
||||
Repository `xorm:"extends"`
|
||||
}
|
||||
|
||||
// Get Blocked By Dependencies, aka all issues this issue is blocked by.
|
||||
func (issue *Issue) getBlockedByDependencies(e Engine) (issueDeps []*Issue, err error) {
|
||||
func (issue *Issue) getBlockedByDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
|
||||
return issueDeps, e.
|
||||
Table("issue_dependency").
|
||||
Select("issue.*").
|
||||
Join("INNER", "issue", "issue.id = issue_dependency.dependency_id").
|
||||
Table("issue").
|
||||
Join("INNER", "repository", "repository.id = issue.repo_id").
|
||||
Join("INNER", "issue_dependency", "issue_dependency.dependency_id = issue.id").
|
||||
Where("issue_id = ?", issue.ID).
|
||||
//sort by repo id then created date, with the issues of the same repo at the beginning of the list
|
||||
OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
|
||||
Find(&issueDeps)
|
||||
}
|
||||
|
||||
// Get Blocking Dependencies, aka all issues this issue blocks.
|
||||
func (issue *Issue) getBlockingDependencies(e Engine) (issueDeps []*Issue, err error) {
|
||||
func (issue *Issue) getBlockingDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
|
||||
return issueDeps, e.
|
||||
Table("issue_dependency").
|
||||
Select("issue.*").
|
||||
Join("INNER", "issue", "issue.id = issue_dependency.issue_id").
|
||||
Table("issue").
|
||||
Join("INNER", "repository", "repository.id = issue.repo_id").
|
||||
Join("INNER", "issue_dependency", "issue_dependency.issue_id = issue.id").
|
||||
Where("dependency_id = ?", issue.ID).
|
||||
//sort by repo id then created date, with the issues of the same repo at the beginning of the list
|
||||
OrderBy("CASE WHEN issue.repo_id = " + strconv.FormatInt(issue.RepoID, 10) + " THEN 0 ELSE issue.repo_id END, issue.created_unix DESC").
|
||||
Find(&issueDeps)
|
||||
}
|
||||
|
||||
// BlockedByDependencies finds all Dependencies an issue is blocked by
|
||||
func (issue *Issue) BlockedByDependencies() ([]*Issue, error) {
|
||||
func (issue *Issue) BlockedByDependencies() ([]*DependencyInfo, error) {
|
||||
return issue.getBlockedByDependencies(x)
|
||||
}
|
||||
|
||||
// BlockingDependencies returns all blocking dependencies, aka all other issues a given issue blocks
|
||||
func (issue *Issue) BlockingDependencies() ([]*Issue, error) {
|
||||
func (issue *Issue) BlockingDependencies() ([]*DependencyInfo, error) {
|
||||
return issue.getBlockingDependencies(x)
|
||||
}
|
||||
|
||||
|
||||
@@ -444,6 +444,10 @@ func (c *Comment) LoadReview() error {
|
||||
func (c *Comment) checkInvalidation(doer *User, repo *git.Repository, branch string) error {
|
||||
// FIXME differentiate between previous and proposed line
|
||||
commit, err := repo.LineBlame(branch, repo.Path, c.TreePath, uint(c.UnsignedLine()))
|
||||
if err != nil && strings.Contains(err.Error(), "fatal: no such path") {
|
||||
c.Invalidated = true
|
||||
return UpdateComment(c, doer)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -250,6 +250,19 @@ func GetLabelIDsInRepoByNames(repoID int64, labelNames []string) ([]int64, error
|
||||
Find(&labelIDs)
|
||||
}
|
||||
|
||||
// GetLabelIDsInReposByNames returns a list of labelIDs by names in one of the given
|
||||
// repositories.
|
||||
// it silently ignores label names that do not belong to the repository.
|
||||
func GetLabelIDsInReposByNames(repoIDs []int64, labelNames []string) ([]int64, error) {
|
||||
labelIDs := make([]int64, 0, len(labelNames))
|
||||
return labelIDs, x.Table("label").
|
||||
In("repo_id", repoIDs).
|
||||
In("name", labelNames).
|
||||
Asc("name").
|
||||
Cols("id").
|
||||
Find(&labelIDs)
|
||||
}
|
||||
|
||||
// GetLabelInRepoByID returns a label by ID in given repository.
|
||||
func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
|
||||
return getLabelInRepoByID(x, repoID, labelID)
|
||||
|
||||
@@ -264,24 +264,23 @@ func TestIssue_loadTotalTimes(t *testing.T) {
|
||||
|
||||
func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
total, ids, err := SearchIssueIDsByKeyword("issue2", 1, 10, 0)
|
||||
total, ids, err := SearchIssueIDsByKeyword("issue2", []int64{1}, 10, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, total)
|
||||
assert.EqualValues(t, []int64{2}, ids)
|
||||
|
||||
total, ids, err = SearchIssueIDsByKeyword("first", 1, 10, 0)
|
||||
total, ids, err = SearchIssueIDsByKeyword("first", []int64{1}, 10, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, total)
|
||||
assert.EqualValues(t, []int64{1}, ids)
|
||||
|
||||
total, ids, err = SearchIssueIDsByKeyword("for", 1, 10, 0)
|
||||
total, ids, err = SearchIssueIDsByKeyword("for", []int64{1}, 10, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 4, total)
|
||||
assert.EqualValues(t, []int64{1, 2, 3, 5}, ids)
|
||||
|
||||
// issue1's comment id 2
|
||||
total, ids, err = SearchIssueIDsByKeyword("good", 1, 10, 0)
|
||||
total, ids, err = SearchIssueIDsByKeyword("good", []int64{1}, 10, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, total)
|
||||
assert.EqualValues(t, []int64{1}, ids)
|
||||
|
||||
-211
@@ -16,7 +16,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
@@ -26,7 +25,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
|
||||
@@ -753,66 +751,6 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
|
||||
return nil
|
||||
}
|
||||
|
||||
// PullRequestsOptions holds the options for PRs
|
||||
type PullRequestsOptions struct {
|
||||
Page int
|
||||
State string
|
||||
SortType string
|
||||
Labels []string
|
||||
MilestoneID int64
|
||||
}
|
||||
|
||||
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
|
||||
sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
|
||||
|
||||
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
|
||||
switch opts.State {
|
||||
case "closed", "open":
|
||||
sess.And("issue.is_closed=?", opts.State == "closed")
|
||||
}
|
||||
|
||||
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
|
||||
return nil, err
|
||||
} else if len(labelIDs) > 0 {
|
||||
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
|
||||
In("issue_label.label_id", labelIDs)
|
||||
}
|
||||
|
||||
if opts.MilestoneID > 0 {
|
||||
sess.And("issue.milestone_id=?", opts.MilestoneID)
|
||||
}
|
||||
|
||||
return sess, nil
|
||||
}
|
||||
|
||||
// PullRequests returns all pull requests for a base Repo by the given conditions
|
||||
func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
|
||||
if opts.Page <= 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
|
||||
countSession, err := listPullRequestStatement(baseRepoID, opts)
|
||||
if err != nil {
|
||||
log.Error("listPullRequestStatement: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
maxResults, err := countSession.Count(new(PullRequest))
|
||||
if err != nil {
|
||||
log.Error("Count PRs: %v", err)
|
||||
return nil, maxResults, err
|
||||
}
|
||||
|
||||
prs := make([]*PullRequest, 0, ItemsPerPage)
|
||||
findSession, err := listPullRequestStatement(baseRepoID, opts)
|
||||
sortIssuesSession(findSession, opts.SortType)
|
||||
if err != nil {
|
||||
log.Error("listPullRequestStatement: %v", err)
|
||||
return nil, maxResults, err
|
||||
}
|
||||
findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
|
||||
return prs, maxResults, findSession.Find(&prs)
|
||||
}
|
||||
|
||||
// GetUnmergedPullRequest returns a pull request that is open and has not been merged
|
||||
// by given head/base and repo/branch.
|
||||
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
|
||||
@@ -831,17 +769,6 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch
|
||||
return pr, nil
|
||||
}
|
||||
|
||||
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
|
||||
// by given head information (repo and branch).
|
||||
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
||||
prs := make([]*PullRequest, 0, 2)
|
||||
return prs, x.
|
||||
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
|
||||
repoID, branch, false, false).
|
||||
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
||||
Find(&prs)
|
||||
}
|
||||
|
||||
// GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status)
|
||||
// by given head information (repo and branch).
|
||||
func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
|
||||
@@ -856,17 +783,6 @@ func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest,
|
||||
return pr, err
|
||||
}
|
||||
|
||||
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
|
||||
// by given base information (repo and branch).
|
||||
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
||||
prs := make([]*PullRequest, 0, 2)
|
||||
return prs, x.
|
||||
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
|
||||
repoID, branch, false, false).
|
||||
Join("INNER", "issue", "issue.id=pull_request.issue_id").
|
||||
Find(&prs)
|
||||
}
|
||||
|
||||
// GetPullRequestByIndex returns a pull request by the given index
|
||||
func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
|
||||
pr := &PullRequest{
|
||||
@@ -1035,72 +951,6 @@ func (pr *PullRequest) AddToTaskQueue() {
|
||||
})
|
||||
}
|
||||
|
||||
// PullRequestList defines a list of pull requests
|
||||
type PullRequestList []*PullRequest
|
||||
|
||||
func (prs PullRequestList) loadAttributes(e Engine) error {
|
||||
if len(prs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load issues.
|
||||
issueIDs := prs.getIssueIDs()
|
||||
issues := make([]*Issue, 0, len(issueIDs))
|
||||
if err := e.
|
||||
Where("id > 0").
|
||||
In("id", issueIDs).
|
||||
Find(&issues); err != nil {
|
||||
return fmt.Errorf("find issues: %v", err)
|
||||
}
|
||||
|
||||
set := make(map[int64]*Issue)
|
||||
for i := range issues {
|
||||
set[issues[i].ID] = issues[i]
|
||||
}
|
||||
for i := range prs {
|
||||
prs[i].Issue = set[prs[i].IssueID]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (prs PullRequestList) getIssueIDs() []int64 {
|
||||
issueIDs := make([]int64, 0, len(prs))
|
||||
for i := range prs {
|
||||
issueIDs = append(issueIDs, prs[i].IssueID)
|
||||
}
|
||||
return issueIDs
|
||||
}
|
||||
|
||||
// LoadAttributes load all the prs attributes
|
||||
func (prs PullRequestList) LoadAttributes() error {
|
||||
return prs.loadAttributes(x)
|
||||
}
|
||||
|
||||
func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
|
||||
if len(prs) == 0 {
|
||||
return nil
|
||||
}
|
||||
issueIDs := prs.getIssueIDs()
|
||||
var codeComments []*Comment
|
||||
if err := e.
|
||||
Where("type = ? and invalidated = ?", CommentTypeCode, false).
|
||||
In("issue_id", issueIDs).
|
||||
Find(&codeComments); err != nil {
|
||||
return fmt.Errorf("find code comments: %v", err)
|
||||
}
|
||||
for _, comment := range codeComments {
|
||||
if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
|
||||
func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
|
||||
return prs.invalidateCodeComments(x, doer, repo, branch)
|
||||
}
|
||||
|
||||
// checkAndUpdateStatus checks if pull request is possible to leaving checking status,
|
||||
// and set to be either conflict or mergeable.
|
||||
func (pr *PullRequest) checkAndUpdateStatus() {
|
||||
@@ -1152,64 +1002,3 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TestPullRequests checks and tests untested patches of pull requests.
|
||||
// TODO: test more pull requests at same time.
|
||||
func TestPullRequests() {
|
||||
prs := make([]*PullRequest, 0, 10)
|
||||
|
||||
err := x.Where("status = ?", PullRequestStatusChecking).Find(&prs)
|
||||
if err != nil {
|
||||
log.Error("Find Checking PRs: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var checkedPRs = make(map[int64]struct{})
|
||||
|
||||
// Update pull request status.
|
||||
for _, pr := range prs {
|
||||
checkedPRs[pr.ID] = struct{}{}
|
||||
if err := pr.GetBaseRepo(); err != nil {
|
||||
log.Error("GetBaseRepo: %v", err)
|
||||
continue
|
||||
}
|
||||
if pr.manuallyMerged() {
|
||||
continue
|
||||
}
|
||||
if err := pr.testPatch(x); err != nil {
|
||||
log.Error("testPatch: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pr.checkAndUpdateStatus()
|
||||
}
|
||||
|
||||
// Start listening on new test requests.
|
||||
for prID := range pullRequestQueue.Queue() {
|
||||
log.Trace("TestPullRequests[%v]: processing test task", prID)
|
||||
pullRequestQueue.Remove(prID)
|
||||
|
||||
id := com.StrTo(prID).MustInt64()
|
||||
if _, ok := checkedPRs[id]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
pr, err := GetPullRequestByID(id)
|
||||
if err != nil {
|
||||
log.Error("GetPullRequestByID[%s]: %v", prID, err)
|
||||
continue
|
||||
} else if pr.manuallyMerged() {
|
||||
continue
|
||||
} else if err = pr.testPatch(x); err != nil {
|
||||
log.Error("testPatch[%d]: %v", pr.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
pr.checkAndUpdateStatus()
|
||||
}
|
||||
}
|
||||
|
||||
// InitTestPullRequests runs the task to test all the checking status pull requests
|
||||
func InitTestPullRequests() {
|
||||
go TestPullRequests()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// PullRequestsOptions holds the options for PRs
|
||||
type PullRequestsOptions struct {
|
||||
Page int
|
||||
State string
|
||||
SortType string
|
||||
Labels []string
|
||||
MilestoneID int64
|
||||
}
|
||||
|
||||
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
|
||||
sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
|
||||
|
||||
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
|
||||
switch opts.State {
|
||||
case "closed", "open":
|
||||
sess.And("issue.is_closed=?", opts.State == "closed")
|
||||
}
|
||||
|
||||
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
|
||||
return nil, err
|
||||
} else if len(labelIDs) > 0 {
|
||||
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
|
||||
In("issue_label.label_id", labelIDs)
|
||||
}
|
||||
|
||||
if opts.MilestoneID > 0 {
|
||||
sess.And("issue.milestone_id=?", opts.MilestoneID)
|
||||
}
|
||||
|
||||
return sess, nil
|
||||
}
|
||||
|
||||
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
|
||||
// by given head information (repo and branch).
|
||||
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
||||
prs := make([]*PullRequest, 0, 2)
|
||||
return prs, x.
|
||||
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
|
||||
repoID, branch, false, false).
|
||||
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
||||
Find(&prs)
|
||||
}
|
||||
|
||||
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
|
||||
// by given base information (repo and branch).
|
||||
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
||||
prs := make([]*PullRequest, 0, 2)
|
||||
return prs, x.
|
||||
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
|
||||
repoID, branch, false, false).
|
||||
Join("INNER", "issue", "issue.id=pull_request.issue_id").
|
||||
Find(&prs)
|
||||
}
|
||||
|
||||
// PullRequests returns all pull requests for a base Repo by the given conditions
|
||||
func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
|
||||
if opts.Page <= 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
|
||||
countSession, err := listPullRequestStatement(baseRepoID, opts)
|
||||
if err != nil {
|
||||
log.Error("listPullRequestStatement: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
maxResults, err := countSession.Count(new(PullRequest))
|
||||
if err != nil {
|
||||
log.Error("Count PRs: %v", err)
|
||||
return nil, maxResults, err
|
||||
}
|
||||
|
||||
prs := make([]*PullRequest, 0, ItemsPerPage)
|
||||
findSession, err := listPullRequestStatement(baseRepoID, opts)
|
||||
sortIssuesSession(findSession, opts.SortType, 0)
|
||||
if err != nil {
|
||||
log.Error("listPullRequestStatement: %v", err)
|
||||
return nil, maxResults, err
|
||||
}
|
||||
findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
|
||||
return prs, maxResults, findSession.Find(&prs)
|
||||
}
|
||||
|
||||
// PullRequestList defines a list of pull requests
|
||||
type PullRequestList []*PullRequest
|
||||
|
||||
func (prs PullRequestList) loadAttributes(e Engine) error {
|
||||
if len(prs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load issues.
|
||||
issueIDs := prs.getIssueIDs()
|
||||
issues := make([]*Issue, 0, len(issueIDs))
|
||||
if err := e.
|
||||
Where("id > 0").
|
||||
In("id", issueIDs).
|
||||
Find(&issues); err != nil {
|
||||
return fmt.Errorf("find issues: %v", err)
|
||||
}
|
||||
|
||||
set := make(map[int64]*Issue)
|
||||
for i := range issues {
|
||||
set[issues[i].ID] = issues[i]
|
||||
}
|
||||
for i := range prs {
|
||||
prs[i].Issue = set[prs[i].IssueID]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (prs PullRequestList) getIssueIDs() []int64 {
|
||||
issueIDs := make([]int64, 0, len(prs))
|
||||
for i := range prs {
|
||||
issueIDs = append(issueIDs, prs[i].IssueID)
|
||||
}
|
||||
return issueIDs
|
||||
}
|
||||
|
||||
// LoadAttributes load all the prs attributes
|
||||
func (prs PullRequestList) LoadAttributes() error {
|
||||
return prs.loadAttributes(x)
|
||||
}
|
||||
|
||||
func (prs PullRequestList) invalidateCodeComments(e Engine, doer *User, repo *git.Repository, branch string) error {
|
||||
if len(prs) == 0 {
|
||||
return nil
|
||||
}
|
||||
issueIDs := prs.getIssueIDs()
|
||||
var codeComments []*Comment
|
||||
if err := e.
|
||||
Where("type = ? and invalidated = ?", CommentTypeCode, false).
|
||||
In("issue_id", issueIDs).
|
||||
Find(&codeComments); err != nil {
|
||||
return fmt.Errorf("find code comments: %v", err)
|
||||
}
|
||||
for _, comment := range codeComments {
|
||||
if err := comment.CheckInvalidation(repo, doer, branch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
|
||||
func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Repository, branch string) error {
|
||||
return prs.invalidateCodeComments(x, doer, repo, branch)
|
||||
}
|
||||
|
||||
// TestPullRequests checks and tests untested patches of pull requests.
|
||||
// TODO: test more pull requests at same time.
|
||||
func TestPullRequests() {
|
||||
prs := make([]*PullRequest, 0, 10)
|
||||
|
||||
err := x.Where("status = ?", PullRequestStatusChecking).Find(&prs)
|
||||
if err != nil {
|
||||
log.Error("Find Checking PRs: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var checkedPRs = make(map[int64]struct{})
|
||||
|
||||
// Update pull request status.
|
||||
for _, pr := range prs {
|
||||
checkedPRs[pr.ID] = struct{}{}
|
||||
if err := pr.GetBaseRepo(); err != nil {
|
||||
log.Error("GetBaseRepo: %v", err)
|
||||
continue
|
||||
}
|
||||
if pr.manuallyMerged() {
|
||||
continue
|
||||
}
|
||||
if err := pr.testPatch(x); err != nil {
|
||||
log.Error("testPatch: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pr.checkAndUpdateStatus()
|
||||
}
|
||||
|
||||
// Start listening on new test requests.
|
||||
for prID := range pullRequestQueue.Queue() {
|
||||
log.Trace("TestPullRequests[%v]: processing test task", prID)
|
||||
pullRequestQueue.Remove(prID)
|
||||
|
||||
id := com.StrTo(prID).MustInt64()
|
||||
if _, ok := checkedPRs[id]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
pr, err := GetPullRequestByID(id)
|
||||
if err != nil {
|
||||
log.Error("GetPullRequestByID[%s]: %v", prID, err)
|
||||
continue
|
||||
} else if pr.manuallyMerged() {
|
||||
continue
|
||||
} else if err = pr.testPatch(x); err != nil {
|
||||
log.Error("testPatch[%d]: %v", pr.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
pr.checkAndUpdateStatus()
|
||||
}
|
||||
}
|
||||
|
||||
// InitTestPullRequests runs the task to test all the checking status pull requests
|
||||
func InitTestPullRequests() {
|
||||
go TestPullRequests()
|
||||
}
|
||||
Reference in New Issue
Block a user