0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-13 00:16:07 +02:00

add notifications for commit comments

notify the commit author (resolved by email) and @mentioned users
when a new commit comment is created. uses the existing
NotificationSourceCommit source and follows the same pattern as
CreateRepoTransferNotification.
This commit is contained in:
yuvrajangadsingh 2026-03-14 15:34:50 +05:30
parent ce204ad532
commit 0084880f6a
No known key found for this signature in database
2 changed files with 51 additions and 0 deletions

View File

@ -115,6 +115,49 @@ func init() {
db.RegisterModel(new(Notification))
}
// CreateCommitCommentNotification creates notifications for a commit comment.
// It notifies the commit author (if they're a Gitea user) and any @mentioned users.
func CreateCommitCommentNotification(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, comment *issues_model.Comment, commitAuthorEmail string, mentionedUsernames []string) error {
return db.WithTx(ctx, func(ctx context.Context) error {
receiverIDs := make(map[int64]struct{})
// Notify the commit author if they map to a Gitea user
if commitAuthorEmail != "" {
author, err := user_model.GetUserByEmail(ctx, commitAuthorEmail)
if err == nil && author.ID != doer.ID {
receiverIDs[author.ID] = struct{}{}
}
}
// Notify @mentioned users
for _, username := range mentionedUsernames {
mentioned, err := user_model.GetUserByName(ctx, username)
if err == nil && mentioned.ID != doer.ID {
receiverIDs[mentioned.ID] = struct{}{}
}
}
if len(receiverIDs) == 0 {
return nil
}
var notifications []*Notification
for uid := range receiverIDs {
notifications = append(notifications, &Notification{
UserID: uid,
RepoID: repo.ID,
Status: NotificationStatusUnread,
Source: NotificationSourceCommit,
CommitID: comment.CommitSHA,
CommentID: comment.ID,
UpdatedBy: doer.ID,
})
}
return db.Insert(ctx, notifications)
})
}
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) error {
return db.WithTx(ctx, func(ctx context.Context) error {

View File

@ -6,11 +6,13 @@ package repo
import (
"net/http"
activities_model "code.gitea.io/gitea/models/activities"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/renderhelper"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/services/context"
@ -109,6 +111,12 @@ func CreateCommitComment(ctx *context.Context) {
return
}
// Send notifications to commit author and @mentioned users
mentions := references.FindAllMentionsMarkdown(content)
if err := activities_model.CreateCommitCommentNotification(ctx, ctx.Doer, ctx.Repo.Repository, comment, commit.Author.Email, mentions); err != nil {
log.Error("CreateCommitCommentNotification: %v", err)
}
// Render markdown content
rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository, renderhelper.RepoCommentOptions{})
comment.RenderedContent, err = markdown.RenderString(rctx, comment.Content)