Skip to content

Commit d0bca15

Browse files
committed
rerun failed cla.yml jobs on cla sign
1 parent 5cd92a8 commit d0bca15

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

routers/web/user/setting/contributor_agreements.go

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
package setting
77

88
import (
9+
"context"
10+
"fmt"
911
"net/http"
1012

13+
actions_model "code.gitea.io/gitea/models/actions"
1114
"code.gitea.io/gitea/models/db"
1215
user_model "code.gitea.io/gitea/models/user"
16+
"code.gitea.io/gitea/modules/log"
1317
"code.gitea.io/gitea/modules/setting"
1418
"code.gitea.io/gitea/modules/templates"
1519
"code.gitea.io/gitea/modules/timeutil"
16-
"code.gitea.io/gitea/services/context"
20+
actions_service "code.gitea.io/gitea/services/actions"
21+
gitea_context "code.gitea.io/gitea/services/context"
22+
notify_service "code.gitea.io/gitea/services/notify"
1723

1824
"xorm.io/builder"
1925
)
@@ -23,7 +29,7 @@ const (
2329
)
2430

2531
// ContributorAgreements lists all agreements and shows which ones were signed.
26-
func ContributorAgreements(ctx *context.Context) {
32+
func ContributorAgreements(ctx *gitea_context.Context) {
2733
contributorAgreements, err := db.Find[user_model.ContributorAgreement](ctx, &db.ListOptions{})
2834
if err != nil {
2935
ctx.ServerError("FindContributorAgreements", err)
@@ -57,7 +63,7 @@ func ContributorAgreements(ctx *context.Context) {
5763
}
5864

5965
// SignContributorAgreement signs a contributor agreement.
60-
func SignContributorAgreement(ctx *context.Context) {
66+
func SignContributorAgreement(ctx *gitea_context.Context) {
6167
slug := ctx.PathParam("slug")
6268
ca, exist, err := db.Get[user_model.ContributorAgreement](ctx, builder.Eq{"slug": slug})
6369
if err != nil {
@@ -72,5 +78,65 @@ func SignContributorAgreement(ctx *context.Context) {
7278
ctx.ServerError("SignContributorAgreement", err)
7379
return
7480
}
81+
// Check if there are any recent action run failures, trigger a re-run.
82+
if err := rerunFailedActions(ctx, ctx.Doer); err != nil {
83+
log.Error("rerunFailedActions for userID=%d failed: %v", ctx.Doer.ID, err)
84+
}
7585
ctx.Redirect(setting.AppSubURL + "/user/settings/contributor_agreements")
7686
}
87+
88+
// rerunFailedActions looks for recent cla.yml failures triggered by the user and reruns those.
89+
func rerunFailedActions(ctx *gitea_context.Context, user *user_model.User) error {
90+
runs, err := db.Find[actions_model.ActionRun](ctx, actions_model.FindRunOptions{
91+
ListOptions: db.ListOptions{PageSize: 10},
92+
Status: []actions_model.Status{actions_model.StatusFailure},
93+
TriggerUserID: user.ID,
94+
WorkflowID: "cla.yml",
95+
})
96+
if err != nil {
97+
return fmt.Errorf("failed to find failed action runs: %w", err)
98+
}
99+
100+
// Core logic copied from Rerun func in routers/web/repo/actions/view.go
101+
for _, run := range runs {
102+
// reset run's start and stop time
103+
run.PreviousDuration = run.Duration()
104+
run.Started = 0
105+
run.Stopped = 0
106+
run.Status = actions_model.StatusWaiting
107+
if err := actions_model.UpdateRun(ctx, run, "started", "stopped", "status", "previous_duration"); err != nil {
108+
return fmt.Errorf("failed to UpdateRun: %w", err)
109+
}
110+
if err = run.LoadAttributes(ctx); err != nil {
111+
return fmt.Errorf("LoadAttributes: %w", err)
112+
}
113+
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, run.TriggerUser, run)
114+
115+
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
116+
if err != nil {
117+
return fmt.Errorf("GetRunJobsByRunID: %w", err)
118+
}
119+
// We always expect exactly one job in cla.yml
120+
if len(jobs) != 1 {
121+
return fmt.Errorf("cla.yml has more than one job, run.ID=%d", run.ID)
122+
}
123+
124+
job := jobs[0]
125+
status := job.Status
126+
job.TaskID = 0
127+
job.Status = actions_model.StatusWaiting
128+
job.Started = 0
129+
job.Stopped = 0
130+
if err := db.WithTx(ctx, func(ctx context.Context) error {
131+
_, err := actions_model.UpdateRunJob(ctx, job, builder.Eq{"status": status}, "task_id", "status", "started", "stopped")
132+
return err
133+
}); err != nil {
134+
return err
135+
}
136+
137+
actions_service.CreateCommitStatus(ctx, job)
138+
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
139+
}
140+
141+
return nil
142+
}

templates/admin/signed_contributor_agreements/batch_sign.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<select name="contributor_agreement_id" required>
1616
<option value="">--</option>
1717
{{range .ContributorAgreements}}
18-
<option value="{{.ID}}" {{if eq .ID $.ContributorAgreementID }}selected{{end}}>{{.Slug}}</option>
18+
<option value="{{.ID}}" {{if eq .ID $.ContributorAgreementID}}selected{{end}}>{{.Slug}}</option>
1919
{{end}}
2020
</select>
2121
</label>

templates/admin/signed_contributor_agreements/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<select name="contributor_agreement_id">
2121
<option value="">{{ctx.Locale.Tr "admin.signed_contributor_agreements.all_option"}}</option>
2222
{{range .ContributorAgreements}}
23-
<option value="{{.ID}}" {{if eq .ID $.ContributorAgreementID }}selected{{end}}>{{.Slug}}</option>
23+
<option value="{{.ID}}" {{if eq .ID $.ContributorAgreementID}}selected{{end}}>{{.Slug}}</option>
2424
{{end}}
2525
</select>
2626
</label>

0 commit comments

Comments
 (0)