Skip to content

Commit bf468fe

Browse files
committed
MAILER
1 parent 1f6c68d commit bf468fe

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

services/mailer/mail_workflow_run.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package mailer
55

66
import (
77
"bytes"
8+
api "code.gitea.io/gitea/modules/structs"
9+
"code.gitea.io/gitea/services/convert"
810
"context"
911
"fmt"
1012
"sort"
@@ -45,13 +47,13 @@ func sendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Rep
4547
}
4648
subject = fmt.Sprintf("%s: %s (%s)", subject, run.WorkflowID, base.ShortSha(run.CommitSHA))
4749

48-
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
50+
jobs0, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
4951
if err != nil {
5052
log.Error("GetRunJobsByRunID: %v", err)
5153
} else {
52-
sort.SliceStable(jobs, func(i, j int) bool {
53-
si := jobs[i].Status
54-
sj := jobs[j].Status
54+
sort.SliceStable(jobs0, func(i, j int) bool {
55+
si := jobs0[i].Status
56+
sj := jobs0[j].Status
5557
if si.IsSuccess() {
5658
si = 99
5759
}
@@ -61,6 +63,15 @@ func sendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Rep
6163
return si < sj
6264
})
6365
}
66+
convertedJobs0 := make([]*api.ActionWorkflowJob, 0, len(jobs0))
67+
for _, job := range jobs0 {
68+
c, err := convert.ToActionWorkflowJob(ctx, repo, nil, job)
69+
if err != nil {
70+
log.Error("convert.ToActionWorkflowJob: %v", err)
71+
continue
72+
}
73+
convertedJobs0 = append(convertedJobs0, c)
74+
}
6475

6576
displayName := fromDisplayName(sender)
6677

@@ -70,14 +81,24 @@ func sendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Rep
7081
}
7182
for lang, tos := range langMap {
7283
locale := translation.NewLocale(lang)
84+
var runStatusText string
85+
switch run.Status {
86+
case actions_model.StatusSuccess:
87+
runStatusText = locale.TrString("actions.status.success")
88+
case actions_model.StatusFailure:
89+
runStatusText = locale.TrString("actions.status.failure")
90+
case actions_model.StatusCancelled:
91+
runStatusText = locale.TrString("actions.status.cancelled")
92+
}
7393
var mailBody bytes.Buffer
7494
if err := bodyTemplates.ExecuteTemplate(&mailBody, tplWorkflowRun, map[string]any{
75-
"Subject": subject,
76-
"Repo": repo,
77-
"Run": run,
78-
"Jobs": jobs,
79-
"locale": locale,
80-
"Language": locale.Language(),
95+
"Subject": subject,
96+
"Repo": repo,
97+
"Run": run,
98+
"RunStatusText": runStatusText,
99+
"Jobs": convertedJobs0,
100+
"locale": locale,
101+
"Language": locale.Language(),
81102
}); err != nil {
82103
log.Error("ExecuteTemplate [%s]: %v", tplWorkflowRun, err)
83104
}

templates/mail/notify/workflow_run.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
</head>
1111
<body>
1212
<h4>
13-
{{.Repo.FullName}} {{.Run.WorkflowID}}: {{if .Run.Status.IsSuccess}}{{.locale.Tr "actions.status.success"}}{{else if .Run.Status.IsFailure}}{{.locale.Tr "actions.status.failure"}}{{else if .Run.Status.IsCancelled}}{{.locale.Tr "actions.status.cancelled"}}{{else}}{{.locale.Tr "actions.status.skipped"}}{{end}}
13+
{{.Repo.FullName}} {{.Run.WorkflowID}}: {{.RunStatusText}}
1414
</h4>
1515
<ul>
1616
{{range $job := .Jobs}}
1717
<li>
18-
{{if $job.Status.IsSuccess}}{{.locale.Tr "actions.status.success"}}{{else if $job.Status.IsFailure}}{{.locale.Tr "actions.status.failure"}}{{else if $job.Status.IsCancelled}}{{.locale.Tr "actions.status.cancelled"}}{{else}}{{.locale.Tr "actions.status.skipped"}}{{end}}: {{$job.Name}}{{if gt $job.Attempt 1}}, Attempt #{{$job.Attempt}}{{end}}
18+
<a href="{{$job.HTMLURL}}">
19+
{{$job.Status}}: {{$job.Name}}{{if gt $job.RunAttempt 1}}, Attempt #{{$job.RunAttempt}}{{end}}
20+
</a>
1921
</li>
2022
{{end}}
2123
</ul>

0 commit comments

Comments
 (0)