Skip to content

Commit

Permalink
Update notify.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank77maruti authored Feb 23, 2025
1 parent 370284c commit 8184bfd
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions services/mailer/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,11 @@ func (m *mailNotifier) NewRelease(ctx context.Context, rel *repo_model.Release)
MailNewRelease(ctx, rel)
}

func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) {
if err := SendRepoTransferNotifyMail(ctx, doer, newOwner, repo); err != nil {
log.Error("SendRepoTransferNotifyMail: %v", err)
}
}

func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {
// Check status first to avoid unnecessary processing
if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure {
return
}

// Load required attributes after status check
if err := run.LoadAttributes(ctx); err != nil {
log.Error("LoadAttributes: %v", err)
return
Expand All @@ -222,19 +214,16 @@ func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model
run.Status,
)

// Safely handle short commit SHA
commitSHA := run.CommitSHA
if len(commitSHA) > 7 {
commitSHA = commitSHA[:7]
}

body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s
Repository: %s
Branch: %s
Commit: %s
Triggered by: %s
View the run details here: %s`,
run.WorkflowID,
run.Index,
Expand All @@ -246,19 +235,27 @@ View the run details here: %s`,
run.HTMLURL(),
)

// Send to repo owner if notifications enabled and email present
// Send to repo owner
if run.Repo.Owner.Email != "" &&
run.Repo.Owner.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
if err := SendMailFrom(ctx, run.Repo.Owner.Email, subject, body); err != nil {
if err := mailer.SendAsyncEmail(ctx, mailer.Mail{
To: []string{run.Repo.Owner.Email},
Subject: subject,
Body: body,
}); err != nil {
log.Error("Failed to send email to repo owner %s: %v", run.Repo.Owner.Email, err)
}
}

// Send to trigger user if different from owner and notifications enabled
// Send to trigger user
if run.TriggerUser.ID != run.Repo.Owner.ID &&
run.TriggerUser.Email != "" &&
run.TriggerUser.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
if err := SendMailFrom(ctx, run.TriggerUser.Email, subject, body); err != nil {
if err := mailer.SendAsyncEmail(ctx, mailer.Mail{
To: []string{run.TriggerUser.Email},
Subject: subject,
Body: body,
}); err != nil {
log.Error("Failed to send email to trigger user %s: %v", run.TriggerUser.Email, err)
}
}
Expand Down

0 comments on commit 8184bfd

Please sign in to comment.