2017-02-22 17:03:59 +03:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
package issues
|
2017-02-22 17:03:59 +03:00
|
|
|
|
2019-02-19 17:39:39 +03:00
|
|
|
import (
|
2022-05-20 17:08:52 +03:00
|
|
|
"context"
|
2019-02-19 17:39:39 +03:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 14:49:59 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-07-26 16:42:23 +03:00
|
|
|
project_model "code.gitea.io/gitea/models/project"
|
2021-11-19 16:39:57 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-03-31 12:20:39 +03:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2019-06-23 18:22:43 +03:00
|
|
|
"xorm.io/builder"
|
2019-02-19 17:39:39 +03:00
|
|
|
)
|
2017-02-22 17:03:59 +03:00
|
|
|
|
|
|
|
// IssueList defines a list of issues
|
|
|
|
type IssueList []*Issue
|
|
|
|
|
2022-05-25 16:33:35 +03:00
|
|
|
// get the repo IDs to be loaded later, these IDs are for issue.Repo and issue.PullRequest.HeadRepo
|
2017-02-22 17:03:59 +03:00
|
|
|
func (issues IssueList) getRepoIDs() []int64 {
|
2022-10-12 08:18:26 +03:00
|
|
|
repoIDs := make(container.Set[int64], len(issues))
|
2017-02-22 17:03:59 +03:00
|
|
|
for _, issue := range issues {
|
2022-05-25 16:33:35 +03:00
|
|
|
if issue.Repo == nil {
|
2022-10-12 08:18:26 +03:00
|
|
|
repoIDs.Add(issue.RepoID)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2022-05-25 16:33:35 +03:00
|
|
|
if issue.PullRequest != nil && issue.PullRequest.HeadRepo == nil {
|
2022-10-12 08:18:26 +03:00
|
|
|
repoIDs.Add(issue.PullRequest.HeadRepoID)
|
2022-05-25 16:33:35 +03:00
|
|
|
}
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2022-10-12 08:18:26 +03:00
|
|
|
return repoIDs.Values()
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Repository, error) {
|
2017-02-22 17:03:59 +03:00
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := issues.getRepoIDs()
|
2021-12-10 04:27:50 +03:00
|
|
|
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
|
2021-03-14 21:52:12 +03:00
|
|
|
left := len(repoIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
err := db.GetEngine(ctx).
|
2018-08-02 16:49:05 +03:00
|
|
|
In("id", repoIDs[:limit]).
|
|
|
|
Find(&repoMaps)
|
|
|
|
if err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return nil, fmt.Errorf("find repository: %w", err)
|
2018-08-02 16:49:05 +03:00
|
|
|
}
|
2019-06-12 22:41:28 +03:00
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
repoIDs = repoIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
2021-12-29 16:02:12 +03:00
|
|
|
if issue.Repo == nil {
|
|
|
|
issue.Repo = repoMaps[issue.RepoID]
|
|
|
|
} else {
|
|
|
|
repoMaps[issue.RepoID] = issue.Repo
|
|
|
|
}
|
2022-05-25 16:33:35 +03:00
|
|
|
if issue.PullRequest != nil {
|
2021-04-15 20:34:43 +03:00
|
|
|
issue.PullRequest.BaseRepo = issue.Repo
|
2022-05-25 16:33:35 +03:00
|
|
|
if issue.PullRequest.HeadRepo == nil {
|
|
|
|
issue.PullRequest.HeadRepo = repoMaps[issue.PullRequest.HeadRepoID]
|
|
|
|
}
|
2021-04-15 20:34:43 +03:00
|
|
|
}
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2022-06-06 11:01:49 +03:00
|
|
|
return repo_model.ValuesRepository(repoMaps), nil
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadRepositories loads issues' all repositories
|
2021-12-10 04:27:50 +03:00
|
|
|
func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadRepositories(db.DefaultContext)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (issues IssueList) getPosterIDs() []int64 {
|
2022-10-12 08:18:26 +03:00
|
|
|
posterIDs := make(container.Set[int64], len(issues))
|
2017-02-22 17:03:59 +03:00
|
|
|
for _, issue := range issues {
|
2022-10-12 08:18:26 +03:00
|
|
|
posterIDs.Add(issue.PosterID)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2022-10-12 08:18:26 +03:00
|
|
|
return posterIDs.Values()
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadPosters(ctx context.Context) error {
|
2017-02-22 17:03:59 +03:00
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 03:52:01 +03:00
|
|
|
posterIDs := issues.getPosterIDs()
|
2021-11-24 12:49:20 +03:00
|
|
|
posterMaps := make(map[int64]*user_model.User, len(posterIDs))
|
2021-03-14 21:52:12 +03:00
|
|
|
left := len(posterIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
err := db.GetEngine(ctx).
|
2018-08-02 16:49:05 +03:00
|
|
|
In("id", posterIDs[:limit]).
|
|
|
|
Find(&posterMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 22:41:28 +03:00
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
posterIDs = posterIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
2017-06-09 04:51:31 +03:00
|
|
|
if issue.PosterID <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-30 09:08:36 +03:00
|
|
|
var ok bool
|
|
|
|
if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
|
2021-11-24 12:49:20 +03:00
|
|
|
issue.Poster = user_model.NewGhostUser()
|
2017-05-30 09:08:36 +03:00
|
|
|
}
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (issues IssueList) getIssueIDs() []int64 {
|
2021-03-14 21:52:12 +03:00
|
|
|
ids := make([]int64, 0, len(issues))
|
2017-02-22 17:03:59 +03:00
|
|
|
for _, issue := range issues {
|
|
|
|
ids = append(ids, issue.ID)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadLabels(ctx context.Context) error {
|
2017-02-22 17:03:59 +03:00
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type LabelIssue struct {
|
|
|
|
Label *Label `xorm:"extends"`
|
|
|
|
IssueLabel *IssueLabel `xorm:"extends"`
|
|
|
|
}
|
|
|
|
|
2021-03-14 21:52:12 +03:00
|
|
|
issueLabels := make(map[int64][]*Label, len(issues)*3)
|
|
|
|
issueIDs := issues.getIssueIDs()
|
|
|
|
left := len(issueIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).Table("label").
|
2018-08-02 16:49:05 +03:00
|
|
|
Join("LEFT", "issue_label", "issue_label.label_id = label.id").
|
|
|
|
In("issue_label.issue_id", issueIDs[:limit]).
|
|
|
|
Asc("label.name").
|
|
|
|
Rows(new(LabelIssue))
|
2017-02-22 17:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var labelIssue LabelIssue
|
|
|
|
err = rows.Scan(&labelIssue)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadLabels: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
issueLabels[labelIssue.IssueLabel.IssueID] = append(issueLabels[labelIssue.IssueLabel.IssueID], labelIssue.Label)
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
// When there are no rows left and we try to close it.
|
2019-06-12 22:41:28 +03:00
|
|
|
// Since that is not relevant for us, we can safely ignore it.
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadLabels: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
issueIDs = issueIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Labels = issueLabels[issue.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (issues IssueList) getMilestoneIDs() []int64 {
|
2022-10-12 08:18:26 +03:00
|
|
|
ids := make(container.Set[int64], len(issues))
|
2017-02-22 17:03:59 +03:00
|
|
|
for _, issue := range issues {
|
2022-10-12 08:18:26 +03:00
|
|
|
ids.Add(issue.MilestoneID)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2022-10-12 08:18:26 +03:00
|
|
|
return ids.Values()
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadMilestones(ctx context.Context) error {
|
2017-02-22 17:03:59 +03:00
|
|
|
milestoneIDs := issues.getMilestoneIDs()
|
|
|
|
if len(milestoneIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
|
2021-03-14 21:52:12 +03:00
|
|
|
left := len(milestoneIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
err := db.GetEngine(ctx).
|
2018-08-02 16:49:05 +03:00
|
|
|
In("id", milestoneIDs[:limit]).
|
|
|
|
Find(&milestoneMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 22:41:28 +03:00
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
milestoneIDs = milestoneIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Milestone = milestoneMaps[issue.MilestoneID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:42:23 +03:00
|
|
|
func (issues IssueList) getProjectIDs() []int64 {
|
2022-10-12 08:18:26 +03:00
|
|
|
ids := make(container.Set[int64], len(issues))
|
2022-07-26 16:42:23 +03:00
|
|
|
for _, issue := range issues {
|
2022-10-12 08:18:26 +03:00
|
|
|
ids.Add(issue.ProjectID())
|
2022-07-26 16:42:23 +03:00
|
|
|
}
|
2022-10-12 08:18:26 +03:00
|
|
|
return ids.Values()
|
2022-07-26 16:42:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (issues IssueList) loadProjects(ctx context.Context) error {
|
|
|
|
projectIDs := issues.getProjectIDs()
|
|
|
|
if len(projectIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
projectMaps := make(map[int64]*project_model.Project, len(projectIDs))
|
|
|
|
left := len(projectIDs)
|
|
|
|
for left > 0 {
|
|
|
|
limit := db.DefaultMaxInSize
|
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
|
|
|
err := db.GetEngine(ctx).
|
|
|
|
In("id", projectIDs[:limit]).
|
|
|
|
Find(&projectMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
left -= limit
|
|
|
|
projectIDs = projectIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Project = projectMaps[issue.ProjectID()]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadAssignees(ctx context.Context) error {
|
2018-05-09 19:29:04 +03:00
|
|
|
if len(issues) == 0 {
|
2017-02-22 17:03:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-09 19:29:04 +03:00
|
|
|
type AssigneeIssue struct {
|
2021-11-24 12:49:20 +03:00
|
|
|
IssueAssignee *IssueAssignees `xorm:"extends"`
|
|
|
|
Assignee *user_model.User `xorm:"extends"`
|
2018-05-09 19:29:04 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
assignees := make(map[int64][]*user_model.User, len(issues))
|
2021-03-14 21:52:12 +03:00
|
|
|
issueIDs := issues.getIssueIDs()
|
|
|
|
left := len(issueIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).Table("issue_assignees").
|
2018-08-02 16:49:05 +03:00
|
|
|
Join("INNER", "`user`", "`user`.id = `issue_assignees`.assignee_id").
|
2022-07-14 19:00:10 +03:00
|
|
|
In("`issue_assignees`.issue_id", issueIDs[:limit]).OrderBy(user_model.GetOrderByName()).
|
2018-08-02 16:49:05 +03:00
|
|
|
Rows(new(AssigneeIssue))
|
2018-05-09 19:29:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-05-30 09:08:36 +03:00
|
|
|
}
|
2018-05-09 19:29:04 +03:00
|
|
|
|
2018-08-02 16:49:05 +03:00
|
|
|
for rows.Next() {
|
|
|
|
var assigneeIssue AssigneeIssue
|
|
|
|
err = rows.Scan(&assigneeIssue)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadAssignees: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
assignees[assigneeIssue.IssueAssignee.IssueID] = append(assignees[assigneeIssue.IssueAssignee.IssueID], assigneeIssue.Assignee)
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadAssignees: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
issueIDs = issueIDs[limit:]
|
2018-05-09 19:29:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Assignees = assignees[issue.ID]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (issues IssueList) getPullIssueIDs() []int64 {
|
2021-03-14 21:52:12 +03:00
|
|
|
ids := make([]int64, 0, len(issues))
|
2017-02-22 17:03:59 +03:00
|
|
|
for _, issue := range issues {
|
|
|
|
if issue.IsPull && issue.PullRequest == nil {
|
|
|
|
ids = append(ids, issue.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadPullRequests(ctx context.Context) error {
|
2017-02-22 17:03:59 +03:00
|
|
|
issuesIDs := issues.getPullIssueIDs()
|
|
|
|
if len(issuesIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
|
2021-03-14 21:52:12 +03:00
|
|
|
left := len(issuesIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).
|
2018-08-02 16:49:05 +03:00
|
|
|
In("issue_id", issuesIDs[:limit]).
|
|
|
|
Rows(new(PullRequest))
|
2017-02-22 17:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var pr PullRequest
|
|
|
|
err = rows.Scan(&pr)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadPullRequests: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
pullRequestMaps[pr.IssueID] = &pr
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadPullRequests: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
issuesIDs = issuesIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.PullRequest = pullRequestMaps[issue.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadAttachments(ctx context.Context) (err error) {
|
2017-02-22 17:03:59 +03:00
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:39:57 +03:00
|
|
|
attachments := make(map[int64][]*repo_model.Attachment, len(issues))
|
2021-03-14 21:52:12 +03:00
|
|
|
issuesIDs := issues.getIssueIDs()
|
|
|
|
left := len(issuesIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).Table("attachment").
|
2018-08-02 16:49:05 +03:00
|
|
|
Join("INNER", "issue", "issue.id = attachment.issue_id").
|
|
|
|
In("issue.id", issuesIDs[:limit]).
|
2021-11-19 16:39:57 +03:00
|
|
|
Rows(new(repo_model.Attachment))
|
2017-02-22 17:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
2021-11-19 16:39:57 +03:00
|
|
|
var attachment repo_model.Attachment
|
2018-08-02 16:49:05 +03:00
|
|
|
err = rows.Scan(&attachment)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadAttachments: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadAttachments: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
issuesIDs = issuesIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Attachments = attachments[issue.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadComments(ctx context.Context, cond builder.Cond) (err error) {
|
2017-02-22 17:03:59 +03:00
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 21:52:12 +03:00
|
|
|
comments := make(map[int64][]*Comment, len(issues))
|
|
|
|
issuesIDs := issues.getIssueIDs()
|
|
|
|
left := len(issuesIDs)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).Table("comment").
|
2018-08-02 16:49:05 +03:00
|
|
|
Join("INNER", "issue", "issue.id = comment.issue_id").
|
|
|
|
In("issue.id", issuesIDs[:limit]).
|
2019-02-19 17:39:39 +03:00
|
|
|
Where(cond).
|
2018-08-02 16:49:05 +03:00
|
|
|
Rows(new(Comment))
|
2017-02-22 17:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var comment Comment
|
|
|
|
err = rows.Scan(&comment)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadComments: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadComments: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
issuesIDs = issuesIDs[limit:]
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.Comments = comments[issue.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadTotalTrackedTimes(ctx context.Context) (err error) {
|
2018-04-29 08:58:47 +03:00
|
|
|
type totalTimesByIssue struct {
|
|
|
|
IssueID int64
|
|
|
|
Time int64
|
|
|
|
}
|
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-14 21:52:12 +03:00
|
|
|
trackedTimes := make(map[int64]int64, len(issues))
|
2018-04-29 08:58:47 +03:00
|
|
|
|
2021-03-14 21:52:12 +03:00
|
|
|
ids := make([]int64, 0, len(issues))
|
2018-04-29 08:58:47 +03:00
|
|
|
for _, issue := range issues {
|
|
|
|
if issue.Repo.IsTimetrackerEnabled() {
|
|
|
|
ids = append(ids, issue.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 21:52:12 +03:00
|
|
|
left := len(ids)
|
2018-08-02 16:49:05 +03:00
|
|
|
for left > 0 {
|
2022-06-13 12:37:59 +03:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-08-02 16:49:05 +03:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2018-04-29 08:58:47 +03:00
|
|
|
|
2018-08-02 16:49:05 +03:00
|
|
|
// select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
|
2022-05-20 17:08:52 +03:00
|
|
|
rows, err := db.GetEngine(ctx).Table("tracked_time").
|
2019-12-27 23:30:58 +03:00
|
|
|
Where("deleted = ?", false).
|
2018-08-02 16:49:05 +03:00
|
|
|
Select("issue_id, sum(time) as time").
|
|
|
|
In("issue_id", ids[:limit]).
|
|
|
|
GroupBy("issue_id").
|
|
|
|
Rows(new(totalTimesByIssue))
|
2018-04-29 08:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var totalTime totalTimesByIssue
|
|
|
|
err = rows.Scan(&totalTime)
|
|
|
|
if err != nil {
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
2018-08-02 16:49:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
trackedTimes[totalTime.IssueID] = totalTime.Time
|
|
|
|
}
|
2019-06-23 18:22:43 +03:00
|
|
|
if err1 := rows.Close(); err1 != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("IssueList.loadTotalTrackedTimes: Close: %w", err1)
|
2019-06-12 22:41:28 +03:00
|
|
|
}
|
|
|
|
left -= limit
|
2018-08-02 16:49:05 +03:00
|
|
|
ids = ids[limit:]
|
2018-04-29 08:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
issue.TotalTrackedTime = trackedTimes[issue.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 08:28:22 +03:00
|
|
|
// loadAttributes loads all attributes, expect for attachments and comments
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) loadAttributes(ctx context.Context) error {
|
|
|
|
if _, err := issues.loadRepositories(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadRepositories: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadPosters(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadPosters: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadLabels(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadLabels: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadMilestones(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadMilestones: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 16:42:23 +03:00
|
|
|
if err := issues.loadProjects(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadProjects: %w", err)
|
2022-07-26 16:42:23 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadAssignees(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadAssignees: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadPullRequests(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadPullRequests: %w", err)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
if err := issues.loadTotalTrackedTimes(ctx); err != nil {
|
2022-10-24 22:29:17 +03:00
|
|
|
return fmt.Errorf("issue.loadAttributes: loadTotalTrackedTimes: %w", err)
|
2018-04-29 08:58:47 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 17:03:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 08:28:22 +03:00
|
|
|
// LoadAttributes loads attributes of the issues, except for attachments and
|
|
|
|
// comments
|
2017-02-22 17:03:59 +03:00
|
|
|
func (issues IssueList) LoadAttributes() error {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadAttributes(db.DefaultContext)
|
2017-02-22 17:03:59 +03:00
|
|
|
}
|
2017-11-21 08:28:22 +03:00
|
|
|
|
|
|
|
// LoadAttachments loads attachments
|
|
|
|
func (issues IssueList) LoadAttachments() error {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadAttachments(db.DefaultContext)
|
2017-11-21 08:28:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadComments loads comments
|
|
|
|
func (issues IssueList) LoadComments() error {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadComments(db.DefaultContext, builder.NewCond())
|
2019-02-19 17:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadDiscussComments loads discuss comments
|
|
|
|
func (issues IssueList) LoadDiscussComments() error {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadComments(db.DefaultContext, builder.Eq{"comment.type": CommentTypeComment})
|
2017-11-21 08:28:22 +03:00
|
|
|
}
|
2020-03-06 06:44:06 +03:00
|
|
|
|
2021-04-15 20:34:43 +03:00
|
|
|
// LoadPullRequests loads pull requests
|
|
|
|
func (issues IssueList) LoadPullRequests() error {
|
2022-05-20 17:08:52 +03:00
|
|
|
return issues.loadPullRequests(db.DefaultContext)
|
2021-04-15 20:34:43 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 06:44:06 +03:00
|
|
|
// GetApprovalCounts returns a map of issue ID to slice of approval counts
|
|
|
|
// FIXME: only returns official counts due to double counting of non-official approvals
|
2022-05-20 17:08:52 +03:00
|
|
|
func (issues IssueList) GetApprovalCounts(ctx context.Context) (map[int64][]*ReviewCount, error) {
|
2020-04-16 13:44:34 +03:00
|
|
|
rCounts := make([]*ReviewCount, 0, 2*len(issues))
|
2020-03-06 06:44:06 +03:00
|
|
|
ids := make([]int64, len(issues))
|
|
|
|
for i, issue := range issues {
|
|
|
|
ids[i] = issue.ID
|
|
|
|
}
|
2022-05-20 17:08:52 +03:00
|
|
|
sess := db.GetEngine(ctx).In("issue_id", ids)
|
2020-04-16 13:44:34 +03:00
|
|
|
err := sess.Select("issue_id, type, count(id) as `count`").
|
2021-02-11 20:32:25 +03:00
|
|
|
Where("official = ? AND dismissed = ?", true, false).
|
2020-04-16 13:44:34 +03:00
|
|
|
GroupBy("issue_id, type").
|
|
|
|
OrderBy("issue_id").
|
|
|
|
Table("review").
|
|
|
|
Find(&rCounts)
|
2020-03-06 06:44:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
approvalCountMap := make(map[int64][]*ReviewCount, len(issues))
|
2020-04-16 13:44:34 +03:00
|
|
|
|
|
|
|
for _, c := range rCounts {
|
|
|
|
approvalCountMap[c.IssueID] = append(approvalCountMap[c.IssueID], c)
|
2020-03-06 06:44:06 +03:00
|
|
|
}
|
2020-04-16 13:44:34 +03:00
|
|
|
|
2020-03-06 06:44:06 +03:00
|
|
|
return approvalCountMap, nil
|
|
|
|
}
|