Skip to content

Commit 587c3e0

Browse files
committed
Cherry-picked go-gitea#22235 and adapted to work based on v1.18.0.
1 parent f4df569 commit 587c3e0

File tree

5 files changed

+119
-7
lines changed

5 files changed

+119
-7
lines changed

models/issues/issue.go

+20
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,26 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
15661566
return issues, nil
15671567
}
15681568

1569+
// Issues returns a list of issues by given conditions and context.
1570+
func IssuesCtx(ctx context.Context, opts *IssuesOptions) ([]*Issue, error) {
1571+
sess := db.GetEngine(ctx).
1572+
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1573+
opts.setupSessionWithLimit(sess)
1574+
1575+
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
1576+
1577+
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
1578+
if err := sess.Find(&issues); err != nil {
1579+
return nil, fmt.Errorf("unable to query Issues: %w", err)
1580+
}
1581+
1582+
if err := IssueList(issues).LoadAttributes(); err != nil {
1583+
return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err)
1584+
}
1585+
1586+
return issues, nil
1587+
}
1588+
15691589
// CountIssues number return of issues by given conditions.
15701590
func CountIssues(opts *IssuesOptions) (int64, error) {
15711591
e := db.GetEngine(db.DefaultContext)

models/issues/issue_list.go

+45
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,46 @@ func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
7878
return issues.loadRepositories(db.DefaultContext)
7979
}
8080

