Skip to content

Commit 7b9bd02

Browse files
committed
Merge remote-tracking branch 'upstream/main'
* upstream/main: Add more stats tables (go-gitea#29730) Use Get but not Post to get actions artifacts (go-gitea#29734) Do some performance optimize for issues list and view issue/pull (go-gitea#29515)
2 parents 81d1696 + e5e2b2f commit 7b9bd02

File tree

13 files changed

+130
-65
lines changed

13 files changed

+130
-65
lines changed

models/activities/statistic.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
asymkey_model "code.gitea.io/gitea/models/asymkey"
1010
"code.gitea.io/gitea/models/auth"
1111
"code.gitea.io/gitea/models/db"
12+
git_model "code.gitea.io/gitea/models/git"
1213
issues_model "code.gitea.io/gitea/models/issues"
1314
"code.gitea.io/gitea/models/organization"
1415
access_model "code.gitea.io/gitea/models/perm/access"
@@ -29,7 +30,8 @@ type Statistic struct {
2930
Mirror, Release, AuthSource, Webhook,
3031
Milestone, Label, HookTask,
3132
Team, UpdateTask, Project,
32-
ProjectBoard, Attachment int64
33+
ProjectBoard, Attachment,
34+
Branches, Tags, CommitStatus int64
3335
IssueByLabel []IssueByLabelCount
3436
IssueByRepository []IssueByRepositoryCount
3537
}
@@ -58,6 +60,9 @@ func GetStatistic(ctx context.Context) (stats Statistic) {
5860
stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
5961
stats.Counter.Star, _ = e.Count(new(repo_model.Star))
6062
stats.Counter.Access, _ = e.Count(new(access_model.Access))
63+
stats.Counter.Branches, _ = e.Count(new(git_model.Branch))
64+
stats.Counter.Tags, _ = e.Where("is_draft=?", false).Count(new(repo_model.Release))
65+
stats.Counter.CommitStatus, _ = e.Count(new(git_model.CommitStatus))
6166

6267
type IssueCount struct {
6368
Count int64

models/issues/comment.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ func (c *Comment) LoadTime(ctx context.Context) error {
673673
return err
674674
}
675675

676-
func (c *Comment) loadReactions(ctx context.Context, repo *repo_model.Repository) (err error) {
676+
// LoadReactions loads comment reactions
677+
func (c *Comment) LoadReactions(ctx context.Context, repo *repo_model.Repository) (err error) {
677678
if c.Reactions != nil {
678679
return nil
679680
}
@@ -691,11 +692,6 @@ func (c *Comment) loadReactions(ctx context.Context, repo *repo_model.Repository
691692
return nil
692693
}
693694

694-
// LoadReactions loads comment reactions
695-
func (c *Comment) LoadReactions(ctx context.Context, repo *repo_model.Repository) error {
696-
return c.loadReactions(ctx, repo)
697-
}
698-
699695
func (c *Comment) loadReview(ctx context.Context) (err error) {
700696
if c.ReviewID == 0 {
701697
return nil

models/issues/comment_code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
122122
}
123123

124124
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
125-
func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) ([]*Comment, error) {
125+
func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) (CommentList, error) {
126126
opts := FindCommentsOptions{
127127
Type: CommentTypeCode,
128128
IssueID: issue.ID,

models/issues/comment_list.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ type CommentList []*Comment
1919
func (comments CommentList) getPosterIDs() []int64 {
2020
posterIDs := make(container.Set[int64], len(comments))
2121
for _, comment := range comments {
22-
posterIDs.Add(comment.PosterID)
22+
if comment.PosterID > 0 {
23+
posterIDs.Add(comment.PosterID)
24+
}
2325
}
2426
return posterIDs.Values()
2527
}
@@ -41,18 +43,12 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
4143
return nil
4244
}
4345

44-
func (comments CommentList) getCommentIDs() []int64 {
45-
ids := make([]int64, 0, len(comments))
46-
for _, comment := range comments {
47-
ids = append(ids, comment.ID)
48-
}
49-
return ids
50-
}
51-
5246
func (comments CommentList) getLabelIDs() []int64 {
5347
ids := make(container.Set[int64], len(comments))
5448
for _, comment := range comments {
55-
ids.Add(comment.LabelID)
49+
if comment.LabelID > 0 {
50+
ids.Add(comment.LabelID)
51+
}
5652
}
5753
return ids.Values()
5854
}
@@ -100,7 +96,9 @@ func (comments CommentList) loadLabels(ctx context.Context) error {
10096
func (comments CommentList) getMilestoneIDs() []int64 {
10197
ids := make(container.Set[int64], len(comments))
10298
for _, comment := range comments {
103-
ids.Add(comment.MilestoneID)
99+
if comment.MilestoneID > 0 {
100+
ids.Add(comment.MilestoneID)
101+
}
104102
}
105103
return ids.Values()
106104
}
@@ -141,7 +139,9 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
141139
func (comments CommentList) getOldMilestoneIDs() []int64 {
142140
ids := make(container.Set[int64], len(comments))
143141
for _, comment := range comments {
144-
ids.Add(comment.OldMilestoneID)
142+
if comment.OldMilestoneID > 0 {
143+
ids.Add(comment.OldMilestoneID)
144+
}
145145
}
146146
return ids.Values()
147147
}
@@ -182,7 +182,9 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
182182
func (comments CommentList) getAssigneeIDs() []int64 {
183183
ids := make(container.Set[int64], len(comments))
184184
for _, comment := range comments {
185-
ids.Add(comment.AssigneeID)
185+
if comment.AssigneeID > 0 {
186+
ids.Add(comment.AssigneeID)
187+
}
186188
}
187189
return ids.Values()
188190
}
@@ -314,7 +316,9 @@ func (comments CommentList) getDependentIssueIDs() []int64 {
314316
if comment.DependentIssue != nil {
315317
continue
316318
}
317-
ids.Add(comment.DependentIssueID)
319+
if comment.DependentIssueID > 0 {
320+
ids.Add(comment.DependentIssueID)
321+
}
318322
}
319323
return ids.Values()
320324
}
@@ -369,23 +373,57 @@ func (comments CommentList) loadDependentIssues(ctx context.Context) error {
369373
return nil
370374
}
371375

376+
// getAttachmentCommentIDs only return the comment ids which possibly has attachments
377+
func (comments CommentList) getAttachmentCommentIDs() []int64 {
378+
ids := make(container.Set[int64], len(comments))
379+
for _, comment := range comments {
380+
if comment.Type == CommentTypeComment ||
381+
comment.Type == CommentTypeReview ||
382+
comment.Type == CommentTypeCode {
383+
ids.Add(comment.ID)
384+
}
385+
}
386+
return ids.Values()
387+
}
388+
389+
// LoadAttachmentsByIssue loads attachments by issue id
390+
func (comments CommentList) LoadAttachmentsByIssue(ctx context.Context) error {
391+
if len(comments) == 0 {
392+
return nil
393+
}
394+
395+
attachments := make([]*repo_model.Attachment, 0, len(comments)/2)
396+
if err := db.GetEngine(ctx).Where("issue_id=? AND comment_id>0", comments[0].IssueID).Find(&attachments); err != nil {
397+
return err
398+
}
399+
400+
commentAttachmentsMap := make(map[int64][]*repo_model.Attachment, len(comments))
401+
for _, attach := range attachments {
402+
commentAttachmentsMap[attach.CommentID] = append(commentAttachmentsMap[attach.CommentID], attach)
403+
}
404+
405+
for _, comment := range comments {
406+
comment.Attachments = commentAttachmentsMap[comment.ID]
407+
}
408+
return nil
409+
}
410+
372411
// LoadAttachments loads attachments
373412
func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
374413
if len(comments) == 0 {
375414
return nil
376415
}
377416

378417
attachments := make(map[int64][]*repo_model.Attachment, len(comments))
379-
commentsIDs := comments.getCommentIDs()
418+
commentsIDs := comments.getAttachmentCommentIDs()
380419
left := len(commentsIDs)
381420
for left > 0 {
382421
limit := db.DefaultMaxInSize
383422
if left < limit {
384423
limit = left
385424
}
386-
rows, err := db.GetEngine(ctx).Table("attachment").
387-
Join("INNER", "comment", "comment.id = attachment.comment_id").
388-
In("comment.id", commentsIDs[:limit]).
425+
rows, err := db.GetEngine(ctx).
426+
In("comment_id", commentsIDs[:limit]).
389427
Rows(new(repo_model.Attachment))
390428
if err != nil {
391429
return err
@@ -415,7 +453,9 @@ func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
415453
func (comments CommentList) getReviewIDs() []int64 {
416454
ids := make(container.Set[int64], len(comments))
417455
for _, comment := range comments {
418-
ids.Add(comment.ReviewID)
456+
if comment.ReviewID > 0 {
457+
ids.Add(comment.ReviewID)
458+
}
419459
}
420460
return ids.Values()
421461
}

models/issues/issue_list.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,8 @@ func (issues IssueList) LoadAttachments(ctx context.Context) (err error) {
388388
if left < limit {
389389
limit = left
390390
}
391-
rows, err := db.GetEngine(ctx).Table("attachment").
392-
Join("INNER", "issue", "issue.id = attachment.issue_id").
393-
In("issue.id", issuesIDs[:limit]).
391+
rows, err := db.GetEngine(ctx).
392+
In("issue_id", issuesIDs[:limit]).
394393
Rows(new(repo_model.Attachment))
395394
if err != nil {
396395
return err
@@ -609,3 +608,23 @@ func (issues IssueList) GetApprovalCounts(ctx context.Context) (map[int64][]*Rev
609608

610609
return approvalCountMap, nil
611610
}
611+
612+
func (issues IssueList) LoadIsRead(ctx context.Context, userID int64) error {
613+
issueIDs := issues.getIssueIDs()
614+
issueUsers := make([]*IssueUser, 0, len(issueIDs))
615+
if err := db.GetEngine(ctx).Where("uid =?", userID).
616+
In("issue_id").
617+
Find(&issueUsers); err != nil {
618+
return err
619+
}
620+
621+
for _, issueUser := range issueUsers {
622+
for _, issue := range issues {
623+
if issue.ID == issueUser.IssueID {
624+
issue.IsRead = issueUser.IsRead
625+
}
626+
}
627+
}
628+
629+
return nil
630+
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ var migrations = []Migration{
566566
NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultWikiBranch),
567567
// v290 -> v291
568568
NewMigration("Add PayloadVersion to HookTask", v1_22.AddPayloadVersionToHookTaskTable),
569+
// v291 -> v292
570+
NewMigration("Add Index to attachment.comment_id", v1_22.AddCommentIDIndexofAttachment),
569571
}
570572

571573
// GetCurrentDBVersion returns the current db version

models/migrations/v1_22/v291.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_22 //nolint
5+
6+
import "xorm.io/xorm"
7+
8+
func AddCommentIDIndexofAttachment(x *xorm.Engine) error {
9+
type Attachment struct {
10+
CommentID int64 `xorm:"INDEX"`
11+
}
12+
13+
return x.Sync(&Attachment{})
14+
}

models/repo/attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Attachment struct {
2424
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
2525
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
2626
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
27-
CommentID int64
27+
CommentID int64 `xorm:"INDEX"`
2828
Name string
2929
DownloadCount int64 `xorm:"DEFAULT 0"`
3030
Size int64 `xorm:"DEFAULT 0"`

routers/api/v1/repo/issue_comment.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,6 @@ func ListRepoIssueComments(ctx *context.APIContext) {
323323
ctx.Error(http.StatusInternalServerError, "LoadIssues", err)
324324
return
325325
}
326-
if err := comments.LoadPosters(ctx); err != nil {
327-
ctx.Error(http.StatusInternalServerError, "LoadPosters", err)
328-
return
329-
}
330326
if err := comments.LoadAttachments(ctx); err != nil {
331327
ctx.Error(http.StatusInternalServerError, "LoadAttachments", err)
332328
return

routers/web/repo/issue.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,15 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
324324
return
325325
}
326326

327-
// Get posters.
328-
for i := range issues {
329-
// Check read status
330-
if !ctx.IsSigned {
331-
issues[i].IsRead = true
332-
} else if err = issues[i].GetIsRead(ctx, ctx.Doer.ID); err != nil {
333-
ctx.ServerError("GetIsRead", err)
327+
if ctx.IsSigned {
328+
if err := issues.LoadIsRead(ctx, ctx.Doer.ID); err != nil {
329+
ctx.ServerError("LoadIsRead", err)
334330
return
335331
}
332+
} else {
333+
for i := range issues {
334+
issues[i].IsRead = true
335+
}
336336
}
337337

338338
commitStatuses, lastStatus, err := pull_service.GetIssuesAllCommitStatus(ctx, issues)
@@ -1604,20 +1604,20 @@ func ViewIssue(ctx *context.Context) {
16041604

16051605
// Render comments and and fetch participants.
16061606
participants[0] = issue.Poster
1607+
1608+
if err := issue.Comments.LoadAttachmentsByIssue(ctx); err != nil {
1609+
ctx.ServerError("LoadAttachmentsByIssue", err)
1610+
return
1611+
}
1612+
if err := issue.Comments.LoadPosters(ctx); err != nil {
1613+
ctx.ServerError("LoadPosters", err)
1614+
return
1615+
}
1616+
16071617
for _, comment = range issue.Comments {
16081618
comment.Issue = issue
16091619

1610-
if err := comment.LoadPoster(ctx); err != nil {
1611-
ctx.ServerError("LoadPoster", err)
1612-
return
1613-
}
1614-
16151620
if comment.Type == issues_model.CommentTypeComment || comment.Type == issues_model.CommentTypeReview {
1616-
if err := comment.LoadAttachments(ctx); err != nil {
1617-
ctx.ServerError("LoadAttachments", err)
1618-
return
1619-
}
1620-
16211621
comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
16221622
Links: markup.Links{
16231623
Base: ctx.Repo.RepoLink,
@@ -1665,7 +1665,6 @@ func ViewIssue(ctx *context.Context) {
16651665
comment.Milestone = ghostMilestone
16661666
}
16671667
} else if comment.Type == issues_model.CommentTypeProject {
1668-
16691668
if err = comment.LoadProject(ctx); err != nil {
16701669
ctx.ServerError("LoadProject", err)
16711670
return
@@ -1731,10 +1730,6 @@ func ViewIssue(ctx *context.Context) {
17311730
for _, codeComments := range comment.Review.CodeComments {
17321731
for _, lineComments := range codeComments {
17331732
for _, c := range lineComments {
1734-
if err := c.LoadAttachments(ctx); err != nil {
1735-
ctx.ServerError("LoadAttachments", err)
1736-
return
1737-
}
17381733
// Check tag.
17391734
role, ok = marked[c.PosterID]
17401735
if ok {

routers/web/repo/pull_review.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,9 @@ func renderConversation(ctx *context.Context, comment *issues_model.Comment, ori
179179
return
180180
}
181181

182-
for _, c := range comments {
183-
if err := c.LoadAttachments(ctx); err != nil {
184-
ctx.ServerError("LoadAttachments", err)
185-
return
186-
}
182+
if err := comments.LoadAttachments(ctx); err != nil {
183+
ctx.ServerError("LoadAttachments", err)
184+
return
187185
}
188186

189187
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ func registerRoutes(m *web.Route) {
13741374
})
13751375
m.Post("/cancel", reqRepoActionsWriter, actions.Cancel)
13761376
m.Post("/approve", reqRepoActionsWriter, actions.Approve)
1377-
m.Post("/artifacts", actions.ArtifactsView)
1377+
m.Get("/artifacts", actions.ArtifactsView)
13781378
m.Get("/artifacts/{artifact_name}", actions.ArtifactsDownloadView)
13791379
m.Delete("/artifacts/{artifact_name}", actions.ArtifactsDeleteView)
13801380
m.Post("/rerun", reqRepoActionsWriter, actions.Rerun)

0 commit comments

Comments
 (0)