feat: laat een pipeline opnieuw draaien
Een schrijver kan naast een pipeline kiezen tussen een nieuwe run op dezelfde commit en het opnieuw starten van alleen de mislukte jobs plus wat daardoor niets deed. Co-authored-by: tim <tim@timfalken.com>
@@ -0,0 +1,44 @@
1
package i18n
2
3
func init() {
4
addCatalog(en, pipeEN)
5
addCatalog(nl, pipeNL)
6
addCatalog(de, pipeDE)
7
addCatalog(fr, pipeFR)
8
}
9
10
var pipeEN = map[string]string{
11
"pipe.retry": "Retry",
12
"pipe.retry.all": "Retry entire pipeline",
13
"pipe.retry.failed": "Retry failed jobs",
14
"flash.pipeRetry": "Pipeline retried",
15
"err.pipeNoFailed": "this pipeline has no failed jobs",
16
"err.pipeRetryMode": "mode must be all or failed",
17
}
18
19
var pipeNL = map[string]string{
20
"pipe.retry": "Opnieuw",
21
"pipe.retry.all": "Hele pipeline opnieuw",
22
"pipe.retry.failed": "Alleen mislukte jobs",
23
"flash.pipeRetry": "Pipeline opnieuw gestart",
24
"err.pipeNoFailed": "deze pipeline heeft geen mislukte jobs",
25
"err.pipeRetryMode": "mode moet all of failed zijn",
26
}
27
28
var pipeDE = map[string]string{
29
"pipe.retry": "Erneut",
30
"pipe.retry.all": "Gesamte Pipeline erneut",
31
"pipe.retry.failed": "Nur fehlgeschlagene Jobs",
32
"flash.pipeRetry": "Pipeline erneut gestartet",
33
"err.pipeNoFailed": "diese Pipeline hat keine fehlgeschlagenen Jobs",
34
"err.pipeRetryMode": "Modus muss all oder failed sein",
35
}
36
37
var pipeFR = map[string]string{
38
"pipe.retry": "Relancer",
39
"pipe.retry.all": "Relancer tout le pipeline",
40
"pipe.retry.failed": "Relancer les jobs en échec",
41
"flash.pipeRetry": "Pipeline relancé",
42
"err.pipeNoFailed": "ce pipeline n'a aucun job en échec",
43
"err.pipeRetryMode": "le mode doit être all ou failed",
44
}
@@ -0,0 +1,44 @@1
package i18n2
3
func init() {4
addCatalog(en, pipeEN)5
addCatalog(nl, pipeNL)6
addCatalog(de, pipeDE)7
addCatalog(fr, pipeFR)8
}9
10
var pipeEN = map[string]string{11
"pipe.retry": "Retry",12
"pipe.retry.all": "Retry entire pipeline",13
"pipe.retry.failed": "Retry failed jobs",14
"flash.pipeRetry": "Pipeline retried",15
"err.pipeNoFailed": "this pipeline has no failed jobs",16
"err.pipeRetryMode": "mode must be all or failed",17
}18
19
var pipeNL = map[string]string{20
"pipe.retry": "Opnieuw",21
"pipe.retry.all": "Hele pipeline opnieuw",22
"pipe.retry.failed": "Alleen mislukte jobs",23
"flash.pipeRetry": "Pipeline opnieuw gestart",24
"err.pipeNoFailed": "deze pipeline heeft geen mislukte jobs",25
"err.pipeRetryMode": "mode moet all of failed zijn",26
}27
28
var pipeDE = map[string]string{29
"pipe.retry": "Erneut",30
"pipe.retry.all": "Gesamte Pipeline erneut",31
"pipe.retry.failed": "Nur fehlgeschlagene Jobs",32
"flash.pipeRetry": "Pipeline erneut gestartet",33
"err.pipeNoFailed": "diese Pipeline hat keine fehlgeschlagenen Jobs",34
"err.pipeRetryMode": "Modus muss all oder failed sein",35
}36
37
var pipeFR = map[string]string{38
"pipe.retry": "Relancer",39
"pipe.retry.all": "Relancer tout le pipeline",40
"pipe.retry.failed": "Relancer les jobs en échec",41
"flash.pipeRetry": "Pipeline relancé",42
"err.pipeNoFailed": "ce pipeline n'a aucun job en échec",43
"err.pipeRetryMode": "le mode doit être all ou failed",44
}@@ -394,6 +394,39 @@ func (s *Server) apiPipeline(w http.ResponseWriter, r *http.Request) {
394
394
writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))
395
395
}
396
396
397
func (s *Server) apiRetryPipeline(w http.ResponseWriter, r *http.Request) {
398
repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
399
if !ok {
400
return
401
}
402
pipeID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
403
if err != nil {
404
s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
405
return
406
}
407
var body struct {
408
Mode string `json:"mode"`
409
}
410
if err := readJSON(r, &body); err != nil {
411
s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
412
return
413
}
414
id, err := s.opRetryPipeline(r.Context(), apiUser(r), repo, pipeID, body.Mode)
415
if !s.apiOK(w, r, err) {
416
return
417
}
418
p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)
419
if err != nil {
420
s.writeAPIErr(w, r, notFound(err))
421
return
422
}
423
code := http.StatusOK
424
if body.Mode == "all" {
425
code = http.StatusCreated
426
}
427
writeJSON(w, code, s.pipeJSON(repo, p))
428
}
429
397
430
func (s *Server) apiCancelPipeline(w http.ResponseWriter, r *http.Request) {
398
431
repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
399
432
if !ok {
@@ -394,6 +394,39 @@ func (s *Server) apiPipeline(w http.ResponseWriter, r *http.Request) {394
writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))394
writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))395
}395
}396
396
397
func (s *Server) apiRetryPipeline(w http.ResponseWriter, r *http.Request) {398
repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)399
if !ok {400
return401
}402
pipeID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)403
if err != nil {404
s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))405
return406
}407
var body struct {408
Mode string `json:"mode"`409
}410
if err := readJSON(r, &body); err != nil {411
s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))412
return413
}414
id, err := s.opRetryPipeline(r.Context(), apiUser(r), repo, pipeID, body.Mode)415
if !s.apiOK(w, r, err) {416
return417
}418
p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)419
if err != nil {420
s.writeAPIErr(w, r, notFound(err))421
return422
}423
code := http.StatusOK424
if body.Mode == "all" {425
code = http.StatusCreated426
}427
writeJSON(w, code, s.pipeJSON(repo, p))428
}429
397
func (s *Server) apiCancelPipeline(w http.ResponseWriter, r *http.Request) {430
func (s *Server) apiCancelPipeline(w http.ResponseWriter, r *http.Request) {398
repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)431
repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)399
if !ok {432
if !ok {@@ -37,6 +37,14 @@ func decorateAPI(paths map[string]map[string]any, schemas map[string]any) {
37
37
"content": jsonSchema(schema),
38
38
}
39
39
}
40
if op := apiOp(paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "POST"); op != nil {
41
responses, _ := op["responses"].(map[string]any)
42
if responses != nil {
43
if okResp, ok := responses["200"]; ok {
44
responses["201"] = okResp
45
}
46
}
47
}
40
48
if op := apiOp(paths, "/repos/{owner}/{repo}/issues/{number}/notes", "POST"); op != nil {
41
49
responses, _ := op["responses"].(map[string]any)
42
50
if responses == nil {
@@ -107,6 +115,7 @@ func apiBinds() []apiBind {
107
115
{path: pipes, method: "POST", code: "201", body: "PipelineCreate", resp: "Pipeline"},
108
116
{path: pipe, method: "GET", code: "200", resp: "Pipeline"},
109
117
{path: pipe + "/cancel", method: "POST", code: "200", resp: "Pipeline"},
118
{path: pipe + "/retry", method: "POST", code: "200", body: "PipelineRetry", resp: "Pipeline"},
110
119
{path: pipe + "/jobs", method: "GET", code: "200", resp: "Job", array: true},
111
120
{path: job, method: "GET", code: "200", resp: "Job"},
112
121
{path: job + "/play", method: "POST", code: "200", resp: "Job"},
@@ -233,6 +242,13 @@ func apiSchemas() map[string]any {
233
242
"PipelineCreate": obj("Start a pipeline.", map[string]any{
234
243
"ref": str("Branch, tag or full ref to run."),
235
244
}),
245
"PipelineRetry": obj("Retry a pipeline. all starts a new pipeline on the same commit, ref, source and merge request. failed requeues failed jobs and the skipped or never-started jobs that depend on them. The response is 201 for all and 200 for failed.", map[string]any{
246
"mode": map[string]any{
247
"type": "string",
248
"description": "all or failed.",
249
"enum": []any{"all", "failed"},
250
},
251
}),
236
252
"Job": obj("CI job.", map[string]any{
237
253
"id": integer("Job id."),
238
254
"name": str("Job name."),
@@ -37,6 +37,14 @@ func decorateAPI(paths map[string]map[string]any, schemas map[string]any) {37
"content": jsonSchema(schema),37
"content": jsonSchema(schema),38
}38
}39
}39
}40
if op := apiOp(paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "POST"); op != nil {41
responses, _ := op["responses"].(map[string]any)42
if responses != nil {43
if okResp, ok := responses["200"]; ok {44
responses["201"] = okResp45
}46
}47
}40
if op := apiOp(paths, "/repos/{owner}/{repo}/issues/{number}/notes", "POST"); op != nil {48
if op := apiOp(paths, "/repos/{owner}/{repo}/issues/{number}/notes", "POST"); op != nil {41
responses, _ := op["responses"].(map[string]any)49
responses, _ := op["responses"].(map[string]any)42
if responses == nil {50
if responses == nil {@@ -107,6 +115,7 @@ func apiBinds() []apiBind {107
{path: pipes, method: "POST", code: "201", body: "PipelineCreate", resp: "Pipeline"},115
{path: pipes, method: "POST", code: "201", body: "PipelineCreate", resp: "Pipeline"},108
{path: pipe, method: "GET", code: "200", resp: "Pipeline"},116
{path: pipe, method: "GET", code: "200", resp: "Pipeline"},109
{path: pipe + "/cancel", method: "POST", code: "200", resp: "Pipeline"},117
{path: pipe + "/cancel", method: "POST", code: "200", resp: "Pipeline"},118
{path: pipe + "/retry", method: "POST", code: "200", body: "PipelineRetry", resp: "Pipeline"},110
{path: pipe + "/jobs", method: "GET", code: "200", resp: "Job", array: true},119
{path: pipe + "/jobs", method: "GET", code: "200", resp: "Job", array: true},111
{path: job, method: "GET", code: "200", resp: "Job"},120
{path: job, method: "GET", code: "200", resp: "Job"},112
{path: job + "/play", method: "POST", code: "200", resp: "Job"},121
{path: job + "/play", method: "POST", code: "200", resp: "Job"},@@ -233,6 +242,13 @@ func apiSchemas() map[string]any {233
"PipelineCreate": obj("Start a pipeline.", map[string]any{242
"PipelineCreate": obj("Start a pipeline.", map[string]any{234
"ref": str("Branch, tag or full ref to run."),243
"ref": str("Branch, tag or full ref to run."),235
}),244
}),245
"PipelineRetry": obj("Retry a pipeline. all starts a new pipeline on the same commit, ref, source and merge request. failed requeues failed jobs and the skipped or never-started jobs that depend on them. The response is 201 for all and 200 for failed.", map[string]any{246
"mode": map[string]any{247
"type": "string",248
"description": "all or failed.",249
"enum": []any{"all", "failed"},250
},251
}),236
"Job": obj("CI job.", map[string]any{252
"Job": obj("CI job.", map[string]any{237
"id": integer("Job id."),253
"id": integer("Job id."),238
"name": str("Job name."),254
"name": str("Job name."),@@ -49,6 +49,9 @@ func TestOpenAPISchemas(t *testing.T) {
49
49
50
50
assertBody(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "PipelineCreate")
51
51
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "201", "#/components/schemas/Pipeline")
52
assertBody(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "PipelineRetry")
53
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "200", "#/components/schemas/Pipeline")
54
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "201", "#/components/schemas/Pipeline")
52
55
assertItems(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs", "get", "200", "#/components/schemas/Job")
53
56
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", "get", "200", "#/components/schemas/Job")
54
57
@@ -49,6 +49,9 @@ func TestOpenAPISchemas(t *testing.T) {49
49
50
assertBody(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "PipelineCreate")50
assertBody(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "PipelineCreate")51
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "201", "#/components/schemas/Pipeline")51
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines", "post", "201", "#/components/schemas/Pipeline")52
assertBody(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "PipelineRetry")53
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "200", "#/components/schemas/Pipeline")54
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/retry", "post", "201", "#/components/schemas/Pipeline")52
assertItems(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs", "get", "200", "#/components/schemas/Job")55
assertItems(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs", "get", "200", "#/components/schemas/Job")53
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", "get", "200", "#/components/schemas/Job")56
assertResp(t, paths, "/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", "get", "200", "#/components/schemas/Job")54
57
@@ -106,6 +106,7 @@ var apiCatalog = []apiRoute{
106
106
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines", access.ScopeWrite, authUser, "Start pipeline", (*Server).apiPipelines},
107
107
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}", access.ScopeRead, authUser, "Get pipeline", (*Server).apiPipeline},
108
108
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/cancel", access.ScopeWrite, authUser, "Cancel pipeline", (*Server).apiCancelPipeline},
109
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/retry", access.ScopeWrite, authUser, "Retry pipeline", (*Server).apiRetryPipeline},
109
110
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs", access.ScopeRead, authUser, "List jobs", (*Server).apiJobs},
110
111
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", access.ScopeRead, authUser, "Get job", (*Server).apiJob},
111
112
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log", access.ScopeRead, authUser, "Job log", (*Server).apiJobLog},
@@ -106,6 +106,7 @@ var apiCatalog = []apiRoute{106
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines", access.ScopeWrite, authUser, "Start pipeline", (*Server).apiPipelines},106
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines", access.ScopeWrite, authUser, "Start pipeline", (*Server).apiPipelines},107
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}", access.ScopeRead, authUser, "Get pipeline", (*Server).apiPipeline},107
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}", access.ScopeRead, authUser, "Get pipeline", (*Server).apiPipeline},108
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/cancel", access.ScopeWrite, authUser, "Cancel pipeline", (*Server).apiCancelPipeline},108
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/cancel", access.ScopeWrite, authUser, "Cancel pipeline", (*Server).apiCancelPipeline},109
{http.MethodPost, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/retry", access.ScopeWrite, authUser, "Retry pipeline", (*Server).apiRetryPipeline},109
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs", access.ScopeRead, authUser, "List jobs", (*Server).apiJobs},110
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs", access.ScopeRead, authUser, "List jobs", (*Server).apiJobs},110
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", access.ScopeRead, authUser, "Get job", (*Server).apiJob},111
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}", access.ScopeRead, authUser, "Get job", (*Server).apiJob},111
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log", access.ScopeRead, authUser, "Job log", (*Server).apiJobLog},112
{http.MethodGet, "/api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/log", access.ScopeRead, authUser, "Job log", (*Server).apiJobLog},@@ -61,6 +61,7 @@ func TestAPICatalogCoversResources(t *testing.T) {
61
61
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/suggestions\n",
62
62
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/merge\n",
63
63
"POST /api/v1/projects/{owner}/{repo}/pipelines\n",
64
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/retry\n",
64
65
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/play\n",
65
66
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/retry\n",
66
67
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/cancel\n",
@@ -61,6 +61,7 @@ func TestAPICatalogCoversResources(t *testing.T) {61
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/suggestions\n",61
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/suggestions\n",62
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/merge\n",62
"POST /api/v1/projects/{owner}/{repo}/merge_requests/{number}/merge\n",63
"POST /api/v1/projects/{owner}/{repo}/pipelines\n",63
"POST /api/v1/projects/{owner}/{repo}/pipelines\n",64
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/retry\n",64
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/play\n",65
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/play\n",65
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/retry\n",66
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/retry\n",66
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/cancel\n",67
"POST /api/v1/projects/{owner}/{repo}/pipelines/{id}/jobs/{job}/cancel\n",@@ -101,6 +101,42 @@ func (s *Server) opJobAction(ctx context.Context, user store.User, repo store.Re
101
101
return job, nil
102
102
}
103
103
104
func (s *Server) opRetryPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64, mode string) (int64, error) {
105
role, err := s.repoRole(ctx, user, repo)
106
if err != nil {
107
return 0, err
108
}
109
if !role.CanWriteCode() {
110
return 0, fail(403, i18n.E("err.forbidden"))
111
}
112
p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
113
if err != nil {
114
return 0, notFound(err)
115
}
116
switch mode {
117
case "all":
118
id, err := s.Store.ClonePipeline(ctx, repo.ID, p.ID)
119
if err != nil {
120
if store.IsNotFound(err) {
121
return 0, notFound(err)
122
}
123
return 0, fail(400, err)
124
}
125
return id, nil
126
case "failed":
127
ok, err := s.Store.RetryFailedJobs(ctx, p.ID)
128
if err != nil {
129
return 0, fail(400, err)
130
}
131
if !ok {
132
return 0, fail(409, i18n.E("err.pipeNoFailed"))
133
}
134
return p.ID, nil
135
default:
136
return 0, fail(400, i18n.E("err.pipeRetryMode"))
137
}
138
}
139
104
140
func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error {
105
141
role, err := s.repoRole(ctx, user, repo)
106
142
if err != nil {
@@ -101,6 +101,42 @@ func (s *Server) opJobAction(ctx context.Context, user store.User, repo store.Re101
return job, nil101
return job, nil102
}102
}103
103
104
func (s *Server) opRetryPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64, mode string) (int64, error) {105
role, err := s.repoRole(ctx, user, repo)106
if err != nil {107
return 0, err108
}109
if !role.CanWriteCode() {110
return 0, fail(403, i18n.E("err.forbidden"))111
}112
p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)113
if err != nil {114
return 0, notFound(err)115
}116
switch mode {117
case "all":118
id, err := s.Store.ClonePipeline(ctx, repo.ID, p.ID)119
if err != nil {120
if store.IsNotFound(err) {121
return 0, notFound(err)122
}123
return 0, fail(400, err)124
}125
return id, nil126
case "failed":127
ok, err := s.Store.RetryFailedJobs(ctx, p.ID)128
if err != nil {129
return 0, fail(400, err)130
}131
if !ok {132
return 0, fail(409, i18n.E("err.pipeNoFailed"))133
}134
return p.ID, nil135
default:136
return 0, fail(400, i18n.E("err.pipeRetryMode"))137
}138
}139
104
func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error {140
func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error {105
role, err := s.repoRole(ctx, user, repo)141
role, err := s.repoRole(ctx, user, repo)106
if err != nil {142
if err != nil {@@ -35,6 +35,8 @@ type PipeRow struct {
35
35
Avatar string
36
36
Initial string
37
37
Stages []StageView
38
Failed bool
39
Live bool
38
40
}
39
41
40
42
// StageView is één bolletje in de stages-kolom.
@@ -160,14 +162,26 @@ func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p sto
160
162
initial = strings.ToUpper(actor[:1])
161
163
}
162
164
sourceLabel, mrNumber := s.pipelineSourceLabel(r, repo, p)
163
return PipeRow{
165
row := PipeRow{
164
166
ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),
165
167
Duration: pipelineClock(p), When: i18n.Ago(lang, p.CreatedAt.Unix()),
166
168
Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),
167
169
Ref: name, RefKind: kind, Source: p.Source, SourceLabel: sourceLabel, MRNumber: mrNumber,
168
170
Actor: actor, ActorURL: actorURL, Avatar: avatar, Initial: initial,
169
171
Stages: stageViews(jobs, ranks, func(key string, args ...any) string { return s.tr(r, key, args...) }),
172
Failed: jobsFailed(jobs),
170
173
}
174
row.Live = rowLive(row)
175
return row
176
}
177
178
func jobsFailed(jobs []store.Job) bool {
179
for _, j := range jobs {
180
if j.Status == "failed" {
181
return true
182
}
183
}
184
return false
171
185
}
172
186
173
187
// pipelineSourceLabel benoemt een pipeline die bij een merge request hoort,
@@ -447,6 +461,43 @@ func (s *Server) jobLog(w http.ResponseWriter, r *http.Request) {
447
461
})
448
462
}
449
463
464
func (s *Server) retryPipeline(w http.ResponseWriter, r *http.Request) {
465
u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
466
if !ok || !s.csrfOK(w, r) {
467
return
468
}
469
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
470
if err != nil {
471
http.NotFound(w, r)
472
return
473
}
474
back := safeRepoBack(repo, r.FormValue("back"))
475
if back == "" {
476
back = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(id, 10)
477
}
478
newID, err := s.opRetryPipeline(r.Context(), u, repo, id, r.FormValue("mode"))
479
if !s.uiOK(w, r, back, err) {
480
return
481
}
482
dest := back
483
if r.FormValue("mode") == "all" {
484
dest = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(newID, 10)
485
}
486
s.redirect(w, r, dest, s.tr(r, "flash.pipeRetry"), "")
487
}
488
489
func safeRepoBack(repo store.Repo, back string) string {
490
back = strings.TrimSpace(back)
491
prefix := "/" + repo.FullPath()
492
if back != prefix && !strings.HasPrefix(back, prefix+"/") {
493
return ""
494
}
495
if strings.Contains(back, "..") || strings.Contains(back, "\\") || strings.Contains(back, "\n") || strings.Contains(back, "?") {
496
return ""
497
}
498
return back
499
}
500
450
501
func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {
451
502
u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
452
503
if !ok || !s.csrfOK(w, r) {
@@ -35,6 +35,8 @@ type PipeRow struct {35
Avatar string35
Avatar string36
Initial string36
Initial string37
Stages []StageView37
Stages []StageView38
Failed bool39
Live bool38
}40
}39
41
40
// StageView is één bolletje in de stages-kolom.42
// StageView is één bolletje in de stages-kolom.@@ -160,14 +162,26 @@ func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p sto160
initial = strings.ToUpper(actor[:1])162
initial = strings.ToUpper(actor[:1])161
}163
}162
sourceLabel, mrNumber := s.pipelineSourceLabel(r, repo, p)164
sourceLabel, mrNumber := s.pipelineSourceLabel(r, repo, p)163
return PipeRow{165
row := PipeRow{164
ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),166
ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),165
Duration: pipelineClock(p), When: i18n.Ago(lang, p.CreatedAt.Unix()),167
Duration: pipelineClock(p), When: i18n.Ago(lang, p.CreatedAt.Unix()),166
Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),168
Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),167
Ref: name, RefKind: kind, Source: p.Source, SourceLabel: sourceLabel, MRNumber: mrNumber,169
Ref: name, RefKind: kind, Source: p.Source, SourceLabel: sourceLabel, MRNumber: mrNumber,168
Actor: actor, ActorURL: actorURL, Avatar: avatar, Initial: initial,170
Actor: actor, ActorURL: actorURL, Avatar: avatar, Initial: initial,169
Stages: stageViews(jobs, ranks, func(key string, args ...any) string { return s.tr(r, key, args...) }),171
Stages: stageViews(jobs, ranks, func(key string, args ...any) string { return s.tr(r, key, args...) }),172
Failed: jobsFailed(jobs),170
}173
}174
row.Live = rowLive(row)175
return row176
}177
178
func jobsFailed(jobs []store.Job) bool {179
for _, j := range jobs {180
if j.Status == "failed" {181
return true182
}183
}184
return false171
}185
}172
186
173
// pipelineSourceLabel benoemt een pipeline die bij een merge request hoort,187
// pipelineSourceLabel benoemt een pipeline die bij een merge request hoort,@@ -447,6 +461,43 @@ func (s *Server) jobLog(w http.ResponseWriter, r *http.Request) {447
})461
})448
}462
}449
463
464
func (s *Server) retryPipeline(w http.ResponseWriter, r *http.Request) {465
u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)466
if !ok || !s.csrfOK(w, r) {467
return468
}469
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)470
if err != nil {471
http.NotFound(w, r)472
return473
}474
back := safeRepoBack(repo, r.FormValue("back"))475
if back == "" {476
back = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(id, 10)477
}478
newID, err := s.opRetryPipeline(r.Context(), u, repo, id, r.FormValue("mode"))479
if !s.uiOK(w, r, back, err) {480
return481
}482
dest := back483
if r.FormValue("mode") == "all" {484
dest = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(newID, 10)485
}486
s.redirect(w, r, dest, s.tr(r, "flash.pipeRetry"), "")487
}488
489
func safeRepoBack(repo store.Repo, back string) string {490
back = strings.TrimSpace(back)491
prefix := "/" + repo.FullPath()492
if back != prefix && !strings.HasPrefix(back, prefix+"/") {493
return ""494
}495
if strings.Contains(back, "..") || strings.Contains(back, "\\") || strings.Contains(back, "\n") || strings.Contains(back, "?") {496
return ""497
}498
return back499
}500
450
func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {501
func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {451
u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)502
u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)452
if !ok || !s.csrfOK(w, r) {503
if !ok || !s.csrfOK(w, r) {@@ -0,0 +1,256 @@
1
package server
2
3
import (
4
"context"
5
"encoding/json"
6
"io"
7
"net/http"
8
"net/http/cookiejar"
9
"net/http/httptest"
10
"net/url"
11
"os/exec"
12
"path/filepath"
13
"strconv"
14
"strings"
15
"testing"
16
17
"gitlab.com/Timfa/bluegit/internal/access"
18
"gitlab.com/Timfa/bluegit/internal/auth"
19
"gitlab.com/Timfa/bluegit/internal/config"
20
"gitlab.com/Timfa/bluegit/internal/gitrepo"
21
"gitlab.com/Timfa/bluegit/internal/store"
22
)
23
24
func TestPipelineRetryButtonAndAPI(t *testing.T) {
25
if _, err := exec.LookPath("git"); err != nil {
26
t.Skip("git ontbreekt")
27
}
28
storage := t.TempDir()
29
st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
30
if err != nil {
31
t.Fatal(err)
32
}
33
t.Cleanup(func() { _ = st.Close() })
34
cfg := config.Config{
35
BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
36
Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
37
Git: config.Git{Binary: "git", DefaultBranch: "master"},
38
}
39
srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
40
ts := httptest.NewServer(srv.Handler())
41
t.Cleanup(ts.Close)
42
jar, _ := cookiejar.New(nil)
43
tim := &http.Client{Jar: jar}
44
page := read(t, mustGet(t, tim, ts.URL+"/login"))
45
if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{
46
"csrf": {csrfOf(t, page)}, "name": {"opnieuw"}, "visibility": {"public"},
47
}); err != nil {
48
t.Fatal(err)
49
}
50
ctx := context.Background()
51
repo, err := st.RepoByPath(ctx, "tim", "opnieuw")
52
if err != nil {
53
t.Fatal(err)
54
}
55
ro, err := st.UpsertUser(ctx, 2, "ro", "", false)
56
if err != nil {
57
t.Fatal(err)
58
}
59
if err := st.SetMember(ctx, "repo", repo.ID, "ro", access.RoleReadonly); err != nil {
60
t.Fatal(err)
61
}
62
roToken, err := st.CreateToken(ctx, ro.ID, "ro")
63
if err != nil {
64
t.Fatal(err)
65
}
66
owner, err := st.UserByUsername(ctx, "tim")
67
if err != nil {
68
t.Fatal(err)
69
}
70
ownerToken, err := st.CreateToken(ctx, owner.ID, "tim")
71
if err != nil {
72
t.Fatal(err)
73
}
74
75
dir := gitrepo.Dir(storage, "tim", "opnieuw")
76
work := t.TempDir()
77
git(t, work, "init", "-b", "master")
78
git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
79
git(t, work, "config", "user.name", "tim")
80
writeFile(t, filepath.Join(work, "README.md"), "hallo\n")
81
git(t, work, "add", "README.md")
82
git(t, work, "commit", "-m", "leesmij")
83
git(t, work, "remote", "add", "origin", dir)
84
git(t, work, "push", "origin", "master")
85
sha := strings.TrimSpace(mustGit(t, dir, "rev-parse", "refs/heads/master"))
86
git(t, dir, "branch", "topic", sha)
87
88
mr, err := st.CreateMergeRequest(ctx, repo.ID, owner.ID, "Opnieuw", "", "topic", "master")
89
if err != nil {
90
t.Fatal(err)
91
}
92
okID := makePipe(t, st, repo.ID, sha, "groen", []store.NewJob{
93
{Name: "lint", Stage: "test", Script: "exit 0"},
94
})
95
finishPipe(t, st, okID, "success")
96
id := makePipe(t, st, repo.ID, sha, "rood", []store.NewJob{
97
{Name: "lint", Stage: "test", Script: "exit 0"},
98
{Name: "unit", Stage: "test", Script: "exit 1"},
99
{Name: "pack", Stage: "deploy", Script: "exit 0"},
100
{Name: "pages", Stage: "deploy", Script: "exit 0", When: "manual"},
101
{Name: "aur", Stage: "deploy", Script: "echo aur", NeedsSet: true},
102
{Name: "note", Stage: "deploy", Script: "exit 0"},
103
})
104
jobs, err := st.JobsByPipeline(ctx, id)
105
if err != nil {
106
t.Fatal(err)
107
}
108
for _, j := range jobs {
109
switch j.Name {
110
case "lint", "aur":
111
if err := st.FinishJob(ctx, j.ID, "success", "ok\n"); err != nil {
112
t.Fatal(err)
113
}
114
case "unit":
115
if err := st.FinishJob(ctx, j.ID, "failed", "nee\n"); err != nil {
116
t.Fatal(err)
117
}
118
case "note":
119
if err := st.FinishJob(ctx, j.ID, "canceled", "stop\n"); err != nil {
120
t.Fatal(err)
121
}
122
case "pack", "pages":
123
if err := st.SkipJob(ctx, j.ID, "skipped\n"); err != nil {
124
t.Fatal(err)
125
}
126
}
127
}
128
if _, err := st.RefreshPipelineStatus(ctx, id); err != nil {
129
t.Fatal(err)
130
}
131
if err := st.AttachPipelineMR(ctx, id, mr.ID); err != nil {
132
t.Fatal(err)
133
}
134
135
list := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines"))
136
detail := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))
137
home := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw"))
138
mrPage := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/merge-requests/1"))
139
for _, body := range []string{list, detail, home, mrPage} {
140
if !strings.Contains(body, `class="rowmenu piperetry"`) || !strings.Contains(body, "Retry entire pipeline") {
141
t.Fatalf("schrijver mist de knop:\n%s", body)
142
}
143
}
144
failedForm := `data-retry-failed data-pipe="` + strconv.FormatInt(id, 10) + `"`
145
okForm := `data-retry-failed data-pipe="` + strconv.FormatInt(okID, 10) + `" hidden`
146
if !strings.Contains(list, failedForm) || strings.Contains(list, failedForm+" hidden") || !strings.Contains(list, okForm) {
147
t.Fatalf("mislukte optie hoort alleen bij de rode pipeline:\n%s", list)
148
}
149
if !strings.Contains(detail, "Retry failed jobs") || strings.Contains(detail, failedForm+" hidden") {
150
t.Fatalf("detail mist de mislukte optie:\n%s", detail)
151
}
152
if !strings.Contains(home, "Retry failed jobs") || !strings.Contains(mrPage, "Retry failed jobs") {
153
t.Fatal("overzicht of merge request mist de mislukte optie")
154
}
155
156
anon := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines"))
157
anonHome := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw"))
158
anonMR := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/merge-requests/1"))
159
anonDetail := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))
160
for _, body := range []string{anon, anonHome, anonMR, anonDetail} {
161
if strings.Contains(body, "piperetry") || strings.Contains(body, "Retry entire pipeline") {
162
t.Fatalf("lezer ziet de knop:\n%s", body)
163
}
164
}
165
166
code, body := retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", roToken, `{"mode":"failed"}`)
167
if code != 403 {
168
t.Fatalf("readonly api %d %s", code, body)
169
}
170
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"nope"}`)
171
if code != 400 {
172
t.Fatalf("mode %d %s", code, body)
173
}
174
res, err := http.PostForm(ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{"mode": {"all"}})
175
if err != nil {
176
t.Fatal(err)
177
}
178
if res.StatusCode != http.StatusForbidden {
179
t.Fatalf("anonieme post %d", res.StatusCode)
180
}
181
res.Body.Close()
182
183
csrf := csrfOf(t, detail)
184
done := read(t, mustPost(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{
185
"csrf": {csrf}, "mode": {"failed"}, "back": {"/tim/opnieuw/-/pipelines/" + strconv.FormatInt(id, 10)},
186
}))
187
if !strings.Contains(done, "Pipeline retried") || !strings.Contains(done, "Running") {
188
t.Fatalf("na mislukte jobs:\n%s", done)
189
}
190
if !strings.Contains(done, failedForm+" hidden") {
191
t.Fatalf("optie blijft staan zonder mislukking:\n%s", done)
192
}
193
got := map[string]string{}
194
jobs, err = st.JobsByPipeline(ctx, id)
195
if err != nil {
196
t.Fatal(err)
197
}
198
for _, j := range jobs {
199
got[j.Name] = j.Status
200
}
201
if got["lint"] != "success" || got["aur"] != "success" || got["unit"] != "queued" || got["pack"] != "queued" || got["pages"] != "manual" || got["note"] != "canceled" {
202
t.Fatalf("jobs na retry: %v", got)
203
}
204
p, err := st.PipelineByID(ctx, repo.ID, id)
205
if err != nil || p.Status != "running" {
206
t.Fatalf("pipelinestatus %s %v", p.Status, err)
207
}
208
209
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"failed"}`)
210
if code != 409 {
211
t.Fatalf("tweede failed %d %s", code, body)
212
}
213
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"all"}`)
214
if code != 201 {
215
t.Fatalf("all %d %s", code, body)
216
}
217
var created map[string]any
218
if err := json.Unmarshal([]byte(body), &created); err != nil {
219
t.Fatal(err)
220
}
221
newID, _ := created["id"].(float64)
222
if int64(newID) == id || created["sha"] != sha || created["source"] != "push" && created["source"] != "merge_request_event" {
223
t.Fatalf("antwoord %s", body)
224
}
225
clone, err := st.PipelineByID(ctx, repo.ID, int64(newID))
226
if err != nil {
227
t.Fatal(err)
228
}
229
if clone.CommitSHA != sha || clone.Ref != "refs/heads/master" || clone.Source != "push" || clone.MRID != mr.ID || clone.Status != "queued" {
230
t.Fatalf("kloon %+v", clone)
231
}
232
old, err := st.PipelineByID(ctx, repo.ID, id)
233
if err != nil || old.Status != "running" {
234
t.Fatalf("oude pipeline %s %v", old.Status, err)
235
}
236
}
237
238
func retryAPI(t *testing.T, ts *httptest.Server, method, path, token, body string) (int, string) {
239
t.Helper()
240
req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body))
241
if err != nil {
242
t.Fatal(err)
243
}
244
req.Header.Set("Content-Type", "application/json")
245
req.Header.Set("Authorization", "Bearer "+token)
246
res, err := http.DefaultClient.Do(req)
247
if err != nil {
248
t.Fatal(err)
249
}
250
defer res.Body.Close()
251
raw, err := io.ReadAll(res.Body)
252
if err != nil {
253
t.Fatal(err)
254
}
255
return res.StatusCode, string(raw)
256
}
@@ -0,0 +1,256 @@1
package server2
3
import (4
"context"5
"encoding/json"6
"io"7
"net/http"8
"net/http/cookiejar"9
"net/http/httptest"10
"net/url"11
"os/exec"12
"path/filepath"13
"strconv"14
"strings"15
"testing"16
17
"gitlab.com/Timfa/bluegit/internal/access"18
"gitlab.com/Timfa/bluegit/internal/auth"19
"gitlab.com/Timfa/bluegit/internal/config"20
"gitlab.com/Timfa/bluegit/internal/gitrepo"21
"gitlab.com/Timfa/bluegit/internal/store"22
)23
24
func TestPipelineRetryButtonAndAPI(t *testing.T) {25
if _, err := exec.LookPath("git"); err != nil {26
t.Skip("git ontbreekt")27
}28
storage := t.TempDir()29
st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())30
if err != nil {31
t.Fatal(err)32
}33
t.Cleanup(func() { _ = st.Close() })34
cfg := config.Config{35
BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,36
Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},37
Git: config.Git{Binary: "git", DefaultBranch: "master"},38
}39
srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}40
ts := httptest.NewServer(srv.Handler())41
t.Cleanup(ts.Close)42
jar, _ := cookiejar.New(nil)43
tim := &http.Client{Jar: jar}44
page := read(t, mustGet(t, tim, ts.URL+"/login"))45
if _, err := tim.PostForm(ts.URL+"/new/repo", url.Values{46
"csrf": {csrfOf(t, page)}, "name": {"opnieuw"}, "visibility": {"public"},47
}); err != nil {48
t.Fatal(err)49
}50
ctx := context.Background()51
repo, err := st.RepoByPath(ctx, "tim", "opnieuw")52
if err != nil {53
t.Fatal(err)54
}55
ro, err := st.UpsertUser(ctx, 2, "ro", "", false)56
if err != nil {57
t.Fatal(err)58
}59
if err := st.SetMember(ctx, "repo", repo.ID, "ro", access.RoleReadonly); err != nil {60
t.Fatal(err)61
}62
roToken, err := st.CreateToken(ctx, ro.ID, "ro")63
if err != nil {64
t.Fatal(err)65
}66
owner, err := st.UserByUsername(ctx, "tim")67
if err != nil {68
t.Fatal(err)69
}70
ownerToken, err := st.CreateToken(ctx, owner.ID, "tim")71
if err != nil {72
t.Fatal(err)73
}74
75
dir := gitrepo.Dir(storage, "tim", "opnieuw")76
work := t.TempDir()77
git(t, work, "init", "-b", "master")78
git(t, work, "config", "user.email", "tim@users.git.timfalken.com")79
git(t, work, "config", "user.name", "tim")80
writeFile(t, filepath.Join(work, "README.md"), "hallo\n")81
git(t, work, "add", "README.md")82
git(t, work, "commit", "-m", "leesmij")83
git(t, work, "remote", "add", "origin", dir)84
git(t, work, "push", "origin", "master")85
sha := strings.TrimSpace(mustGit(t, dir, "rev-parse", "refs/heads/master"))86
git(t, dir, "branch", "topic", sha)87
88
mr, err := st.CreateMergeRequest(ctx, repo.ID, owner.ID, "Opnieuw", "", "topic", "master")89
if err != nil {90
t.Fatal(err)91
}92
okID := makePipe(t, st, repo.ID, sha, "groen", []store.NewJob{93
{Name: "lint", Stage: "test", Script: "exit 0"},94
})95
finishPipe(t, st, okID, "success")96
id := makePipe(t, st, repo.ID, sha, "rood", []store.NewJob{97
{Name: "lint", Stage: "test", Script: "exit 0"},98
{Name: "unit", Stage: "test", Script: "exit 1"},99
{Name: "pack", Stage: "deploy", Script: "exit 0"},100
{Name: "pages", Stage: "deploy", Script: "exit 0", When: "manual"},101
{Name: "aur", Stage: "deploy", Script: "echo aur", NeedsSet: true},102
{Name: "note", Stage: "deploy", Script: "exit 0"},103
})104
jobs, err := st.JobsByPipeline(ctx, id)105
if err != nil {106
t.Fatal(err)107
}108
for _, j := range jobs {109
switch j.Name {110
case "lint", "aur":111
if err := st.FinishJob(ctx, j.ID, "success", "ok\n"); err != nil {112
t.Fatal(err)113
}114
case "unit":115
if err := st.FinishJob(ctx, j.ID, "failed", "nee\n"); err != nil {116
t.Fatal(err)117
}118
case "note":119
if err := st.FinishJob(ctx, j.ID, "canceled", "stop\n"); err != nil {120
t.Fatal(err)121
}122
case "pack", "pages":123
if err := st.SkipJob(ctx, j.ID, "skipped\n"); err != nil {124
t.Fatal(err)125
}126
}127
}128
if _, err := st.RefreshPipelineStatus(ctx, id); err != nil {129
t.Fatal(err)130
}131
if err := st.AttachPipelineMR(ctx, id, mr.ID); err != nil {132
t.Fatal(err)133
}134
135
list := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines"))136
detail := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))137
home := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw"))138
mrPage := read(t, mustGet(t, tim, ts.URL+"/tim/opnieuw/-/merge-requests/1"))139
for _, body := range []string{list, detail, home, mrPage} {140
if !strings.Contains(body, `class="rowmenu piperetry"`) || !strings.Contains(body, "Retry entire pipeline") {141
t.Fatalf("schrijver mist de knop:\n%s", body)142
}143
}144
failedForm := `data-retry-failed data-pipe="` + strconv.FormatInt(id, 10) + `"`145
okForm := `data-retry-failed data-pipe="` + strconv.FormatInt(okID, 10) + `" hidden`146
if !strings.Contains(list, failedForm) || strings.Contains(list, failedForm+" hidden") || !strings.Contains(list, okForm) {147
t.Fatalf("mislukte optie hoort alleen bij de rode pipeline:\n%s", list)148
}149
if !strings.Contains(detail, "Retry failed jobs") || strings.Contains(detail, failedForm+" hidden") {150
t.Fatalf("detail mist de mislukte optie:\n%s", detail)151
}152
if !strings.Contains(home, "Retry failed jobs") || !strings.Contains(mrPage, "Retry failed jobs") {153
t.Fatal("overzicht of merge request mist de mislukte optie")154
}155
156
anon := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines"))157
anonHome := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw"))158
anonMR := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/merge-requests/1"))159
anonDetail := read(t, mustGet(t, http.DefaultClient, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)))160
for _, body := range []string{anon, anonHome, anonMR, anonDetail} {161
if strings.Contains(body, "piperetry") || strings.Contains(body, "Retry entire pipeline") {162
t.Fatalf("lezer ziet de knop:\n%s", body)163
}164
}165
166
code, body := retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", roToken, `{"mode":"failed"}`)167
if code != 403 {168
t.Fatalf("readonly api %d %s", code, body)169
}170
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"nope"}`)171
if code != 400 {172
t.Fatalf("mode %d %s", code, body)173
}174
res, err := http.PostForm(ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{"mode": {"all"}})175
if err != nil {176
t.Fatal(err)177
}178
if res.StatusCode != http.StatusForbidden {179
t.Fatalf("anonieme post %d", res.StatusCode)180
}181
res.Body.Close()182
183
csrf := csrfOf(t, detail)184
done := read(t, mustPost(t, tim, ts.URL+"/tim/opnieuw/-/pipelines/"+strconv.FormatInt(id, 10)+"/retry", url.Values{185
"csrf": {csrf}, "mode": {"failed"}, "back": {"/tim/opnieuw/-/pipelines/" + strconv.FormatInt(id, 10)},186
}))187
if !strings.Contains(done, "Pipeline retried") || !strings.Contains(done, "Running") {188
t.Fatalf("na mislukte jobs:\n%s", done)189
}190
if !strings.Contains(done, failedForm+" hidden") {191
t.Fatalf("optie blijft staan zonder mislukking:\n%s", done)192
}193
got := map[string]string{}194
jobs, err = st.JobsByPipeline(ctx, id)195
if err != nil {196
t.Fatal(err)197
}198
for _, j := range jobs {199
got[j.Name] = j.Status200
}201
if got["lint"] != "success" || got["aur"] != "success" || got["unit"] != "queued" || got["pack"] != "queued" || got["pages"] != "manual" || got["note"] != "canceled" {202
t.Fatalf("jobs na retry: %v", got)203
}204
p, err := st.PipelineByID(ctx, repo.ID, id)205
if err != nil || p.Status != "running" {206
t.Fatalf("pipelinestatus %s %v", p.Status, err)207
}208
209
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"failed"}`)210
if code != 409 {211
t.Fatalf("tweede failed %d %s", code, body)212
}213
code, body = retryAPI(t, ts, http.MethodPost, "/api/v1/projects/tim/opnieuw/pipelines/"+strconv.FormatInt(id, 10)+"/retry", ownerToken, `{"mode":"all"}`)214
if code != 201 {215
t.Fatalf("all %d %s", code, body)216
}217
var created map[string]any218
if err := json.Unmarshal([]byte(body), &created); err != nil {219
t.Fatal(err)220
}221
newID, _ := created["id"].(float64)222
if int64(newID) == id || created["sha"] != sha || created["source"] != "push" && created["source"] != "merge_request_event" {223
t.Fatalf("antwoord %s", body)224
}225
clone, err := st.PipelineByID(ctx, repo.ID, int64(newID))226
if err != nil {227
t.Fatal(err)228
}229
if clone.CommitSHA != sha || clone.Ref != "refs/heads/master" || clone.Source != "push" || clone.MRID != mr.ID || clone.Status != "queued" {230
t.Fatalf("kloon %+v", clone)231
}232
old, err := st.PipelineByID(ctx, repo.ID, id)233
if err != nil || old.Status != "running" {234
t.Fatalf("oude pipeline %s %v", old.Status, err)235
}236
}237
238
func retryAPI(t *testing.T, ts *httptest.Server, method, path, token, body string) (int, string) {239
t.Helper()240
req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body))241
if err != nil {242
t.Fatal(err)243
}244
req.Header.Set("Content-Type", "application/json")245
req.Header.Set("Authorization", "Bearer "+token)246
res, err := http.DefaultClient.Do(req)247
if err != nil {248
t.Fatal(err)249
}250
defer res.Body.Close()251
raw, err := io.ReadAll(res.Body)252
if err != nil {253
t.Fatal(err)254
}255
return res.StatusCode, string(raw)256
}@@ -123,6 +123,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C
123
123
tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }
124
124
row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)
125
125
row.PipeLive = jobsLive(p.Status, jobs)
126
row.PipeFailed = jobsFailed(jobs)
126
127
}
127
128
128
129
func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
@@ -123,6 +123,7 @@ func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *C123
tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }123
tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }124
row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)124
row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)125
row.PipeLive = jobsLive(p.Status, jobs)125
row.PipeLive = jobsLive(p.Status, jobs)126
row.PipeFailed = jobsFailed(jobs)126
}127
}127
128
128
func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {129
func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {@@ -383,6 +383,7 @@ func (s *Server) pipeline(w http.ResponseWriter, r *http.Request) {
383
383
v.PipeSource = s.pipelineSourceText(r, repo, p)
384
384
v.PipeJobs = linksFor(jobs, ranks, tr)
385
385
v.PipeStages = stageViews(jobs, ranks, tr)
386
v.PipeFailed = jobsFailed(jobs)
386
387
v.Live = jobsLive(p.Status, jobs)
387
388
v.LiveURL = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
388
389
if wantsJSON(r) {
@@ -383,6 +383,7 @@ func (s *Server) pipeline(w http.ResponseWriter, r *http.Request) {383
v.PipeSource = s.pipelineSourceText(r, repo, p)383
v.PipeSource = s.pipelineSourceText(r, repo, p)384
v.PipeJobs = linksFor(jobs, ranks, tr)384
v.PipeJobs = linksFor(jobs, ranks, tr)385
v.PipeStages = stageViews(jobs, ranks, tr)385
v.PipeStages = stageViews(jobs, ranks, tr)386
v.PipeFailed = jobsFailed(jobs)386
v.Live = jobsLive(p.Status, jobs)387
v.Live = jobsLive(p.Status, jobs)387
v.LiveURL = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)388
v.LiveURL = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)388
if wantsJSON(r) {389
if wantsJSON(r) {@@ -6,6 +6,7 @@ import (
6
6
"crypto/rand"
7
7
"embed"
8
8
"encoding/hex"
9
"fmt"
9
10
"html/template"
10
11
"io/fs"
11
12
"log/slog"
@@ -198,6 +199,7 @@ func (s *Server) Handler() http.Handler {
198
199
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)
199
200
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)
200
201
mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)
202
mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/retry", s.retryPipeline)
201
203
mux.HandleFunc("GET /{owner}/{repo}/-/ci/editor", s.ciEditor)
202
204
mux.HandleFunc("POST /{owner}/{repo}/-/ci/editor", s.saveCIEditor)
203
205
mux.HandleFunc("POST /{owner}/{repo}/-/ci/lint", s.lintCI)
@@ -280,6 +282,21 @@ func (s *Server) mustTemplates() {
280
282
}
281
283
return label
282
284
},
285
"printf": fmt.Sprintf,
286
"dict": func(kv ...any) (map[string]any, error) {
287
if len(kv)%2 != 0 {
288
return nil, fmt.Errorf("dict")
289
}
290
m := make(map[string]any, len(kv)/2)
291
for i := 0; i < len(kv); i += 2 {
292
key, ok := kv[i].(string)
293
if !ok {
294
return nil, fmt.Errorf("dict")
295
}
296
m[key] = kv[i+1]
297
}
298
return m, nil
299
},
283
300
}).ParseFS(webFS, "templates/*.html"))
284
301
}
285
302
s.tmpl = s.byLang[string(i18n.EN)]
@@ -553,6 +570,7 @@ type View struct {
553
570
PipeLabel string
554
571
PipeJobs []JobLink
555
572
PipeStages []StageView
573
PipeFailed bool
556
574
Live bool
557
575
LiveURL string
558
576
Jobs []store.Job
@@ -715,7 +733,7 @@ type CommitRow struct {
715
733
PipeID int64
716
734
PipeStatus, PipeLabel, PipeDuration string
717
735
PipeStages []StageView
718
PipeLive bool
736
PipeLive, PipeFailed bool
719
737
}
720
738
721
739
// CommitDay is één dag op de commitlijst, nieuwste dag eerst.
@@ -6,6 +6,7 @@ import (6
"crypto/rand"6
"crypto/rand"7
"embed"7
"embed"8
"encoding/hex"8
"encoding/hex"9
"fmt"9
"html/template"10
"html/template"10
"io/fs"11
"io/fs"11
"log/slog"12
"log/slog"@@ -198,6 +199,7 @@ func (s *Server) Handler() http.Handler {198
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)199
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)199
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)200
mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)200
mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)201
mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)202
mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/retry", s.retryPipeline)201
mux.HandleFunc("GET /{owner}/{repo}/-/ci/editor", s.ciEditor)203
mux.HandleFunc("GET /{owner}/{repo}/-/ci/editor", s.ciEditor)202
mux.HandleFunc("POST /{owner}/{repo}/-/ci/editor", s.saveCIEditor)204
mux.HandleFunc("POST /{owner}/{repo}/-/ci/editor", s.saveCIEditor)203
mux.HandleFunc("POST /{owner}/{repo}/-/ci/lint", s.lintCI)205
mux.HandleFunc("POST /{owner}/{repo}/-/ci/lint", s.lintCI)@@ -280,6 +282,21 @@ func (s *Server) mustTemplates() {280
}282
}281
return label283
return label282
},284
},285
"printf": fmt.Sprintf,286
"dict": func(kv ...any) (map[string]any, error) {287
if len(kv)%2 != 0 {288
return nil, fmt.Errorf("dict")289
}290
m := make(map[string]any, len(kv)/2)291
for i := 0; i < len(kv); i += 2 {292
key, ok := kv[i].(string)293
if !ok {294
return nil, fmt.Errorf("dict")295
}296
m[key] = kv[i+1]297
}298
return m, nil299
},283
}).ParseFS(webFS, "templates/*.html"))300
}).ParseFS(webFS, "templates/*.html"))284
}301
}285
s.tmpl = s.byLang[string(i18n.EN)]302
s.tmpl = s.byLang[string(i18n.EN)]@@ -553,6 +570,7 @@ type View struct {553
PipeLabel string570
PipeLabel string554
PipeJobs []JobLink571
PipeJobs []JobLink555
PipeStages []StageView572
PipeStages []StageView573
PipeFailed bool556
Live bool574
Live bool557
LiveURL string575
LiveURL string558
Jobs []store.Job576
Jobs []store.Job@@ -715,7 +733,7 @@ type CommitRow struct {715
PipeID int64733
PipeID int64716
PipeStatus, PipeLabel, PipeDuration string734
PipeStatus, PipeLabel, PipeDuration string717
PipeStages []StageView735
PipeStages []StageView718
PipeLive bool736
PipeLive, PipeFailed bool719
}737
}720
738
721
// CommitDay is één dag op de commitlijst, nieuwste dag eerst.739
// CommitDay is één dag op de commitlijst, nieuwste dag eerst.@@ -157,6 +157,41 @@
157
157
q.textContent = text;
158
158
}
159
159
160
function jobMarkedFailed(status) {
161
return status === "failed" || status === "warning";
162
}
163
164
function stagesHaveFailure(stages) {
165
var failed = false;
166
(stages || []).forEach(function (st) {
167
(st.jobs || []).forEach(function (job) {
168
if (jobMarkedFailed(job.status)) {
169
failed = true;
170
}
171
});
172
});
173
return failed;
174
}
175
176
function jobsHaveFailure(jobs) {
177
var failed = false;
178
(jobs || []).forEach(function (job) {
179
if (jobMarkedFailed(job.status)) {
180
failed = true;
181
}
182
});
183
return failed;
184
}
185
186
function syncRetry(pipeID, failed) {
187
document.querySelectorAll("[data-retry-failed]").forEach(function (el) {
188
if (pipeID && el.getAttribute("data-pipe") !== String(pipeID)) {
189
return;
190
}
191
el.hidden = !failed;
192
});
193
}
194
160
195
function patchPipeRow(live, row) {
161
196
var status = live.querySelector("[data-pipe-status]");
162
197
if (status) {
@@ -174,6 +209,7 @@
174
209
if (when) {
175
210
when.textContent = row.when || "";
176
211
}
212
syncRetry(live.getAttribute("data-pipe"), stagesHaveFailure(row.stages));
177
213
var stages = live.querySelector(".stages");
178
214
if (!stages) {
179
215
return;
@@ -311,6 +347,14 @@
311
347
if (head) {
312
348
patchPipeRow(head, data);
313
349
}
350
var mr = document.querySelector("[data-mr-pipe]");
351
if (mr) {
352
patchPipeRow(mr, data);
353
}
354
var pipeHead = document.querySelector(".pipehead[data-pipe]");
355
if (pipeHead) {
356
syncRetry(pipeHead.getAttribute("data-pipe"), jobsHaveFailure(data.jobs) || stagesHaveFailure(data.stages));
357
}
314
358
}
315
359
316
360
var liveEl = document.querySelector("[data-live-on]");
@@ -157,6 +157,41 @@157
q.textContent = text;157
q.textContent = text;158
}158
}159
159
160
function jobMarkedFailed(status) {161
return status === "failed" || status === "warning";162
}163
164
function stagesHaveFailure(stages) {165
var failed = false;166
(stages || []).forEach(function (st) {167
(st.jobs || []).forEach(function (job) {168
if (jobMarkedFailed(job.status)) {169
failed = true;170
}171
});172
});173
return failed;174
}175
176
function jobsHaveFailure(jobs) {177
var failed = false;178
(jobs || []).forEach(function (job) {179
if (jobMarkedFailed(job.status)) {180
failed = true;181
}182
});183
return failed;184
}185
186
function syncRetry(pipeID, failed) {187
document.querySelectorAll("[data-retry-failed]").forEach(function (el) {188
if (pipeID && el.getAttribute("data-pipe") !== String(pipeID)) {189
return;190
}191
el.hidden = !failed;192
});193
}194
160
function patchPipeRow(live, row) {195
function patchPipeRow(live, row) {161
var status = live.querySelector("[data-pipe-status]");196
var status = live.querySelector("[data-pipe-status]");162
if (status) {197
if (status) {@@ -174,6 +209,7 @@174
if (when) {209
if (when) {175
when.textContent = row.when || "";210
when.textContent = row.when || "";176
}211
}212
syncRetry(live.getAttribute("data-pipe"), stagesHaveFailure(row.stages));177
var stages = live.querySelector(".stages");213
var stages = live.querySelector(".stages");178
if (!stages) {214
if (!stages) {179
return;215
return;@@ -311,6 +347,14 @@311
if (head) {347
if (head) {312
patchPipeRow(head, data);348
patchPipeRow(head, data);313
}349
}350
var mr = document.querySelector("[data-mr-pipe]");351
if (mr) {352
patchPipeRow(mr, data);353
}354
var pipeHead = document.querySelector(".pipehead[data-pipe]");355
if (pipeHead) {356
syncRetry(pipeHead.getAttribute("data-pipe"), jobsHaveFailure(data.jobs) || stagesHaveFailure(data.stages));357
}314
}358
}315
359
316
var liveEl = document.querySelector("[data-live-on]");360
var liveEl = document.querySelector("[data-live-on]");@@ -484,6 +484,17 @@ table.code td.src { white-space: pre; }
484
484
.rowmenu .menu-panel { position: absolute; right: 0; z-index: 3; min-width: 9rem; }
485
485
.rowmenu .menu-panel button, .rowmenu .menu-panel form { width: 100%; }
486
486
.rowmenu .menu-panel button { text-align: left; border-radius: 8px; }
487
details.piperetry { display: inline-block; vertical-align: middle; flex: none; }
488
.stagecell { display: flex; align-items: center; gap: .45rem; }
489
.pipehead { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
490
.menu-panel.piperetry { min-width: 13.5rem; padding: .3rem; }
491
.menu-panel.piperetry form { margin: 0; }
492
.menu-panel.piperetry form[hidden] { display: none !important; }
493
.menu-panel.piperetry button {
494
display: block; width: 100%; text-align: left; background: transparent; border: 0;
495
border-radius: 8px; padding: .45rem .6rem; cursor: pointer; font: inherit; color: inherit;
496
}
497
.menu-panel.piperetry button:hover { background: #f3f6fb; }
487
498
img.tile { object-fit: cover; background: #fff; }
488
499
@font-face {
489
500
font-family: "Noto Sans Cuneiform";
@@ -484,6 +484,17 @@ table.code td.src { white-space: pre; }484
.rowmenu .menu-panel { position: absolute; right: 0; z-index: 3; min-width: 9rem; }484
.rowmenu .menu-panel { position: absolute; right: 0; z-index: 3; min-width: 9rem; }485
.rowmenu .menu-panel button, .rowmenu .menu-panel form { width: 100%; }485
.rowmenu .menu-panel button, .rowmenu .menu-panel form { width: 100%; }486
.rowmenu .menu-panel button { text-align: left; border-radius: 8px; }486
.rowmenu .menu-panel button { text-align: left; border-radius: 8px; }487
details.piperetry { display: inline-block; vertical-align: middle; flex: none; }488
.stagecell { display: flex; align-items: center; gap: .45rem; }489
.pipehead { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }490
.menu-panel.piperetry { min-width: 13.5rem; padding: .3rem; }491
.menu-panel.piperetry form { margin: 0; }492
.menu-panel.piperetry form[hidden] { display: none !important; }493
.menu-panel.piperetry button {494
display: block; width: 100%; text-align: left; background: transparent; border: 0;495
border-radius: 8px; padding: .45rem .6rem; cursor: pointer; font: inherit; color: inherit;496
}497
.menu-panel.piperetry button:hover { background: #f3f6fb; }487
img.tile { object-fit: cover; background: #fff; }498
img.tile { object-fit: cover; background: #fff; }488
@font-face {499
@font-face {489
font-family: "Noto Sans Cuneiform";500
font-family: "Noto Sans Cuneiform";@@ -105,9 +105,9 @@
105
105
<h2>{{t "mr.pipeline"}}</h2>
106
106
{{if .MRPipe}}
107
107
{{$pipe := .MRPipe}}
108
<div class="mrpipe">
108
<div class="mrpipe" data-mr-pipe data-pipe="{{$pipe.ID}}"{{if $pipe.Live}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}" data-live-on="1"{{end}}>
109
109
<a class="pipeid" href="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}">#{{$pipe.ID}}</a>
110
<span class="status {{$pipe.Status}}"><span class="mark"></span>{{$pipe.Label}}</span>
110
<span class="status {{$pipe.Status}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{$pipe.Label}}</span></span>
111
111
<div class="stages">
112
112
{{range $pipe.Stages}}
113
113
<span class="stage">
@@ -121,6 +121,7 @@
121
121
</span>
122
122
{{end}}
123
123
</div>
124
{{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" $pipe.ID "Failed" $pipe.Failed "Back" (printf "/%s/-/merge-requests/%d" .Repo.FullPath .MR.Number))}}
124
125
</div>
125
126
{{else}}<span class="muted">{{t "mr.noPipeline"}}</span>{{end}}
126
127
</div>
@@ -248,7 +249,7 @@
248
249
<h2>{{t "mr.pipelines"}}</h2>
249
250
{{if .PipeRows}}
250
251
<ul class="clean">
251
{{range .PipeRows}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{.ID}}">#{{.ID}}</a> <span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span></li>{{end}}
252
{{range .PipeRows}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{.ID}}">#{{.ID}}</a> <span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span> {{template "pipe-retry" (dict "Can" $.CanWrite "CSRF" $.CSRF "Path" $.Repo.FullPath "ID" .ID "Failed" .Failed "Back" (printf "/%s/-/merge-requests/%d" $.Repo.FullPath $.MR.Number))}}</li>{{end}}
252
253
</ul>
253
254
{{else}}<p class="muted">{{t "mr.noPipeline"}}</p>{{end}}
254
255
</section>
@@ -347,7 +348,7 @@
347
348
{{template "layout-start" .}}
348
349
<header class="pagehead"><h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1></header>
349
350
{{template "pipenav" .}}
350
<p><span class="status {{.PipeStatus}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{.PipeLabel}}</span></span>{{if .PipeSource}} <span class="pill">{{.PipeSource}}</span>{{end}} {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a></p>
351
<div class="pipehead" data-pipe="{{.Pipe.ID}}"><span class="status {{.PipeStatus}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{.PipeLabel}}</span></span>{{if .PipeSource}} <span class="pill">{{.PipeSource}}</span>{{end}} {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a> {{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" .Pipe.ID "Failed" .PipeFailed "Back" (printf "/%s/-/pipelines/%d" .Repo.FullPath .Pipe.ID))}}</div>
351
352
<p class="muted">{{t "pipe.runningOn" .RunnerName}}</p>
352
353
<ul class="clean"{{if .Live}} data-live="{{.LiveURL}}" data-live-on="1"{{end}}>
353
354
{{range .PipeJobs}}<li data-job="{{.ID}}"><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="status {{.Status}}" data-job-status><span class="mark"></span><span data-job-label>{{.Label}}</span></span>{{if .Queue}} <span class="queue" data-queue>{{.Queue}}</span>{{end}} <span class="muted">{{.Stage}}</span></li>{{end}}
@@ -377,6 +378,7 @@
377
378
</td>
378
379
<td>{{if .Actor}}{{if .ActorURL}}<a class="avatarlink" href="{{.ActorURL}}">{{end}}{{if .Avatar}}<img class="avatar" src="{{.Avatar}}" alt="{{.Actor}}">{{else}}<span class="avatar">{{.Initial}}</span>{{end}}{{if .ActorURL}}</a>{{end}}{{end}}</td>
379
380
<td>
381
<div class="stagecell">
380
382
<div class="stages">
381
383
{{range .Stages}}
382
384
<span class="stage">
@@ -390,11 +392,35 @@
390
392
</span>
391
393
{{end}}
392
394
</div>
395
{{template "pipe-retry" (dict "Can" $.CanWrite "CSRF" $.CSRF "Path" $.Repo.FullPath "ID" .ID "Failed" .Failed "Back" (printf "/%s/-/pipelines" $.Repo.FullPath))}}
396
</div>
393
397
</td>
394
398
</tr>
395
399
{{end}}
396
400
{{end}}
397
401
402
{{define "pipe-retry"}}
403
{{if .Can}}
404
<details class="rowmenu piperetry">
405
<summary aria-label="{{t "pipe.retry"}}">{{t "pipe.retry"}}</summary>
406
<div class="menu-panel piperetry" data-pipe="{{.ID}}">
407
<form method="post" action="/{{.Path}}/-/pipelines/{{.ID}}/retry">
408
<input type="hidden" name="csrf" value="{{.CSRF}}">
409
<input type="hidden" name="mode" value="all">
410
<input type="hidden" name="back" value="{{.Back}}">
411
<button type="submit">{{t "pipe.retry.all"}}</button>
412
</form>
413
<form method="post" action="/{{.Path}}/-/pipelines/{{.ID}}/retry" data-retry-failed data-pipe="{{.ID}}"{{if not .Failed}} hidden{{end}}>
414
<input type="hidden" name="csrf" value="{{.CSRF}}">
415
<input type="hidden" name="mode" value="failed">
416
<input type="hidden" name="back" value="{{.Back}}">
417
<button type="submit">{{t "pipe.retry.failed"}}</button>
418
</form>
419
</div>
420
</details>
421
{{end}}
422
{{end}}
423
398
424
{{define "job"}}
399
425
{{template "layout-start" .}}
400
426
<header class="pagehead"><h1>{{with .JobPage}}{{.Name}}{{end}}</h1></header>
@@ -105,9 +105,9 @@105
<h2>{{t "mr.pipeline"}}</h2>105
<h2>{{t "mr.pipeline"}}</h2>106
{{if .MRPipe}}106
{{if .MRPipe}}107
{{$pipe := .MRPipe}}107
{{$pipe := .MRPipe}}108
<div class="mrpipe">108
<div class="mrpipe" data-mr-pipe data-pipe="{{$pipe.ID}}"{{if $pipe.Live}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}" data-live-on="1"{{end}}>109
<a class="pipeid" href="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}">#{{$pipe.ID}}</a>109
<a class="pipeid" href="/{{.Repo.FullPath}}/-/pipelines/{{$pipe.ID}}">#{{$pipe.ID}}</a>110
<span class="status {{$pipe.Status}}"><span class="mark"></span>{{$pipe.Label}}</span>110
<span class="status {{$pipe.Status}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{$pipe.Label}}</span></span>111
<div class="stages">111
<div class="stages">112
{{range $pipe.Stages}}112
{{range $pipe.Stages}}113
<span class="stage">113
<span class="stage">@@ -121,6 +121,7 @@121
</span>121
</span>122
{{end}}122
{{end}}123
</div>123
</div>124
{{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" $pipe.ID "Failed" $pipe.Failed "Back" (printf "/%s/-/merge-requests/%d" .Repo.FullPath .MR.Number))}}124
</div>125
</div>125
{{else}}<span class="muted">{{t "mr.noPipeline"}}</span>{{end}}126
{{else}}<span class="muted">{{t "mr.noPipeline"}}</span>{{end}}126
</div>127
</div>@@ -248,7 +249,7 @@248
<h2>{{t "mr.pipelines"}}</h2>249
<h2>{{t "mr.pipelines"}}</h2>249
{{if .PipeRows}}250
{{if .PipeRows}}250
<ul class="clean">251
<ul class="clean">251
{{range .PipeRows}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{.ID}}">#{{.ID}}</a> <span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span></li>{{end}}252
{{range .PipeRows}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{.ID}}">#{{.ID}}</a> <span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span> {{template "pipe-retry" (dict "Can" $.CanWrite "CSRF" $.CSRF "Path" $.Repo.FullPath "ID" .ID "Failed" .Failed "Back" (printf "/%s/-/merge-requests/%d" $.Repo.FullPath $.MR.Number))}}</li>{{end}}252
</ul>253
</ul>253
{{else}}<p class="muted">{{t "mr.noPipeline"}}</p>{{end}}254
{{else}}<p class="muted">{{t "mr.noPipeline"}}</p>{{end}}254
</section>255
</section>@@ -347,7 +348,7 @@347
{{template "layout-start" .}}348
{{template "layout-start" .}}348
<header class="pagehead"><h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1></header>349
<header class="pagehead"><h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1></header>349
{{template "pipenav" .}}350
{{template "pipenav" .}}350
<p><span class="status {{.PipeStatus}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{.PipeLabel}}</span></span>{{if .PipeSource}} <span class="pill">{{.PipeSource}}</span>{{end}} {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a></p>351
<div class="pipehead" data-pipe="{{.Pipe.ID}}"><span class="status {{.PipeStatus}}" data-pipe-status><span class="mark"></span><span data-pipe-label>{{.PipeLabel}}</span></span>{{if .PipeSource}} <span class="pill">{{.PipeSource}}</span>{{end}} {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a> {{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" .Pipe.ID "Failed" .PipeFailed "Back" (printf "/%s/-/pipelines/%d" .Repo.FullPath .Pipe.ID))}}</div>351
<p class="muted">{{t "pipe.runningOn" .RunnerName}}</p>352
<p class="muted">{{t "pipe.runningOn" .RunnerName}}</p>352
<ul class="clean"{{if .Live}} data-live="{{.LiveURL}}" data-live-on="1"{{end}}>353
<ul class="clean"{{if .Live}} data-live="{{.LiveURL}}" data-live-on="1"{{end}}>353
{{range .PipeJobs}}<li data-job="{{.ID}}"><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="status {{.Status}}" data-job-status><span class="mark"></span><span data-job-label>{{.Label}}</span></span>{{if .Queue}} <span class="queue" data-queue>{{.Queue}}</span>{{end}} <span class="muted">{{.Stage}}</span></li>{{end}}354
{{range .PipeJobs}}<li data-job="{{.ID}}"><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="status {{.Status}}" data-job-status><span class="mark"></span><span data-job-label>{{.Label}}</span></span>{{if .Queue}} <span class="queue" data-queue>{{.Queue}}</span>{{end}} <span class="muted">{{.Stage}}</span></li>{{end}}@@ -377,6 +378,7 @@377
</td>378
</td>378
<td>{{if .Actor}}{{if .ActorURL}}<a class="avatarlink" href="{{.ActorURL}}">{{end}}{{if .Avatar}}<img class="avatar" src="{{.Avatar}}" alt="{{.Actor}}">{{else}}<span class="avatar">{{.Initial}}</span>{{end}}{{if .ActorURL}}</a>{{end}}{{end}}</td>379
<td>{{if .Actor}}{{if .ActorURL}}<a class="avatarlink" href="{{.ActorURL}}">{{end}}{{if .Avatar}}<img class="avatar" src="{{.Avatar}}" alt="{{.Actor}}">{{else}}<span class="avatar">{{.Initial}}</span>{{end}}{{if .ActorURL}}</a>{{end}}{{end}}</td>379
<td>380
<td>381
<div class="stagecell">380
<div class="stages">382
<div class="stages">381
{{range .Stages}}383
{{range .Stages}}382
<span class="stage">384
<span class="stage">@@ -390,11 +392,35 @@390
</span>392
</span>391
{{end}}393
{{end}}392
</div>394
</div>395
{{template "pipe-retry" (dict "Can" $.CanWrite "CSRF" $.CSRF "Path" $.Repo.FullPath "ID" .ID "Failed" .Failed "Back" (printf "/%s/-/pipelines" $.Repo.FullPath))}}396
</div>393
</td>397
</td>394
</tr>398
</tr>395
{{end}}399
{{end}}396
{{end}}400
{{end}}397
401
402
{{define "pipe-retry"}}403
{{if .Can}}404
<details class="rowmenu piperetry">405
<summary aria-label="{{t "pipe.retry"}}">{{t "pipe.retry"}}</summary>406
<div class="menu-panel piperetry" data-pipe="{{.ID}}">407
<form method="post" action="/{{.Path}}/-/pipelines/{{.ID}}/retry">408
<input type="hidden" name="csrf" value="{{.CSRF}}">409
<input type="hidden" name="mode" value="all">410
<input type="hidden" name="back" value="{{.Back}}">411
<button type="submit">{{t "pipe.retry.all"}}</button>412
</form>413
<form method="post" action="/{{.Path}}/-/pipelines/{{.ID}}/retry" data-retry-failed data-pipe="{{.ID}}"{{if not .Failed}} hidden{{end}}>414
<input type="hidden" name="csrf" value="{{.CSRF}}">415
<input type="hidden" name="mode" value="failed">416
<input type="hidden" name="back" value="{{.Back}}">417
<button type="submit">{{t "pipe.retry.failed"}}</button>418
</form>419
</div>420
</details>421
{{end}}422
{{end}}423
398
{{define "job"}}424
{{define "job"}}399
{{template "layout-start" .}}425
{{template "layout-start" .}}400
<header class="pagehead"><h1>{{with .JobPage}}{{.Name}}{{end}}</h1></header>426
<header class="pagehead"><h1>{{with .JobPage}}{{.Name}}{{end}}</h1></header>@@ -348,11 +348,12 @@
348
348
<div class="muted">{{template "commitwho" .Head}} · {{.Head.When}}</div>
349
349
</div>
350
350
{{if and .Head.PipeStatus (eq .Page "overview")}}
351
<span class="headpipe" data-head-pipe{{if .Head.PipeLive}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}" data-live-on="1"{{end}}>
351
<span class="headpipe" data-head-pipe data-pipe="{{.Head.PipeID}}"{{if .Head.PipeLive}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}" data-live-on="1"{{end}}>
352
352
<a class="status {{.Head.PipeStatus}}" data-pipe-status href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span><span data-pipe-label>{{.Head.PipeLabel}}</span></a>
353
353
<span class="muted" data-pipe-duration>{{.Head.PipeDuration}}</span>
354
354
{{template "stage-dots" .}}
355
355
</span>
356
{{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" .Head.PipeID "Failed" .Head.PipeFailed "Back" (printf "/%s" .Repo.FullPath))}}
356
357
{{else if .Head.PipeStatus}}<a class="status {{.Head.PipeStatus}}" href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span>{{.Head.PipeLabel}}</a>{{end}}
357
358
<code class="sha">{{slice .Head.SHA 0 8}}</code>
358
359
<button type="button" class="iconbtn" data-copy="{{.Head.SHA}}" aria-label="{{t "action.copy"}}">{{t "action.copy"}}</button>
@@ -348,11 +348,12 @@348
<div class="muted">{{template "commitwho" .Head}} · {{.Head.When}}</div>348
<div class="muted">{{template "commitwho" .Head}} · {{.Head.When}}</div>349
</div>349
</div>350
{{if and .Head.PipeStatus (eq .Page "overview")}}350
{{if and .Head.PipeStatus (eq .Page "overview")}}351
<span class="headpipe" data-head-pipe{{if .Head.PipeLive}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}" data-live-on="1"{{end}}>351
<span class="headpipe" data-head-pipe data-pipe="{{.Head.PipeID}}"{{if .Head.PipeLive}} data-live="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}" data-live-on="1"{{end}}>352
<a class="status {{.Head.PipeStatus}}" data-pipe-status href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span><span data-pipe-label>{{.Head.PipeLabel}}</span></a>352
<a class="status {{.Head.PipeStatus}}" data-pipe-status href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span><span data-pipe-label>{{.Head.PipeLabel}}</span></a>353
<span class="muted" data-pipe-duration>{{.Head.PipeDuration}}</span>353
<span class="muted" data-pipe-duration>{{.Head.PipeDuration}}</span>354
{{template "stage-dots" .}}354
{{template "stage-dots" .}}355
</span>355
</span>356
{{template "pipe-retry" (dict "Can" .CanWrite "CSRF" .CSRF "Path" .Repo.FullPath "ID" .Head.PipeID "Failed" .Head.PipeFailed "Back" (printf "/%s" .Repo.FullPath))}}356
{{else if .Head.PipeStatus}}<a class="status {{.Head.PipeStatus}}" href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span>{{.Head.PipeLabel}}</a>{{end}}357
{{else if .Head.PipeStatus}}<a class="status {{.Head.PipeStatus}}" href="/{{.Repo.FullPath}}/-/pipelines/{{.Head.PipeID}}"><span class="mark"></span>{{.Head.PipeLabel}}</a>{{end}}357
<code class="sha">{{slice .Head.SHA 0 8}}</code>358
<code class="sha">{{slice .Head.SHA 0 8}}</code>358
<button type="button" class="iconbtn" data-copy="{{.Head.SHA}}" aria-label="{{t "action.copy"}}">{{t "action.copy"}}</button>359
<button type="button" class="iconbtn" data-copy="{{.Head.SHA}}" aria-label="{{t "action.copy"}}">{{t "action.copy"}}</button>@@ -0,0 +1,177 @@
1
package store
2
3
import "context"
4
5
// ClonePipeline maakt een nieuwe pipeline op dezelfde commit, ref, bron
6
// en merge-requestkoppeling. De jobs beginnen opnieuw. Een geslaagde
7
// job van de oude pipeline blijft daar staan.
8
func (s *Store) ClonePipeline(ctx context.Context, repoID, id int64) (int64, error) {
9
p, err := s.PipelineByID(ctx, repoID, id)
10
if err != nil {
11
return 0, err
12
}
13
jobs, err := s.JobsByPipeline(ctx, id)
14
if err != nil {
15
return 0, err
16
}
17
tx, err := s.db.BeginTx(ctx, nil)
18
if err != nil {
19
return 0, err
20
}
21
defer func() { _ = tx.Rollback() }()
22
res, err := tx.ExecContext(ctx, `INSERT INTO pipelines (repo_id, commit_sha, ref, status, source, actor, commit_title, created_at, mr_id, target_sha)
23
VALUES (?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?)`,
24
p.RepoID, p.CommitSHA, p.Ref, p.Source, p.Actor, p.CommitTitle, now(), p.MRID, p.TargetSHA)
25
if err != nil {
26
return 0, err
27
}
28
newID, _ := res.LastInsertId()
29
if err := insertPipelineJobs(ctx, tx, newID, p.RepoID, jobsToNew(jobs)); err != nil {
30
return 0, err
31
}
32
if err := tx.Commit(); err != nil {
33
return 0, err
34
}
35
return newID, nil
36
}
37
38
// RetryFailedJobs zet mislukte jobs terug in de wachtrij, plus de jobs
39
// die daardoor niets deden: overgeslagen of nooit gestarte jobs die via
40
// needs of een latere stage van een mislukte job afhangen.
41
// Geslaagde jobs blijven. false betekent dat er geen mislukte job is.
42
func (s *Store) RetryFailedJobs(ctx context.Context, pipelineID int64) (bool, error) {
43
jobs, err := s.JobsByPipeline(ctx, pipelineID)
44
if err != nil {
45
return false, err
46
}
47
plan := retryPlan(jobs)
48
if plan == nil {
49
return false, nil
50
}
51
tx, err := s.db.BeginTx(ctx, nil)
52
if err != nil {
53
return false, err
54
}
55
defer func() { _ = tx.Rollback() }()
56
for id, status := range plan {
57
if _, err := tx.ExecContext(ctx, `UPDATE jobs SET status = ?, log = '', attempt = 0, finished_at = NULL, started_at = NULL WHERE id = ? AND pipeline_id = ?`, status, id, pipelineID); err != nil {
58
return false, err
59
}
60
}
61
if err := tx.Commit(); err != nil {
62
return false, err
63
}
64
if _, err := s.RefreshPipelineStatus(ctx, pipelineID); err != nil {
65
return false, err
66
}
67
return true, nil
68
}
69
70
func jobsToNew(jobs []Job) []NewJob {
71
out := make([]NewJob, len(jobs))
72
for i, j := range jobs {
73
out[i] = NewJob{
74
Name: j.Name, Stage: j.Stage, Position: j.Position,
75
Image: j.Image, Before: j.Before, Script: j.Script, After: j.After,
76
Needs: j.Needs, Optional: j.Optional, NeedsSet: j.NeedsSet,
77
Variables: j.Variables, ArtifactPaths: j.ArtifactPaths, ArtifactExpire: j.ArtifactExpire,
78
AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout,
79
Tags: j.Tags, CachePaths: j.CachePaths, CacheKey: j.CacheKey,
80
ResourceGroup: j.ResourceGroup, RetryMax: j.RetryMax, RetryWhen: j.RetryWhen,
81
Services: j.Services, Environment: j.Environment, EnvironmentURL: j.EnvironmentURL,
82
Interruptible: j.Interruptible,
83
}
84
}
85
return out
86
}
87
88
// retryPlan kiest per job de nieuwe status.
89
// nil betekent dat geen enkele job de status failed heeft.
90
func retryPlan(jobs []Job) map[int64]string {
91
failed := map[int]bool{}
92
hasFailed := false
93
for i, j := range jobs {
94
if j.Status == "failed" {
95
failed[i] = true
96
hasFailed = true
97
}
98
}
99
if !hasFailed {
100
return nil
101
}
102
preds := predecessorIndexes(jobs)
103
down := map[int]bool{}
104
for {
105
moved := false
106
for i, j := range jobs {
107
if failed[i] || down[i] || !jobIdle(j.Status) {
108
continue
109
}
110
for _, p := range preds[i] {
111
if failed[p] || down[p] {
112
down[i] = true
113
moved = true
114
break
115
}
116
}
117
}
118
if !moved {
119
break
120
}
121
}
122
out := make(map[int64]string, len(failed)+len(down))
123
for i, j := range jobs {
124
switch {
125
case failed[i]:
126
out[j.ID] = "queued"
127
case down[i]:
128
if j.When == "manual" {
129
out[j.ID] = "manual"
130
} else {
131
out[j.ID] = "queued"
132
}
133
}
134
}
135
return out
136
}
137
138
// jobIdle is een job die nog niet gedraaid heeft of is overgeslagen.
139
func jobIdle(status string) bool {
140
switch status {
141
case "skipped", "created", "pending", "queued", "manual", "":
142
return true
143
default:
144
return false
145
}
146
}
147
148
// predecessorIndexes volgt needs als die gezet zijn, ook bij een lege lijst.
149
// Zonder needs zijn alle jobs in eerdere stages de voorgangers.
150
func predecessorIndexes(jobs []Job) [][]int {
151
byName := map[string][]int{}
152
stagePos := map[string]int{}
153
order := 0
154
for i, j := range jobs {
155
byName[j.Name] = append(byName[j.Name], i)
156
if _, ok := stagePos[j.Stage]; !ok {
157
stagePos[j.Stage] = order
158
order++
159
}
160
}
161
out := make([][]int, len(jobs))
162
for i, j := range jobs {
163
if j.NeedsSet {
164
for _, n := range j.Needs {
165
out[i] = append(out[i], byName[n]...)
166
}
167
continue
168
}
169
my := stagePos[j.Stage]
170
for k, other := range jobs {
171
if k != i && stagePos[other.Stage] < my {
172
out[i] = append(out[i], k)
173
}
174
}
175
}
176
return out
177
}
@@ -0,0 +1,177 @@1
package store2
3
import "context"4
5
// ClonePipeline maakt een nieuwe pipeline op dezelfde commit, ref, bron6
// en merge-requestkoppeling. De jobs beginnen opnieuw. Een geslaagde7
// job van de oude pipeline blijft daar staan.8
func (s *Store) ClonePipeline(ctx context.Context, repoID, id int64) (int64, error) {9
p, err := s.PipelineByID(ctx, repoID, id)10
if err != nil {11
return 0, err12
}13
jobs, err := s.JobsByPipeline(ctx, id)14
if err != nil {15
return 0, err16
}17
tx, err := s.db.BeginTx(ctx, nil)18
if err != nil {19
return 0, err20
}21
defer func() { _ = tx.Rollback() }()22
res, err := tx.ExecContext(ctx, `INSERT INTO pipelines (repo_id, commit_sha, ref, status, source, actor, commit_title, created_at, mr_id, target_sha)23
VALUES (?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?)`,24
p.RepoID, p.CommitSHA, p.Ref, p.Source, p.Actor, p.CommitTitle, now(), p.MRID, p.TargetSHA)25
if err != nil {26
return 0, err27
}28
newID, _ := res.LastInsertId()29
if err := insertPipelineJobs(ctx, tx, newID, p.RepoID, jobsToNew(jobs)); err != nil {30
return 0, err31
}32
if err := tx.Commit(); err != nil {33
return 0, err34
}35
return newID, nil36
}37
38
// RetryFailedJobs zet mislukte jobs terug in de wachtrij, plus de jobs39
// die daardoor niets deden: overgeslagen of nooit gestarte jobs die via40
// needs of een latere stage van een mislukte job afhangen.41
// Geslaagde jobs blijven. false betekent dat er geen mislukte job is.42
func (s *Store) RetryFailedJobs(ctx context.Context, pipelineID int64) (bool, error) {43
jobs, err := s.JobsByPipeline(ctx, pipelineID)44
if err != nil {45
return false, err46
}47
plan := retryPlan(jobs)48
if plan == nil {49
return false, nil50
}51
tx, err := s.db.BeginTx(ctx, nil)52
if err != nil {53
return false, err54
}55
defer func() { _ = tx.Rollback() }()56
for id, status := range plan {57
if _, err := tx.ExecContext(ctx, `UPDATE jobs SET status = ?, log = '', attempt = 0, finished_at = NULL, started_at = NULL WHERE id = ? AND pipeline_id = ?`, status, id, pipelineID); err != nil {58
return false, err59
}60
}61
if err := tx.Commit(); err != nil {62
return false, err63
}64
if _, err := s.RefreshPipelineStatus(ctx, pipelineID); err != nil {65
return false, err66
}67
return true, nil68
}69
70
func jobsToNew(jobs []Job) []NewJob {71
out := make([]NewJob, len(jobs))72
for i, j := range jobs {73
out[i] = NewJob{74
Name: j.Name, Stage: j.Stage, Position: j.Position,75
Image: j.Image, Before: j.Before, Script: j.Script, After: j.After,76
Needs: j.Needs, Optional: j.Optional, NeedsSet: j.NeedsSet,77
Variables: j.Variables, ArtifactPaths: j.ArtifactPaths, ArtifactExpire: j.ArtifactExpire,78
AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout,79
Tags: j.Tags, CachePaths: j.CachePaths, CacheKey: j.CacheKey,80
ResourceGroup: j.ResourceGroup, RetryMax: j.RetryMax, RetryWhen: j.RetryWhen,81
Services: j.Services, Environment: j.Environment, EnvironmentURL: j.EnvironmentURL,82
Interruptible: j.Interruptible,83
}84
}85
return out86
}87
88
// retryPlan kiest per job de nieuwe status.89
// nil betekent dat geen enkele job de status failed heeft.90
func retryPlan(jobs []Job) map[int64]string {91
failed := map[int]bool{}92
hasFailed := false93
for i, j := range jobs {94
if j.Status == "failed" {95
failed[i] = true96
hasFailed = true97
}98
}99
if !hasFailed {100
return nil101
}102
preds := predecessorIndexes(jobs)103
down := map[int]bool{}104
for {105
moved := false106
for i, j := range jobs {107
if failed[i] || down[i] || !jobIdle(j.Status) {108
continue109
}110
for _, p := range preds[i] {111
if failed[p] || down[p] {112
down[i] = true113
moved = true114
break115
}116
}117
}118
if !moved {119
break120
}121
}122
out := make(map[int64]string, len(failed)+len(down))123
for i, j := range jobs {124
switch {125
case failed[i]:126
out[j.ID] = "queued"127
case down[i]:128
if j.When == "manual" {129
out[j.ID] = "manual"130
} else {131
out[j.ID] = "queued"132
}133
}134
}135
return out136
}137
138
// jobIdle is een job die nog niet gedraaid heeft of is overgeslagen.139
func jobIdle(status string) bool {140
switch status {141
case "skipped", "created", "pending", "queued", "manual", "":142
return true143
default:144
return false145
}146
}147
148
// predecessorIndexes volgt needs als die gezet zijn, ook bij een lege lijst.149
// Zonder needs zijn alle jobs in eerdere stages de voorgangers.150
func predecessorIndexes(jobs []Job) [][]int {151
byName := map[string][]int{}152
stagePos := map[string]int{}153
order := 0154
for i, j := range jobs {155
byName[j.Name] = append(byName[j.Name], i)156
if _, ok := stagePos[j.Stage]; !ok {157
stagePos[j.Stage] = order158
order++159
}160
}161
out := make([][]int, len(jobs))162
for i, j := range jobs {163
if j.NeedsSet {164
for _, n := range j.Needs {165
out[i] = append(out[i], byName[n]...)166
}167
continue168
}169
my := stagePos[j.Stage]170
for k, other := range jobs {171
if k != i && stagePos[other.Stage] < my {172
out[i] = append(out[i], k)173
}174
}175
}176
return out177
}@@ -0,0 +1,141 @@
1
package store
2
3
import (
4
"context"
5
"testing"
6
)
7
8
func TestRetryPlanFollowsNeedsAndLaterStages(t *testing.T) {
9
jobs := []Job{
10
{ID: 1, Name: "lint", Stage: "test", Status: "success"},
11
{ID: 2, Name: "unit", Stage: "test", Status: "failed"},
12
{ID: 3, Name: "soft", Stage: "test", Status: "failed", AllowFailure: true},
13
{ID: 4, Name: "side", Stage: "test", Status: "skipped"},
14
{ID: 5, Name: "pack", Stage: "package", Status: "skipped"},
15
{ID: 6, Name: "aur", Stage: "package", Status: "success", NeedsSet: true},
16
{ID: 7, Name: "release", Stage: "package", Status: "skipped", Needs: []string{"pack"}, NeedsSet: true},
17
{ID: 8, Name: "pages", Stage: "package", Status: "skipped", When: "manual"},
18
{ID: 9, Name: "note", Stage: "package", Status: "canceled"},
19
{ID: 10, Name: "hold", Stage: "package", Status: "created"},
20
{ID: 11, Name: "wait", Stage: "package", Status: "pending"},
21
{ID: 12, Name: "run", Stage: "package", Status: "running"},
22
}
23
plan := retryPlan(jobs)
24
want := map[int64]string{
25
2: "queued", 3: "queued", 5: "queued", 7: "queued",
26
8: "manual", 10: "queued", 11: "queued",
27
}
28
if len(plan) != len(want) {
29
t.Fatalf("plan %v, wil %v", plan, want)
30
}
31
for id, status := range want {
32
if plan[id] != status {
33
t.Fatalf("job %d is %q, wil %q in %v", id, plan[id], status, plan)
34
}
35
}
36
if retryPlan([]Job{{ID: 1, Name: "lint", Status: "success"}, {ID: 2, Name: "pack", Status: "skipped"}}) != nil {
37
t.Fatal("zonder mislukte job is er niets om opnieuw te draaien")
38
}
39
}
40
41
func TestCloneAndRetryFailedJobs(t *testing.T) {
42
s := openTest(t)
43
ctx := context.Background()
44
tim, err := s.UpsertUser(ctx, 1, "tim", "", false)
45
if err != nil {
46
t.Fatal(err)
47
}
48
repo, err := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")
49
if err != nil {
50
t.Fatal(err)
51
}
52
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
53
ref := "refs/heads/topic"
54
target := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
55
id, err := s.CreatePipeline(ctx, repo.ID, sha, ref, "merge_request_event", "tim", "titel", []NewJob{
56
{Name: "lint", Stage: "test", Position: 0, Script: "exit 0"},
57
{Name: "unit", Stage: "test", Position: 1, Script: "exit 1"},
58
{Name: "pack", Stage: "deploy", Position: 2, Script: "exit 0"},
59
{Name: "pages", Stage: "deploy", Position: 3, Script: "exit 0", When: "manual"},
60
{Name: "aur", Stage: "deploy", Position: 4, Script: "echo aur", NeedsSet: true},
61
})
62
if err != nil {
63
t.Fatal(err)
64
}
65
if _, err := s.db.ExecContext(ctx, `UPDATE pipelines SET mr_id = ?, target_sha = ? WHERE id = ?`, 9, target, id); err != nil {
66
t.Fatal(err)
67
}
68
set := func(name, status string) {
69
t.Helper()
70
if _, err := s.db.ExecContext(ctx, `UPDATE jobs SET status = ? WHERE pipeline_id = ? AND name = ?`, status, id, name); err != nil {
71
t.Fatal(err)
72
}
73
}
74
set("lint", "success")
75
set("unit", "failed")
76
set("pack", "skipped")
77
set("pages", "skipped")
78
set("aur", "success")
79
if _, err := s.RefreshPipelineStatus(ctx, id); err != nil {
80
t.Fatal(err)
81
}
82
83
ok, err := s.RetryFailedJobs(ctx, id)
84
if err != nil || !ok {
85
t.Fatalf("retry: %v %v", ok, err)
86
}
87
got := jobStatus(t, s, id)
88
if got["lint"] != "success" || got["aur"] != "success" {
89
t.Fatalf("geslaagd hoort te blijven: %v", got)
90
}
91
if got["unit"] != "queued" || got["pack"] != "queued" || got["pages"] != "manual" {
92
t.Fatalf("opnieuw: %v", got)
93
}
94
p, err := s.PipelineByID(ctx, repo.ID, id)
95
if err != nil || p.Status != "running" {
96
t.Fatalf("status na retry: %+v %v", p, err)
97
}
98
again, err := s.RetryFailedJobs(ctx, id)
99
if err != nil || again {
100
t.Fatalf("tweede keer zonder mislukking: %v %v", again, err)
101
}
102
103
newID, err := s.ClonePipeline(ctx, repo.ID, id)
104
if err != nil || newID == id {
105
t.Fatalf("kloon %d %v", newID, err)
106
}
107
clone, err := s.PipelineByID(ctx, repo.ID, newID)
108
if err != nil {
109
t.Fatal(err)
110
}
111
if clone.CommitSHA != sha || clone.Ref != ref || clone.Source != "merge_request_event" || clone.Actor != "tim" || clone.CommitTitle != "titel" || clone.MRID != 9 || clone.TargetSHA != target || clone.Status != "queued" {
112
t.Fatalf("kloon wijkt af: %+v", clone)
113
}
114
cloned := jobStatus(t, s, newID)
115
if cloned["lint"] != "queued" || cloned["unit"] != "queued" || cloned["pack"] != "queued" || cloned["aur"] != "queued" || cloned["pages"] != "manual" {
116
t.Fatalf("nieuwe jobs: %v", cloned)
117
}
118
kept := jobStatus(t, s, id)
119
if kept["lint"] != "success" || kept["unit"] != "queued" {
120
t.Fatalf("oude pipeline veranderde: %v", kept)
121
}
122
againID, created, err := s.OpenPipeline(ctx, repo.ID, sha, ref, "merge_request_event", "tim", "titel", target, 9, []NewJob{
123
{Name: "lint", Stage: "test", Script: "exit 0"},
124
})
125
if err != nil || created || againID != id {
126
t.Fatalf("dedupe hoort de oudste rij te houden: %d created=%v %v", againID, created, err)
127
}
128
}
129
130
func jobStatus(t *testing.T, s *Store, id int64) map[string]string {
131
t.Helper()
132
jobs, err := s.JobsByPipeline(context.Background(), id)
133
if err != nil {
134
t.Fatal(err)
135
}
136
out := map[string]string{}
137
for _, j := range jobs {
138
out[j.Name] = j.Status
139
}
140
return out
141
}
@@ -0,0 +1,141 @@1
package store2
3
import (4
"context"5
"testing"6
)7
8
func TestRetryPlanFollowsNeedsAndLaterStages(t *testing.T) {9
jobs := []Job{10
{ID: 1, Name: "lint", Stage: "test", Status: "success"},11
{ID: 2, Name: "unit", Stage: "test", Status: "failed"},12
{ID: 3, Name: "soft", Stage: "test", Status: "failed", AllowFailure: true},13
{ID: 4, Name: "side", Stage: "test", Status: "skipped"},14
{ID: 5, Name: "pack", Stage: "package", Status: "skipped"},15
{ID: 6, Name: "aur", Stage: "package", Status: "success", NeedsSet: true},16
{ID: 7, Name: "release", Stage: "package", Status: "skipped", Needs: []string{"pack"}, NeedsSet: true},17
{ID: 8, Name: "pages", Stage: "package", Status: "skipped", When: "manual"},18
{ID: 9, Name: "note", Stage: "package", Status: "canceled"},19
{ID: 10, Name: "hold", Stage: "package", Status: "created"},20
{ID: 11, Name: "wait", Stage: "package", Status: "pending"},21
{ID: 12, Name: "run", Stage: "package", Status: "running"},22
}23
plan := retryPlan(jobs)24
want := map[int64]string{25
2: "queued", 3: "queued", 5: "queued", 7: "queued",26
8: "manual", 10: "queued", 11: "queued",27
}28
if len(plan) != len(want) {29
t.Fatalf("plan %v, wil %v", plan, want)30
}31
for id, status := range want {32
if plan[id] != status {33
t.Fatalf("job %d is %q, wil %q in %v", id, plan[id], status, plan)34
}35
}36
if retryPlan([]Job{{ID: 1, Name: "lint", Status: "success"}, {ID: 2, Name: "pack", Status: "skipped"}}) != nil {37
t.Fatal("zonder mislukte job is er niets om opnieuw te draaien")38
}39
}40
41
func TestCloneAndRetryFailedJobs(t *testing.T) {42
s := openTest(t)43
ctx := context.Background()44
tim, err := s.UpsertUser(ctx, 1, "tim", "", false)45
if err != nil {46
t.Fatal(err)47
}48
repo, err := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")49
if err != nil {50
t.Fatal(err)51
}52
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"53
ref := "refs/heads/topic"54
target := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"55
id, err := s.CreatePipeline(ctx, repo.ID, sha, ref, "merge_request_event", "tim", "titel", []NewJob{56
{Name: "lint", Stage: "test", Position: 0, Script: "exit 0"},57
{Name: "unit", Stage: "test", Position: 1, Script: "exit 1"},58
{Name: "pack", Stage: "deploy", Position: 2, Script: "exit 0"},59
{Name: "pages", Stage: "deploy", Position: 3, Script: "exit 0", When: "manual"},60
{Name: "aur", Stage: "deploy", Position: 4, Script: "echo aur", NeedsSet: true},61
})62
if err != nil {63
t.Fatal(err)64
}65
if _, err := s.db.ExecContext(ctx, `UPDATE pipelines SET mr_id = ?, target_sha = ? WHERE id = ?`, 9, target, id); err != nil {66
t.Fatal(err)67
}68
set := func(name, status string) {69
t.Helper()70
if _, err := s.db.ExecContext(ctx, `UPDATE jobs SET status = ? WHERE pipeline_id = ? AND name = ?`, status, id, name); err != nil {71
t.Fatal(err)72
}73
}74
set("lint", "success")75
set("unit", "failed")76
set("pack", "skipped")77
set("pages", "skipped")78
set("aur", "success")79
if _, err := s.RefreshPipelineStatus(ctx, id); err != nil {80
t.Fatal(err)81
}82
83
ok, err := s.RetryFailedJobs(ctx, id)84
if err != nil || !ok {85
t.Fatalf("retry: %v %v", ok, err)86
}87
got := jobStatus(t, s, id)88
if got["lint"] != "success" || got["aur"] != "success" {89
t.Fatalf("geslaagd hoort te blijven: %v", got)90
}91
if got["unit"] != "queued" || got["pack"] != "queued" || got["pages"] != "manual" {92
t.Fatalf("opnieuw: %v", got)93
}94
p, err := s.PipelineByID(ctx, repo.ID, id)95
if err != nil || p.Status != "running" {96
t.Fatalf("status na retry: %+v %v", p, err)97
}98
again, err := s.RetryFailedJobs(ctx, id)99
if err != nil || again {100
t.Fatalf("tweede keer zonder mislukking: %v %v", again, err)101
}102
103
newID, err := s.ClonePipeline(ctx, repo.ID, id)104
if err != nil || newID == id {105
t.Fatalf("kloon %d %v", newID, err)106
}107
clone, err := s.PipelineByID(ctx, repo.ID, newID)108
if err != nil {109
t.Fatal(err)110
}111
if clone.CommitSHA != sha || clone.Ref != ref || clone.Source != "merge_request_event" || clone.Actor != "tim" || clone.CommitTitle != "titel" || clone.MRID != 9 || clone.TargetSHA != target || clone.Status != "queued" {112
t.Fatalf("kloon wijkt af: %+v", clone)113
}114
cloned := jobStatus(t, s, newID)115
if cloned["lint"] != "queued" || cloned["unit"] != "queued" || cloned["pack"] != "queued" || cloned["aur"] != "queued" || cloned["pages"] != "manual" {116
t.Fatalf("nieuwe jobs: %v", cloned)117
}118
kept := jobStatus(t, s, id)119
if kept["lint"] != "success" || kept["unit"] != "queued" {120
t.Fatalf("oude pipeline veranderde: %v", kept)121
}122
againID, created, err := s.OpenPipeline(ctx, repo.ID, sha, ref, "merge_request_event", "tim", "titel", target, 9, []NewJob{123
{Name: "lint", Stage: "test", Script: "exit 0"},124
})125
if err != nil || created || againID != id {126
t.Fatalf("dedupe hoort de oudste rij te houden: %d created=%v %v", againID, created, err)127
}128
}129
130
func jobStatus(t *testing.T, s *Store, id int64) map[string]string {131
t.Helper()132
jobs, err := s.JobsByPipeline(context.Background(), id)133
if err != nil {134
t.Fatal(err)135
}136
out := map[string]string{}137
for _, j := range jobs {138
out[j.Name] = j.Status139
}140
return out141
}