|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package mailer |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "sort" |
| 11 | + |
| 12 | + actions_model "code.gitea.io/gitea/models/actions" |
| 13 | + "code.gitea.io/gitea/models/db" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + user_model "code.gitea.io/gitea/models/user" |
| 16 | + "code.gitea.io/gitea/modules/base" |
| 17 | + "code.gitea.io/gitea/modules/log" |
| 18 | + "code.gitea.io/gitea/modules/setting" |
| 19 | + sender_service "code.gitea.io/gitea/services/mailer/sender" |
| 20 | +) |
| 21 | + |
| 22 | +const tplWorkflowRun = "notify/workflow_run" |
| 23 | + |
| 24 | +func generateMessageIDForActionsWorkflowRunStatusEmail(repo *repo_model.Repository, run *actions_model.ActionRun) string { |
| 25 | + return fmt.Sprintf("<%s/actions/runs/%d@%s>", repo.FullName(), run.Index, setting.Domain) |
| 26 | +} |
| 27 | + |
| 28 | +func sendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun, sender *user_model.User, recipients []*user_model.User) { |
| 29 | + msgs := make([]*sender_service.Message, 0, len(recipients)) |
| 30 | + |
| 31 | + messageID := generateMessageIDForActionsWorkflowRunStatusEmail(repo, run) |
| 32 | + headers := generateMetadataHeaders(repo) |
| 33 | + |
| 34 | + subject := "Run" |
| 35 | + if run.IsForkPullRequest { |
| 36 | + subject = "PR run" |
| 37 | + } |
| 38 | + switch run.Status { |
| 39 | + case actions_model.StatusFailure: |
| 40 | + subject = subject + " failed" |
| 41 | + case actions_model.StatusCancelled: |
| 42 | + subject = subject + " cancelled" |
| 43 | + case actions_model.StatusSuccess: |
| 44 | + subject = subject + " is successful" |
| 45 | + } |
| 46 | + subject = fmt.Sprintf("%s: %s (%s)", subject, run.WorkflowID, base.ShortSha(run.CommitSHA)) |
| 47 | + |
| 48 | + jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID) |
| 49 | + if err != nil { |
| 50 | + log.Error("GetRunJobsByRunID: %v", err) |
| 51 | + } else { |
| 52 | + sort.SliceStable(jobs, func(i, j int) bool { |
| 53 | + si := jobs[i].Status |
| 54 | + sj := jobs[j].Status |
| 55 | + if si.IsSuccess() { |
| 56 | + si = 99 |
| 57 | + } |
| 58 | + if sj.IsSuccess() { |
| 59 | + sj = 99 |
| 60 | + } |
| 61 | + return si < sj |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + var mailBody bytes.Buffer |
| 66 | + if err := bodyTemplates.ExecuteTemplate(&mailBody, tplWorkflowRun, map[string]any{ |
| 67 | + "Subject": subject, |
| 68 | + "Repo": repo, |
| 69 | + "Run": run, |
| 70 | + "Jobs": jobs, |
| 71 | + }); err != nil { |
| 72 | + log.Error("ExecuteTemplate [%s]: %v", tplWorkflowRun, err) |
| 73 | + } |
| 74 | + |
| 75 | + for _, recipient := range recipients { |
| 76 | + msg := sender_service.NewMessageFrom( |
| 77 | + recipient.Email, |
| 78 | + fromDisplayName(sender), |
| 79 | + setting.MailService.FromEmail, |
| 80 | + subject, |
| 81 | + mailBody.String(), |
| 82 | + ) |
| 83 | + msg.Info = subject |
| 84 | + for k, v := range generateSenderRecipientHeaders(sender, recipient) { |
| 85 | + msg.SetHeader(k, v) |
| 86 | + } |
| 87 | + for k, v := range headers { |
| 88 | + msg.SetHeader(k, v) |
| 89 | + } |
| 90 | + msg.SetHeader("Message-ID", messageID) |
| 91 | + msgs = append(msgs, msg) |
| 92 | + } |
| 93 | + SendAsync(msgs...) |
| 94 | +} |
| 95 | + |
| 96 | +func SendActionsWorkflowRunStatusEmail(ctx context.Context, sender *user_model.User, repo *repo_model.Repository, run *actions_model.ActionRun) { |
| 97 | + if setting.MailService == nil { |
| 98 | + return |
| 99 | + } |
| 100 | + if run.Status.IsSkipped() { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + recipients := make([]*user_model.User, 0) |
| 105 | + |
| 106 | + if !sender.IsGiteaActions() && !sender.IsGhost() && sender.IsMailable() { |
| 107 | + if run.Status.IsSuccess() { |
| 108 | + if sender.EmailNotificationsPreference == user_model.EmailNotificationsAndYourOwn { |
| 109 | + recipients = append(recipients, sender) |
| 110 | + } |
| 111 | + sendActionsWorkflowRunStatusEmail(ctx, repo, run, sender, recipients) |
| 112 | + return |
| 113 | + } else if sender.EmailNotificationsPreference != user_model.EmailNotificationsOnMention && |
| 114 | + sender.EmailNotificationsPreference != user_model.EmailNotificationsDisabled { |
| 115 | + recipients = append(recipients, sender) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + watchers, err := repo_model.GetRepoWatchers(ctx, repo.ID, db.ListOptionsAll) |
| 120 | + if err != nil { |
| 121 | + log.Error("GetWatchers: %v", err) |
| 122 | + } |
| 123 | + for _, watcher := range watchers { |
| 124 | + if watcher.ID == sender.ID { |
| 125 | + continue |
| 126 | + } |
| 127 | + if watcher.IsMailable() && watcher.EmailNotificationsPreference != user_model.EmailNotificationsOnMention && |
| 128 | + watcher.EmailNotificationsPreference != user_model.EmailNotificationsDisabled { |
| 129 | + recipients = append(recipients, watcher) |
| 130 | + } |
| 131 | + } |
| 132 | + sendActionsWorkflowRunStatusEmail(ctx, repo, run, sender, recipients) |
| 133 | +} |
0 commit comments