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