81+
// LoadRepositories loads issues' all repositories in context
82+
func (issues IssueList) LoadRepositoriesCtx(ctx context.Context) ([]*repo_model.Repository, error) {
83+
if len(issues) == 0 {
84+
return nil, nil
85+
}
86+
87+
repoIDs := issues.getRepoIDs()
88+
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
89+
left := len(repoIDs)
90+
for left > 0 {
91+
limit := db.DefaultMaxInSize
92+
if left < limit {
93+
limit = left
94+
}
95+
err := db.GetEngine(ctx).
96+
In("id", repoIDs[:limit]).
97+
Find(&repoMaps)
98+
if err != nil {
99+
return nil, fmt.Errorf("find repository: %w", err)
100+
}
101+
left -= limit
102+
repoIDs = repoIDs[limit:]
103+
}
104+
105+
for _, issue := range issues {
106+
if issue.Repo == nil {
107+
issue.Repo = repoMaps[issue.RepoID]
108+
} else {
109+
repoMaps[issue.RepoID] = issue.Repo
110+
}
111+
if issue.PullRequest != nil {
112+
issue.PullRequest.BaseRepo = issue.Repo
113+
if issue.PullRequest.HeadRepo == nil {
114+
issue.PullRequest.HeadRepo = repoMaps[issue.PullRequest.HeadRepoID]
115+
}
116+
}
117+
}
118+
return repo_model.ValuesRepository(repoMaps), nil
119+
}
120+
81121
func (issues IssueList) getPosterIDs() []int64 {
82122
posterIDs := make(container.Set[int64], len(issues))
83123
for _, issue := range issues {
@@ -564,6 +604,11 @@ func (issues IssueList) LoadComments() error {
564604
return issues.loadComments(db.DefaultContext, builder.NewCond())
565605
}
566606

607+
// LoadComments loads comments for given context
608+
func (issues IssueList) LoadCommentsCtx(ctx context.Context) error {
609+
return issues.loadComments(ctx, builder.NewCond())
610+
}
611+
567612
// LoadDiscussComments loads discuss comments
568613
func (issues IssueList) LoadDiscussComments() error {
569614
return issues.loadComments(db.DefaultContext, builder.Eq{"comment.type": CommentTypeComment})

models/issues/issue_project.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,41 @@ func LoadIssuesFromBoard(b *project_model.Board) (IssueList, error) {
9595
return issueList, nil
9696
}
9797

98+
// LoadIssuesFromBoard load issues assigned to this board
99+
func LoadIssuesFromBoardCtx(ctx context.Context, b *project_model.Board) (IssueList, error) {
100+
issueList := make([]*Issue, 0, 10)
101+
102+
if b.ID != 0 {
103+
issues, err := IssuesCtx(ctx, &IssuesOptions{
104+
ProjectBoardID: b.ID,
105+
ProjectID: b.ProjectID,
106+
SortType: "project-column-sorting",
107+
})
108+
if err != nil {
109+
return nil, err
110+
}
111+
issueList = issues
112+
}
113+
114+
if b.Default {
115+
issues, err := IssuesCtx(ctx, &IssuesOptions{
116+
ProjectBoardID: -1, // Issues without ProjectBoardID
117+
ProjectID: b.ProjectID,
118+
SortType: "project-column-sorting",
119+
})
120+
if err != nil {
121+
return nil, err
122+
}
123+
issueList = append(issueList, issues...)
124+
}
125+
126+
if err := IssueList(issueList).LoadCommentsCtx(ctx); err != nil {
127+
return nil, err
128+
}
129+
130+
return issueList, nil
131+
}
132+
98133
// LoadIssuesFromBoardList load issues assigned to the boards
99134
func LoadIssuesFromBoardList(bs project_model.BoardList) (map[int64]IssueList, error) {
100135
issuesMap := make(map[int64]IssueList, len(bs))
@@ -108,7 +143,19 @@ func LoadIssuesFromBoardList(bs project_model.BoardList) (map[int64]IssueList, e
108143
return issuesMap, nil
109144
}
110145

111-
// ChangeProjectAssign changes the project associated with an issue
146+
// LoadIssuesFromBoardList load issues assigned to the boards
147+
func LoadIssuesFromBoardListCtx(ctx context.Context, bs project_model.BoardList) (map[int64]IssueList, error) {
148+
issuesMap := make(map[int64]IssueList, len(bs))
149+
for i := range bs {
150+
il, err := LoadIssuesFromBoardCtx(ctx, bs[i])
151+
if err != nil {
152+
return nil, err
153+
}
154+
issuesMap[bs[i].ID] = il
155+
}
156+
return issuesMap, nil
157+
}
158+
112159
func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error {
113160
ctx, committer, err := db.TxContext()
114161
if err != nil {

models/project/project.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ func (p *Project) LoadOwner(ctx context.Context) (err error) {
106106
if p.Owner != nil {
107107
return nil
108108
}
109-
p.Owner, err = user_model.GetUserByID(ctx, p.OwnerID)
109+
p.Owner, err = user_model.GetUserByIDCtx(ctx, p.OwnerID)
110110
return err
111111
}
112112

113113
func (p *Project) LoadRepo(ctx context.Context) (err error) {
114114
if p.RepoID == 0 || p.Repo != nil {
115115
return nil
116116
}
117-
p.Repo, err = repo_model.GetRepositoryByID(ctx, p.RepoID)
117+
p.Repo, err = repo_model.GetRepositoryByIDCtx(ctx, p.RepoID)
118118
return err
119119
}
120120

routers/web/org/projects.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func DeleteProject(ctx *context.Context) {
198198
return
199199
}
200200

201-
if err := project_model.DeleteProjectByID(ctx, p.ID); err != nil {
201+
if err := project_model.DeleteProjectByIDCtx(ctx, p.ID); err != nil {
202202
ctx.Flash.Error("DeleteProjectByID: " + err.Error())
203203
} else {
204204
ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success"))
@@ -302,7 +302,7 @@ func ViewProject(ctx *context.Context) {
302302
boards[0].Title = ctx.Tr("repo.projects.type.uncategorized")
303303
}
304304

305-
issuesMap, err := issues_model.LoadIssuesFromBoardList(ctx, boards)
305+
issuesMap, err := issues_model.LoadIssuesFromBoardListCtx(ctx, boards)
306306
if err != nil {
307307
ctx.ServerError("LoadIssuesOfBoards", err)
308308
return
@@ -319,7 +319,7 @@ func ViewProject(ctx *context.Context) {
319319
}
320320

321321
if len(referencedIds) > 0 {
322-
if linkedPrs, err := issues_model.Issues(ctx, &issues_model.IssuesOptions{
322+
if linkedPrs, err := issues_model.IssuesCtx(ctx, &issues_model.IssuesOptions{
323323
IssueIDs: referencedIds,
324324
IsPull: util.OptionalBoolTrue,
325325
}); err == nil {
@@ -647,7 +647,7 @@ func MoveIssues(ctx *context.Context) {
647647
return
648648
}
649649

650-
if _, err = movedIssues.LoadRepositories(ctx); err != nil {
650+
if _, err = movedIssues.LoadRepositoriesCtx(ctx); err != nil {
651651
ctx.ServerError("LoadRepositories", err)
652652
return
653653
}

0 commit comments

Comments
 (0)