2019-10-31 02:03:10 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-31 02:03:10 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
package issues
|
2019-10-31 02:03:10 +05:30
|
|
|
|
|
|
|
import (
|
2022-04-28 17:18:48 +05:30
|
|
|
"context"
|
2019-10-31 02:03:10 +05:30
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-31 02:03:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2023-02-05 17:27:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-07 08:14:10 +05:30
|
|
|
|
2019-10-31 02:03:10 +05:30
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PullRequestsOptions holds the options for PRs
|
|
|
|
type PullRequestsOptions struct {
|
2021-09-24 17:02:56 +05:30
|
|
|
db.ListOptions
|
2019-10-31 02:03:10 +05:30
|
|
|
State string
|
|
|
|
SortType string
|
|
|
|
Labels []string
|
|
|
|
MilestoneID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("pull_request.base_repo_id=?", baseRepoID)
|
2019-10-31 02:03:10 +05:30
|
|
|
|
|
|
|
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).
|
Fix cannot reopen after pushing commits to a closed PR (#23189)
Close: #22784
1. On GH, we can reopen a PR which was closed before after pushing
commits. After reopening PR, we can see the commits that were pushed
after closing PR in the time line. So the case of
[issue](https://github.com/go-gitea/gitea/issues/22784) is a bug which
needs to be fixed.
2. After closing a PR and pushing commits, `headBranchSha` is not equal
to `sha`(which is the last commit ID string of reference). If the
judgement exists, the button of reopen will not display. So, skip the
judgement if the status of PR is closed.
![image](https://user-images.githubusercontent.com/33891828/222037529-651fccf9-0bba-433e-b2f0-79c17e0cc812.png)
3. Even if PR is already close, we should still insert comment record
into DB when we push commits.
So we should still call function `CreatePushPullComment()`.
https://github.com/go-gitea/gitea/blob/067b0c2664d127c552ccdfd264257caca4907a77/services/pull/pull.go#L260-L282
So, I add a switch(`includeClosed`) to the
`GetUnmergedPullRequestsByHeadInfo` func to control whether the status
of PR must be open. In this case, by setting `includeClosed` to `true`,
we can query the closed PR.
![image](https://user-images.githubusercontent.com/33891828/222621045-bb80987c-10c5-4eac-aa0c-1fb9c6aefb51.png)
4. In the loop of comments, I use the`latestCloseCommentID` variable to
record the last occurrence of the close comment.
In the go template, if the status of PR is closed, the comments whose
type is `CommentTypePullRequestPush(29)` after `latestCloseCommentID`
won't be rendered.
![image](https://user-images.githubusercontent.com/33891828/222058913-c91cf3e3-819b-40c5-8015-654b31eeccff.png)
e.g.
1). The initial status of the PR is opened.
![image](https://user-images.githubusercontent.com/33891828/222453617-33c5093e-f712-4cd6-8489-9f87e2075869.png)
2). Then I click the button of `Close`. PR is closed now.
![image](https://user-images.githubusercontent.com/33891828/222453694-25c588a9-c121-4897-9ae5-0b13cf33d20b.png)
3). I try to push a commit to this PR, even though its current status is
closed.
![image](https://user-images.githubusercontent.com/33891828/222453916-361678fb-7321-410d-9e37-5a26e8095638.png)
But in comments list, this commit do not display.This is as expected :)
![image](https://user-images.githubusercontent.com/33891828/222454169-7617a791-78d2-404e-be5e-77d555f93313.png)
4). Click the `Reopen` button, the commit which is pushed after closing
PR display now.
![image](https://user-images.githubusercontent.com/33891828/222454533-897893b6-b96e-4701-b5cb-b1800f382b8f.png)
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-03-03 18:46:58 +05:30
|
|
|
// arg `includeClosed` controls whether the SQL returns closed PRs
|
|
|
|
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string, includeClosed bool) ([]*PullRequest, error) {
|
2019-10-31 02:03:10 +05:30
|
|
|
prs := make([]*PullRequest, 0, 2)
|
Fix cannot reopen after pushing commits to a closed PR (#23189)
Close: #22784
1. On GH, we can reopen a PR which was closed before after pushing
commits. After reopening PR, we can see the commits that were pushed
after closing PR in the time line. So the case of
[issue](https://github.com/go-gitea/gitea/issues/22784) is a bug which
needs to be fixed.
2. After closing a PR and pushing commits, `headBranchSha` is not equal
to `sha`(which is the last commit ID string of reference). If the
judgement exists, the button of reopen will not display. So, skip the
judgement if the status of PR is closed.
![image](https://user-images.githubusercontent.com/33891828/222037529-651fccf9-0bba-433e-b2f0-79c17e0cc812.png)
3. Even if PR is already close, we should still insert comment record
into DB when we push commits.
So we should still call function `CreatePushPullComment()`.
https://github.com/go-gitea/gitea/blob/067b0c2664d127c552ccdfd264257caca4907a77/services/pull/pull.go#L260-L282
So, I add a switch(`includeClosed`) to the
`GetUnmergedPullRequestsByHeadInfo` func to control whether the status
of PR must be open. In this case, by setting `includeClosed` to `true`,
we can query the closed PR.
![image](https://user-images.githubusercontent.com/33891828/222621045-bb80987c-10c5-4eac-aa0c-1fb9c6aefb51.png)
4. In the loop of comments, I use the`latestCloseCommentID` variable to
record the last occurrence of the close comment.
In the go template, if the status of PR is closed, the comments whose
type is `CommentTypePullRequestPush(29)` after `latestCloseCommentID`
won't be rendered.
![image](https://user-images.githubusercontent.com/33891828/222058913-c91cf3e3-819b-40c5-8015-654b31eeccff.png)
e.g.
1). The initial status of the PR is opened.
![image](https://user-images.githubusercontent.com/33891828/222453617-33c5093e-f712-4cd6-8489-9f87e2075869.png)
2). Then I click the button of `Close`. PR is closed now.
![image](https://user-images.githubusercontent.com/33891828/222453694-25c588a9-c121-4897-9ae5-0b13cf33d20b.png)
3). I try to push a commit to this PR, even though its current status is
closed.
![image](https://user-images.githubusercontent.com/33891828/222453916-361678fb-7321-410d-9e37-5a26e8095638.png)
But in comments list, this commit do not display.This is as expected :)
![image](https://user-images.githubusercontent.com/33891828/222454169-7617a791-78d2-404e-be5e-77d555f93313.png)
4). Click the `Reopen` button, the commit which is pushed after closing
PR display now.
![image](https://user-images.githubusercontent.com/33891828/222454533-897893b6-b96e-4701-b5cb-b1800f382b8f.png)
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-03-03 18:46:58 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).
|
2019-10-31 02:03:10 +05:30
|
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
Fix cannot reopen after pushing commits to a closed PR (#23189)
Close: #22784
1. On GH, we can reopen a PR which was closed before after pushing
commits. After reopening PR, we can see the commits that were pushed
after closing PR in the time line. So the case of
[issue](https://github.com/go-gitea/gitea/issues/22784) is a bug which
needs to be fixed.
2. After closing a PR and pushing commits, `headBranchSha` is not equal
to `sha`(which is the last commit ID string of reference). If the
judgement exists, the button of reopen will not display. So, skip the
judgement if the status of PR is closed.
![image](https://user-images.githubusercontent.com/33891828/222037529-651fccf9-0bba-433e-b2f0-79c17e0cc812.png)
3. Even if PR is already close, we should still insert comment record
into DB when we push commits.
So we should still call function `CreatePushPullComment()`.
https://github.com/go-gitea/gitea/blob/067b0c2664d127c552ccdfd264257caca4907a77/services/pull/pull.go#L260-L282
So, I add a switch(`includeClosed`) to the
`GetUnmergedPullRequestsByHeadInfo` func to control whether the status
of PR must be open. In this case, by setting `includeClosed` to `true`,
we can query the closed PR.
![image](https://user-images.githubusercontent.com/33891828/222621045-bb80987c-10c5-4eac-aa0c-1fb9c6aefb51.png)
4. In the loop of comments, I use the`latestCloseCommentID` variable to
record the last occurrence of the close comment.
In the go template, if the status of PR is closed, the comments whose
type is `CommentTypePullRequestPush(29)` after `latestCloseCommentID`
won't be rendered.
![image](https://user-images.githubusercontent.com/33891828/222058913-c91cf3e3-819b-40c5-8015-654b31eeccff.png)
e.g.
1). The initial status of the PR is opened.
![image](https://user-images.githubusercontent.com/33891828/222453617-33c5093e-f712-4cd6-8489-9f87e2075869.png)
2). Then I click the button of `Close`. PR is closed now.
![image](https://user-images.githubusercontent.com/33891828/222453694-25c588a9-c121-4897-9ae5-0b13cf33d20b.png)
3). I try to push a commit to this PR, even though its current status is
closed.
![image](https://user-images.githubusercontent.com/33891828/222453916-361678fb-7321-410d-9e37-5a26e8095638.png)
But in comments list, this commit do not display.This is as expected :)
![image](https://user-images.githubusercontent.com/33891828/222454169-7617a791-78d2-404e-be5e-77d555f93313.png)
4). Click the `Reopen` button, the commit which is pushed after closing
PR display now.
![image](https://user-images.githubusercontent.com/33891828/222454533-897893b6-b96e-4701-b5cb-b1800f382b8f.png)
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-03-03 18:46:58 +05:30
|
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND flow = ?", repoID, branch, false, PullRequestFlowGithub)
|
|
|
|
if !includeClosed {
|
|
|
|
sess.Where("issue.is_closed = ?", false)
|
|
|
|
}
|
|
|
|
return prs, sess.Find(&prs)
|
2019-10-31 02:03:10 +05:30
|
|
|
}
|
|
|
|
|
Fix various typos (#20338)
* Fix various typos
Found via `codespell -q 3 -S ./options/locale,./options/license,./public/vendor -L actived,allways,attachements,ba,befores,commiter,pullrequest,pullrequests,readby,splitted,te,unknwon`
Co-authored-by: zeripath <art27@cantab.net>
2022-07-13 03:02:37 +05:30
|
|
|
// CanMaintainerWriteToBranch check whether user is a maintainer and could write to the branch
|
2022-05-11 15:39:36 +05:30
|
|
|
func CanMaintainerWriteToBranch(p access_model.Permission, branch string, user *user_model.User) bool {
|
|
|
|
if p.CanWrite(unit.TypeCode) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Units) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
Fix cannot reopen after pushing commits to a closed PR (#23189)
Close: #22784
1. On GH, we can reopen a PR which was closed before after pushing
commits. After reopening PR, we can see the commits that were pushed
after closing PR in the time line. So the case of
[issue](https://github.com/go-gitea/gitea/issues/22784) is a bug which
needs to be fixed.
2. After closing a PR and pushing commits, `headBranchSha` is not equal
to `sha`(which is the last commit ID string of reference). If the
judgement exists, the button of reopen will not display. So, skip the
judgement if the status of PR is closed.
![image](https://user-images.githubusercontent.com/33891828/222037529-651fccf9-0bba-433e-b2f0-79c17e0cc812.png)
3. Even if PR is already close, we should still insert comment record
into DB when we push commits.
So we should still call function `CreatePushPullComment()`.
https://github.com/go-gitea/gitea/blob/067b0c2664d127c552ccdfd264257caca4907a77/services/pull/pull.go#L260-L282
So, I add a switch(`includeClosed`) to the
`GetUnmergedPullRequestsByHeadInfo` func to control whether the status
of PR must be open. In this case, by setting `includeClosed` to `true`,
we can query the closed PR.
![image](https://user-images.githubusercontent.com/33891828/222621045-bb80987c-10c5-4eac-aa0c-1fb9c6aefb51.png)
4. In the loop of comments, I use the`latestCloseCommentID` variable to
record the last occurrence of the close comment.
In the go template, if the status of PR is closed, the comments whose
type is `CommentTypePullRequestPush(29)` after `latestCloseCommentID`
won't be rendered.
![image](https://user-images.githubusercontent.com/33891828/222058913-c91cf3e3-819b-40c5-8015-654b31eeccff.png)
e.g.
1). The initial status of the PR is opened.
![image](https://user-images.githubusercontent.com/33891828/222453617-33c5093e-f712-4cd6-8489-9f87e2075869.png)
2). Then I click the button of `Close`. PR is closed now.
![image](https://user-images.githubusercontent.com/33891828/222453694-25c588a9-c121-4897-9ae5-0b13cf33d20b.png)
3). I try to push a commit to this PR, even though its current status is
closed.
![image](https://user-images.githubusercontent.com/33891828/222453916-361678fb-7321-410d-9e37-5a26e8095638.png)
But in comments list, this commit do not display.This is as expected :)
![image](https://user-images.githubusercontent.com/33891828/222454169-7617a791-78d2-404e-be5e-77d555f93313.png)
4). Click the `Reopen` button, the commit which is pushed after closing
PR display now.
![image](https://user-images.githubusercontent.com/33891828/222454533-897893b6-b96e-4701-b5cb-b1800f382b8f.png)
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-03-03 18:46:58 +05:30
|
|
|
prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch, false)
|
2022-05-11 15:39:36 +05:30
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
if pr.AllowMaintainerEdit {
|
2022-11-19 13:42:33 +05:30
|
|
|
err = pr.LoadBaseRepo(db.DefaultContext)
|
2022-05-11 15:39:36 +05:30
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prPerm, err := access_model.GetUserRepoPermission(db.DefaultContext, pr.BaseRepo, user)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prPerm.CanWrite(unit.TypeCode) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-04 01:15:58 +05:30
|
|
|
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
|
|
|
|
// by given head information (repo and branch)
|
2022-05-04 01:16:28 +05:30
|
|
|
func HasUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) (bool, error) {
|
|
|
|
return db.GetEngine(ctx).
|
2022-01-04 01:15:58 +05:30
|
|
|
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-31 02:03:10 +05:30
|
|
|
// 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 21:15:36 +05:30
|
|
|
return prs, db.GetEngine(db.DefaultContext).
|
2019-10-31 02:03:10 +05:30
|
|
|
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
|
|
|
|
repoID, branch, false, false).
|
2023-03-02 00:44:02 +05:30
|
|
|
OrderBy("issue.updated_unix DESC").
|
2019-10-31 02:03:10 +05:30
|
|
|
Join("INNER", "issue", "issue.id=pull_request.issue_id").
|
|
|
|
Find(&prs)
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:21:28 +05:30
|
|
|
// GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
|
|
|
|
func GetPullRequestIDsByCheckStatus(status PullRequestStatus) ([]int64, error) {
|
|
|
|
prs := make([]int64, 0, 10)
|
2021-09-23 21:15:36 +05:30
|
|
|
return prs, db.GetEngine(db.DefaultContext).Table("pull_request").
|
2019-12-07 08:14:10 +05:30
|
|
|
Where("status=?", status).
|
2019-12-15 15:21:28 +05:30
|
|
|
Cols("pull_request.id").
|
2019-12-07 08:14:10 +05:30
|
|
|
Find(&prs)
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:03:10 +05:30
|
|
|
// 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 10:36:10 +05:30
|
|
|
sortIssuesSession(findSession, opts.SortType, 0)
|
2019-10-31 02:03:10 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("listPullRequestStatement: %v", err)
|
|
|
|
return nil, maxResults, err
|
|
|
|
}
|
2021-09-24 17:02:56 +05:30
|
|
|
findSession = db.SetSessionPagination(findSession, opts)
|
2020-01-25 00:30:29 +05:30
|
|
|
prs := make([]*PullRequest, 0, opts.PageSize)
|
2019-10-31 02:03:10 +05:30
|
|
|
return prs, maxResults, findSession.Find(&prs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequestList defines a list of pull requests
|
|
|
|
type PullRequestList []*PullRequest
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
func (prs PullRequestList) loadAttributes(ctx context.Context) error {
|
2019-10-31 02:03:10 +05:30
|
|
|
if len(prs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load issues.
|
2023-01-18 02:33:44 +05:30
|
|
|
issueIDs := prs.GetIssueIDs()
|
2019-10-31 02:03:10 +05:30
|
|
|
issues := make([]*Issue, 0, len(issueIDs))
|
2022-05-20 19:38:52 +05:30
|
|
|
if err := db.GetEngine(ctx).
|
2019-10-31 02:03:10 +05:30
|
|
|
Where("id > 0").
|
|
|
|
In("id", issueIDs).
|
|
|
|
Find(&issues); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("find issues: %w", err)
|
2019-10-31 02:03:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
set := make(map[int64]*Issue)
|
|
|
|
for i := range issues {
|
|
|
|
set[issues[i].ID] = issues[i]
|
|
|
|
}
|
2023-02-02 09:19:28 +05:30
|
|
|
for _, pr := range prs {
|
|
|
|
pr.Issue = set[pr.IssueID]
|
2023-02-05 17:27:38 +05:30
|
|
|
/*
|
|
|
|
Old code:
|
|
|
|
pr.Issue.PullRequest = pr // panic here means issueIDs and prs are not in sync
|
|
|
|
|
|
|
|
It's worth panic because it's almost impossible to happen under normal use.
|
|
|
|
But in integration testing, an asynchronous task could read a database that has been reset.
|
|
|
|
So returning an error would make more sense, let the caller has a choice to ignore it.
|
|
|
|
*/
|
|
|
|
if pr.Issue == nil {
|
|
|
|
return fmt.Errorf("issues and prs may be not in sync: cannot find issue %v for pr %v: %w", pr.IssueID, pr.ID, util.ErrNotExist)
|
|
|
|
}
|
2023-02-05 19:54:43 +05:30
|
|
|
pr.Issue.PullRequest = pr
|
2019-10-31 02:03:10 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 02:33:44 +05:30
|
|
|
// GetIssueIDs returns all issue ids
|
|
|
|
func (prs PullRequestList) GetIssueIDs() []int64 {
|
2019-10-31 02:03:10 +05:30
|
|
|
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 {
|
2022-05-20 19:38:52 +05:30
|
|
|
return prs.loadAttributes(db.DefaultContext)
|
2019-10-31 02:03:10 +05:30
|
|
|
}
|