This commit is contained in:
Lunny Xiao 2025-06-20 10:55:30 -07:00
parent 6b055ddb9f
commit 803a3a4426
4 changed files with 90 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import (
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
@ -70,6 +71,7 @@ type Notification struct {
Comment *issues_model.Comment `xorm:"-"`
User *user_model.User `xorm:"-"`
Release *repo_model.Release `xorm:"-"`
Commit *git.Commit `xorm:"-"`
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`

View File

@ -13,6 +13,8 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
@ -457,12 +459,25 @@ func (nl NotificationList) LoadComments(ctx context.Context) ([]int, error) {
return failures, nil
}
func (nl NotificationList) getPendingReleaseIDs() []int64 {
ids := make(container.Set[int64], len(nl))
for _, notification := range nl {
if notification.Release != nil {
continue
}
if notification.ReleaseID > 0 {
ids.Add(notification.ReleaseID)
}
}
return ids.Values()
}
func (nl NotificationList) LoadReleases(ctx context.Context) ([]int, error) {
if len(nl) == 0 {
return []int{}, nil
}
releaseIDs := nl.getPendingCommentIDs()
releaseIDs := nl.getPendingReleaseIDs()
releases := make(map[int64]*repo_model.Release, len(releaseIDs))
if err := db.GetEngine(ctx).In("id", releaseIDs).Find(&releases); err != nil {
return nil, err
@ -482,6 +497,50 @@ func (nl NotificationList) LoadReleases(ctx context.Context) ([]int, error) {
return failures, nil
}
func (nl NotificationList) LoadCommits(ctx context.Context) ([]int, error) {
if len(nl) == 0 {
return []int{}, nil
}
_, _, err := nl.LoadRepos(ctx)
if err != nil {
return nil, err
}
failures := []int{}
repos := make(map[int64]*git.Repository, len(nl))
for i, n := range nl {
if n.Source != NotificationSourceCommit || n.CommitID == "" {
continue
}
repo, ok := repos[n.RepoID]
if !ok {
repo, err = gitrepo.OpenRepository(ctx, n.Repository)
if err != nil {
log.Error("Notification[%d]: Failed to get repo for commit %s: %v", n.ID, n.CommitID, err)
failures = append(failures, i)
continue
}
repos[n.RepoID] = repo
}
n.Commit, err = repo.GetCommit(n.CommitID)
if err != nil {
log.Error("Notification[%d]: Failed to get repo for commit %s: %v", n.ID, n.CommitID, err)
failures = append(failures, i)
continue
}
}
for _, repo := range repos {
if err := repo.Close(); err != nil {
log.Error("Failed to close repository: %v", err)
}
}
return failures, nil
}
// LoadIssuePullRequests loads all issues' pull requests if possible
func (nl NotificationList) LoadIssuePullRequests(ctx context.Context) error {
issues := make(map[int64]*issues_model.Issue, len(nl))

View File

@ -137,6 +137,22 @@ func getNotifications(ctx *context.Context) {
notifications = notifications.Without(failures)
failCount += len(failures)
failures, err = notifications.LoadCommits(ctx)
if err != nil {
ctx.ServerError("LoadCommits", err)
return
}
notifications = notifications.Without(failures)
failCount += len(failures)
failures, err = notifications.LoadReleases(ctx)
if err != nil {
ctx.ServerError("LoadReleases", err)
return
}
notifications = notifications.Without(failures)
failCount += len(failures)
if failCount > 0 {
ctx.Flash.Error(fmt.Sprintf("ERROR: %d notifications were removed due to missing parts - check the logs", failCount))
}

View File

@ -39,6 +39,10 @@
<div class="notifications-icon tw-ml-2 tw-mr-1 tw-self-start tw-mt-1">
{{if .Issue}}
{{template "shared/issueicon" .Issue}}
{{else if .Release}}
{{svg "octicon-tag" 16 "text grey"}}
{{else if .Commit}}
{{svg "octicon-git-commit" 16 "text grey"}}
{{else}}
{{svg "octicon-repo" 16 "text grey"}}
{{end}}
@ -54,6 +58,10 @@
<span class="issue-title tw-break-anywhere">
{{if .Issue}}
{{.Issue.Title | ctx.RenderUtils.RenderIssueSimpleTitle}}
{{else if .Release}}
{{.Release.Title}}
{{else if .Commit}}
{{.Commit.Summary}}
{{else}}
{{.Repository.FullName}}
{{end}}
@ -63,6 +71,10 @@
<div class="notifications-updated tw-items-center tw-mr-2">
{{if .Issue}}
{{DateUtils.TimeSince .Issue.UpdatedUnix}}
{{else if .Release}}
{{DateUtils.TimeSince .Release.UpdatedUnix}}
{{else if .Commit}}
{{DateUtils.TimeSince .Commit.Committer.When}}
{{else}}
{{DateUtils.TimeSince .UpdatedUnix}}
{{end}}