Skip to content

Commit 81ae04e

Browse files
committed
Merge branch 'main' into patch-mail-templates-1
2 parents 968f724 + c7b99c8 commit 81ae04e

File tree

5 files changed

+503
-55
lines changed

5 files changed

+503
-55
lines changed

routers/web/repo/actions/view.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ func Rerun(ctx *context_module.Context) {
429429
ctx.ServerError("UpdateRun", err)
430430
return
431431
}
432+
433+
if err := run.LoadAttributes(ctx); err != nil {
434+
ctx.ServerError("run.LoadAttributes", err)
435+
return
436+
}
437+
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, run.TriggerUser, run)
432438
}
433439

434440
job, jobs := getRunJobs(ctx, runIndex, jobIndex)
@@ -485,7 +491,6 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shou
485491
}
486492

487493
actions_service.CreateCommitStatus(ctx, job)
488-
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
489494
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
490495

491496
return nil
@@ -560,9 +565,8 @@ func Cancel(ctx *context_module.Context) {
560565
if len(updatedjobs) > 0 {
561566
job := updatedjobs[0]
562567
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
563-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
564568
}
565-
ctx.JSON(http.StatusOK, struct{}{})
569+
ctx.JSONOK()
566570
}
567571

568572
func Approve(ctx *context_module.Context) {
@@ -606,15 +610,14 @@ func Approve(ctx *context_module.Context) {
606610
if len(updatedjobs) > 0 {
607611
job := updatedjobs[0]
608612
actions_service.NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
609-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
610613
}
611614

612615
for _, job := range updatedjobs {
613616
_ = job.LoadAttributes(ctx)
614617
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
615618
}
616619

617-
ctx.JSON(http.StatusOK, struct{}{})
620+
ctx.JSONOK()
618621
}
619622

620623
func Delete(ctx *context_module.Context) {

services/actions/clear_tasks.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ func notifyWorkflowJobStatusUpdate(ctx context.Context, jobs []*actions_model.Ac
4242
_ = job.LoadAttributes(ctx)
4343
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
4444
}
45-
if len(jobs) > 0 {
46-
job := jobs[0]
47-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
48-
}
45+
job := jobs[0]
46+
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
4947
}
5048
}
5149

@@ -101,7 +99,7 @@ func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error {
10199
return nil
102100
}
103101

104-
// CancelAbandonedJobs cancels the jobs which have waiting status, but haven't been picked by a runner for a long time
102+
// CancelAbandonedJobs cancels jobs that have not been picked by any runner for a long time
105103
func CancelAbandonedJobs(ctx context.Context) error {
106104
jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
107105
Statuses: []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusBlocked},
@@ -113,24 +111,40 @@ func CancelAbandonedJobs(ctx context.Context) error {
113111
}
114112

115113
now := timeutil.TimeStampNow()
114+
115+
// Collect one job per run to send workflow run status update
116+
updatedRuns := map[int64]*actions_model.ActionRunJob{}
117+
116118
for _, job := range jobs {
117119
job.Status = actions_model.StatusCancelled
118120
job.Stopped = now
119121
updated := false
120122
if err := db.WithTx(ctx, func(ctx context.Context) error {
121123
n, err := actions_model.UpdateRunJob(ctx, job, nil, "status", "stopped")
122-
updated = err == nil && n > 0
123-
return err
124+
if err != nil {
125+
return err
126+
}
127+
if err := job.LoadAttributes(ctx); err != nil {
128+
return err
129+
}
130+
updated = n > 0
131+
if updated && job.Run.Status.IsDone() {
132+
updatedRuns[job.RunID] = job
133+
}
134+
return nil
124135
}); err != nil {
125136
log.Warn("cancel abandoned job %v: %v", job.ID, err)
126137
// go on
127138
}
128139
CreateCommitStatus(ctx, job)
129140
if updated {
130-
NotifyWorkflowRunStatusUpdateWithReload(ctx, job)
131141
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
132142
}
133143
}
134144

145+
for _, job := range updatedRuns {
146+
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
147+
}
148+
135149
return nil
136150
}

services/mailer/mail_workflow_run.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ func generateMessageIDForActionsWorkflowRunStatusEmail(repo *repo_model.Reposito
3636
return fmt.Sprintf("<%s/actions/runs/%d@%s>", repo.FullName(), run.Index, setting.Domain)
3737
}
3838

