BlueGit

timfa bluegit

Merge branch 'cursor/ci-editor-65da' into 'master'

Pipeline-editor en mail bij een mislukte run

See merge request Timfa/bluegit!11

timfa Tim Falken · pushed by timfa

Authored 2026-10-22 · 3 weeks ago

Hash
9c228ee408d880fb775b12228c5f9ee41e1e0970
21 files changed +1172 −6
cmd/bluegit/main.go +3 −0
@@ -104,6 +104,9 @@ func serve(args []string) error {
104 104 Cfg: cfg, Store: st, Auth: provider, Dev: dev, Git: cfg.Git.Binary, HookBin: exe, Log: log,
105 105 StateDir: cfg.StatePath,
106 106 }
107 if m, ok := provider.(auth.Mailer); ok && cfg.Auth.ClientSecret != "" {
108 srv.Mail = m
109 }
107 110 var rn *runner.Runner
108 111 if cfg.CI.Enabled {
109 112 rn = &runner.Runner{
@@ -104,6 +104,9 @@ func serve(args []string) error {
104 Cfg: cfg, Store: st, Auth: provider, Dev: dev, Git: cfg.Git.Binary, HookBin: exe, Log: log,
104 Cfg: cfg, Store: st, Auth: provider, Dev: dev, Git: cfg.Git.Binary, HookBin: exe, Log: log,
105 StateDir: cfg.StatePath,
105 StateDir: cfg.StatePath,
106 }
106 }
107 if m, ok := provider.(auth.Mailer); ok && cfg.Auth.ClientSecret != "" {
108 srv.Mail = m
109 }
107 var rn *runner.Runner
110 var rn *runner.Runner
108 if cfg.CI.Enabled {
111 if cfg.CI.Enabled {
109 rn = &runner.Runner{
112 rn = &runner.Runner{
docs/CI.md +8 −0
@@ -68,6 +68,14 @@ Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT
68 68
69 69 `services`, `pages`, `workflow`, `spec`, een include van een ander project of van een URL, `parallel`/`matrix`, trigger- en child-pipelines, `environment`, `retry`, `coverage`, `id_tokens`, `inherit` en `hooks`. Die sleutels geven een waarschuwing in de editor en houden de rest van het bestand niet tegen. Er is één runner, in het BlueGit-proces, die Docker op dezelfde host aanroept.
70 70
71 ## Editor
72
73 `/-/ci/editor` opent `blue-ci-cd.yml` van een branch. De pagina kleurt YAML en zet fouten en waarschuwingen op het regelnummer. Opslaan maakt één commit op de gekozen branch. Bestaat die branch nog niet, dan wordt hij vanaf de geopende branch gemaakt. Een beschermde branch accepteert alleen een beheerder, dezelfde regel als een gewone push.
74
75 ## Mail bij een mislukte pipeline
76
77 Komt een pipeline op `failed`, dan gaat er één mail naar de gebruiker in `actor` (wie pushte, of wie de run met de hand startte). Dat loopt via `POST /api/email` op auth.timfalken.com, met de clientgegevens van BlueGit, niet met het token van de gebruiker. Onderwerp en tekst volgen `users.language`. De mail noemt de repository, het pipeline-nummer, de branch of tag, de jobs die zonder `allow_failure` faalden, en links naar de pipeline en het joblog. Zonder `client_secret` (de dev-modus) gaat er niets weg. Een mailfout laat de pipeline met rust.
78
71 79 ## Geheimen
72 80
73 81 Repositorygeheimen komen als omgevingsvariabele mee, zoals CI/CD-variabelen. Bij een groepsrepository eerst de groep, daarna de repository; dezelfde naam laat de repository winnen. Een geheim dat als beschermd is gemarkeerd, komt alleen mee als de branch of tag op een beschermd patroon past. Waarden van acht tekens of meer worden in het joblog vervangen door `[MASKED]`. `CI_JOB_TOKEN` ook.
@@ -68,6 +68,14 @@ Voorgedefinieerd, in rules en in de container: `CI`, `CI_COMMIT_SHA`, `CI_COMMIT
68
68
69`services`, `pages`, `workflow`, `spec`, een include van een ander project of van een URL, `parallel`/`matrix`, trigger- en child-pipelines, `environment`, `retry`, `coverage`, `id_tokens`, `inherit` en `hooks`. Die sleutels geven een waarschuwing in de editor en houden de rest van het bestand niet tegen. Er is één runner, in het BlueGit-proces, die Docker op dezelfde host aanroept.
69`services`, `pages`, `workflow`, `spec`, een include van een ander project of van een URL, `parallel`/`matrix`, trigger- en child-pipelines, `environment`, `retry`, `coverage`, `id_tokens`, `inherit` en `hooks`. Die sleutels geven een waarschuwing in de editor en houden de rest van het bestand niet tegen. Er is één runner, in het BlueGit-proces, die Docker op dezelfde host aanroept.
70
70
71## Editor
72
73`/-/ci/editor` opent `blue-ci-cd.yml` van een branch. De pagina kleurt YAML en zet fouten en waarschuwingen op het regelnummer. Opslaan maakt één commit op de gekozen branch. Bestaat die branch nog niet, dan wordt hij vanaf de geopende branch gemaakt. Een beschermde branch accepteert alleen een beheerder, dezelfde regel als een gewone push.
74
75## Mail bij een mislukte pipeline
76
77Komt een pipeline op `failed`, dan gaat er één mail naar de gebruiker in `actor` (wie pushte, of wie de run met de hand startte). Dat loopt via `POST /api/email` op auth.timfalken.com, met de clientgegevens van BlueGit, niet met het token van de gebruiker. Onderwerp en tekst volgen `users.language`. De mail noemt de repository, het pipeline-nummer, de branch of tag, de jobs die zonder `allow_failure` faalden, en links naar de pipeline en het joblog. Zonder `client_secret` (de dev-modus) gaat er niets weg. Een mailfout laat de pipeline met rust.
78
71## Geheimen
79## Geheimen
72
80
73Repositorygeheimen komen als omgevingsvariabele mee, zoals CI/CD-variabelen. Bij een groepsrepository eerst de groep, daarna de repository; dezelfde naam laat de repository winnen. Een geheim dat als beschermd is gemarkeerd, komt alleen mee als de branch of tag op een beschermd patroon past. Waarden van acht tekens of meer worden in het joblog vervangen door `[MASKED]`. `CI_JOB_TOKEN` ook.
81Repositorygeheimen komen als omgevingsvariabele mee, zoals CI/CD-variabelen. Bij een groepsrepository eerst de groep, daarna de repository; dezelfde naam laat de repository winnen. Een geheim dat als beschermd is gemarkeerd, komt alleen mee als de branch of tag op een beschermd patroon past. Waarden van acht tekens of meer worden in het joblog vervangen door `[MASKED]`. `CI_JOB_TOKEN` ook.
internal/auth/mail.go added +51 −0
@@ -0,0 +1,51 @@
1 package auth
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "strings"
10 )
11
12 // Mailer stuurt mail via de auth-server, zonder het e-mailadres te zien.
13 type Mailer interface {
14 SendEmail(ctx context.Context, username, subject, html string) error
15 }
16
17 // SendEmail post naar /api/email met de clientgegevens van deze site.
18 // Het bearer-token van de gebruiker hoort hier niet bij.
19 func (t *TimFalken) SendEmail(ctx context.Context, username, subject, htmlBody string) error {
20 if strings.TrimSpace(t.ClientID) == "" || strings.TrimSpace(t.Secret) == "" {
21 return fmt.Errorf("auth: geen clientgegevens voor mail")
22 }
23 payload, err := json.Marshal(map[string]string{
24 "client_id": t.ClientID,
25 "client_secret": t.Secret,
26 "username": username,
27 "subject": subject,
28 "html": htmlBody,
29 })
30 if err != nil {
31 return err
32 }
33 req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.BaseURL+"/api/email", bytes.NewReader(payload))
34 if err != nil {
35 return err
36 }
37 req.Header.Set("Content-Type", "application/json")
38 req.Header.Set("Accept", "application/json")
39 req.Header.Set("User-Agent", t.UserAgent)
40 var body struct {
41 OK bool `json:"ok"`
42 Error string `json:"error"`
43 }
44 if err := t.do(req, &body); err != nil {
45 return err
46 }
47 if !body.OK {
48 return fmt.Errorf("auth: email geweigerd")
49 }
50 return nil
51 }
@@ -0,0 +1,51 @@
1package auth
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "strings"
10)
11
12// Mailer stuurt mail via de auth-server, zonder het e-mailadres te zien.
13type Mailer interface {
14 SendEmail(ctx context.Context, username, subject, html string) error
15}
16
17// SendEmail post naar /api/email met de clientgegevens van deze site.
18// Het bearer-token van de gebruiker hoort hier niet bij.
19func (t *TimFalken) SendEmail(ctx context.Context, username, subject, htmlBody string) error {
20 if strings.TrimSpace(t.ClientID) == "" || strings.TrimSpace(t.Secret) == "" {
21 return fmt.Errorf("auth: geen clientgegevens voor mail")
22 }
23 payload, err := json.Marshal(map[string]string{
24 "client_id": t.ClientID,
25 "client_secret": t.Secret,
26 "username": username,
27 "subject": subject,
28 "html": htmlBody,
29 })
30 if err != nil {
31 return err
32 }
33 req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.BaseURL+"/api/email", bytes.NewReader(payload))
34 if err != nil {
35 return err
36 }
37 req.Header.Set("Content-Type", "application/json")
38 req.Header.Set("Accept", "application/json")
39 req.Header.Set("User-Agent", t.UserAgent)
40 var body struct {
41 OK bool `json:"ok"`
42 Error string `json:"error"`
43 }
44 if err := t.do(req, &body); err != nil {
45 return err
46 }
47 if !body.OK {
48 return fmt.Errorf("auth: email geweigerd")
49 }
50 return nil
51}
internal/auth/mail_test.go added +68 −0
@@ -0,0 +1,68 @@
1 package auth
2
3 import (
4 "context"
5 "encoding/json"
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11 )
12
13 func TestSendEmail(t *testing.T) {
14 var got struct {
15 ClientID string `json:"client_id"`
16 ClientSecret string `json:"client_secret"`
17 Username string `json:"username"`
18 Subject string `json:"subject"`
19 HTML string `json:"html"`
20 }
21 var authz, ctype, accept, ua string
22 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23 if r.URL.Path != "/api/email" || r.Method != http.MethodPost {
24 t.Errorf("pad %s %s", r.Method, r.URL.Path)
25 }
26 authz = r.Header.Get("Authorization")
27 ctype = r.Header.Get("Content-Type")
28 accept = r.Header.Get("Accept")
29 ua = r.Header.Get("User-Agent")
30 raw, _ := io.ReadAll(r.Body)
31 if err := json.Unmarshal(raw, &got); err != nil {
32 t.Error(err)
33 }
34 w.Header().Set("Content-Type", "application/json")
35 _, _ = w.Write([]byte(`{"ok":true}`))
36 }))
37 t.Cleanup(ts.Close)
38 client := NewTimFalken(ts.URL, "bluegit.test", "site-secret", "BlueGitAccountClient/0.1")
39 if err := client.SendEmail(context.Background(), "tim", "Pipeline failed", "<p>hallo</p>"); err != nil {
40 t.Fatal(err)
41 }
42 if authz != "" {
43 t.Fatal("mail gebruikt geen bearer-token")
44 }
45 if ctype != "application/json" || accept != "application/json" || ua != "BlueGitAccountClient/0.1" {
46 t.Fatalf("headers %s %s %s", ctype, accept, ua)
47 }
48 if got.ClientID != "bluegit.test" || got.ClientSecret != "site-secret" || got.Username != "tim" || got.Subject != "Pipeline failed" || got.HTML != "<p>hallo</p>" {
49 t.Fatalf("body klopt niet: id=%s user=%s subject=%s html=%s secret-ok=%v", got.ClientID, got.Username, got.Subject, got.HTML, got.ClientSecret == "site-secret")
50 }
51
52 client.Secret = ""
53 if err := client.SendEmail(context.Background(), "tim", "x", "<p>x</p>"); err == nil {
54 t.Fatal("zonder geheim hoort mail te weigeren")
55 }
56
57 denied := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 w.Header().Set("Content-Type", "application/json")
59 w.WriteHeader(http.StatusForbidden)
60 _, _ = w.Write([]byte(`{"error":"This user has turned off email from this site.","code":"email_opted_out"}`))
61 }))
62 t.Cleanup(denied.Close)
63 client = NewTimFalken(denied.URL, "bluegit.test", "site-secret", "BlueGitAccountClient/0.1")
64 err := client.SendEmail(context.Background(), "tim", "x", "<p>x</p>")
65 if err == nil || !strings.Contains(err.Error(), "turned off") {
66 t.Fatal(err)
67 }
68 }
@@ -0,0 +1,68 @@
1package auth
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11)
12
13func TestSendEmail(t *testing.T) {
14 var got struct {
15 ClientID string `json:"client_id"`
16 ClientSecret string `json:"client_secret"`
17 Username string `json:"username"`
18 Subject string `json:"subject"`
19 HTML string `json:"html"`
20 }
21 var authz, ctype, accept, ua string
22 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23 if r.URL.Path != "/api/email" || r.Method != http.MethodPost {
24 t.Errorf("pad %s %s", r.Method, r.URL.Path)
25 }
26 authz = r.Header.Get("Authorization")
27 ctype = r.Header.Get("Content-Type")
28 accept = r.Header.Get("Accept")
29 ua = r.Header.Get("User-Agent")
30 raw, _ := io.ReadAll(r.Body)
31 if err := json.Unmarshal(raw, &got); err != nil {
32 t.Error(err)
33 }
34 w.Header().Set("Content-Type", "application/json")
35 _, _ = w.Write([]byte(`{"ok":true}`))
36 }))
37 t.Cleanup(ts.Close)
38 client := NewTimFalken(ts.URL, "bluegit.test", "site-secret", "BlueGitAccountClient/0.1")
39 if err := client.SendEmail(context.Background(), "tim", "Pipeline failed", "<p>hallo</p>"); err != nil {
40 t.Fatal(err)
41 }
42 if authz != "" {
43 t.Fatal("mail gebruikt geen bearer-token")
44 }
45 if ctype != "application/json" || accept != "application/json" || ua != "BlueGitAccountClient/0.1" {
46 t.Fatalf("headers %s %s %s", ctype, accept, ua)
47 }
48 if got.ClientID != "bluegit.test" || got.ClientSecret != "site-secret" || got.Username != "tim" || got.Subject != "Pipeline failed" || got.HTML != "<p>hallo</p>" {
49 t.Fatalf("body klopt niet: id=%s user=%s subject=%s html=%s secret-ok=%v", got.ClientID, got.Username, got.Subject, got.HTML, got.ClientSecret == "site-secret")
50 }
51
52 client.Secret = ""
53 if err := client.SendEmail(context.Background(), "tim", "x", "<p>x</p>"); err == nil {
54 t.Fatal("zonder geheim hoort mail te weigeren")
55 }
56
57 denied := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 w.Header().Set("Content-Type", "application/json")
59 w.WriteHeader(http.StatusForbidden)
60 _, _ = w.Write([]byte(`{"error":"This user has turned off email from this site.","code":"email_opted_out"}`))
61 }))
62 t.Cleanup(denied.Close)
63 client = NewTimFalken(denied.URL, "bluegit.test", "site-secret", "BlueGitAccountClient/0.1")
64 err := client.SendEmail(context.Background(), "tim", "x", "<p>x</p>")
65 if err == nil || !strings.Contains(err.Error(), "turned off") {
66 t.Fatal(err)
67 }
68}
internal/ci/parse.go +9 −1
@@ -4,6 +4,7 @@
4 4 package ci
5 5
6 6 import (
7 "errors"
7 8 "fmt"
8 9 "strconv"
9 10 "strings"
@@ -37,7 +38,14 @@ func ParseIn(data []byte, read func(string) ([]byte, error)) (Pipeline, error) {
37 38 func Lint(data []byte, read func(string) ([]byte, error)) (Pipeline, []Issue) {
38 39 p, issues, err := parseBytes(data, read, nil)
39 40 if err != nil {
40 issues = append([]Issue{{Line: 1, Key: "ci.err.yaml", Args: []any{err.Error()}}}, issues...)
41 // parseBytes verpakt een YAML-fout al als ci.err.yaml. Nog eens
42 // verpakken geeft "invalid YAML: invalid YAML: ...".
43 line := Issue{Line: 1, Key: "ci.err.yaml", Args: []any{err.Error()}}
44 var ie i18n.Error
45 if errors.As(err, &ie) {
46 line.Key, line.Args = ie.Key, ie.Args
47 }
48 issues = append([]Issue{line}, issues...)
41 49 }
42 50 return p, issues
43 51 }
@@ -4,6 +4,7 @@
4package ci
4package ci
5
5
6import (
6import (
7 "errors"
7 "fmt"
8 "fmt"
8 "strconv"
9 "strconv"
9 "strings"
10 "strings"
@@ -37,7 +38,14 @@ func ParseIn(data []byte, read func(string) ([]byte, error)) (Pipeline, error) {
37func Lint(data []byte, read func(string) ([]byte, error)) (Pipeline, []Issue) {
38func Lint(data []byte, read func(string) ([]byte, error)) (Pipeline, []Issue) {
38 p, issues, err := parseBytes(data, read, nil)
39 p, issues, err := parseBytes(data, read, nil)
39 if err != nil {
40 if err != nil {
40 issues = append([]Issue{{Line: 1, Key: "ci.err.yaml", Args: []any{err.Error()}}}, issues...)
41 // parseBytes verpakt een YAML-fout al als ci.err.yaml. Nog eens
42 // verpakken geeft "invalid YAML: invalid YAML: ...".
43 line := Issue{Line: 1, Key: "ci.err.yaml", Args: []any{err.Error()}}
44 var ie i18n.Error
45 if errors.As(err, &ie) {
46 line.Key, line.Args = ie.Key, ie.Args
47 }
48 issues = append([]Issue{line}, issues...)
41 }
49 }
42 return p, issues
50 return p, issues
43}
51}
internal/i18n/catalog.go +20 −0
@@ -198,6 +198,26 @@ var en = map[string]string{
198 198 "pipe.src.push": "Push",
199 199 "pipe.src.web": "Web",
200 200
201 "edit.title": "Pipeline Editor",
202 "edit.clean": "No issues found",
203 "edit.issues": "%d issues",
204 "edit.issueOne": "%d issue",
205 "edit.warnings": "%d warnings",
206 "edit.warningOne": "%d warning",
207 "edit.message": "Commit message",
208 "edit.branch": "Branch",
209 "edit.save": "Commit",
210 "edit.same": "Nothing changed.",
211 "edit.saved": "Committed.",
212 "edit.fail": "The file could not be committed.",
213 "edit.protected": "This branch is protected.",
214 "edit.badBranch": "That branch name cannot be used.",
215 "edit.needMessage": "A commit message is required.",
216 "mail.fail.subject": "Pipeline #%d in %s failed",
217 "mail.fail.intro": "Pipeline #%d in %s on %s failed.",
218 "mail.fail.jobs": "Failed jobs",
219 "mail.fail.open": "Open the pipeline",
220
201 221 "rel.none": "No releases yet.",
202 222 "rel.bytes": "bytes",
203 223
@@ -198,6 +198,26 @@ var en = map[string]string{
198 "pipe.src.push": "Push",
198 "pipe.src.push": "Push",
199 "pipe.src.web": "Web",
199 "pipe.src.web": "Web",
200
200
201 "edit.title": "Pipeline Editor",
202 "edit.clean": "No issues found",
203 "edit.issues": "%d issues",
204 "edit.issueOne": "%d issue",
205 "edit.warnings": "%d warnings",
206 "edit.warningOne": "%d warning",
207 "edit.message": "Commit message",
208 "edit.branch": "Branch",
209 "edit.save": "Commit",
210 "edit.same": "Nothing changed.",
211 "edit.saved": "Committed.",
212 "edit.fail": "The file could not be committed.",
213 "edit.protected": "This branch is protected.",
214 "edit.badBranch": "That branch name cannot be used.",
215 "edit.needMessage": "A commit message is required.",
216 "mail.fail.subject": "Pipeline #%d in %s failed",
217 "mail.fail.intro": "Pipeline #%d in %s on %s failed.",
218 "mail.fail.jobs": "Failed jobs",
219 "mail.fail.open": "Open the pipeline",
220
201 "rel.none": "No releases yet.",
221 "rel.none": "No releases yet.",
202 "rel.bytes": "bytes",
222 "rel.bytes": "bytes",
203
223
internal/i18n/catalog_lang.go +57 −0
@@ -204,6 +204,25 @@ var nl = map[string]string{
204 204 "pipe.play": "Start",
205 205 "pipe.src.push": "Push",
206 206 "pipe.src.web": "Web",
207 "edit.title": "Pipeline-editor",
208 "edit.clean": "Geen problemen gevonden",
209 "edit.issues": "%d fouten",
210 "edit.issueOne": "%d fout",
211 "edit.warnings": "%d waarschuwingen",
212 "edit.warningOne": "%d waarschuwing",
213 "edit.message": "Commitbericht",
214 "edit.branch": "Branch",
215 "edit.save": "Commit",
216 "edit.same": "Er is niets gewijzigd.",
217 "edit.saved": "Gecommit.",
218 "edit.fail": "Het bestand kon niet worden gecommit.",
219 "edit.protected": "Deze branch is beschermd.",
220 "edit.badBranch": "Die branchnaam kan niet.",
221 "edit.needMessage": "Een commitbericht is verplicht.",
222 "mail.fail.subject": "Pipeline #%d in %s is mislukt",
223 "mail.fail.intro": "Pipeline #%d in %s op %s is mislukt.",
224 "mail.fail.jobs": "Mislukte jobs",
225 "mail.fail.open": "Open de pipeline",
207 226 "rel.none": "Nog geen releases.",
208 227 "rel.bytes": "bytes",
209 228 "set.onlyAdmin": "Alleen een beheerder wijzigt instellingen, geheimen en webhooks. Jouw rol: %s.",
@@ -706,6 +725,25 @@ var de = map[string]string{
706 725 "pipe.play": "Starten",
707 726 "pipe.src.push": "Push",
708 727 "pipe.src.web": "Web",
728 "edit.title": "Pipeline-Editor",
729 "edit.clean": "Keine Probleme gefunden",
730 "edit.issues": "%d Probleme",
731 "edit.issueOne": "%d Problem",
732 "edit.warnings": "%d Warnungen",
733 "edit.warningOne": "%d Warnung",
734 "edit.message": "Commit-Nachricht",
735 "edit.branch": "Branch",
736 "edit.save": "Committen",
737 "edit.same": "Nichts geändert.",
738 "edit.saved": "Committet.",
739 "edit.fail": "Die Datei konnte nicht committet werden.",
740 "edit.protected": "Dieser Branch ist geschützt.",
741 "edit.badBranch": "Dieser Branchname geht nicht.",
742 "edit.needMessage": "Eine Commit-Nachricht ist nötig.",
743 "mail.fail.subject": "Pipeline #%d in %s ist fehlgeschlagen",
744 "mail.fail.intro": "Pipeline #%d in %s auf %s ist fehlgeschlagen.",
745 "mail.fail.jobs": "Fehlgeschlagene Jobs",
746 "mail.fail.open": "Pipeline öffnen",
709 747 "rel.none": "Noch keine Releases.",
710 748 "rel.bytes": "Bytes",
711 749 "set.onlyAdmin": "Nur ein Betreuer ändert Einstellungen, Geheimnisse und Webhooks. Deine Rolle: %s.",
@@ -1208,6 +1246,25 @@ var fr = map[string]string{
1208 1246 "pipe.play": "Lancer",
1209 1247 "pipe.src.push": "Push",
1210 1248 "pipe.src.web": "Web",
1249 "edit.title": "Éditeur de pipeline",
1250 "edit.clean": "Aucun problème",
1251 "edit.issues": "%d problèmes",
1252 "edit.issueOne": "%d problème",
1253 "edit.warnings": "%d avertissements",
1254 "edit.warningOne": "%d avertissement",
1255 "edit.message": "Message de commit",
1256 "edit.branch": "Branche",
1257 "edit.save": "Commiter",
1258 "edit.same": "Rien n'a changé.",
1259 "edit.saved": "Commit créé.",
1260 "edit.fail": "Le fichier n'a pas pu être commité.",
1261 "edit.protected": "Cette branche est protégée.",
1262 "edit.badBranch": "Ce nom de branche est inutilisable.",
1263 "edit.needMessage": "Un message de commit est requis.",
1264 "mail.fail.subject": "Le pipeline #%d dans %s a échoué",
1265 "mail.fail.intro": "Le pipeline #%d dans %s sur %s a échoué.",
1266 "mail.fail.jobs": "Jobs échoués",
1267 "mail.fail.open": "Ouvrir le pipeline",
1211 1268 "rel.none": "Pas encore de versions.",
1212 1269 "rel.bytes": "octets",
1213 1270 "set.onlyAdmin": "Seul un mainteneur modifie les paramètres, les secrets et les webhooks. Ton rôle : %s.",
@@ -204,6 +204,25 @@ var nl = map[string]string{
204 "pipe.play": "Start",
204 "pipe.play": "Start",
205 "pipe.src.push": "Push",
205 "pipe.src.push": "Push",
206 "pipe.src.web": "Web",
206 "pipe.src.web": "Web",
207 "edit.title": "Pipeline-editor",
208 "edit.clean": "Geen problemen gevonden",
209 "edit.issues": "%d fouten",
210 "edit.issueOne": "%d fout",
211 "edit.warnings": "%d waarschuwingen",
212 "edit.warningOne": "%d waarschuwing",
213 "edit.message": "Commitbericht",
214 "edit.branch": "Branch",
215 "edit.save": "Commit",
216 "edit.same": "Er is niets gewijzigd.",
217 "edit.saved": "Gecommit.",
218 "edit.fail": "Het bestand kon niet worden gecommit.",
219 "edit.protected": "Deze branch is beschermd.",
220 "edit.badBranch": "Die branchnaam kan niet.",
221 "edit.needMessage": "Een commitbericht is verplicht.",
222 "mail.fail.subject": "Pipeline #%d in %s is mislukt",
223 "mail.fail.intro": "Pipeline #%d in %s op %s is mislukt.",
224 "mail.fail.jobs": "Mislukte jobs",
225 "mail.fail.open": "Open de pipeline",
207 "rel.none": "Nog geen releases.",
226 "rel.none": "Nog geen releases.",
208 "rel.bytes": "bytes",
227 "rel.bytes": "bytes",
209 "set.onlyAdmin": "Alleen een beheerder wijzigt instellingen, geheimen en webhooks. Jouw rol: %s.",
228 "set.onlyAdmin": "Alleen een beheerder wijzigt instellingen, geheimen en webhooks. Jouw rol: %s.",
@@ -706,6 +725,25 @@ var de = map[string]string{
706 "pipe.play": "Starten",
725 "pipe.play": "Starten",
707 "pipe.src.push": "Push",
726 "pipe.src.push": "Push",
708 "pipe.src.web": "Web",
727 "pipe.src.web": "Web",
728 "edit.title": "Pipeline-Editor",
729 "edit.clean": "Keine Probleme gefunden",
730 "edit.issues": "%d Probleme",
731 "edit.issueOne": "%d Problem",
732 "edit.warnings": "%d Warnungen",
733 "edit.warningOne": "%d Warnung",
734 "edit.message": "Commit-Nachricht",
735 "edit.branch": "Branch",
736 "edit.save": "Committen",
737 "edit.same": "Nichts geändert.",
738 "edit.saved": "Committet.",
739 "edit.fail": "Die Datei konnte nicht committet werden.",
740 "edit.protected": "Dieser Branch ist geschützt.",
741 "edit.badBranch": "Dieser Branchname geht nicht.",
742 "edit.needMessage": "Eine Commit-Nachricht ist nötig.",
743 "mail.fail.subject": "Pipeline #%d in %s ist fehlgeschlagen",
744 "mail.fail.intro": "Pipeline #%d in %s auf %s ist fehlgeschlagen.",
745 "mail.fail.jobs": "Fehlgeschlagene Jobs",
746 "mail.fail.open": "Pipeline öffnen",
709 "rel.none": "Noch keine Releases.",
747 "rel.none": "Noch keine Releases.",
710 "rel.bytes": "Bytes",
748 "rel.bytes": "Bytes",
711 "set.onlyAdmin": "Nur ein Betreuer ändert Einstellungen, Geheimnisse und Webhooks. Deine Rolle: %s.",
749 "set.onlyAdmin": "Nur ein Betreuer ändert Einstellungen, Geheimnisse und Webhooks. Deine Rolle: %s.",
@@ -1208,6 +1246,25 @@ var fr = map[string]string{
1208 "pipe.play": "Lancer",
1246 "pipe.play": "Lancer",
1209 "pipe.src.push": "Push",
1247 "pipe.src.push": "Push",
1210 "pipe.src.web": "Web",
1248 "pipe.src.web": "Web",
1249 "edit.title": "Éditeur de pipeline",
1250 "edit.clean": "Aucun problème",
1251 "edit.issues": "%d problèmes",
1252 "edit.issueOne": "%d problème",
1253 "edit.warnings": "%d avertissements",
1254 "edit.warningOne": "%d avertissement",
1255 "edit.message": "Message de commit",
1256 "edit.branch": "Branche",
1257 "edit.save": "Commiter",
1258 "edit.same": "Rien n'a changé.",
1259 "edit.saved": "Commit créé.",
1260 "edit.fail": "Le fichier n'a pas pu être commité.",
1261 "edit.protected": "Cette branche est protégée.",
1262 "edit.badBranch": "Ce nom de branche est inutilisable.",
1263 "edit.needMessage": "Un message de commit est requis.",
1264 "mail.fail.subject": "Le pipeline #%d dans %s a échoué",
1265 "mail.fail.intro": "Le pipeline #%d dans %s sur %s a échoué.",
1266 "mail.fail.jobs": "Jobs échoués",
1267 "mail.fail.open": "Ouvrir le pipeline",
1211 "rel.none": "Pas encore de versions.",
1268 "rel.none": "Pas encore de versions.",
1212 "rel.bytes": "octets",
1269 "rel.bytes": "octets",
1213 "set.onlyAdmin": "Seul un mainteneur modifie les paramètres, les secrets et les webhooks. Ton rôle : %s.",
1270 "set.onlyAdmin": "Seul un mainteneur modifie les paramètres, les secrets et les webhooks. Ton rôle : %s.",
internal/runner/runner.go +47 −0
@@ -84,6 +84,7 @@ func (r *Runner) Tick(ctx context.Context) {
84 84 }
85 85
86 86 func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
87 r.settle(ctx, p.ID)
87 88 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
88 89 if err != nil {
89 90 r.Log.Error("jobs", "err", err)
@@ -125,6 +126,51 @@ func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
125 126 }
126 127 }
127 128
129 // settle slaat jobs over die door een harde mislukking niet meer hoeven.
130 // Dat gebeurt meteen, ook als de pipelinestatus al op failed was gezet.
131 func (r *Runner) settle(ctx context.Context, pipelineID int64) {
132 for {
133 jobs, err := r.Store.JobsByPipeline(ctx, pipelineID)
134 if err != nil {
135 if r.Log != nil {
136 r.Log.Error("jobs", "err", err)
137 }
138 return
139 }
140 specs := make([]ci.Job, len(jobs))
141 status := map[string]string{}
142 for i, j := range jobs {
143 specs[i] = ci.Job{Name: j.Name, Stage: j.Stage, Needs: j.Needs, Optional: j.Optional, NeedsSet: j.NeedsSet, AllowFailure: j.AllowFailure, When: j.When}
144 status[j.Name] = j.Status
145 }
146 moved := false
147 for _, j := range jobs {
148 if j.Status != "queued" && j.Status != "manual" {
149 continue
150 }
151 switch ci.Gate(specs, status, specs[indexOf(jobs, j.Name)]) {
152 case "skip":
153 if err := r.Store.SkipJob(ctx, j.ID, "skipped because an earlier job failed\n"); err != nil && r.Log != nil {
154 r.Log.Error("skip", "err", err)
155 continue
156 }
157 status[j.Name] = "skipped"
158 moved = true
159 case "fail":
160 if err := r.Store.FinishJob(ctx, j.ID, "failed", "a required job is not part of this pipeline\n"); err != nil && r.Log != nil {
161 r.Log.Error("fail", "err", err)
162 continue
163 }
164 status[j.Name] = "failed"
165 moved = true
166 }
167 }
168 if !moved {
169 return
170 }
171 }
172 }
173
128 174 func indexOf(jobs []store.Job, name string) int {
129 175 for i, j := range jobs {
130 176 if j.Name == name {
@@ -150,6 +196,7 @@ func (r *Runner) execute(ctx context.Context, p store.Pipeline, job store.Job) {
150 196 if ferr := r.Store.FinishJob(context.Background(), job.ID, status, extra); ferr != nil {
151 197 r.Log.Error("job afronden", "err", ferr)
152 198 }
199 r.settle(context.Background(), p.ID)
153 200 final, rerr := r.Store.RefreshPipelineStatus(context.Background(), p.ID)
154 201 if rerr != nil {
155 202 r.Log.Error("pipelinestatus", "err", rerr)
@@ -84,6 +84,7 @@ func (r *Runner) Tick(ctx context.Context) {
84}
84}
85
85
86func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
86func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
87 r.settle(ctx, p.ID)
87 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
88 jobs, err := r.Store.JobsByPipeline(ctx, p.ID)
88 if err != nil {
89 if err != nil {
89 r.Log.Error("jobs", "err", err)
90 r.Log.Error("jobs", "err", err)
@@ -125,6 +126,51 @@ func (r *Runner) schedule(ctx context.Context, p store.Pipeline) {
125 }
126 }
126}
127}
127
128
129// settle slaat jobs over die door een harde mislukking niet meer hoeven.
130// Dat gebeurt meteen, ook als de pipelinestatus al op failed was gezet.
131func (r *Runner) settle(ctx context.Context, pipelineID int64) {
132 for {
133 jobs, err := r.Store.JobsByPipeline(ctx, pipelineID)
134 if err != nil {
135 if r.Log != nil {
136 r.Log.Error("jobs", "err", err)
137 }
138 return
139 }
140 specs := make([]ci.Job, len(jobs))
141 status := map[string]string{}
142 for i, j := range jobs {
143 specs[i] = ci.Job{Name: j.Name, Stage: j.Stage, Needs: j.Needs, Optional: j.Optional, NeedsSet: j.NeedsSet, AllowFailure: j.AllowFailure, When: j.When}
144 status[j.Name] = j.Status
145 }
146 moved := false
147 for _, j := range jobs {
148 if j.Status != "queued" && j.Status != "manual" {
149 continue
150 }
151 switch ci.Gate(specs, status, specs[indexOf(jobs, j.Name)]) {
152 case "skip":
153 if err := r.Store.SkipJob(ctx, j.ID, "skipped because an earlier job failed\n"); err != nil && r.Log != nil {
154 r.Log.Error("skip", "err", err)
155 continue
156 }
157 status[j.Name] = "skipped"
158 moved = true
159 case "fail":
160 if err := r.Store.FinishJob(ctx, j.ID, "failed", "a required job is not part of this pipeline\n"); err != nil && r.Log != nil {
161 r.Log.Error("fail", "err", err)
162 continue
163 }
164 status[j.Name] = "failed"
165 moved = true
166 }
167 }
168 if !moved {
169 return
170 }
171 }
172}
173
128func indexOf(jobs []store.Job, name string) int {
174func indexOf(jobs []store.Job, name string) int {
129 for i, j := range jobs {
175 for i, j := range jobs {
130 if j.Name == name {
176 if j.Name == name {
@@ -150,6 +196,7 @@ func (r *Runner) execute(ctx context.Context, p store.Pipeline, job store.Job) {
150 if ferr := r.Store.FinishJob(context.Background(), job.ID, status, extra); ferr != nil {
196 if ferr := r.Store.FinishJob(context.Background(), job.ID, status, extra); ferr != nil {
151 r.Log.Error("job afronden", "err", ferr)
197 r.Log.Error("job afronden", "err", ferr)
152 }
198 }
199 r.settle(context.Background(), p.ID)
153 final, rerr := r.Store.RefreshPipelineStatus(context.Background(), p.ID)
200 final, rerr := r.Store.RefreshPipelineStatus(context.Background(), p.ID)
154 if rerr != nil {
201 if rerr != nil {
155 r.Log.Error("pipelinestatus", "err", rerr)
202 r.Log.Error("pipelinestatus", "err", rerr)
internal/runner/settle_test.go added +58 −0
@@ -0,0 +1,58 @@
1 package runner
2
3 import (
4 "context"
5 "path/filepath"
6 "testing"
7
8 "gitlab.com/Timfa/bluegit/internal/store"
9 )
10
11 func TestSettleSkipsLaterStageButNotAllowFailure(t *testing.T) {
12 key := make([]byte, 32)
13 st, err := store.Open(filepath.Join(t.TempDir(), "bluegit.db"), key)
14 if err != nil {
15 t.Fatal(err)
16 }
17 t.Cleanup(func() { _ = st.Close() })
18 ctx := context.Background()
19 tim, err := st.UpsertUser(ctx, 1, "tim", "", false)
20 if err != nil {
21 t.Fatal(err)
22 }
23 repo, err := st.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")
24 if err != nil {
25 t.Fatal(err)
26 }
27 id, err := st.CreatePipeline(ctx, repo.ID, "abc", "refs/heads/master", "push", "tim", "fail", []store.NewJob{
28 {Name: "hard", Stage: "test", Script: "exit 1"},
29 {Name: "soft", Stage: "test", Script: "exit 1", AllowFailure: true},
30 {Name: "later", Stage: "deploy", Script: "exit 0"},
31 {Name: "anyway", Stage: "deploy", Script: "exit 0", When: "always"},
32 })
33 if err != nil {
34 t.Fatal(err)
35 }
36 jobs, _ := st.JobsByPipeline(ctx, id)
37 for _, j := range jobs {
38 if j.Name == "hard" || j.Name == "soft" {
39 if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil {
40 t.Fatal(err)
41 }
42 }
43 }
44 rn := &Runner{Store: st, Storage: t.TempDir()}
45 rn.settle(ctx, id)
46 got := map[string]string{}
47 jobs, _ = st.JobsByPipeline(ctx, id)
48 for _, j := range jobs {
49 got[j.Name] = j.Status
50 }
51 if got["later"] != "skipped" || got["anyway"] != "queued" || got["soft"] != "failed" {
52 t.Fatalf("status: %v", got)
53 }
54 status, err := st.RefreshPipelineStatus(ctx, id)
55 if err != nil || status != "running" {
56 t.Fatalf("always-job houdt de pipeline open: %s %v", status, err)
57 }
58 }
@@ -0,0 +1,58 @@
1package runner
2
3import (
4 "context"
5 "path/filepath"
6 "testing"
7
8 "gitlab.com/Timfa/bluegit/internal/store"
9)
10
11func TestSettleSkipsLaterStageButNotAllowFailure(t *testing.T) {
12 key := make([]byte, 32)
13 st, err := store.Open(filepath.Join(t.TempDir(), "bluegit.db"), key)
14 if err != nil {
15 t.Fatal(err)
16 }
17 t.Cleanup(func() { _ = st.Close() })
18 ctx := context.Background()
19 tim, err := st.UpsertUser(ctx, 1, "tim", "", false)
20 if err != nil {
21 t.Fatal(err)
22 }
23 repo, err := st.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")
24 if err != nil {
25 t.Fatal(err)
26 }
27 id, err := st.CreatePipeline(ctx, repo.ID, "abc", "refs/heads/master", "push", "tim", "fail", []store.NewJob{
28 {Name: "hard", Stage: "test", Script: "exit 1"},
29 {Name: "soft", Stage: "test", Script: "exit 1", AllowFailure: true},
30 {Name: "later", Stage: "deploy", Script: "exit 0"},
31 {Name: "anyway", Stage: "deploy", Script: "exit 0", When: "always"},
32 })
33 if err != nil {
34 t.Fatal(err)
35 }
36 jobs, _ := st.JobsByPipeline(ctx, id)
37 for _, j := range jobs {
38 if j.Name == "hard" || j.Name == "soft" {
39 if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil {
40 t.Fatal(err)
41 }
42 }
43 }
44 rn := &Runner{Store: st, Storage: t.TempDir()}
45 rn.settle(ctx, id)
46 got := map[string]string{}
47 jobs, _ = st.JobsByPipeline(ctx, id)
48 for _, j := range jobs {
49 got[j.Name] = j.Status
50 }
51 if got["later"] != "skipped" || got["anyway"] != "queued" || got["soft"] != "failed" {
52 t.Fatalf("status: %v", got)
53 }
54 status, err := st.RefreshPipelineStatus(ctx, id)
55 if err != nil || status != "running" {
56 t.Fatalf("always-job houdt de pipeline open: %s %v", status, err)
57 }
58}
internal/server/editor.go added +258 −0
@@ -0,0 +1,258 @@
1 package server
2
3 import (
4 "encoding/json"
5 "html/template"
6 "net/http"
7 "net/url"
8 "os"
9 "os/exec"
10 "strings"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/ci"
14 "gitlab.com/Timfa/bluegit/internal/gitrepo"
15 "gitlab.com/Timfa/bluegit/internal/highlight"
16 )
17
18 const ciFile = "blue-ci-cd.yml"
19 const ciMaxBytes = 200_000
20
21 // CIEditor is de Pipeline Editor voor blue-ci-cd.yml.
22 type CIEditor struct {
23 Ref string
24 Body string
25 Rows []EditorLine
26 Issues []LintRow
27 Errors int
28 Warnings int
29 Status string
30 Class string
31 }
32
33 // EditorLine is één gekleurde regel, met een markering als de linter daar klaagt.
34 type EditorLine struct {
35 N int
36 HTML template.HTML
37 Bad bool
38 }
39
40 // LintRow is één melding onder de editor.
41 type LintRow struct {
42 Line int `json:"line"`
43 Text string `json:"text"`
44 Warning bool `json:"warning"`
45 }
46
47 func (s *Server) ciEditor(w http.ResponseWriter, r *http.Request) {
48 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
49 if !ok {
50 return
51 }
52 ref := strings.TrimSpace(r.URL.Query().Get("ref"))
53 if ref == "" {
54 ref = r.URL.Query().Get("rev")
55 }
56 ref = safeRev(ref, repo.DefaultBranch)
57 v := s.repoView(w, r, repo, role, "edit.title")
58 v.Wide = true
59 v.Page = "editor"
60 v.Rev = ref
61 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
62 s.branchList(r, &v, dir)
63 body := s.ciFileAt(r, dir, ref)
64 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
65 model.Ref = ref
66 v.Editor = model
67 s.render(w, "editor", v)
68 }
69
70 func (s *Server) lintCI(w http.ResponseWriter, r *http.Request) {
71 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
72 if !ok || !s.csrfOK(w, r) {
73 return
74 }
75 body := r.FormValue("body")
76 if len(body) > ciMaxBytes {
77 http.Error(w, s.tr(r, "edit.fail"), http.StatusBadRequest)
78 return
79 }
80 ref := safeRev(r.FormValue("ref"), repo.DefaultBranch)
81 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
82 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
83 lines := make([]string, len(model.Rows))
84 var bad []int
85 for i, row := range model.Rows {
86 lines[i] = string(row.HTML)
87 if row.Bad {
88 bad = append(bad, row.N)
89 }
90 }
91 w.Header().Set("Content-Type", "application/json")
92 _ = json.NewEncoder(w).Encode(map[string]any{
93 "status": model.Status,
94 "statusClass": model.Class,
95 "errors": model.Errors,
96 "warnings": model.Warnings,
97 "issues": model.Issues,
98 "lines": lines,
99 "bad": bad,
100 })
101 }
102
103 func (s *Server) saveCIEditor(w http.ResponseWriter, r *http.Request) {
104 u, repo, role, ok := s.guard(w, r, access.RoleDeveloper)
105 if !ok || !s.csrfOK(w, r) {
106 return
107 }
108 base := safeRev(r.FormValue("base"), repo.DefaultBranch)
109 branch := strings.TrimSpace(r.FormValue("branch"))
110 if !validBranchName(branch) {
111 s.redirect(w, r, repoURL(repo)+"/-/ci/editor?ref="+url.QueryEscape(base), "", s.tr(r, "edit.badBranch"))
112 return
113 }
114 back := repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(branch)
115 message := strings.TrimSpace(r.FormValue("message"))
116 if message == "" {
117 s.redirect(w, r, back, "", s.tr(r, "edit.needMessage"))
118 return
119 }
120 body := r.FormValue("body")
121 if len(body) > ciMaxBytes {
122 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
123 return
124 }
125 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
126 if access.BranchProtected(patterns, branch) && !role.CanPushBranch(true) {
127 s.redirect(w, r, back, "", s.tr(r, "edit.protected"))
128 return
129 }
130 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
131 if err := s.ensureBranch(r, dir, branch, base); err != nil {
132 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
133 return
134 }
135 work, err := os.MkdirTemp("", "bluegit-edit-")
136 if err != nil {
137 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
138 return
139 }
140 defer os.RemoveAll(work)
141 email := u.Username + "@users.git.timfalken.com"
142 err = gitrepo.CommitOnBranch(r.Context(), s.Git, dir, work, branch, u.Username, email, message, "", map[string]string{ciFile: body}, role, patterns)
143 if err != nil {
144 low := strings.ToLower(err.Error())
145 if strings.Contains(low, "nothing to commit") || strings.Contains(low, "nothing added") {
146 s.redirect(w, r, back, "", s.tr(r, "edit.same"))
147 return
148 }
149 if s.Log != nil {
150 s.Log.Error("ci-editor", "err", err)
151 }
152 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
153 return
154 }
155 s.redirect(w, r, back, s.tr(r, "edit.saved"), "")
156 }
157
158 func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor {
159 _, issues := ci.Lint([]byte(body), read)
160 bad := map[int]bool{}
161 var rows []LintRow
162 errors, warnings := 0, 0
163 for _, is := range issues {
164 line := is.Line
165 if line < 1 {
166 line = 1
167 }
168 text := s.tr(r, is.Key, is.Args...)
169 rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning})
170 if is.Warning {
171 warnings++
172 continue
173 }
174 errors++
175 bad[line] = true
176 }
177 painted := highlight.Lines(ciFile, body)
178 if len(painted) == 0 {
179 painted = []string{""}
180 }
181 if strings.HasSuffix(body, "\n") {
182 painted = append(painted, "")
183 }
184 lines := make([]EditorLine, len(painted))
185 for i, htmlLine := range painted {
186 n := i + 1
187 lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]}
188 }
189 status, class := s.tr(r, "edit.clean"), "passed"
190 if errors > 0 {
191 key := "edit.issues"
192 if errors == 1 {
193 key = "edit.issueOne"
194 }
195 status, class = s.tr(r, key, errors), "failed"
196 } else if warnings > 0 {
197 key := "edit.warnings"
198 if warnings == 1 {
199 key = "edit.warningOne"
200 }
201 status, class = s.tr(r, key, warnings), "warning"
202 }
203 if rows == nil {
204 rows = []LintRow{}
205 }
206 return &CIEditor{
207 Body: body, Rows: lines, Issues: rows,
208 Errors: errors, Warnings: warnings, Status: status, Class: class,
209 }
210 }
211
212 func (s *Server) ciFileAt(r *http.Request, dir, ref string) string {
213 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output()
214 if err != nil || len(out) > ciMaxBytes {
215 return ""
216 }
217 return string(out)
218 }
219
220 func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) {
221 return func(path string) ([]byte, error) {
222 if !validInclude(path) {
223 return nil, os.ErrInvalid
224 }
225 return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
226 }
227 }
228
229 func (s *Server) ensureBranch(r *http.Request, dir, branch, base string) error {
230 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" {
231 return nil
232 }
233 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) == "" {
234 return os.ErrNotExist
235 }
236 cmd := exec.CommandContext(r.Context(), s.Git, "-C", dir, "branch", branch, base)
237 if out, err := cmd.CombinedOutput(); err != nil {
238 if s.Log != nil {
239 s.Log.Error("ci-branch", "err", err, "out", string(out))
240 }
241 return err
242 }
243 return nil
244 }
245
246 func validBranchName(s string) bool {
247 if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") {
248 return false
249 }
250 return !strings.ContainsAny(s, " \t\n~^:?*[\\")
251 }
252
253 func validInclude(p string) bool {
254 if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") {
255 return false
256 }
257 return true
258 }
@@ -0,0 +1,258 @@
1package server
2
3import (
4 "encoding/json"
5 "html/template"
6 "net/http"
7 "net/url"
8 "os"
9 "os/exec"
10 "strings"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/ci"
14 "gitlab.com/Timfa/bluegit/internal/gitrepo"
15 "gitlab.com/Timfa/bluegit/internal/highlight"
16)
17
18const ciFile = "blue-ci-cd.yml"
19const ciMaxBytes = 200_000
20
21// CIEditor is de Pipeline Editor voor blue-ci-cd.yml.
22type CIEditor struct {
23 Ref string
24 Body string
25 Rows []EditorLine
26 Issues []LintRow
27 Errors int
28 Warnings int
29 Status string
30 Class string
31}
32
33// EditorLine is één gekleurde regel, met een markering als de linter daar klaagt.
34type EditorLine struct {
35 N int
36 HTML template.HTML
37 Bad bool
38}
39
40// LintRow is één melding onder de editor.
41type LintRow struct {
42 Line int `json:"line"`
43 Text string `json:"text"`
44 Warning bool `json:"warning"`
45}
46
47func (s *Server) ciEditor(w http.ResponseWriter, r *http.Request) {
48 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
49 if !ok {
50 return
51 }
52 ref := strings.TrimSpace(r.URL.Query().Get("ref"))
53 if ref == "" {
54 ref = r.URL.Query().Get("rev")
55 }
56 ref = safeRev(ref, repo.DefaultBranch)
57 v := s.repoView(w, r, repo, role, "edit.title")
58 v.Wide = true
59 v.Page = "editor"
60 v.Rev = ref
61 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
62 s.branchList(r, &v, dir)
63 body := s.ciFileAt(r, dir, ref)
64 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
65 model.Ref = ref
66 v.Editor = model
67 s.render(w, "editor", v)
68}
69
70func (s *Server) lintCI(w http.ResponseWriter, r *http.Request) {
71 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
72 if !ok || !s.csrfOK(w, r) {
73 return
74 }
75 body := r.FormValue("body")
76 if len(body) > ciMaxBytes {
77 http.Error(w, s.tr(r, "edit.fail"), http.StatusBadRequest)
78 return
79 }
80 ref := safeRev(r.FormValue("ref"), repo.DefaultBranch)
81 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
82 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
83 lines := make([]string, len(model.Rows))
84 var bad []int
85 for i, row := range model.Rows {
86 lines[i] = string(row.HTML)
87 if row.Bad {
88 bad = append(bad, row.N)
89 }
90 }
91 w.Header().Set("Content-Type", "application/json")
92 _ = json.NewEncoder(w).Encode(map[string]any{
93 "status": model.Status,
94 "statusClass": model.Class,
95 "errors": model.Errors,
96 "warnings": model.Warnings,
97 "issues": model.Issues,
98 "lines": lines,
99 "bad": bad,
100 })
101}
102
103func (s *Server) saveCIEditor(w http.ResponseWriter, r *http.Request) {
104 u, repo, role, ok := s.guard(w, r, access.RoleDeveloper)
105 if !ok || !s.csrfOK(w, r) {
106 return
107 }
108 base := safeRev(r.FormValue("base"), repo.DefaultBranch)
109 branch := strings.TrimSpace(r.FormValue("branch"))
110 if !validBranchName(branch) {
111 s.redirect(w, r, repoURL(repo)+"/-/ci/editor?ref="+url.QueryEscape(base), "", s.tr(r, "edit.badBranch"))
112 return
113 }
114 back := repoURL(repo) + "/-/ci/editor?ref=" + url.QueryEscape(branch)
115 message := strings.TrimSpace(r.FormValue("message"))
116 if message == "" {
117 s.redirect(w, r, back, "", s.tr(r, "edit.needMessage"))
118 return
119 }
120 body := r.FormValue("body")
121 if len(body) > ciMaxBytes {
122 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
123 return
124 }
125 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
126 if access.BranchProtected(patterns, branch) && !role.CanPushBranch(true) {
127 s.redirect(w, r, back, "", s.tr(r, "edit.protected"))
128 return
129 }
130 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
131 if err := s.ensureBranch(r, dir, branch, base); err != nil {
132 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
133 return
134 }
135 work, err := os.MkdirTemp("", "bluegit-edit-")
136 if err != nil {
137 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
138 return
139 }
140 defer os.RemoveAll(work)
141 email := u.Username + "@users.git.timfalken.com"
142 err = gitrepo.CommitOnBranch(r.Context(), s.Git, dir, work, branch, u.Username, email, message, "", map[string]string{ciFile: body}, role, patterns)
143 if err != nil {
144 low := strings.ToLower(err.Error())
145 if strings.Contains(low, "nothing to commit") || strings.Contains(low, "nothing added") {
146 s.redirect(w, r, back, "", s.tr(r, "edit.same"))
147 return
148 }
149 if s.Log != nil {
150 s.Log.Error("ci-editor", "err", err)
151 }
152 s.redirect(w, r, back, "", s.tr(r, "edit.fail"))
153 return
154 }
155 s.redirect(w, r, back, s.tr(r, "edit.saved"), "")
156}
157
158func (s *Server) ciEditorModel(r *http.Request, body string, read func(string) ([]byte, error)) *CIEditor {
159 _, issues := ci.Lint([]byte(body), read)
160 bad := map[int]bool{}
161 var rows []LintRow
162 errors, warnings := 0, 0
163 for _, is := range issues {
164 line := is.Line
165 if line < 1 {
166 line = 1
167 }
168 text := s.tr(r, is.Key, is.Args...)
169 rows = append(rows, LintRow{Line: line, Text: text, Warning: is.Warning})
170 if is.Warning {
171 warnings++
172 continue
173 }
174 errors++
175 bad[line] = true
176 }
177 painted := highlight.Lines(ciFile, body)
178 if len(painted) == 0 {
179 painted = []string{""}
180 }
181 if strings.HasSuffix(body, "\n") {
182 painted = append(painted, "")
183 }
184 lines := make([]EditorLine, len(painted))
185 for i, htmlLine := range painted {
186 n := i + 1
187 lines[i] = EditorLine{N: n, HTML: template.HTML(htmlLine), Bad: bad[n]}
188 }
189 status, class := s.tr(r, "edit.clean"), "passed"
190 if errors > 0 {
191 key := "edit.issues"
192 if errors == 1 {
193 key = "edit.issueOne"
194 }
195 status, class = s.tr(r, key, errors), "failed"
196 } else if warnings > 0 {
197 key := "edit.warnings"
198 if warnings == 1 {
199 key = "edit.warningOne"
200 }
201 status, class = s.tr(r, key, warnings), "warning"
202 }
203 if rows == nil {
204 rows = []LintRow{}
205 }
206 return &CIEditor{
207 Body: body, Rows: lines, Issues: rows,
208 Errors: errors, Warnings: warnings, Status: status, Class: class,
209 }
210}
211
212func (s *Server) ciFileAt(r *http.Request, dir, ref string) string {
213 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+ciFile).Output()
214 if err != nil || len(out) > ciMaxBytes {
215 return ""
216 }
217 return string(out)
218}
219
220func (s *Server) includeFrom(r *http.Request, dir, ref string) func(string) ([]byte, error) {
221 return func(path string) ([]byte, error) {
222 if !validInclude(path) {
223 return nil, os.ErrInvalid
224 }
225 return exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
226 }
227}
228
229func (s *Server) ensureBranch(r *http.Request, dir, branch, base string) error {
230 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch) != "" {
231 return nil
232 }
233 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+base) == "" {
234 return os.ErrNotExist
235 }
236 cmd := exec.CommandContext(r.Context(), s.Git, "-C", dir, "branch", branch, base)
237 if out, err := cmd.CombinedOutput(); err != nil {
238 if s.Log != nil {
239 s.Log.Error("ci-branch", "err", err, "out", string(out))
240 }
241 return err
242 }
243 return nil
244}
245
246func validBranchName(s string) bool {
247 if s == "" || s == "@" || strings.HasPrefix(s, "-") || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.HasSuffix(s, ".") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") {
248 return false
249 }
250 return !strings.ContainsAny(s, " \t\n~^:?*[\\")
251}
252
253func validInclude(p string) bool {
254 if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, `\`) || strings.Contains(p, "..") {
255 return false
256 }
257 return true
258}
internal/server/editor_test.go added +165 −0
@@ -0,0 +1,165 @@
1 package server
2
3 import (
4 "encoding/json"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/auth"
15 "gitlab.com/Timfa/bluegit/internal/config"
16 "gitlab.com/Timfa/bluegit/internal/gitrepo"
17 "gitlab.com/Timfa/bluegit/internal/store"
18 )
19
20 func TestPipelineEditorAndProtectedBranch(t *testing.T) {
21 if _, err := exec.LookPath("git"); err != nil {
22 t.Skip("git ontbreekt")
23 }
24 storage := t.TempDir()
25 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
26 if err != nil {
27 t.Fatal(err)
28 }
29 t.Cleanup(func() { _ = st.Close() })
30 cfg := config.Config{
31 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
32 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
33 Git: config.Git{Binary: "git", DefaultBranch: "master"},
34 }
35 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
36 ts := httptest.NewServer(srv.Handler())
37 t.Cleanup(ts.Close)
38 jar, _ := cookiejar.New(nil)
39 tim := &http.Client{Jar: jar}
40 res, err := tim.Get(ts.URL + "/login")
41 if err != nil {
42 t.Fatal(err)
43 }
44 page := read(t, res)
45 res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{
46 "csrf": {csrfOf(t, page)}, "name": {"cied"}, "visibility": {"public"},
47 })
48 if err != nil {
49 t.Fatal(err)
50 }
51 page = read(t, res)
52 res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}})
53 if err != nil {
54 t.Fatal(err)
55 }
56 token := between(read(t, res), `<p class="code">`, "</p>")
57 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/cied.git"
58 work := t.TempDir()
59 git(t, work, "init", "-b", "master")
60 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
61 git(t, work, "config", "user.name", "tim")
62 yml := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo ok\n"
63 writeFile(t, filepath.Join(work, "blue-ci-cd.yml"), yml)
64 git(t, work, "add", "blue-ci-cd.yml")
65 git(t, work, "commit", "-m", "ci")
66 git(t, work, "remote", "add", "origin", remote)
67 git(t, work, "push", "-u", "origin", "master")
68 git(t, work, "checkout", "-b", "feature")
69 git(t, work, "push", "-u", "origin", "feature")
70
71 res, err = tim.Get(ts.URL + "/tim/cied/-/ci/editor?ref=feature")
72 if err != nil {
73 t.Fatal(err)
74 }
75 editor := read(t, res)
76 for _, want := range []string{"Pipeline Editor", "No issues found", "echo ok", "ci-gutter", "Commit message"} {
77 if !strings.Contains(editor, want) {
78 t.Errorf("editor mist %q", want)
79 }
80 }
81 csrf := csrfOf(t, editor)
82 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/lint", url.Values{
83 "csrf": {csrf}, "ref": {"feature"}, "body": {"stages: [\n"},
84 })
85 if err != nil {
86 t.Fatal(err)
87 }
88 raw := read(t, res)
89 var lint struct {
90 Status string `json:"status"`
91 StatusClass string `json:"statusClass"`
92 Issues []struct {
93 Line int `json:"line"`
94 Text string `json:"text"`
95 } `json:"issues"`
96 }
97 if err := json.Unmarshal([]byte(raw), &lint); err != nil {
98 t.Fatalf("lint json: %v\n%s", err, raw)
99 }
100 if lint.StatusClass != "failed" || len(lint.Issues) == 0 || strings.Count(lint.Issues[0].Text, "invalid YAML") != 1 {
101 t.Fatalf("lint: %+v", lint)
102 }
103
104 changed := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo changed\n"
105 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
106 "csrf": {csrf}, "base": {"feature"}, "branch": {"feature"}, "message": {"pas de editor aan"}, "body": {changed},
107 })
108 if err != nil {
109 t.Fatal(err)
110 }
111 saved := read(t, res)
112 if !strings.Contains(saved, "Committed.") || !strings.Contains(saved, "echo changed") {
113 t.Fatalf("na commit:\n%s", saved)
114 }
115 dir := gitrepo.Dir(storage, "tim", "cied")
116 if got := mustGit(t, dir, "show", "feature:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") {
117 t.Fatalf("bestand: %s", got)
118 }
119 if msg := mustGit(t, dir, "log", "-1", "--format=%s", "feature"); strings.TrimSpace(msg) != "pas de editor aan" {
120 t.Fatalf("bericht: %s", msg)
121 }
122
123 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
124 "csrf": {csrfOf(t, saved)}, "base": {"feature"}, "branch": {"ci-try"}, "message": {"nieuwe branch"}, "body": {changed},
125 })
126 if err != nil {
127 t.Fatal(err)
128 }
129 _ = read(t, res)
130 if got := mustGit(t, dir, "show", "ci-try:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") {
131 t.Fatalf("nieuwe branch: %s", got)
132 }
133
134 ctx := res.Request.Context()
135 repo, err := st.RepoByPath(ctx, "tim", "cied")
136 if err != nil {
137 t.Fatal(err)
138 }
139 ada, err := st.UpsertUser(ctx, 3, "ada", "", false)
140 if err != nil {
141 t.Fatal(err)
142 }
143 if err := st.SetMember(ctx, "repo", repo.ID, "ada", "developer"); err != nil {
144 t.Fatal(err)
145 }
146 adaC := clientAs(t, ts.URL, st, ada.ID)
147 res, err = adaC.Get(ts.URL + "/tim/cied/-/ci/editor?ref=master")
148 if err != nil {
149 t.Fatal(err)
150 }
151 adaPage := read(t, res)
152 res, err = adaC.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
153 "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"master"}, "message": {"ada op master"}, "body": {changed + "\n"},
154 })
155 if err != nil {
156 t.Fatal(err)
157 }
158 denied := read(t, res)
159 if !strings.Contains(denied, "This branch is protected.") {
160 t.Fatalf("beschermd:\n%s", denied)
161 }
162 if got := mustGit(t, dir, "show", "master:blue-ci-cd.yml"); strings.Contains(got, "echo changed") {
163 t.Fatalf("master veranderde:\n%s", got)
164 }
165 }
@@ -0,0 +1,165 @@
1package server
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/cookiejar"
7 "net/http/httptest"
8 "net/url"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "gitlab.com/Timfa/bluegit/internal/auth"
15 "gitlab.com/Timfa/bluegit/internal/config"
16 "gitlab.com/Timfa/bluegit/internal/gitrepo"
17 "gitlab.com/Timfa/bluegit/internal/store"
18)
19
20func TestPipelineEditorAndProtectedBranch(t *testing.T) {
21 if _, err := exec.LookPath("git"); err != nil {
22 t.Skip("git ontbreekt")
23 }
24 storage := t.TempDir()
25 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
26 if err != nil {
27 t.Fatal(err)
28 }
29 t.Cleanup(func() { _ = st.Close() })
30 cfg := config.Config{
31 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
32 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
33 Git: config.Git{Binary: "git", DefaultBranch: "master"},
34 }
35 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
36 ts := httptest.NewServer(srv.Handler())
37 t.Cleanup(ts.Close)
38 jar, _ := cookiejar.New(nil)
39 tim := &http.Client{Jar: jar}
40 res, err := tim.Get(ts.URL + "/login")
41 if err != nil {
42 t.Fatal(err)
43 }
44 page := read(t, res)
45 res, err = tim.PostForm(ts.URL+"/new/repo", url.Values{
46 "csrf": {csrfOf(t, page)}, "name": {"cied"}, "visibility": {"public"},
47 })
48 if err != nil {
49 t.Fatal(err)
50 }
51 page = read(t, res)
52 res, err = tim.PostForm(ts.URL+"/settings/tokens", url.Values{"csrf": {csrfOf(t, page)}, "name": {"laptop"}})
53 if err != nil {
54 t.Fatal(err)
55 }
56 token := between(read(t, res), `<p class="code">`, "</p>")
57 remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/cied.git"
58 work := t.TempDir()
59 git(t, work, "init", "-b", "master")
60 git(t, work, "config", "user.email", "tim@users.git.timfalken.com")
61 git(t, work, "config", "user.name", "tim")
62 yml := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo ok\n"
63 writeFile(t, filepath.Join(work, "blue-ci-cd.yml"), yml)
64 git(t, work, "add", "blue-ci-cd.yml")
65 git(t, work, "commit", "-m", "ci")
66 git(t, work, "remote", "add", "origin", remote)
67 git(t, work, "push", "-u", "origin", "master")
68 git(t, work, "checkout", "-b", "feature")
69 git(t, work, "push", "-u", "origin", "feature")
70
71 res, err = tim.Get(ts.URL + "/tim/cied/-/ci/editor?ref=feature")
72 if err != nil {
73 t.Fatal(err)
74 }
75 editor := read(t, res)
76 for _, want := range []string{"Pipeline Editor", "No issues found", "echo ok", "ci-gutter", "Commit message"} {
77 if !strings.Contains(editor, want) {
78 t.Errorf("editor mist %q", want)
79 }
80 }
81 csrf := csrfOf(t, editor)
82 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/lint", url.Values{
83 "csrf": {csrf}, "ref": {"feature"}, "body": {"stages: [\n"},
84 })
85 if err != nil {
86 t.Fatal(err)
87 }
88 raw := read(t, res)
89 var lint struct {
90 Status string `json:"status"`
91 StatusClass string `json:"statusClass"`
92 Issues []struct {
93 Line int `json:"line"`
94 Text string `json:"text"`
95 } `json:"issues"`
96 }
97 if err := json.Unmarshal([]byte(raw), &lint); err != nil {
98 t.Fatalf("lint json: %v\n%s", err, raw)
99 }
100 if lint.StatusClass != "failed" || len(lint.Issues) == 0 || strings.Count(lint.Issues[0].Text, "invalid YAML") != 1 {
101 t.Fatalf("lint: %+v", lint)
102 }
103
104 changed := "stages:\n - test\nunit:\n stage: test\n image: alpine:3.20\n script:\n - echo changed\n"
105 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
106 "csrf": {csrf}, "base": {"feature"}, "branch": {"feature"}, "message": {"pas de editor aan"}, "body": {changed},
107 })
108 if err != nil {
109 t.Fatal(err)
110 }
111 saved := read(t, res)
112 if !strings.Contains(saved, "Committed.") || !strings.Contains(saved, "echo changed") {
113 t.Fatalf("na commit:\n%s", saved)
114 }
115 dir := gitrepo.Dir(storage, "tim", "cied")
116 if got := mustGit(t, dir, "show", "feature:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") {
117 t.Fatalf("bestand: %s", got)
118 }
119 if msg := mustGit(t, dir, "log", "-1", "--format=%s", "feature"); strings.TrimSpace(msg) != "pas de editor aan" {
120 t.Fatalf("bericht: %s", msg)
121 }
122
123 res, err = tim.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
124 "csrf": {csrfOf(t, saved)}, "base": {"feature"}, "branch": {"ci-try"}, "message": {"nieuwe branch"}, "body": {changed},
125 })
126 if err != nil {
127 t.Fatal(err)
128 }
129 _ = read(t, res)
130 if got := mustGit(t, dir, "show", "ci-try:blue-ci-cd.yml"); !strings.Contains(got, "echo changed") {
131 t.Fatalf("nieuwe branch: %s", got)
132 }
133
134 ctx := res.Request.Context()
135 repo, err := st.RepoByPath(ctx, "tim", "cied")
136 if err != nil {
137 t.Fatal(err)
138 }
139 ada, err := st.UpsertUser(ctx, 3, "ada", "", false)
140 if err != nil {
141 t.Fatal(err)
142 }
143 if err := st.SetMember(ctx, "repo", repo.ID, "ada", "developer"); err != nil {
144 t.Fatal(err)
145 }
146 adaC := clientAs(t, ts.URL, st, ada.ID)
147 res, err = adaC.Get(ts.URL + "/tim/cied/-/ci/editor?ref=master")
148 if err != nil {
149 t.Fatal(err)
150 }
151 adaPage := read(t, res)
152 res, err = adaC.PostForm(ts.URL+"/tim/cied/-/ci/editor", url.Values{
153 "csrf": {csrfOf(t, adaPage)}, "base": {"master"}, "branch": {"master"}, "message": {"ada op master"}, "body": {changed + "\n"},
154 })
155 if err != nil {
156 t.Fatal(err)
157 }
158 denied := read(t, res)
159 if !strings.Contains(denied, "This branch is protected.") {
160 t.Fatalf("beschermd:\n%s", denied)
161 }
162 if got := mustGit(t, dir, "show", "master:blue-ci-cd.yml"); strings.Contains(got, "echo changed") {
163 t.Fatalf("master veranderde:\n%s", got)
164 }
165}
internal/server/events.go +3 −0
@@ -190,6 +190,9 @@ func (s *Server) Dispatch(repoID int64, ev webhook.Event) {
190 190 if repo.OwnerKind == "group" {
191 191 s.deliver(ctx, "group", repo.OwnerID, ev, protected)
192 192 }
193 if ev.Type == webhook.EventPipelineFailure {
194 s.queueFailureMail(repo, ev)
195 }
193 196 }
194 197
195 198 func (s *Server) deliver(ctx context.Context, kind string, scopeID int64, ev webhook.Event, refProtected bool) {
@@ -190,6 +190,9 @@ func (s *Server) Dispatch(repoID int64, ev webhook.Event) {
190 if repo.OwnerKind == "group" {
190 if repo.OwnerKind == "group" {
191 s.deliver(ctx, "group", repo.OwnerID, ev, protected)
191 s.deliver(ctx, "group", repo.OwnerID, ev, protected)
192 }
192 }
193 if ev.Type == webhook.EventPipelineFailure {
194 s.queueFailureMail(repo, ev)
195 }
193}
196}
194
197
195func (s *Server) deliver(ctx context.Context, kind string, scopeID int64, ev webhook.Event, refProtected bool) {
198func (s *Server) deliver(ctx context.Context, kind string, scopeID int64, ev webhook.Event, refProtected bool) {
internal/server/pipeline_mail.go added +87 −0
@@ -0,0 +1,87 @@
1 package server
2
3 import (
4 "context"
5 "fmt"
6 "html"
7 "strconv"
8 "strings"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/ci"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/store"
14 "gitlab.com/Timfa/bluegit/internal/webhook"
15 )
16
17 func (s *Server) queueFailureMail(repo store.Repo, ev webhook.Event) {
18 if s.Mail == nil || ev.Type != webhook.EventPipelineFailure {
19 return
20 }
21 go func() {
22 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
23 defer cancel()
24 if err := s.sendFailureMail(ctx, repo, ev); err != nil && s.Log != nil {
25 s.Log.Error("pipeline-mail", "err", err)
26 }
27 }()
28 }
29
30 // sendFailureMail mailt de veroorzaker in diens taal. Een fout hier stopt de pipeline niet.
31 func (s *Server) sendFailureMail(ctx context.Context, repo store.Repo, ev webhook.Event) error {
32 if s.Mail == nil {
33 return nil
34 }
35 id, err := strconv.ParseInt(ev.PipelineID, 10, 64)
36 if err != nil || id <= 0 {
37 return nil
38 }
39 p, err := s.Store.PipelineByID(ctx, repo.ID, id)
40 if err != nil {
41 return err
42 }
43 actor := strings.TrimSpace(p.Actor)
44 if actor == "" {
45 return nil
46 }
47 lang := i18n.EN
48 if u, err := s.Store.UserByUsername(ctx, actor); err == nil {
49 lang = i18n.Parse(u.Language)
50 }
51 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
52 if err != nil {
53 return err
54 }
55 refName, _ := ci.SplitRef(p.Ref)
56 if refName == "" {
57 refName = p.Ref
58 }
59 pipeURL := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
60 subject := clipRunes(i18n.T(lang, "mail.fail.subject", p.ID, repo.FullPath()), 200)
61 var b strings.Builder
62 fmt.Fprintf(&b, "<p>%s</p>", html.EscapeString(i18n.T(lang, "mail.fail.intro", p.ID, repo.FullPath(), refName)))
63 var hard []store.Job
64 for _, j := range jobs {
65 if j.Status == "failed" && !j.AllowFailure {
66 hard = append(hard, j)
67 }
68 }
69 if len(hard) > 0 {
70 fmt.Fprintf(&b, "<p>%s</p><ul>", html.EscapeString(i18n.T(lang, "mail.fail.jobs")))
71 for _, j := range hard {
72 jobURL := pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10)
73 fmt.Fprintf(&b, `<li><a href="%s">%s</a></li>`, html.EscapeString(jobURL), html.EscapeString(j.Name))
74 }
75 b.WriteString("</ul>")
76 }
77 fmt.Fprintf(&b, `<p><a href="%s">%s</a></p>`, html.EscapeString(pipeURL), html.EscapeString(i18n.T(lang, "mail.fail.open")))
78 return s.Mail.SendEmail(ctx, actor, subject, b.String())
79 }
80
81 func clipRunes(s string, n int) string {
82 r := []rune(s)
83 if len(r) <= n {
84 return s
85 }
86 return string(r[:n])
87 }
@@ -0,0 +1,87 @@
1package server
2
3import (
4 "context"
5 "fmt"
6 "html"
7 "strconv"
8 "strings"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/ci"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/store"
14 "gitlab.com/Timfa/bluegit/internal/webhook"
15)
16
17func (s *Server) queueFailureMail(repo store.Repo, ev webhook.Event) {
18 if s.Mail == nil || ev.Type != webhook.EventPipelineFailure {
19 return
20 }
21 go func() {
22 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
23 defer cancel()
24 if err := s.sendFailureMail(ctx, repo, ev); err != nil && s.Log != nil {
25 s.Log.Error("pipeline-mail", "err", err)
26 }
27 }()
28}
29
30// sendFailureMail mailt de veroorzaker in diens taal. Een fout hier stopt de pipeline niet.
31func (s *Server) sendFailureMail(ctx context.Context, repo store.Repo, ev webhook.Event) error {
32 if s.Mail == nil {
33 return nil
34 }
35 id, err := strconv.ParseInt(ev.PipelineID, 10, 64)
36 if err != nil || id <= 0 {
37 return nil
38 }
39 p, err := s.Store.PipelineByID(ctx, repo.ID, id)
40 if err != nil {
41 return err
42 }
43 actor := strings.TrimSpace(p.Actor)
44 if actor == "" {
45 return nil
46 }
47 lang := i18n.EN
48 if u, err := s.Store.UserByUsername(ctx, actor); err == nil {
49 lang = i18n.Parse(u.Language)
50 }
51 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
52 if err != nil {
53 return err
54 }
55 refName, _ := ci.SplitRef(p.Ref)
56 if refName == "" {
57 refName = p.Ref
58 }
59 pipeURL := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10)
60 subject := clipRunes(i18n.T(lang, "mail.fail.subject", p.ID, repo.FullPath()), 200)
61 var b strings.Builder
62 fmt.Fprintf(&b, "<p>%s</p>", html.EscapeString(i18n.T(lang, "mail.fail.intro", p.ID, repo.FullPath(), refName)))
63 var hard []store.Job
64 for _, j := range jobs {
65 if j.Status == "failed" && !j.AllowFailure {
66 hard = append(hard, j)
67 }
68 }
69 if len(hard) > 0 {
70 fmt.Fprintf(&b, "<p>%s</p><ul>", html.EscapeString(i18n.T(lang, "mail.fail.jobs")))
71 for _, j := range hard {
72 jobURL := pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10)
73 fmt.Fprintf(&b, `<li><a href="%s">%s</a></li>`, html.EscapeString(jobURL), html.EscapeString(j.Name))
74 }
75 b.WriteString("</ul>")
76 }
77 fmt.Fprintf(&b, `<p><a href="%s">%s</a></p>`, html.EscapeString(pipeURL), html.EscapeString(i18n.T(lang, "mail.fail.open")))
78 return s.Mail.SendEmail(ctx, actor, subject, b.String())
79}
80
81func clipRunes(s string, n int) string {
82 r := []rune(s)
83 if len(r) <= n {
84 return s
85 }
86 return string(r[:n])
87}
internal/server/pipeline_mail_test.go added +108 −0
@@ -0,0 +1,108 @@
1 package server
2
3 import (
4 "context"
5 "net/http/httptest"
6 "path/filepath"
7 "strconv"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "gitlab.com/Timfa/bluegit/internal/auth"
14 "gitlab.com/Timfa/bluegit/internal/config"
15 "gitlab.com/Timfa/bluegit/internal/store"
16 "gitlab.com/Timfa/bluegit/internal/webhook"
17 )
18
19 type boxMail struct {
20 mu sync.Mutex
21 n int
22 user, subject, html string
23 wait chan struct{}
24 }
25
26 func (b *boxMail) SendEmail(ctx context.Context, username, subject, html string) error {
27 b.mu.Lock()
28 b.n++
29 b.user, b.subject, b.html = username, subject, html
30 b.mu.Unlock()
31 if b.wait != nil {
32 b.wait <- struct{}{}
33 }
34 return nil
35 }
36
37 func TestFailureMailUsesRecipientLanguage(t *testing.T) {
38 storage := t.TempDir()
39 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
40 if err != nil {
41 t.Fatal(err)
42 }
43 t.Cleanup(func() { _ = st.Close() })
44 cfg := config.Config{
45 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
46 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"},
47 Git: config.Git{Binary: "git", DefaultBranch: "master"},
48 }
49 wait := make(chan struct{}, 1)
50 box := &boxMail{wait: wait}
51 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", Mail: box}
52 ts := httptest.NewServer(srv.Handler())
53 t.Cleanup(ts.Close)
54 res, err := ts.Client().Get(ts.URL + "/login")
55 if err != nil {
56 t.Fatal(err)
57 }
58 _ = res.Body.Close()
59
60 ctx := context.Background()
61 user, err := st.UserByUsername(ctx, "tim")
62 if err != nil {
63 t.Fatal(err)
64 }
65 if err := st.SetLanguage(ctx, user.ID, "nl"); err != nil {
66 t.Fatal(err)
67 }
68 repo, err := st.CreateRepo(ctx, "user", user.ID, "mail", "", "master", "private")
69 if err != nil {
70 t.Fatal(err)
71 }
72 id, err := st.CreatePipeline(ctx, repo.ID, "abc", "refs/tags/v0.0.26", "push", "tim", "tag die faalt", []store.NewJob{
73 {Name: "hardjob", Stage: "test", Script: "exit 1"},
74 {Name: "softjob", Stage: "test", Script: "exit 1", AllowFailure: true},
75 })
76 if err != nil {
77 t.Fatal(err)
78 }
79 jobs, _ := st.JobsByPipeline(ctx, id)
80 for _, j := range jobs {
81 if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil {
82 t.Fatal(err)
83 }
84 }
85 srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineSuccess, PipelineID: strconv.FormatInt(id, 10)})
86 if box.n != 0 {
87 t.Fatalf("succes mailde %d", box.n)
88 }
89 srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineFailure, PipelineID: strconv.FormatInt(id, 10)})
90 select {
91 case <-wait:
92 case <-time.After(2 * time.Second):
93 t.Fatal("geen mail")
94 }
95 box.mu.Lock()
96 defer box.mu.Unlock()
97 if box.user != "tim" || !strings.Contains(box.subject, "mislukt") || !strings.Contains(box.subject, "tim/mail") {
98 t.Fatalf("onderwerp %s voor %s", box.subject, box.user)
99 }
100 for _, want := range []string{"v0.0.26", "hardjob", "Mislukte jobs", "/-/pipelines/" + strconv.FormatInt(id, 10), "/jobs/"} {
101 if !strings.Contains(box.html, want) {
102 t.Errorf("html mist %q\n%s", want, box.html)
103 }
104 }
105 if strings.Contains(box.html, "softjob") {
106 t.Fatalf("allow_failure hoort niet in de mail:\n%s", box.html)
107 }
108 }
@@ -0,0 +1,108 @@
1package server
2
3import (
4 "context"
5 "net/http/httptest"
6 "path/filepath"
7 "strconv"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "gitlab.com/Timfa/bluegit/internal/auth"
14 "gitlab.com/Timfa/bluegit/internal/config"
15 "gitlab.com/Timfa/bluegit/internal/store"
16 "gitlab.com/Timfa/bluegit/internal/webhook"
17)
18
19type boxMail struct {
20 mu sync.Mutex
21 n int
22 user, subject, html string
23 wait chan struct{}
24}
25
26func (b *boxMail) SendEmail(ctx context.Context, username, subject, html string) error {
27 b.mu.Lock()
28 b.n++
29 b.user, b.subject, b.html = username, subject, html
30 b.mu.Unlock()
31 if b.wait != nil {
32 b.wait <- struct{}{}
33 }
34 return nil
35}
36
37func TestFailureMailUsesRecipientLanguage(t *testing.T) {
38 storage := t.TempDir()
39 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
40 if err != nil {
41 t.Fatal(err)
42 }
43 t.Cleanup(func() { _ = st.Close() })
44 cfg := config.Config{
45 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
46 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"},
47 Git: config.Git{Binary: "git", DefaultBranch: "master"},
48 }
49 wait := make(chan struct{}, 1)
50 box := &boxMail{wait: wait}
51 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", Mail: box}
52 ts := httptest.NewServer(srv.Handler())
53 t.Cleanup(ts.Close)
54 res, err := ts.Client().Get(ts.URL + "/login")
55 if err != nil {
56 t.Fatal(err)
57 }
58 _ = res.Body.Close()
59
60 ctx := context.Background()
61 user, err := st.UserByUsername(ctx, "tim")
62 if err != nil {
63 t.Fatal(err)
64 }
65 if err := st.SetLanguage(ctx, user.ID, "nl"); err != nil {
66 t.Fatal(err)
67 }
68 repo, err := st.CreateRepo(ctx, "user", user.ID, "mail", "", "master", "private")
69 if err != nil {
70 t.Fatal(err)
71 }
72 id, err := st.CreatePipeline(ctx, repo.ID, "abc", "refs/tags/v0.0.26", "push", "tim", "tag die faalt", []store.NewJob{
73 {Name: "hardjob", Stage: "test", Script: "exit 1"},
74 {Name: "softjob", Stage: "test", Script: "exit 1", AllowFailure: true},
75 })
76 if err != nil {
77 t.Fatal(err)
78 }
79 jobs, _ := st.JobsByPipeline(ctx, id)
80 for _, j := range jobs {
81 if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil {
82 t.Fatal(err)
83 }
84 }
85 srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineSuccess, PipelineID: strconv.FormatInt(id, 10)})
86 if box.n != 0 {
87 t.Fatalf("succes mailde %d", box.n)
88 }
89 srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineFailure, PipelineID: strconv.FormatInt(id, 10)})
90 select {
91 case <-wait:
92 case <-time.After(2 * time.Second):
93 t.Fatal("geen mail")
94 }
95 box.mu.Lock()
96 defer box.mu.Unlock()
97 if box.user != "tim" || !strings.Contains(box.subject, "mislukt") || !strings.Contains(box.subject, "tim/mail") {
98 t.Fatalf("onderwerp %s voor %s", box.subject, box.user)
99 }
100 for _, want := range []string{"v0.0.26", "hardjob", "Mislukte jobs", "/-/pipelines/" + strconv.FormatInt(id, 10), "/jobs/"} {
101 if !strings.Contains(box.html, want) {
102 t.Errorf("html mist %q\n%s", want, box.html)
103 }
104 }
105 if strings.Contains(box.html, "softjob") {
106 t.Fatalf("allow_failure hoort niet in de mail:\n%s", box.html)
107 }
108}
internal/server/server.go +5 −0
@@ -34,6 +34,7 @@ type Server struct {
34 34 Cfg config.Config
35 35 Store *store.Store
36 36 Auth auth.Provider
37 Mail auth.Mailer
37 38 Dev bool
38 39 Git string
39 40 HookBin string
@@ -123,6 +124,9 @@ func (s *Server) Handler() http.Handler {
123 124 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)
124 125 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)
125 126 mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)
127 mux.HandleFunc("GET /{owner}/{repo}/-/ci/editor", s.ciEditor)
128 mux.HandleFunc("POST /{owner}/{repo}/-/ci/editor", s.saveCIEditor)
129 mux.HandleFunc("POST /{owner}/{repo}/-/ci/lint", s.lintCI)
126 130 mux.HandleFunc("GET /{owner}/{repo}/-/releases", s.releases)
127 131 mux.HandleFunc("GET /{owner}/{repo}/-/releases/{tag}/assets/{name}", s.releaseAsset)
128 132 mux.HandleFunc("GET /{owner}/{repo}/-/logo", s.repoLogo)
@@ -441,6 +445,7 @@ type View struct {
441 445 Pipe *store.Pipeline
442 446 Jobs []store.Job
443 447 JobPage *JobPage
448 Editor *CIEditor
444 449 Releases []store.Release
445 450 Members []store.Member
446 451 Secrets []store.SecretMeta
@@ -34,6 +34,7 @@ type Server struct {
34 Cfg config.Config
34 Cfg config.Config
35 Store *store.Store
35 Store *store.Store
36 Auth auth.Provider
36 Auth auth.Provider
37 Mail auth.Mailer
37 Dev bool
38 Dev bool
38 Git string
39 Git string
39 HookBin string
40 HookBin string
@@ -123,6 +124,9 @@ func (s *Server) Handler() http.Handler {
123 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)
124 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)
124 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)
125 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)
125 mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)
126 mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)
127 mux.HandleFunc("GET /{owner}/{repo}/-/ci/editor", s.ciEditor)
128 mux.HandleFunc("POST /{owner}/{repo}/-/ci/editor", s.saveCIEditor)
129 mux.HandleFunc("POST /{owner}/{repo}/-/ci/lint", s.lintCI)
126 mux.HandleFunc("GET /{owner}/{repo}/-/releases", s.releases)
130 mux.HandleFunc("GET /{owner}/{repo}/-/releases", s.releases)
127 mux.HandleFunc("GET /{owner}/{repo}/-/releases/{tag}/assets/{name}", s.releaseAsset)
131 mux.HandleFunc("GET /{owner}/{repo}/-/releases/{tag}/assets/{name}", s.releaseAsset)
128 mux.HandleFunc("GET /{owner}/{repo}/-/logo", s.repoLogo)
132 mux.HandleFunc("GET /{owner}/{repo}/-/logo", s.repoLogo)
@@ -441,6 +445,7 @@ type View struct {
441 Pipe *store.Pipeline
445 Pipe *store.Pipeline
442 Jobs []store.Job
446 Jobs []store.Job
443 JobPage *JobPage
447 JobPage *JobPage
448 Editor *CIEditor
444 Releases []store.Release
449 Releases []store.Release
445 Members []store.Member
450 Members []store.Member
446 Secrets []store.SecretMeta
451 Secrets []store.SecretMeta
internal/server/static/editor.js added +89 −0
@@ -0,0 +1,89 @@
1 (function () {
2 var form = document.querySelector("[data-lint-url]");
3 if (!form) {
4 return;
5 }
6 var area = form.querySelector("textarea[name=body]");
7 var back = form.querySelector("[data-back]");
8 var gutter = form.querySelector("[data-gutter]");
9 var list = form.querySelector("[data-issues]");
10 var status = form.querySelector("[data-lint-status]");
11 if (!area || !back || !gutter) {
12 return;
13 }
14
15 function syncScroll() {
16 back.scrollTop = area.scrollTop;
17 back.scrollLeft = area.scrollLeft;
18 gutter.scrollTop = area.scrollTop;
19 }
20 area.addEventListener("scroll", syncScroll);
21
22 function paint(data) {
23 var bad = {};
24 (data.bad || []).forEach(function (n) { bad[n] = true; });
25 var lines = data.lines || [];
26 if (!lines.length) {
27 lines = [""];
28 }
29 back.textContent = "";
30 gutter.textContent = "";
31 lines.forEach(function (html, i) {
32 var row = document.createElement("div");
33 row.className = "codeline" + (bad[i + 1] ? " bad" : "");
34 row.innerHTML = html;
35 back.appendChild(row);
36 var num = document.createElement("span");
37 num.textContent = String(i + 1);
38 if (bad[i + 1]) {
39 num.className = "bad";
40 }
41 gutter.appendChild(num);
42 });
43 if (list) {
44 list.textContent = "";
45 (data.issues || []).forEach(function (is) {
46 var li = document.createElement("li");
47 li.className = is.warning ? "warn" : "bad";
48 li.textContent = is.line + ": " + is.text;
49 list.appendChild(li);
50 });
51 }
52 if (status) {
53 status.textContent = data.status || "";
54 status.className = "status " + (data.statusClass || "passed");
55 }
56 syncScroll();
57 }
58
59 var timer = 0;
60 function lint() {
61 var payload = new URLSearchParams();
62 var csrf = form.querySelector("[name=csrf]");
63 var ref = form.querySelector("[name=base]");
64 payload.set("csrf", csrf ? csrf.value : "");
65 payload.set("ref", ref ? ref.value : "");
66 payload.set("body", area.value);
67 fetch(form.getAttribute("data-lint-url"), {
68 method: "POST",
69 headers: { "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json" },
70 body: payload
71 }).then(function (res) {
72 if (!res.ok) {
73 return null;
74 }
75 return res.json();
76 }).then(function (data) {
77 if (data) {
78 paint(data);
79 }
80 }).catch(function () {});
81 }
82
83 if (!area.readOnly) {
84 area.addEventListener("input", function () {
85 clearTimeout(timer);
86 timer = setTimeout(lint, 300);
87 });
88 }
89 })();
@@ -0,0 +1,89 @@
1(function () {
2 var form = document.querySelector("[data-lint-url]");
3 if (!form) {
4 return;
5 }
6 var area = form.querySelector("textarea[name=body]");
7 var back = form.querySelector("[data-back]");
8 var gutter = form.querySelector("[data-gutter]");
9 var list = form.querySelector("[data-issues]");
10 var status = form.querySelector("[data-lint-status]");
11 if (!area || !back || !gutter) {
12 return;
13 }
14
15 function syncScroll() {
16 back.scrollTop = area.scrollTop;
17 back.scrollLeft = area.scrollLeft;
18 gutter.scrollTop = area.scrollTop;
19 }
20 area.addEventListener("scroll", syncScroll);
21
22 function paint(data) {
23 var bad = {};
24 (data.bad || []).forEach(function (n) { bad[n] = true; });
25 var lines = data.lines || [];
26 if (!lines.length) {
27 lines = [""];
28 }
29 back.textContent = "";
30 gutter.textContent = "";
31 lines.forEach(function (html, i) {
32 var row = document.createElement("div");
33 row.className = "codeline" + (bad[i + 1] ? " bad" : "");
34 row.innerHTML = html;
35 back.appendChild(row);
36 var num = document.createElement("span");
37 num.textContent = String(i + 1);
38 if (bad[i + 1]) {
39 num.className = "bad";
40 }
41 gutter.appendChild(num);
42 });
43 if (list) {
44 list.textContent = "";
45 (data.issues || []).forEach(function (is) {
46 var li = document.createElement("li");
47 li.className = is.warning ? "warn" : "bad";
48 li.textContent = is.line + ": " + is.text;
49 list.appendChild(li);
50 });
51 }
52 if (status) {
53 status.textContent = data.status || "";
54 status.className = "status " + (data.statusClass || "passed");
55 }
56 syncScroll();
57 }
58
59 var timer = 0;
60 function lint() {
61 var payload = new URLSearchParams();
62 var csrf = form.querySelector("[name=csrf]");
63 var ref = form.querySelector("[name=base]");
64 payload.set("csrf", csrf ? csrf.value : "");
65 payload.set("ref", ref ? ref.value : "");
66 payload.set("body", area.value);
67 fetch(form.getAttribute("data-lint-url"), {
68 method: "POST",
69 headers: { "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json" },
70 body: payload
71 }).then(function (res) {
72 if (!res.ok) {
73 return null;
74 }
75 return res.json();
76 }).then(function (data) {
77 if (data) {
78 paint(data);
79 }
80 }).catch(function () {});
81 }
82
83 if (!area.readOnly) {
84 area.addEventListener("input", function () {
85 clearTimeout(timer);
86 timer = setTimeout(lint, 300);
87 });
88 }
89})();
internal/server/static/style.css +30 −0
@@ -448,6 +448,36 @@ button.section-toggle {
448 448 .related { list-style: none; margin: 0; padding: 0; }
449 449 .related li { display: flex; align-items: center; gap: 0.3rem; }
450 450 .related .arrow { width: 1rem; color: var(--muted); }
451 .editorhead { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 0.7rem; }
452 .editorhead h2 { margin: 0; font-size: 1.15rem; }
453 .editorhead .status { margin: 0; }
454 .ciedit {
455 display: grid; grid-template-columns: 3.2rem minmax(0, 1fr);
456 border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: white;
457 }
458 .ci-gutter {
459 overflow: hidden; background: #f6f8fa; color: #8c959f; text-align: right;
460 font-family: ui-monospace, monospace; font-size: 0.86rem; line-height: 1.45;
461 padding: 0.55rem 0.45rem; user-select: none;
462 }
463 .ci-gutter span { display: block; }
464 .ci-gutter span.bad { background: #fde8e8; color: var(--bad); font-weight: 700; }
465 .ci-stage { position: relative; height: 24rem; }
466 .ci-back, .ciedit textarea {
467 font-family: ui-monospace, monospace; font-size: 0.86rem; line-height: 1.45;
468 padding: 0.55rem 0.7rem; margin: 0; white-space: pre; tab-size: 2;
469 }
470 .ci-back { position: absolute; inset: 0; overflow: hidden; color: var(--ink); pointer-events: none; }
471 .codeline { min-height: 1.45em; }
472 .codeline.bad { background: #fff5f5; }
473 .ciedit textarea {
474 position: relative; width: 100%; height: 100%; border: 0; border-radius: 0;
475 background: transparent; color: transparent; caret-color: var(--ink); resize: none; overflow: auto;
476 }
477 .ciedit textarea::selection { background: rgba(31, 111, 235, 0.25); }
478 .ci-issues { list-style: none; padding: 0; margin: 0.7rem 0 0; }
479 .ci-issues li.bad { color: var(--bad); }
480 .ci-issues li.warn { color: #8a5a00; }
451 481 @media (max-width: 800px) {
452 482 .proj, .fileview, .joblayout { grid-template-columns: 1fr; }
453 483 .heat-foot { flex-direction: column; align-items: flex-start; }
@@ -448,6 +448,36 @@ button.section-toggle {
448.related { list-style: none; margin: 0; padding: 0; }
448.related { list-style: none; margin: 0; padding: 0; }
449.related li { display: flex; align-items: center; gap: 0.3rem; }
449.related li { display: flex; align-items: center; gap: 0.3rem; }
450.related .arrow { width: 1rem; color: var(--muted); }
450.related .arrow { width: 1rem; color: var(--muted); }
451.editorhead { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 0.7rem; }
452.editorhead h2 { margin: 0; font-size: 1.15rem; }
453.editorhead .status { margin: 0; }
454.ciedit {
455 display: grid; grid-template-columns: 3.2rem minmax(0, 1fr);
456 border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: white;
457}
458.ci-gutter {
459 overflow: hidden; background: #f6f8fa; color: #8c959f; text-align: right;
460 font-family: ui-monospace, monospace; font-size: 0.86rem; line-height: 1.45;
461 padding: 0.55rem 0.45rem; user-select: none;
462}
463.ci-gutter span { display: block; }
464.ci-gutter span.bad { background: #fde8e8; color: var(--bad); font-weight: 700; }
465.ci-stage { position: relative; height: 24rem; }
466.ci-back, .ciedit textarea {
467 font-family: ui-monospace, monospace; font-size: 0.86rem; line-height: 1.45;
468 padding: 0.55rem 0.7rem; margin: 0; white-space: pre; tab-size: 2;
469}
470.ci-back { position: absolute; inset: 0; overflow: hidden; color: var(--ink); pointer-events: none; }
471.codeline { min-height: 1.45em; }
472.codeline.bad { background: #fff5f5; }
473.ciedit textarea {
474 position: relative; width: 100%; height: 100%; border: 0; border-radius: 0;
475 background: transparent; color: transparent; caret-color: var(--ink); resize: none; overflow: auto;
476}
477.ciedit textarea::selection { background: rgba(31, 111, 235, 0.25); }
478.ci-issues { list-style: none; padding: 0; margin: 0.7rem 0 0; }
479.ci-issues li.bad { color: var(--bad); }
480.ci-issues li.warn { color: #8a5a00; }
451@media (max-width: 800px) {
481@media (max-width: 800px) {
452 .proj, .fileview, .joblayout { grid-template-columns: 1fr; }
482 .proj, .fileview, .joblayout { grid-template-columns: 1fr; }
453 .heat-foot { flex-direction: column; align-items: flex-start; }
483 .heat-foot { flex-direction: column; align-items: flex-start; }
internal/server/templates/collab.html +47 −0
@@ -109,10 +109,18 @@
109 109 {{template "layout-end" .}}
110 110 {{end}}
111 111
112 {{define "pipenav"}}
113 <p class="sub">
114 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if eq .Page "pipelines"}} class="on"{{end}}>{{t "pipe.title"}}</a>
115 <a href="/{{.Repo.FullPath}}/-/ci/editor"{{if eq .Page "editor"}} class="on"{{end}}>{{t "edit.title"}}</a>
116 </p>
117 {{end}}
118
112 119 {{define "pipelines"}}
113 120 {{template "layout-start" .}}
114 121 <h1>{{t "pipe.title"}}</h1>
115 122 {{template "repotabs" .}}
123 {{template "pipenav" .}}
116 124 <div class="pipebar">
117 125 <form method="get" action="/{{.Repo.FullPath}}/-/pipelines" class="pipe-filter">
118 126 <input name="q" value="{{.Query}}" placeholder="{{t "pipe.filter"}}" aria-label="{{t "pipe.filter"}}">
@@ -182,6 +190,7 @@
182 190 {{template "layout-start" .}}
183 191 <h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1>
184 192 {{template "repotabs" .}}
193 {{template "pipenav" .}}
185 194 <p><span class="pill">{{.Pipe.Status}}</span> {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a></p>
186 195 <ul class="clean">
187 196 {{range .Jobs}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="pill">{{.Status}}</span> <span class="muted">{{.Stage}}</span></li>{{end}}
@@ -195,6 +204,7 @@
195 204 <h1>{{.Name}}</h1>
196 205 {{end}}
197 206 {{template "repotabs" $}}
207 {{template "pipenav" $}}
198 208 {{with .JobPage}}
199 209 {{$page := .}}
200 210 <p class="jobhead"><span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span> <span class="muted">{{t "pipe.started"}} {{.When}} {{t "pipe.by"}}</span> {{if .Avatar}}<img class="avatar sm" src="{{.Avatar}}" alt="">{{else if .Actor}}<span class="avatar sm">{{.Initial}}</span>{{end}} <strong>{{.Actor}}</strong></p>
@@ -262,6 +272,43 @@
262 272 {{template "layout-end" .}}
263 273 {{end}}
264 274
275 {{define "editor"}}
276 {{template "layout-start" .}}
277 <h1>{{t "edit.title"}}</h1>
278 {{template "repotabs" .}}
279 {{template "pipenav" .}}
280 <form method="post" action="/{{.Repo.FullPath}}/-/ci/editor" data-lint-url="/{{.Repo.FullPath}}/-/ci/lint">
281 <input type="hidden" name="csrf" value="{{.CSRF}}">
282 <input type="hidden" name="base" value="{{.Editor.Ref}}">
283 <section class="panel">
284 <div class="editorhead">
285 <h2>{{t "edit.title"}}</h2>
286 <p data-lint-status class="status {{.Editor.Class}}">{{.Editor.Status}}</p>
287 </div>
288 <div class="ciedit">
289 <div class="ci-gutter" data-gutter>{{range .Editor.Rows}}<span{{if .Bad}} class="bad"{{end}}>{{.N}}</span>{{end}}</div>
290 <div class="ci-stage">
291 <div class="ci-back" data-back>{{range .Editor.Rows}}<div class="codeline{{if .Bad}} bad{{end}}">{{.HTML}}</div>{{end}}</div>
292 <textarea name="body" spellcheck="false"{{if not .CanWrite}} readonly{{end}}>{{.Editor.Body}}</textarea>
293 </div>
294 </div>
295 <ul class="ci-issues" data-issues>
296 {{range .Editor.Issues}}<li class="{{if .Warning}}warn{{else}}bad{{end}}" data-line="{{.Line}}">{{.Line}}: {{.Text}}</li>{{end}}
297 </ul>
298 </section>
299 {{if .CanWrite}}
300 <label>{{t "edit.message"}} <input name="message" required></label>
301 <label>{{t "edit.branch"}}
302 <input name="branch" value="{{.Editor.Ref}}" list="ci-branches" required>
303 </label>
304 <datalist id="ci-branches">{{range .Branches}}<option value="{{.Name}}"></option>{{end}}</datalist>
305 <button type="submit">{{t "edit.save"}}</button>
306 {{end}}
307 </form>
308 <script src="/assets/editor.js"></script>
309 {{template "layout-end" .}}
310 {{end}}
311
265 312 {{define "releases"}}
266 313 {{template "layout-start" .}}
267 314 <h1>{{t "title.releases"}}</h1>
@@ -109,10 +109,18 @@
109{{template "layout-end" .}}
109{{template "layout-end" .}}
110{{end}}
110{{end}}
111
111
112{{define "pipenav"}}
113<p class="sub">
114 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if eq .Page "pipelines"}} class="on"{{end}}>{{t "pipe.title"}}</a>
115 <a href="/{{.Repo.FullPath}}/-/ci/editor"{{if eq .Page "editor"}} class="on"{{end}}>{{t "edit.title"}}</a>
116</p>
117{{end}}
118
112{{define "pipelines"}}
119{{define "pipelines"}}
113{{template "layout-start" .}}
120{{template "layout-start" .}}
114<h1>{{t "pipe.title"}}</h1>
121<h1>{{t "pipe.title"}}</h1>
115{{template "repotabs" .}}
122{{template "repotabs" .}}
123{{template "pipenav" .}}
116<div class="pipebar">
124<div class="pipebar">
117 <form method="get" action="/{{.Repo.FullPath}}/-/pipelines" class="pipe-filter">
125 <form method="get" action="/{{.Repo.FullPath}}/-/pipelines" class="pipe-filter">
118 <input name="q" value="{{.Query}}" placeholder="{{t "pipe.filter"}}" aria-label="{{t "pipe.filter"}}">
126 <input name="q" value="{{.Query}}" placeholder="{{t "pipe.filter"}}" aria-label="{{t "pipe.filter"}}">
@@ -182,6 +190,7 @@
182{{template "layout-start" .}}
190{{template "layout-start" .}}
183<h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1>
191<h1>{{t "pipe.one"}} #{{.Pipe.ID}}</h1>
184{{template "repotabs" .}}
192{{template "repotabs" .}}
193{{template "pipenav" .}}
185<p><span class="pill">{{.Pipe.Status}}</span> {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a></p>
194<p><span class="pill">{{.Pipe.Status}}</span> {{.Pipe.Ref}} <a href="/{{.Repo.FullPath}}/-/commit/{{.Pipe.CommitSHA}}"><code>{{.Pipe.CommitSHA}}</code></a></p>
186<ul class="clean">
195<ul class="clean">
187{{range .Jobs}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="pill">{{.Status}}</span> <span class="muted">{{.Stage}}</span></li>{{end}}
196{{range .Jobs}}<li><a href="/{{$.Repo.FullPath}}/-/pipelines/{{$.Pipe.ID}}/jobs/{{.ID}}">{{.Name}}</a> <span class="pill">{{.Status}}</span> <span class="muted">{{.Stage}}</span></li>{{end}}
@@ -195,6 +204,7 @@
195<h1>{{.Name}}</h1>
204<h1>{{.Name}}</h1>
196{{end}}
205{{end}}
197{{template "repotabs" $}}
206{{template "repotabs" $}}
207{{template "pipenav" $}}
198{{with .JobPage}}
208{{with .JobPage}}
199{{$page := .}}
209{{$page := .}}
200<p class="jobhead"><span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span> <span class="muted">{{t "pipe.started"}} {{.When}} {{t "pipe.by"}}</span> {{if .Avatar}}<img class="avatar sm" src="{{.Avatar}}" alt="">{{else if .Actor}}<span class="avatar sm">{{.Initial}}</span>{{end}} <strong>{{.Actor}}</strong></p>
210<p class="jobhead"><span class="status {{.Status}}"><span class="mark"></span>{{.Label}}</span> <span class="muted">{{t "pipe.started"}} {{.When}} {{t "pipe.by"}}</span> {{if .Avatar}}<img class="avatar sm" src="{{.Avatar}}" alt="">{{else if .Actor}}<span class="avatar sm">{{.Initial}}</span>{{end}} <strong>{{.Actor}}</strong></p>
@@ -262,6 +272,43 @@
262{{template "layout-end" .}}
272{{template "layout-end" .}}
263{{end}}
273{{end}}
264
274
275{{define "editor"}}
276{{template "layout-start" .}}
277<h1>{{t "edit.title"}}</h1>
278{{template "repotabs" .}}
279{{template "pipenav" .}}
280<form method="post" action="/{{.Repo.FullPath}}/-/ci/editor" data-lint-url="/{{.Repo.FullPath}}/-/ci/lint">
281 <input type="hidden" name="csrf" value="{{.CSRF}}">
282 <input type="hidden" name="base" value="{{.Editor.Ref}}">
283 <section class="panel">
284 <div class="editorhead">
285 <h2>{{t "edit.title"}}</h2>
286 <p data-lint-status class="status {{.Editor.Class}}">{{.Editor.Status}}</p>
287 </div>
288 <div class="ciedit">
289 <div class="ci-gutter" data-gutter>{{range .Editor.Rows}}<span{{if .Bad}} class="bad"{{end}}>{{.N}}</span>{{end}}</div>
290 <div class="ci-stage">
291 <div class="ci-back" data-back>{{range .Editor.Rows}}<div class="codeline{{if .Bad}} bad{{end}}">{{.HTML}}</div>{{end}}</div>
292 <textarea name="body" spellcheck="false"{{if not .CanWrite}} readonly{{end}}>{{.Editor.Body}}</textarea>
293 </div>
294 </div>
295 <ul class="ci-issues" data-issues>
296 {{range .Editor.Issues}}<li class="{{if .Warning}}warn{{else}}bad{{end}}" data-line="{{.Line}}">{{.Line}}: {{.Text}}</li>{{end}}
297 </ul>
298 </section>
299 {{if .CanWrite}}
300 <label>{{t "edit.message"}} <input name="message" required></label>
301 <label>{{t "edit.branch"}}
302 <input name="branch" value="{{.Editor.Ref}}" list="ci-branches" required>
303 </label>
304 <datalist id="ci-branches">{{range .Branches}}<option value="{{.Name}}"></option>{{end}}</datalist>
305 <button type="submit">{{t "edit.save"}}</button>
306 {{end}}
307</form>
308<script src="/assets/editor.js"></script>
309{{template "layout-end" .}}
310{{end}}
311
265{{define "releases"}}
312{{define "releases"}}
266{{template "layout-start" .}}
313{{template "layout-start" .}}
267<h1>{{t "title.releases"}}</h1>
314<h1>{{t "title.releases"}}</h1>
internal/server/templates/layout.html +1 −1
@@ -41,7 +41,7 @@
41 41 <a href="/{{.Repo.FullPath}}/-/commits{{if .Rev}}?rev={{.Rev}}{{end}}"{{if eq .Page "commits"}} class="on"{{end}}>{{t "tab.commits"}}</a>
42 42 <a href="/{{.Repo.FullPath}}/-/branches">{{t "tab.branches"}}</a>
43 43 <a href="/{{.Repo.FullPath}}/-/merge-requests">{{t "tab.mrs"}}</a>
44 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if eq .Page "pipelines"}} class="on"{{end}}>{{t "tab.pipelines"}}</a>
44 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if or (eq .Page "pipelines") (eq .Page "editor")}} class="on"{{end}}>{{t "tab.pipelines"}}</a>
45 45 <a href="/{{.Repo.FullPath}}/-/releases">{{t "tab.releases"}}</a>
46 46 {{if .CanAdmin}}<a href="/{{.Repo.FullPath}}/-/settings">{{t "tab.settings"}}</a>{{end}}
47 47 </p>
@@ -41,7 +41,7 @@
41 <a href="/{{.Repo.FullPath}}/-/commits{{if .Rev}}?rev={{.Rev}}{{end}}"{{if eq .Page "commits"}} class="on"{{end}}>{{t "tab.commits"}}</a>
41 <a href="/{{.Repo.FullPath}}/-/commits{{if .Rev}}?rev={{.Rev}}{{end}}"{{if eq .Page "commits"}} class="on"{{end}}>{{t "tab.commits"}}</a>
42 <a href="/{{.Repo.FullPath}}/-/branches">{{t "tab.branches"}}</a>
42 <a href="/{{.Repo.FullPath}}/-/branches">{{t "tab.branches"}}</a>
43 <a href="/{{.Repo.FullPath}}/-/merge-requests">{{t "tab.mrs"}}</a>
43 <a href="/{{.Repo.FullPath}}/-/merge-requests">{{t "tab.mrs"}}</a>
44 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if eq .Page "pipelines"}} class="on"{{end}}>{{t "tab.pipelines"}}</a>
44 <a href="/{{.Repo.FullPath}}/-/pipelines"{{if or (eq .Page "pipelines") (eq .Page "editor")}} class="on"{{end}}>{{t "tab.pipelines"}}</a>
45 <a href="/{{.Repo.FullPath}}/-/releases">{{t "tab.releases"}}</a>
45 <a href="/{{.Repo.FullPath}}/-/releases">{{t "tab.releases"}}</a>
46 {{if .CanAdmin}}<a href="/{{.Repo.FullPath}}/-/settings">{{t "tab.settings"}}</a>{{end}}
46 {{if .CanAdmin}}<a href="/{{.Repo.FullPath}}/-/settings">{{t "tab.settings"}}</a>{{end}}
47</p>
47</p>
internal/store/flow.go +6 −4
@@ -469,7 +469,7 @@ func scanJob(rows *sql.Rows) (Job, error) {
469 469
470 470 // ActivePipelines zijn nog niet af.
471 471 func (s *Store) ActivePipelines(ctx context.Context) ([]Pipeline, error) {
472 rows, err := s.db.QueryContext(ctx, pipelineCols+` WHERE status IN ('queued', 'running', 'manual') ORDER BY id`)
472 rows, err := s.db.QueryContext(ctx, pipelineCols+` WHERE status IN ('queued', 'running', 'manual') OR id IN (SELECT pipeline_id FROM jobs WHERE status IN ('queued', 'running')) ORDER BY id`)
473 473 if err != nil {
474 474 return nil, err
475 475 }
@@ -532,11 +532,13 @@ func (s *Store) RefreshPipelineStatus(ctx context.Context, pipelineID int64) (st
532 532 }
533 533 }
534 534 switch {
535 case status == "failed":
536 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'failed', finished_at = ? WHERE id = ?`, now(), pipelineID)
537 535 case open:
536 // Een harde mislukking wint pas als er niets meer in de wachtrij of
537 // aan het werk is. Anders blijft de runner latere jobs overslaan.
538 538 status = "running"
539 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'running' WHERE id = ?`, pipelineID)
539 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'running', finished_at = NULL WHERE id = ?`, pipelineID)
540 case status == "failed":
541 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'failed', finished_at = ? WHERE id = ?`, now(), pipelineID)
540 542 case manual:
541 543 status = "manual"
542 544 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'manual', finished_at = NULL WHERE id = ?`, pipelineID)
@@ -469,7 +469,7 @@ func scanJob(rows *sql.Rows) (Job, error) {
469
469
470// ActivePipelines zijn nog niet af.
470// ActivePipelines zijn nog niet af.
471func (s *Store) ActivePipelines(ctx context.Context) ([]Pipeline, error) {
471func (s *Store) ActivePipelines(ctx context.Context) ([]Pipeline, error) {
472 rows, err := s.db.QueryContext(ctx, pipelineCols+` WHERE status IN ('queued', 'running', 'manual') ORDER BY id`)
472 rows, err := s.db.QueryContext(ctx, pipelineCols+` WHERE status IN ('queued', 'running', 'manual') OR id IN (SELECT pipeline_id FROM jobs WHERE status IN ('queued', 'running')) ORDER BY id`)
473 if err != nil {
473 if err != nil {
474 return nil, err
474 return nil, err
475 }
475 }
@@ -532,11 +532,13 @@ func (s *Store) RefreshPipelineStatus(ctx context.Context, pipelineID int64) (st
532 }
532 }
533 }
533 }
534 switch {
534 switch {
535 case status == "failed":
536 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'failed', finished_at = ? WHERE id = ?`, now(), pipelineID)
537 case open:
535 case open:
536 // Een harde mislukking wint pas als er niets meer in de wachtrij of
537 // aan het werk is. Anders blijft de runner latere jobs overslaan.
538 status = "running"
538 status = "running"
539 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'running' WHERE id = ?`, pipelineID)
539 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'running', finished_at = NULL WHERE id = ?`, pipelineID)
540 case status == "failed":
541 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'failed', finished_at = ? WHERE id = ?`, now(), pipelineID)
540 case manual:
542 case manual:
541 status = "manual"
543 status = "manual"
542 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'manual', finished_at = NULL WHERE id = ?`, pipelineID)
544 _, err = s.db.ExecContext(ctx, `UPDATE pipelines SET status = 'manual', finished_at = NULL WHERE id = ?`, pipelineID)
internal/store/pipeline_status_test.go added +52 −0
@@ -0,0 +1,52 @@
1 package store
2
3 import (
4 "context"
5 "testing"
6 )
7
8 func TestRefreshWaitsForQueuedJobs(t *testing.T) {
9 s := openTest(t)
10 ctx := context.Background()
11 tim, err := s.UpsertUser(ctx, 1, "tim", "", false)
12 if err != nil {
13 t.Fatal(err)
14 }
15 repo, err := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")
16 if err != nil {
17 t.Fatal(err)
18 }
19 id, err := s.CreatePipeline(ctx, repo.ID, "abc", "refs/heads/master", "push", "tim", "fail", []NewJob{
20 {Name: "hard", Stage: "test", Script: "exit 1"},
21 {Name: "later", Stage: "deploy", Script: "exit 0"},
22 })
23 if err != nil {
24 t.Fatal(err)
25 }
26 jobs, err := s.JobsByPipeline(ctx, id)
27 if err != nil {
28 t.Fatal(err)
29 }
30 for _, j := range jobs {
31 if j.Name == "hard" {
32 if err := s.FinishJob(ctx, j.ID, "failed", ""); err != nil {
33 t.Fatal(err)
34 }
35 }
36 }
37 status, err := s.RefreshPipelineStatus(ctx, id)
38 if err != nil || status != "running" {
39 t.Fatalf("wachtrij hoort de pipeline open te houden: %s %v", status, err)
40 }
41 for _, j := range jobs {
42 if j.Name == "later" {
43 if err := s.SkipJob(ctx, j.ID, "skipped"); err != nil {
44 t.Fatal(err)
45 }
46 }
47 }
48 status, err = s.RefreshPipelineStatus(ctx, id)
49 if err != nil || status != "failed" {
50 t.Fatalf("na het overslaan: %s %v", status, err)
51 }
52 }
@@ -0,0 +1,52 @@
1package store
2
3import (
4 "context"
5 "testing"
6)
7
8func TestRefreshWaitsForQueuedJobs(t *testing.T) {
9 s := openTest(t)
10 ctx := context.Background()
11 tim, err := s.UpsertUser(ctx, 1, "tim", "", false)
12 if err != nil {
13 t.Fatal(err)
14 }
15 repo, err := s.CreateRepo(ctx, "user", tim.ID, "ci", "", "master", "private")
16 if err != nil {
17 t.Fatal(err)
18 }
19 id, err := s.CreatePipeline(ctx, repo.ID, "abc", "refs/heads/master", "push", "tim", "fail", []NewJob{
20 {Name: "hard", Stage: "test", Script: "exit 1"},
21 {Name: "later", Stage: "deploy", Script: "exit 0"},
22 })
23 if err != nil {
24 t.Fatal(err)
25 }
26 jobs, err := s.JobsByPipeline(ctx, id)
27 if err != nil {
28 t.Fatal(err)
29 }
30 for _, j := range jobs {
31 if j.Name == "hard" {
32 if err := s.FinishJob(ctx, j.ID, "failed", ""); err != nil {
33 t.Fatal(err)
34 }
35 }
36 }
37 status, err := s.RefreshPipelineStatus(ctx, id)
38 if err != nil || status != "running" {
39 t.Fatalf("wachtrij hoort de pipeline open te houden: %s %v", status, err)
40 }
41 for _, j := range jobs {
42 if j.Name == "later" {
43 if err := s.SkipJob(ctx, j.ID, "skipped"); err != nil {
44 t.Fatal(err)
45 }
46 }
47 }
48 status, err = s.RefreshPipelineStatus(ctx, id)
49 if err != nil || status != "failed" {
50 t.Fatalf("na het overslaan: %s %v", status, err)
51 }
52}