@@ -229,39 +229,41 @@ func (issues IssueList) loadMilestones(ctx context.Context) error {
229229 return nil
230230}
231231
232- func (issues IssueList ) getProjectIDs () []int64 {
233- ids := make (container.Set [int64 ], len (issues ))
234- for _ , issue := range issues {
235- ids .Add (issue .ProjectID ())
236- }
237- return ids .Values ()
238- }
232+ func (issues IssueList ) LoadProjects (ctx context.Context ) error {
233+ issueIDs := issues .getIssueIDs ()
234+ projectMaps := make (map [int64 ]* project_model.Project , len (issues ))
235+ left := len (issueIDs )
239236
240- func (issues IssueList ) loadProjects (ctx context.Context ) error {
241- projectIDs := issues .getProjectIDs ()
242- if len (projectIDs ) == 0 {
243- return nil
237+ type projectWithIssueID struct {
238+ * project_model.Project `xorm:"extends"`
239+ IssueID int64
244240 }
245241
246- projectMaps := make (map [int64 ]* project_model.Project , len (projectIDs ))
247- left := len (projectIDs )
248242 for left > 0 {
249243 limit := db .DefaultMaxInSize
250244 if left < limit {
251245 limit = left
252246 }
247+
248+ projects := make ([]* projectWithIssueID , 0 , limit )
253249 err := db .GetEngine (ctx ).
254- In ("id" , projectIDs [:limit ]).
255- Find (& projectMaps )
250+ Table ("project" ).
251+ Select ("project.*, project_issue.issue_id" ).
252+ Join ("INNER" , "project_issue" , "project.id = project_issue.project_id" ).
253+ In ("project_issue.issue_id" , issueIDs [:limit ]).
254+ Find (& projects )
256255 if err != nil {
257256 return err
258257 }
258+ for _ , project := range projects {
259+ projectMaps [project .IssueID ] = project .Project
260+ }
259261 left -= limit
260- projectIDs = projectIDs [limit :]
262+ issueIDs = issueIDs [limit :]
261263 }
262264
263265 for _ , issue := range issues {
264- issue .Project = projectMaps [issue .ProjectID () ]
266+ issue .Project = projectMaps [issue .ID ]
265267 }
266268 return nil
267269}
@@ -541,7 +543,7 @@ func (issues IssueList) loadAttributes(ctx context.Context) error {
541543 return fmt .Errorf ("issue.loadAttributes: loadMilestones: %w" , err )
542544 }
543545
544- if err := issues .loadProjects (ctx ); err != nil {
546+ if err := issues .LoadProjects (ctx ); err != nil {
545547 return fmt .Errorf ("issue.loadAttributes: loadProjects: %w" , err )
546548 }
547549
0 commit comments