Skip to content

Commit 95ee4c6

Browse files
Notify action added
Signed-off-by: Mayank Mohapatra <[email protected]>
1 parent 70f02e0 commit 95ee4c6

File tree

1 file changed

+57
-61
lines changed

1 file changed

+57
-61
lines changed

services/mailer/notify.go

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -204,66 +204,62 @@ func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *
204204
}
205205
}
206206

207-
// func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {
208-
// // Check status first to avoid unnecessary processing
209-
// if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure {
210-
// return
211-
// }
212-
213-
// // Load required attributes after status check
214-
// if err := run.LoadAttributes(ctx); err != nil {
215-
// log.Error("LoadAttributes: %v", err)
216-
// return
217-
// }
218-
219-
// subject := fmt.Sprintf("[%s] Workflow run %s: %s",
220-
// run.Repo.FullName(),
221-
// run.WorkflowName,
222-
// run.Status,
223-
// )
224-
225-
// // Safely handle short commit SHA
226-
// commitSHA := run.CommitSHA
227-
// if len(commitSHA) > 7 {
228-
// commitSHA = commitSHA[:7]
229-
// }
230-
231-
// body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s
232-
233-
// Repository: %s
234-
// Branch: %s
235-
// Commit: %s
236-
// Triggered by: %s
237-
238-
// View the run details here: %s`,
239-
// run.WorkflowName,
240-
// run.Index,
241-
// run.Status,
242-
// run.Repo.FullName(),
243-
// run.RefName,
244-
// commitSHA,
245-
// run.TriggerUser.Name,
246-
// run.HTMLURL(),
247-
// )
248-
249-
// // Send to repo owner if notifications enabled and email present
250-
// if run.Repo.Owner.Email != "" &&
251-
// run.Repo.Owner.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
252-
// if err := SendMail(ctx, []string{run.Repo.Owner.Email}, subject, body); err != nil {
253-
// log.Error("Failed to send email to repo owner %s: %v", run.Repo.Owner.Email, err)
254-
// }
255-
// }
256-
257-
// // Send to commit author if different from trigger user and notifications enabled
258-
// if run.TriggerUser.ID != run.CommitAuthor.ID &&
259-
// run.CommitAuthor.Email != "" &&
260-
// run.CommitAuthor.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
261-
// if err := SendMail(ctx, []string{run.CommitAuthor.Email}, subject, body); err != nil {
262-
// log.Error("Failed to send email to commit author %s: %v", run.CommitAuthor.Email, err)
263-
// }
264-
// }
265-
// }
266-
267207
func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {
268-
// TODO: send email to related users
208+
// Check status first to avoid unnecessary processing
209+
if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure {
210+
return
211+
}
212+
213+
// Load required attributes after status check
214+
if err := run.LoadAttributes(ctx); err != nil {
215+
log.Error("LoadAttributes: %v", err)
216+
return
217+
}
218+
219+
subject := fmt.Sprintf("[%s] Workflow run %s: %s",
220+
run.Repo.FullName(),
221+
run.WorkflowName,
222+
run.Status,
223+
)
224+
225+
// Safely handle short commit SHA
226+
commitSHA := run.CommitSHA
227+
if len(commitSHA) > 7 {
228+
commitSHA = commitSHA[:7]
229+
}
230+
231+
body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s
232+
233+
Repository: %s
234+
Branch: %s
235+
Commit: %s
236+
Triggered by: %s
237+
238+
View the run details here: %s`,
239+
run.WorkflowName,
240+
run.Index,
241+
run.Status,
242+
run.Repo.FullName(),
243+
run.RefName,
244+
commitSHA,
245+
run.TriggerUser.Name,
246+
run.HTMLURL(),
247+
)
248+
249+
// Send to repo owner if notifications enabled and email present
250+
if run.Repo.Owner.Email != "" &&
251+
run.Repo.Owner.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
252+
if err := SendMail(ctx, []string{run.Repo.Owner.Email}, subject, body); err != nil {
253+
log.Error("Failed to send email to repo owner %s: %v", run.Repo.Owner.Email, err)
254+
}
255+
}
256+
257+
// Send to commit author if different from trigger user and notifications enabled
258+
if run.TriggerUser.ID != run.CommitAuthor.ID &&
259+
run.CommitAuthor.Email != "" &&
260+
run.CommitAuthor.EmailNotificationsPreference != user_model.EmailNotificationsDisabled {
261+
if err := SendMail(ctx, []string{run.CommitAuthor.Email}, subject, body); err != nil {
262+
log.Error("Failed to send email to commit author %s: %v", run.CommitAuthor.Email, err)
263+
}
264+
}
269265
}

0 commit comments

Comments
 (0)