summaryrefslogtreecommitdiffstats
path: root/models/issues
diff options
context:
space:
mode:
authoroliverpool <3864879+oliverpool@users.noreply.github.com>2024-04-09 14:27:30 +0200
committerGitHub <noreply@github.com>2024-04-09 20:27:30 +0800
commitd547b53cca8a9a7ac96449910bae5d811728c251 (patch)
tree2d356c8a7e06855b16ecc289ba9dce92ceeb5f77 /models/issues
parent8d14266269f1b4fd5e13d701830919c1a1613444 (diff)
downloadgitea-d547b53cca8a9a7ac96449910bae5d811728c251.tar.gz
gitea-d547b53cca8a9a7ac96449910bae5d811728c251.zip
Add container.FilterSlice function (#30339)
Many places have the following logic: ```go func (jobs ActionJobList) GetRunIDs() []int64 { ids := make(container.Set[int64], len(jobs)) for _, j := range jobs { if j.RunID == 0 { continue } ids.Add(j.RunID) } return ids.Values() } ``` this introduces a `container.FilterMapUnique` function, which reduces the code above to: ```go func (jobs ActionJobList) GetRunIDs() []int64 { return container.FilterMapUnique(jobs, func(j *ActionRunJob) (int64, bool) { return j.RunID, j.RunID != 0 }) } ```
Diffstat (limited to 'models/issues')
-rw-r--r--models/issues/comment.go9
-rw-r--r--models/issues/comment_list.go95
-rw-r--r--models/issues/issue_list.go16
-rw-r--r--models/issues/reaction.go10
-rw-r--r--models/issues/review_list.go9
5 files changed, 46 insertions, 93 deletions
diff --git a/models/issues/comment.go b/models/issues/comment.go
index 6f65a5dbbc..353163ebd6 100644
--- a/models/issues/comment.go
+++ b/models/issues/comment.go
@@ -1272,10 +1272,9 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
return nil
}
- issueIDs := make(container.Set[int64])
- for _, comment := range comments {
- issueIDs.Add(comment.IssueID)
- }
+ issueIDs := container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.IssueID, true
+ })
ctx, committer, err := db.TxContext(ctx)
if err != nil {
@@ -1298,7 +1297,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
}
}
- for issueID := range issueIDs {
+ for _, issueID := range issueIDs {
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
issueID, CommentTypeComment, issueID); err != nil {
return err
diff --git a/models/issues/comment_list.go b/models/issues/comment_list.go
index 0047b054ba..370b5396e0 100644
--- a/models/issues/comment_list.go
+++ b/models/issues/comment_list.go
@@ -17,13 +17,9 @@ import (
type CommentList []*Comment
func (comments CommentList) getPosterIDs() []int64 {
- posterIDs := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.PosterID > 0 {
- posterIDs.Add(comment.PosterID)
- }
- }
- return posterIDs.Values()
+ return container.FilterSlice(comments, func(c *Comment) (int64, bool) {
+ return c.PosterID, c.PosterID > 0
+ })
}
// LoadPosters loads posters
@@ -44,13 +40,9 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
}
func (comments CommentList) getLabelIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.LabelID > 0 {
- ids.Add(comment.LabelID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.LabelID, comment.LabelID > 0
+ })
}
func (comments CommentList) loadLabels(ctx context.Context) error {
@@ -94,13 +86,9 @@ func (comments CommentList) loadLabels(ctx context.Context) error {
}
func (comments CommentList) getMilestoneIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.MilestoneID > 0 {
- ids.Add(comment.MilestoneID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.MilestoneID, comment.MilestoneID > 0
+ })
}
func (comments CommentList) loadMilestones(ctx context.Context) error {
@@ -137,13 +125,9 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
}
func (comments CommentList) getOldMilestoneIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.OldMilestoneID > 0 {
- ids.Add(comment.OldMilestoneID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.OldMilestoneID, comment.OldMilestoneID > 0
+ })
}
func (comments CommentList) loadOldMilestones(ctx context.Context) error {
@@ -180,13 +164,9 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
}
func (comments CommentList) getAssigneeIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.AssigneeID > 0 {
- ids.Add(comment.AssigneeID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.AssigneeID, comment.AssigneeID > 0
+ })
}
func (comments CommentList) loadAssignees(ctx context.Context) error {
@@ -237,14 +217,9 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
func (comments CommentList) getIssueIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.Issue != nil {
- continue
- }
- ids.Add(comment.IssueID)
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.IssueID, comment.Issue == nil
+ })
}
// Issues returns all the issues of comments
@@ -311,16 +286,12 @@ func (comments CommentList) LoadIssues(ctx context.Context) error {
}
func (comments CommentList) getDependentIssueIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
if comment.DependentIssue != nil {
- continue
- }
- if comment.DependentIssueID > 0 {
- ids.Add(comment.DependentIssueID)
+ return 0, false
}
- }
- return ids.Values()
+ return comment.DependentIssueID, comment.DependentIssueID > 0
+ })
}
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -375,15 +346,9 @@ func (comments CommentList) loadDependentIssues(ctx context.Context) error {
// getAttachmentCommentIDs only return the comment ids which possibly has attachments
func (comments CommentList) getAttachmentCommentIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.Type == CommentTypeComment ||
- comment.Type == CommentTypeReview ||
- comment.Type == CommentTypeCode {
- ids.Add(comment.ID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.ID, comment.Type.HasAttachmentSupport()
+ })
}
// LoadAttachmentsByIssue loads attachments by issue id
@@ -451,13 +416,9 @@ func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
}
func (comments CommentList) getReviewIDs() []int64 {
- ids := make(container.Set[int64], len(comments))
- for _, comment := range comments {
- if comment.ReviewID > 0 {
- ids.Add(comment.ReviewID)
- }
- }
- return ids.Values()
+ return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
+ return comment.ReviewID, comment.ReviewID > 0
+ })
}
func (comments CommentList) loadReviews(ctx context.Context) error {
diff --git a/models/issues/issue_list.go b/models/issues/issue_list.go
index 218891ad35..1b05f0aa35 100644
--- a/models/issues/issue_list.go
+++ b/models/issues/issue_list.go
@@ -74,11 +74,9 @@ func (issues IssueList) LoadRepositories(ctx context.Context) (repo_model.Reposi
}
func (issues IssueList) getPosterIDs() []int64 {
- posterIDs := make(container.Set[int64], len(issues))
- for _, issue := range issues {
- posterIDs.Add(issue.PosterID)
- }
- return posterIDs.Values()
+ return container.FilterSlice(issues, func(issue *Issue) (int64, bool) {
+ return issue.PosterID, true
+ })
}
func (issues IssueList) loadPosters(ctx context.Context) error {
@@ -193,11 +191,9 @@ func (issues IssueList) loadLabels(ctx context.Context) error {
}
func (issues IssueList) getMilestoneIDs() []int64 {
- ids := make(container.Set[int64], len(issues))
- for _, issue := range issues {
- ids.Add(issue.MilestoneID)
- }
- return ids.Values()
+ return container.FilterSlice(issues, func(issue *Issue) (int64, bool) {
+ return issue.MilestoneID, true
+ })
}
func (issues IssueList) loadMilestones(ctx context.Context) error {
diff --git a/models/issues/reaction.go b/models/issues/reaction.go
index d5448636fe..eb7faefc79 100644
--- a/models/issues/reaction.go
+++ b/models/issues/reaction.go
@@ -305,14 +305,12 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
}
func (list ReactionList) getUserIDs() []int64 {
- userIDs := make(container.Set[int64], len(list))
- for _, reaction := range list {
+ return container.FilterSlice(list, func(reaction *Reaction) (int64, bool) {
if reaction.OriginalAuthor != "" {
- continue
+ return 0, false
}
- userIDs.Add(reaction.UserID)
- }
- return userIDs.Values()
+ return reaction.UserID, true
+ })
}
func valuesUser(m map[int64]*user_model.User) []*user_model.User {
diff --git a/models/issues/review_list.go b/models/issues/review_list.go
index ec6cb07988..7b8c3d319c 100644
--- a/models/issues/review_list.go
+++ b/models/issues/review_list.go
@@ -38,12 +38,11 @@ func (reviews ReviewList) LoadReviewers(ctx context.Context) error {
}
func (reviews ReviewList) LoadIssues(ctx context.Context) error {
- issueIDs := container.Set[int64]{}
- for i := 0; i < len(reviews); i++ {
- issueIDs.Add(reviews[i].IssueID)
- }
+ issueIDs := container.FilterSlice(reviews, func(review *Review) (int64, bool) {
+ return review.IssueID, true
+ })
- issues, err := GetIssuesByIDs(ctx, issueIDs.Values())
+ issues, err := GetIssuesByIDs(ctx, issueIDs)
if err != nil {
return err
}