2018-12-13 10:55:43 -05:00
|
|
|
// Copyright 2018 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 05:37:59 -04:00
|
|
|
package issues
|
2018-12-13 10:55:43 -05:00
|
|
|
|
2021-11-19 08:39:57 -05:00
|
|
|
import (
|
2021-12-09 20:27:50 -05:00
|
|
|
"context"
|
|
|
|
|
2021-11-19 08:39:57 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-03-31 05:20:39 -04:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2021-11-19 08:39:57 -05:00
|
|
|
)
|
2021-09-19 07:49:59 -04:00
|
|
|
|
2018-12-13 10:55:43 -05:00
|
|
|
// CommentList defines a list of comments
|
|
|
|
type CommentList []*Comment
|
|
|
|
|
|
|
|
func (comments CommentList) getPosterIDs() []int64 {
|
2019-04-18 01:00:03 -04:00
|
|
|
posterIDs := make(map[int64]struct{}, len(comments))
|
2018-12-13 10:55:43 -05:00
|
|
|
for _, comment := range comments {
|
2019-04-18 01:00:03 -04:00
|
|
|
if _, ok := posterIDs[comment.PosterID]; !ok {
|
|
|
|
posterIDs[comment.PosterID] = struct{}{}
|
2018-12-13 10:55:43 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(posterIDs)
|
2018-12-13 10:55:43 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadPosters(ctx context.Context) error {
|
2018-12-13 10:55:43 -05:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
posterIDs := comments.getPosterIDs()
|
2021-11-24 04:49:20 -05:00
|
|
|
posterMaps := make(map[int64]*user_model.User, len(posterIDs))
|
2021-03-14 14:52:12 -04:00
|
|
|
left := len(posterIDs)
|
2018-12-13 10:55:43 -05:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2018-12-13 10:55:43 -05:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
err := db.GetEngine(ctx).
|
2018-12-13 10:55:43 -05:00
|
|
|
In("id", posterIDs[:limit]).
|
|
|
|
Find(&posterMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2018-12-13 10:55:43 -05:00
|
|
|
posterIDs = posterIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.PosterID <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
|
2021-11-24 04:49:20 -05:00
|
|
|
comment.Poster = user_model.NewGhostUser()
|
2018-12-13 10:55:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-18 01:00:03 -04:00
|
|
|
|
|
|
|
func (comments CommentList) getCommentIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make([]int64, 0, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
ids = append(ids, comment.ID)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getLabelIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if _, ok := ids[comment.LabelID]; !ok {
|
|
|
|
ids[comment.LabelID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
labelIDs := comments.getLabelIDs()
|
|
|
|
commentLabels := make(map[int64]*Label, len(labelIDs))
|
|
|
|
left := len(labelIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
rows, err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", labelIDs[:limit]).
|
|
|
|
Rows(new(Label))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var label Label
|
|
|
|
err = rows.Scan(&label)
|
|
|
|
if err != nil {
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
commentLabels[label.ID] = &label
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
labelIDs = labelIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
comment.Label = commentLabels[comment.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getMilestoneIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if _, ok := ids[comment.MilestoneID]; !ok {
|
|
|
|
ids[comment.MilestoneID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadMilestones(ctx context.Context) error {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
milestoneIDs := comments.getMilestoneIDs()
|
|
|
|
if len(milestoneIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
|
2021-03-14 14:52:12 -04:00
|
|
|
left := len(milestoneIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", milestoneIDs[:limit]).
|
|
|
|
Find(&milestoneMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
milestoneIDs = milestoneIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range comments {
|
|
|
|
issue.Milestone = milestoneMaps[issue.MilestoneID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getOldMilestoneIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if _, ok := ids[comment.OldMilestoneID]; !ok {
|
|
|
|
ids[comment.OldMilestoneID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadOldMilestones(ctx context.Context) error {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
milestoneIDs := comments.getOldMilestoneIDs()
|
|
|
|
if len(milestoneIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
|
2021-03-14 14:52:12 -04:00
|
|
|
left := len(milestoneIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", milestoneIDs[:limit]).
|
|
|
|
Find(&milestoneMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
milestoneIDs = milestoneIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range comments {
|
|
|
|
issue.OldMilestone = milestoneMaps[issue.MilestoneID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getAssigneeIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if _, ok := ids[comment.AssigneeID]; !ok {
|
|
|
|
ids[comment.AssigneeID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadAssignees(ctx context.Context) error {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
assigneeIDs := comments.getAssigneeIDs()
|
2021-11-24 04:49:20 -05:00
|
|
|
assignees := make(map[int64]*user_model.User, len(assigneeIDs))
|
2021-03-14 14:52:12 -04:00
|
|
|
left := len(assigneeIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
rows, err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", assigneeIDs[:limit]).
|
2021-11-24 04:49:20 -05:00
|
|
|
Rows(new(user_model.User))
|
2019-04-18 01:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
2021-11-24 04:49:20 -05:00
|
|
|
var user user_model.User
|
2019-04-18 01:00:03 -04:00
|
|
|
err = rows.Scan(&user)
|
|
|
|
if err != nil {
|
|
|
|
rows.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
assignees[user.ID] = &user
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
assigneeIDs = assigneeIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
comment.Assignee = assignees[comment.AssigneeID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
|
|
|
|
func (comments CommentList) getIssueIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.Issue != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := ids[comment.IssueID]; !ok {
|
|
|
|
ids[comment.IssueID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Issues returns all the issues of comments
|
|
|
|
func (comments CommentList) Issues() IssueList {
|
2021-03-14 14:52:12 -04:00
|
|
|
issues := make(map[int64]*Issue, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.Issue != nil {
|
|
|
|
if _, ok := issues[comment.Issue.ID]; !ok {
|
|
|
|
issues[comment.Issue.ID] = comment.Issue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
issueList := make([]*Issue, 0, len(issues))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, issue := range issues {
|
|
|
|
issueList = append(issueList, issue)
|
|
|
|
}
|
|
|
|
return issueList
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadIssues(ctx context.Context) error {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
issueIDs := comments.getIssueIDs()
|
|
|
|
issues := make(map[int64]*Issue, len(issueIDs))
|
|
|
|
left := len(issueIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
rows, err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", issueIDs[:limit]).
|
|
|
|
Rows(new(Issue))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var issue Issue
|
|
|
|
err = rows.Scan(&issue)
|
|
|
|
if err != nil {
|
|
|
|
rows.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
issues[issue.ID] = &issue
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
issueIDs = issueIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.Issue == nil {
|
|
|
|
comment.Issue = issues[comment.IssueID]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getDependentIssueIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.DependentIssue != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := ids[comment.DependentIssueID]; !ok {
|
|
|
|
ids[comment.DependentIssueID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
e := db.GetEngine(ctx)
|
2021-03-14 14:52:12 -04:00
|
|
|
issueIDs := comments.getDependentIssueIDs()
|
|
|
|
issues := make(map[int64]*Issue, len(issueIDs))
|
|
|
|
left := len(issueIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
|
|
|
rows, err := e.
|
|
|
|
In("id", issueIDs[:limit]).
|
|
|
|
Rows(new(Issue))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var issue Issue
|
|
|
|
err = rows.Scan(&issue)
|
|
|
|
if err != nil {
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
issues[issue.ID] = &issue
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
issueIDs = issueIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
if comment.DependentIssue == nil {
|
|
|
|
comment.DependentIssue = issues[comment.DependentIssueID]
|
2020-01-21 05:18:52 -05:00
|
|
|
if comment.DependentIssue != nil {
|
2022-04-08 05:11:15 -04:00
|
|
|
if err := comment.DependentIssue.LoadRepo(ctx); err != nil {
|
2020-01-21 05:18:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 08:39:57 -05:00
|
|
|
attachments := make(map[int64][]*repo_model.Attachment, len(comments))
|
2021-03-14 14:52:12 -04:00
|
|
|
commentsIDs := comments.getCommentIDs()
|
|
|
|
left := len(commentsIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
rows, err := db.GetEngine(ctx).Table("attachment").
|
2019-04-18 01:00:03 -04:00
|
|
|
Join("INNER", "comment", "comment.id = attachment.comment_id").
|
|
|
|
In("comment.id", commentsIDs[:limit]).
|
2021-11-19 08:39:57 -05:00
|
|
|
Rows(new(repo_model.Attachment))
|
2019-04-18 01:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
2021-11-19 08:39:57 -05:00
|
|
|
var attachment repo_model.Attachment
|
2019-04-18 01:00:03 -04:00
|
|
|
err = rows.Scan(&attachment)
|
|
|
|
if err != nil {
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
attachments[attachment.CommentID] = append(attachments[attachment.CommentID], &attachment)
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
commentsIDs = commentsIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
comment.Attachments = attachments[comment.ID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comments CommentList) getReviewIDs() []int64 {
|
2021-03-14 14:52:12 -04:00
|
|
|
ids := make(map[int64]struct{}, len(comments))
|
2019-04-18 01:00:03 -04:00
|
|
|
for _, comment := range comments {
|
|
|
|
if _, ok := ids[comment.ReviewID]; !ok {
|
|
|
|
ids[comment.ReviewID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(ids)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
func (comments CommentList) loadReviews(ctx context.Context) error { //nolint
|
2019-04-18 01:00:03 -04:00
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
reviewIDs := comments.getReviewIDs()
|
|
|
|
reviews := make(map[int64]*Review, len(reviewIDs))
|
|
|
|
left := len(reviewIDs)
|
2019-04-18 01:00:03 -04:00
|
|
|
for left > 0 {
|
2022-06-13 05:37:59 -04:00
|
|
|
limit := db.DefaultMaxInSize
|
2019-04-18 01:00:03 -04:00
|
|
|
if left < limit {
|
|
|
|
limit = left
|
|
|
|
}
|
2022-05-20 10:08:52 -04:00
|
|
|
rows, err := db.GetEngine(ctx).
|
2019-04-18 01:00:03 -04:00
|
|
|
In("id", reviewIDs[:limit]).
|
|
|
|
Rows(new(Review))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var review Review
|
|
|
|
err = rows.Scan(&review)
|
|
|
|
if err != nil {
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reviews[review.ID] = &review
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = rows.Close()
|
2019-04-18 01:00:03 -04:00
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
left -= limit
|
2019-04-18 01:00:03 -04:00
|
|
|
reviewIDs = reviewIDs[limit:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range comments {
|
|
|
|
comment.Review = reviews[comment.ReviewID]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadAttributes loads all attributes
|
2021-12-09 20:27:50 -05:00
|
|
|
func (comments CommentList) loadAttributes(ctx context.Context) (err error) {
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadPosters(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadLabels(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadMilestones(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadOldMilestones(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadAssignees(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadAttachments(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadReviews(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err = comments.loadIssues(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
if err = comments.loadDependentIssues(ctx); err != nil {
|
2019-04-18 01:00:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAttributes loads attributes of the comments, except for attachments and
|
|
|
|
// comments
|
|
|
|
func (comments CommentList) LoadAttributes() error {
|
2021-12-09 20:27:50 -05:00
|
|
|
return comments.loadAttributes(db.DefaultContext)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAttachments loads attachments
|
|
|
|
func (comments CommentList) LoadAttachments() error {
|
2022-05-20 10:08:52 -04:00
|
|
|
return comments.loadAttachments(db.DefaultContext)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPosters loads posters
|
|
|
|
func (comments CommentList) LoadPosters() error {
|
2022-05-20 10:08:52 -04:00
|
|
|
return comments.loadPosters(db.DefaultContext)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIssues loads issues of comments
|
|
|
|
func (comments CommentList) LoadIssues() error {
|
2022-05-20 10:08:52 -04:00
|
|
|
return comments.loadIssues(db.DefaultContext)
|
2019-04-18 01:00:03 -04:00
|
|
|
}
|