39-
func composeAndSendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun, sender *user_model.User, recipients []*user_model.User) {
39+
func composeAndSendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun, sender *user_model.User, recipients []*user_model.User) error {
40+
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
41+
if err != nil {
42+
return err
43+
}
44+
for _, job := range jobs {
45+
if !job.Status.IsDone() {
46+
log.Debug("composeAndSendActionsWorkflowRunStatusEmail: A job is not done. Will not compose and send actions email.")
47+
return nil
48+
}
49+
}
50+
4051
var subjectTrString string
4152
switch run.Status {
4253
case actions_model.StatusFailure:
@@ -50,11 +61,6 @@ func composeAndSendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo
5061
messageID := generateMessageIDForActionsWorkflowRunStatusEmail(repo, run)
5162
metadataHeaders := generateMetadataHeaders(repo)
5263

53-
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
54-
if err != nil {
55-
log.Error("GetRunJobsByRunID: %v", err)
56-
return
57-
}
5864
sort.SliceStable(jobs, func(i, j int) bool {
5965
si, sj := jobs[i].Status, jobs[j].Status
6066
/*
@@ -115,11 +121,11 @@ func composeAndSendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo
115121
"Jobs": convertedJobs,
116122
"locale": locale,
117123
}); err != nil {
118-
log.Error("ExecuteTemplate [%s]: %v", tplWorkflowRun, err)
119-
return
124+
return err
120125
}
121126
msgs := make([]*sender_service.Message, 0, len(tos))
122127
for _, rec := range tos {
128+
log.Trace("Sending actions email to %s (UID: %d)", rec.Name, rec.ID)
123129
msg := sender_service.NewMessageFrom(
124130
rec.Email,
125131
displayName,
@@ -139,14 +145,16 @@ func composeAndSendActionsWorkflowRunStatusEmail(ctx context.Context, repo *repo
139145
}
140146
SendAsync(msgs...)
141147
}
148+
149+
return nil
142150
}
143151

144-
func MailActionsTrigger(ctx context.Context, sender *user_model.User, repo *repo_model.Repository, run *actions_model.ActionRun) {
152+
func MailActionsTrigger(ctx context.Context, sender *user_model.User, repo *repo_model.Repository, run *actions_model.ActionRun) error {
145153
if setting.MailService == nil {
146-
return
154+
return nil
147155
}
148-
if run.Status.IsSkipped() {
149-
return
156+
if !run.Status.IsDone() || run.Status.IsSkipped() {
157+
return nil
150158
}
151159

152160
recipients := make([]*user_model.User, 0)
@@ -155,15 +163,16 @@ func MailActionsTrigger(ctx context.Context, sender *user_model.User, repo *repo
155163
notifyPref, err := user_model.GetUserSetting(ctx, sender.ID,
156164
user_model.SettingsKeyEmailNotificationGiteaActions, user_model.SettingEmailNotificationGiteaActionsFailureOnly)
157165
if err != nil {
158-
log.Error("GetUserSetting: %v", err)
159-
return
166+
return err
160167
}
161168
if notifyPref == user_model.SettingEmailNotificationGiteaActionsAll || !run.Status.IsSuccess() && notifyPref != user_model.SettingEmailNotificationGiteaActionsDisabled {
162169
recipients = append(recipients, sender)
163170
}
164171
}
165172

166173
if len(recipients) > 0 {
167-
composeAndSendActionsWorkflowRunStatusEmail(ctx, repo, run, sender, recipients)
174+
log.Debug("MailActionsTrigger: Initiate email composition")
175+
return composeAndSendActionsWorkflowRunStatusEmail(ctx, repo, run, sender, recipients)
168176
}
177+
return nil
169178
}

services/mailer/notify.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *
208208
}
209209

210210
func (m *mailNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
211-
if !run.Status.IsDone() {
212-
return
211+
if err := MailActionsTrigger(ctx, sender, repo, run); err != nil {
212+
log.Error("MailActionsTrigger: %v", err)
213213
}
214-
MailActionsTrigger(ctx, sender, repo, run)
215214
}

0 commit comments

Comments
 (0)