2019-10-30 21:33:10 +01:00
|
|
|
// 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 (
|
2022-04-28 13:48:48 +02:00
|
|
|
"context"
|
2019-10-30 21:33:10 +01:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-30 21:33:10 +01:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-12-07 03:44:10 +01:00
|
|
|
|
2019-10-30 21:33:10 +01:00
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PullRequestsOptions holds the options for PRs
|
|
|
|
type PullRequestsOptions struct {
|
2021-09-24 13:32:56 +02:00
|
|
|
db.ListOptions
|
2019-10-30 21:33:10 +01:00
|
|
|
State string
|
|
|
|
SortType string
|
|
|
|
Labels []string
|
|
|
|
MilestoneID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
|
2021-09-23 17:45:36 +02:00
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("pull_request.base_repo_id=?", baseRepoID)
|
2019-10-30 21:33:10 +01:00
|
|
|
|
|
|
|
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)
|
2021-09-23 17:45:36 +02:00
|
|
|
return prs, db.GetEngine(db.DefaultContext).
|
2021-07-28 11:42:56 +02:00
|
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
|
|
|
|
repoID, branch, false, false, PullRequestFlowGithub).
|
2019-10-30 21:33:10 +01:00
|
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
|
|
|
Find(&prs)
|
|
|
|
}
|
|
|
|
|
2022-01-03 20:45:58 +01:00
|
|
|
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
|
|
|
|
// by given head information (repo and branch)
|
2022-05-03 21:46:28 +02:00
|
|
|
func HasUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) (bool, error) {
|
|
|
|
return db.GetEngine(ctx).
|
2022-01-03 20:45:58 +01:00
|
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
|
|
|
|
repoID, branch, false, false, PullRequestFlowGithub).
|
|
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
|
|
|
Exist(&PullRequest{})
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:33:10 +01:00
|
|
|
// 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)
|
2021-09-23 17:45:36 +02:00
|
|
|
return prs, db.GetEngine(db.DefaultContext).
|
2019-10-30 21:33:10 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-15 10:51:28 +01:00
|
|
|
// GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
|
|
|
|
func GetPullRequestIDsByCheckStatus(status PullRequestStatus) ([]int64, error) {
|
|
|
|
prs := make([]int64, 0, 10)
|
2021-09-23 17:45:36 +02:00
|
|
|
return prs, db.GetEngine(db.DefaultContext).Table("pull_request").
|
2019-12-07 03:44:10 +01:00
|
|
|
Where("status=?", status).
|
2019-12-15 10:51:28 +01:00
|
|
|
Cols("pull_request.id").
|
2019-12-07 03:44:10 +01:00
|
|
|
Find(&prs)
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:33:10 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
findSession, err := listPullRequestStatement(baseRepoID, opts)
|
Allow cross-repository dependencies on issues (#7901)
* in progress changes for #7405, added ability to add cross-repo dependencies
* removed unused repolink var
* fixed query that was breaking ci tests; fixed check in issue dependency add so that the id of the issue and dependency is checked rather than the indexes
* reverted removal of string in local files becasue these are done via crowdin, not updated manually
* removed 'Select("issue.*")' from getBlockedByDependencies and getBlockingDependencies based on comments in PR review
* changed getBlockedByDependencies and getBlockingDependencies to use a more xorm-like query, also updated the sidebar as a result
* simplified the getBlockingDependencies and getBlockedByDependencies methods; changed the sidebar to show the dependencies in a different format where you can see the name of the repository
* made some changes to the issue view in the dependencies (issue name on top, repo full name on separate line). Change view of issue in the dependency search results (also showing the full repo name on separate line)
* replace call to FindUserAccessibleRepoIDs with SearchRepositoryByName. The former was hardcoded to use isPrivate = false on the repo search, but this code needed it to be true. The SearchRepositoryByName method is used more in the code including on the user's dashboard
* some more tweaks to the layout of the issues when showing dependencies and in the search box when you add new dependencies
* added Name to the RepositoryMeta struct
* updated swagger doc
* fixed total count for link header on SearchIssues
* fixed indentation
* fixed aligment of remove icon on dependencies in issue sidebar
* removed unnecessary nil check (unnecessary because issue.loadRepo is called prior to this block)
* reverting .css change, somehow missed or forgot that less is used
* updated less file and generated css; updated sidebar template with styles to line up delete and issue index
* added ordering to the blocked by/depends on queries
* fixed sorting in issue dependency search and the depends on/blocks views to show issues from the current repo first, then by created date descending; added a "all cross repository dependencies" setting to allow this feature to be turned off, if turned off, the issue dependency search will work the way it did before (restricted to the current repository)
* re-applied my swagger changes after merge
* fixed split string condition in issue search
* changed ALLOW_CROSS_REPOSITORY_DEPENDENCIES description to sound more global than just the issue dependency search; returning 400 in the cross repo issue search api method if not enabled; fixed bug where the issue count did not respect the state parameter
* when adding a dependency to an issue, added a check to make sure the issue and dependency are in the same repo if cross repo dependencies is not enabled
* updated sortIssuesSession call in PullRequests, another commit moved this method from pull.go to pull_list.go so I had to re-apply my change here
* fixed incorrect setting of user id parameter in search repos call
2019-10-31 06:06:10 +01:00
|
|
|
sortIssuesSession(findSession, opts.SortType, 0)
|
2019-10-30 21:33:10 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("listPullRequestStatement: %v", err)
|
|
|
|
return nil, maxResults, err
|
|
|
|
}
|
2021-09-24 13:32:56 +02:00
|
|
|
findSession = db.SetSessionPagination(findSession, opts)
|
2020-01-24 20:00:29 +01:00
|
|
|
prs := make([]*PullRequest, 0, opts.PageSize)
|
2019-10-30 21:33:10 +01:00
|
|
|
return prs, maxResults, findSession.Find(&prs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequestList defines a list of pull requests
|
|
|
|
type PullRequestList []*PullRequest
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func (prs PullRequestList) loadAttributes(e db.Engine) error {
|
2019-10-30 21:33:10 +01:00
|
|
|
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 {
|
2021-09-23 17:45:36 +02:00
|
|
|
return prs.loadAttributes(db.GetEngine(db.DefaultContext))
|
2019-10-30 21:33:10 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 13:48:48 +02:00
|
|
|
// InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
|
|
|
|
func (prs PullRequestList) InvalidateCodeComments(ctx context.Context, doer *user_model.User, repo *git.Repository, branch string) error {
|
2019-10-30 21:33:10 +01:00
|
|
|
if len(prs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
issueIDs := prs.getIssueIDs()
|
|
|
|
var codeComments []*Comment
|
2022-04-28 13:48:48 +02:00
|
|
|
if err := db.GetEngine(ctx).
|
2019-10-30 21:33:10 +01:00
|
|
|
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
|
|
|
|
}
|