Skip to content

Commit 803a3a4

Browse files
committed
fix
1 parent 6b055dd commit 803a3a4

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

models/activities/notification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
issues_model "code.gitea.io/gitea/models/issues"
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
16+
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/setting"
1718
"code.gitea.io/gitea/modules/timeutil"
1819

@@ -70,6 +71,7 @@ type Notification struct {
7071
Comment *issues_model.Comment `xorm:"-"`
7172
User *user_model.User `xorm:"-"`
7273
Release *repo_model.Release `xorm:"-"`
74+
Commit *git.Commit `xorm:"-"`
7375

7476
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
7577
UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`

models/activities/notification_list.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"code.gitea.io/gitea/models/unit"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/container"
16+
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/gitrepo"
1618
"code.gitea.io/gitea/modules/log"
1719
"code.gitea.io/gitea/modules/util"
1820

@@ -457,12 +459,25 @@ func (nl NotificationList) LoadComments(ctx context.Context) ([]int, error) {
457459
return failures, nil
458460
}
459461

462+
func (nl NotificationList) getPendingReleaseIDs() []int64 {
463+
ids := make(container.Set[int64], len(nl))
464+
for _, notification := range nl {
465+
if notification.Release != nil {
466+
continue
467+
}
468+
if notification.ReleaseID > 0 {
469+
ids.Add(notification.ReleaseID)
470+
}
471+
}
472+
return ids.Values()
473+
}
474+
460475
func (nl NotificationList) LoadReleases(ctx context.Context) ([]int, error) {
461476
if len(nl) == 0 {
462477
return []int{}, nil
463478
}
464479

465-
releaseIDs := nl.getPendingCommentIDs()
480+
releaseIDs := nl.getPendingReleaseIDs()
466481
releases := make(map[int64]*repo_model.Release, len(releaseIDs))
467482
if err := db.GetEngine(ctx).In("id", releaseIDs).Find(&releases); err != nil {
468483
return nil, err
@@ -482,6 +497,50 @@ func (nl NotificationList) LoadReleases(ctx context.Context) ([]int, error) {
482497
return failures, nil
483498
}
484499

500+
func (nl NotificationList) LoadCommits(ctx context.Context) ([]int, error) {
501+
if len(nl) == 0 {
502+
return []int{}, nil
503+
}
504+
505+
_, _, err := nl.LoadRepos(ctx)
506+
if err != nil {
507+
return nil, err
508+
}
509+
510+
failures := []int{}
511+
repos := make(map[int64]*git.Repository, len(nl))
512+
for i, n := range nl {
513+
if n.Source != NotificationSourceCommit || n.CommitID == "" {
514+
continue
515+
}
516+
517+
repo, ok := repos[n.RepoID]
518+
if !ok {
519+
repo, err = gitrepo.OpenRepository(ctx, n.Repository)
520+
if err != nil {
521+
log.Error("Notification[%d]: Failed to get repo for commit %s: %v", n.ID, n.CommitID, err)
522+
failures = append(failures, i)
523+
continue
524+
}
525+
repos[n.RepoID] = repo
526+
}
527+
n.Commit, err = repo.GetCommit(n.CommitID)
528+
if err != nil {
529+
log.Error("Notification[%d]: Failed to get repo for commit %s: %v", n.ID, n.CommitID, err)
530+
failures = append(failures, i)
531+
continue
532+
}
533+
}
534+
535+
for _, repo := range repos {
536+
if err := repo.Close(); err != nil {
537+
log.Error("Failed to close repository: %v", err)
538+
}
539+
}
540+
541+
return failures, nil
542+
}
543+
485544
// LoadIssuePullRequests loads all issues' pull requests if possible
486545
func (nl NotificationList) LoadIssuePullRequests(ctx context.Context) error {
487546
issues := make(map[int64]*issues_model.Issue, len(nl))

routers/web/user/notification.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ func getNotifications(ctx *context.Context) {
137137
notifications = notifications.Without(failures)
138138
failCount += len(failures)
139139

140+
failures, err = notifications.LoadCommits(ctx)
141+
if err != nil {
142+
ctx.ServerError("LoadCommits", err)
143+
return
144+
}
145+
notifications = notifications.Without(failures)
146+
failCount += len(failures)
147+
148+
failures, err = notifications.LoadReleases(ctx)
149+
if err != nil {
150+
ctx.ServerError("LoadReleases", err)
151+
return
152+
}
153+
notifications = notifications.Without(failures)
154+
failCount += len(failures)
155+
140156
if failCount > 0 {
141157
ctx.Flash.Error(fmt.Sprintf("ERROR: %d notifications were removed due to missing parts - check the logs", failCount))
142158
}

templates/user/notification/notification_div.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<div class="notifications-icon tw-ml-2 tw-mr-1 tw-self-start tw-mt-1">
4040
{{if .Issue}}
4141
{{template "shared/issueicon" .Issue}}
42+
{{else if .Release}}
43+
{{svg "octicon-tag" 16 "text grey"}}
44+
{{else if .Commit}}
45+
{{svg "octicon-git-commit" 16 "text grey"}}
4246
{{else}}
4347
{{svg "octicon-repo" 16 "text grey"}}
4448
{{end}}
@@ -54,6 +58,10 @@
5458
<span class="issue-title tw-break-anywhere">
5559
{{if .Issue}}
5660
{{.Issue.Title | ctx.RenderUtils.RenderIssueSimpleTitle}}
61+
{{else if .Release}}
62+
{{.Release.Title}}
63+
{{else if .Commit}}
64+
{{.Commit.Summary}}
5765
{{else}}
5866
{{.Repository.FullName}}
5967
{{end}}
@@ -63,6 +71,10 @@
6371
<div class="notifications-updated tw-items-center tw-mr-2">
6472
{{if .Issue}}
6573
{{DateUtils.TimeSince .Issue.UpdatedUnix}}
74+
{{else if .Release}}
75+
{{DateUtils.TimeSince .Release.UpdatedUnix}}
76+
{{else if .Commit}}
77+
{{DateUtils.TimeSince .Commit.Committer.When}}
6678
{{else}}
6779
{{DateUtils.TimeSince .UpdatedUnix}}
6880
{{end}}

0 commit comments

Comments
 (0)