fix: stuur de console-output van elke job mee in pipeline-webhooks
Een te lange log blijft afgekapt, met log_truncated en een API-URL naar de volledige gemaskeerde log. Co-authored-by: tim <tim@timfalken.com>
@@ -10,7 +10,7 @@ func init() {
10
10
var webhookEN = map[string]string{
11
11
"info.sec.webhooks": "Webhook payloads",
12
12
"info.wh.spec": "OpenAPI",
13
"info.wh.lead": "An empty body template sends this JSON document. pipeline_success and pipeline_failure include the pipeline id, status, ref, sha, commit message, author and duration, and every job with its name, stage, status, duration, allow_failure and web URL. A failed job also includes the last 200 log lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. log_truncated is true when the log was cut. log_url points at the full log. A push includes ref, before, after and up to 20 commits, each with sha, message, author and URL. A merge request includes id, title, description, source branch, target branch, status, author and the action opened, merged or commented. A comment action also includes the comment text. A release includes the tag, name, commit and assets with URLs.",
13
"info.wh.lead": "An empty body template sends this JSON document. pipeline_success and pipeline_failure include the pipeline id, status, ref, sha, commit message, author and duration, and every job with its name, stage, status, duration, allow_failure, web URL and console output. Each log is the last 200 lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. log_truncated is true when the log was cut. log_url is then present and is GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. That request needs Authorization: Bearer and a personal token that can read the repo. A push includes ref, before, after and up to 20 commits, each with sha, message, author and URL. A merge request includes id, title, description, source branch, target branch, status, author and the action opened, merged or commented. A comment action also includes the comment text. A release includes the tag, name, commit and assets with URLs.",
14
14
"hook.mrMerged": "merge request !%s merged: %s",
15
15
"hook.mrComment": "comment on merge request !%s",
16
16
}
@@ -18,7 +18,7 @@ var webhookEN = map[string]string{
18
18
var webhookNL = map[string]string{
19
19
"info.sec.webhooks": "Webhook-payloads",
20
20
"info.wh.spec": "OpenAPI",
21
"info.wh.lead": "Een leeg body-sjabloon stuurt dit JSON-document. pipeline_success en pipeline_failure bevatten het pipeline-id, de status, ref, sha, commitboodschap, auteur en duur, en elke job met naam, stage, status, duur, allow_failure en web-URL. Een gefaalde job bevat ook de laatste 200 logregels, maximaal 64 KiB. Geheimwaarden van minstens 8 tekens worden [MASKED]. log_truncated is true als de log is afgekapt. log_url wijst naar de volledige log. Een push bevat ref, before, after en hoogstens 20 commits, elk met sha, message, auteur en URL. Een merge request bevat id, titel, beschrijving, bronbranch, doelbranch, status, auteur en de actie opened, merged of commented. Bij een commentaar hoort ook de tekst. Een release bevat tag, naam, commit en assets met URL's.",
21
"info.wh.lead": "Een leeg body-sjabloon stuurt dit JSON-document. pipeline_success en pipeline_failure bevatten het pipeline-id, de status, ref, sha, commitboodschap, auteur en duur, en elke job met naam, stage, status, duur, allow_failure, web-URL en console-output. Elke log bevat de laatste 200 regels, maximaal 64 KiB. Geheimwaarden van minstens 8 tekens worden [MASKED]. log_truncated is true als de log is afgekapt. log_url staat er dan bij en is GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Die aanvraag heeft Authorization: Bearer en een persoonlijk token dat de repo mag lezen. Een push bevat ref, before, after en hoogstens 20 commits, elk met sha, message, auteur en URL. Een merge request bevat id, titel, beschrijving, bronbranch, doelbranch, status, auteur en de actie opened, merged of commented. Bij een commentaar hoort ook de tekst. Een release bevat tag, naam, commit en assets met URL's.",
22
22
"hook.mrMerged": "merge request !%s samengevoegd: %s",
23
23
"hook.mrComment": "reactie op merge request !%s",
24
24
}
@@ -26,7 +26,7 @@ var webhookNL = map[string]string{
26
26
var webhookDE = map[string]string{
27
27
"info.sec.webhooks": "Webhook-Nutzdaten",
28
28
"info.wh.spec": "OpenAPI",
29
"info.wh.lead": "Eine leere Body-Vorlage sendet dieses JSON-Dokument. pipeline_success und pipeline_failure enthalten Pipeline-Id, Status, Ref, SHA, Commit-Nachricht, Autor und Dauer, sowie jeden Job mit Name, Stage, Status, Dauer, allow_failure und Web-URL. Ein fehlgeschlagener Job enthält außerdem die letzten 200 Logzeilen, höchstens 64 KiB. Geheimwerte ab 8 Zeichen werden zu [MASKED]. log_truncated ist true, wenn das Log gekürzt wurde. log_url zeigt auf das vollständige Log. Ein Push enthält ref, before, after und höchstens 20 Commits mit SHA, Nachricht, Autor und URL. Ein Merge Request enthält Id, Titel, Beschreibung, Quellbranch, Zielbranch, Status, Autor und die Aktion opened, merged oder commented. Bei einem Kommentar steht der Text dabei. Ein Release enthält Tag, Name, Commit und Assets mit URLs.",
29
"info.wh.lead": "Eine leere Body-Vorlage sendet dieses JSON-Dokument. pipeline_success und pipeline_failure enthalten Pipeline-Id, Status, Ref, SHA, Commit-Nachricht, Autor und Dauer, sowie jeden Job mit Name, Stage, Status, Dauer, allow_failure, Web-URL und Konsolenausgabe. Jedes Log enthält die letzten 200 Zeilen, höchstens 64 KiB. Geheimwerte ab 8 Zeichen werden zu [MASKED]. log_truncated ist true, wenn das Log gekürzt wurde. log_url steht dann dabei und ist GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Diese Anfrage braucht Authorization: Bearer und ein persönliches Token, das das Repo lesen darf. Ein Push enthält ref, before, after und höchstens 20 Commits mit SHA, Nachricht, Autor und URL. Ein Merge Request enthält Id, Titel, Beschreibung, Quellbranch, Zielbranch, Status, Autor und die Aktion opened, merged oder commented. Bei einem Kommentar steht der Text dabei. Ein Release enthält Tag, Name, Commit und Assets mit URLs.",
30
30
"hook.mrMerged": "Merge-Request !%s zusammengeführt: %s",
31
31
"hook.mrComment": "Kommentar zu Merge-Request !%s",
32
32
}
@@ -34,7 +34,7 @@ var webhookDE = map[string]string{
34
34
var webhookFR = map[string]string{
35
35
"info.sec.webhooks": "Charges utiles des webhooks",
36
36
"info.wh.spec": "OpenAPI",
37
"info.wh.lead": "Un modèle de corps vide envoie ce document JSON. pipeline_success et pipeline_failure contiennent l'id du pipeline, le statut, la ref, le sha, le message de commit, l'auteur et la durée, plus chaque job avec nom, stage, statut, durée, allow_failure et URL web. Un job en échec ajoute les 200 dernières lignes de log, 64 KiB au plus. Les valeurs secrètes d'au moins 8 caractères deviennent [MASKED]. log_truncated vaut true quand le log est coupé. log_url pointe vers le log complet. Un push contient ref, before, after et 20 commits au plus, chacun avec sha, message, auteur et URL. Une merge request contient id, titre, description, branche source, branche cible, statut, auteur et l'action opened, merged ou commented. Une action commented contient aussi le texte. Une release contient le tag, le nom, le commit et les assets avec leurs URL.",
37
"info.wh.lead": "Un modèle de corps vide envoie ce document JSON. pipeline_success et pipeline_failure contiennent l'id du pipeline, le statut, la ref, le sha, le message de commit, l'auteur et la durée, plus chaque job avec nom, stage, statut, durée, allow_failure, URL web et sortie console. Chaque log contient les 200 dernières lignes, 64 KiB au plus. Les valeurs secrètes d'au moins 8 caractères deviennent [MASKED]. log_truncated vaut true quand le log est coupé. log_url est alors présent et vaut GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Cette requête exige Authorization: Bearer et un jeton personnel qui peut lire le dépôt. Un push contient ref, before, after et 20 commits au plus, chacun avec sha, message, auteur et URL. Une merge request contient id, titre, description, branche source, branche cible, statut, auteur et l'action opened, merged ou commented. Une action commented contient aussi le texte. Une release contient le tag, le nom, le commit et les assets avec leurs URL.",
38
38
"hook.mrMerged": "merge request !%s fusionnée : %s",
39
39
"hook.mrComment": "commentaire sur la merge request !%s",
40
40
}
@@ -10,7 +10,7 @@ func init() {10
var webhookEN = map[string]string{10
var webhookEN = map[string]string{11
"info.sec.webhooks": "Webhook payloads",11
"info.sec.webhooks": "Webhook payloads",12
"info.wh.spec": "OpenAPI",12
"info.wh.spec": "OpenAPI",13
"info.wh.lead": "An empty body template sends this JSON document. pipeline_success and pipeline_failure include the pipeline id, status, ref, sha, commit message, author and duration, and every job with its name, stage, status, duration, allow_failure and web URL. A failed job also includes the last 200 log lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. log_truncated is true when the log was cut. log_url points at the full log. A push includes ref, before, after and up to 20 commits, each with sha, message, author and URL. A merge request includes id, title, description, source branch, target branch, status, author and the action opened, merged or commented. A comment action also includes the comment text. A release includes the tag, name, commit and assets with URLs.",13
"info.wh.lead": "An empty body template sends this JSON document. pipeline_success and pipeline_failure include the pipeline id, status, ref, sha, commit message, author and duration, and every job with its name, stage, status, duration, allow_failure, web URL and console output. Each log is the last 200 lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. log_truncated is true when the log was cut. log_url is then present and is GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. That request needs Authorization: Bearer and a personal token that can read the repo. A push includes ref, before, after and up to 20 commits, each with sha, message, author and URL. A merge request includes id, title, description, source branch, target branch, status, author and the action opened, merged or commented. A comment action also includes the comment text. A release includes the tag, name, commit and assets with URLs.",14
"hook.mrMerged": "merge request !%s merged: %s",14
"hook.mrMerged": "merge request !%s merged: %s",15
"hook.mrComment": "comment on merge request !%s",15
"hook.mrComment": "comment on merge request !%s",16
}16
}@@ -18,7 +18,7 @@ var webhookEN = map[string]string{18
var webhookNL = map[string]string{18
var webhookNL = map[string]string{19
"info.sec.webhooks": "Webhook-payloads",19
"info.sec.webhooks": "Webhook-payloads",20
"info.wh.spec": "OpenAPI",20
"info.wh.spec": "OpenAPI",21
"info.wh.lead": "Een leeg body-sjabloon stuurt dit JSON-document. pipeline_success en pipeline_failure bevatten het pipeline-id, de status, ref, sha, commitboodschap, auteur en duur, en elke job met naam, stage, status, duur, allow_failure en web-URL. Een gefaalde job bevat ook de laatste 200 logregels, maximaal 64 KiB. Geheimwaarden van minstens 8 tekens worden [MASKED]. log_truncated is true als de log is afgekapt. log_url wijst naar de volledige log. Een push bevat ref, before, after en hoogstens 20 commits, elk met sha, message, auteur en URL. Een merge request bevat id, titel, beschrijving, bronbranch, doelbranch, status, auteur en de actie opened, merged of commented. Bij een commentaar hoort ook de tekst. Een release bevat tag, naam, commit en assets met URL's.",21
"info.wh.lead": "Een leeg body-sjabloon stuurt dit JSON-document. pipeline_success en pipeline_failure bevatten het pipeline-id, de status, ref, sha, commitboodschap, auteur en duur, en elke job met naam, stage, status, duur, allow_failure, web-URL en console-output. Elke log bevat de laatste 200 regels, maximaal 64 KiB. Geheimwaarden van minstens 8 tekens worden [MASKED]. log_truncated is true als de log is afgekapt. log_url staat er dan bij en is GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Die aanvraag heeft Authorization: Bearer en een persoonlijk token dat de repo mag lezen. Een push bevat ref, before, after en hoogstens 20 commits, elk met sha, message, auteur en URL. Een merge request bevat id, titel, beschrijving, bronbranch, doelbranch, status, auteur en de actie opened, merged of commented. Bij een commentaar hoort ook de tekst. Een release bevat tag, naam, commit en assets met URL's.",22
"hook.mrMerged": "merge request !%s samengevoegd: %s",22
"hook.mrMerged": "merge request !%s samengevoegd: %s",23
"hook.mrComment": "reactie op merge request !%s",23
"hook.mrComment": "reactie op merge request !%s",24
}24
}@@ -26,7 +26,7 @@ var webhookNL = map[string]string{26
var webhookDE = map[string]string{26
var webhookDE = map[string]string{27
"info.sec.webhooks": "Webhook-Nutzdaten",27
"info.sec.webhooks": "Webhook-Nutzdaten",28
"info.wh.spec": "OpenAPI",28
"info.wh.spec": "OpenAPI",29
"info.wh.lead": "Eine leere Body-Vorlage sendet dieses JSON-Dokument. pipeline_success und pipeline_failure enthalten Pipeline-Id, Status, Ref, SHA, Commit-Nachricht, Autor und Dauer, sowie jeden Job mit Name, Stage, Status, Dauer, allow_failure und Web-URL. Ein fehlgeschlagener Job enthält außerdem die letzten 200 Logzeilen, höchstens 64 KiB. Geheimwerte ab 8 Zeichen werden zu [MASKED]. log_truncated ist true, wenn das Log gekürzt wurde. log_url zeigt auf das vollständige Log. Ein Push enthält ref, before, after und höchstens 20 Commits mit SHA, Nachricht, Autor und URL. Ein Merge Request enthält Id, Titel, Beschreibung, Quellbranch, Zielbranch, Status, Autor und die Aktion opened, merged oder commented. Bei einem Kommentar steht der Text dabei. Ein Release enthält Tag, Name, Commit und Assets mit URLs.",29
"info.wh.lead": "Eine leere Body-Vorlage sendet dieses JSON-Dokument. pipeline_success und pipeline_failure enthalten Pipeline-Id, Status, Ref, SHA, Commit-Nachricht, Autor und Dauer, sowie jeden Job mit Name, Stage, Status, Dauer, allow_failure, Web-URL und Konsolenausgabe. Jedes Log enthält die letzten 200 Zeilen, höchstens 64 KiB. Geheimwerte ab 8 Zeichen werden zu [MASKED]. log_truncated ist true, wenn das Log gekürzt wurde. log_url steht dann dabei und ist GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Diese Anfrage braucht Authorization: Bearer und ein persönliches Token, das das Repo lesen darf. Ein Push enthält ref, before, after und höchstens 20 Commits mit SHA, Nachricht, Autor und URL. Ein Merge Request enthält Id, Titel, Beschreibung, Quellbranch, Zielbranch, Status, Autor und die Aktion opened, merged oder commented. Bei einem Kommentar steht der Text dabei. Ein Release enthält Tag, Name, Commit und Assets mit URLs.",30
"hook.mrMerged": "Merge-Request !%s zusammengeführt: %s",30
"hook.mrMerged": "Merge-Request !%s zusammengeführt: %s",31
"hook.mrComment": "Kommentar zu Merge-Request !%s",31
"hook.mrComment": "Kommentar zu Merge-Request !%s",32
}32
}@@ -34,7 +34,7 @@ var webhookDE = map[string]string{34
var webhookFR = map[string]string{34
var webhookFR = map[string]string{35
"info.sec.webhooks": "Charges utiles des webhooks",35
"info.sec.webhooks": "Charges utiles des webhooks",36
"info.wh.spec": "OpenAPI",36
"info.wh.spec": "OpenAPI",37
"info.wh.lead": "Un modèle de corps vide envoie ce document JSON. pipeline_success et pipeline_failure contiennent l'id du pipeline, le statut, la ref, le sha, le message de commit, l'auteur et la durée, plus chaque job avec nom, stage, statut, durée, allow_failure et URL web. Un job en échec ajoute les 200 dernières lignes de log, 64 KiB au plus. Les valeurs secrètes d'au moins 8 caractères deviennent [MASKED]. log_truncated vaut true quand le log est coupé. log_url pointe vers le log complet. Un push contient ref, before, after et 20 commits au plus, chacun avec sha, message, auteur et URL. Une merge request contient id, titre, description, branche source, branche cible, statut, auteur et l'action opened, merged ou commented. Une action commented contient aussi le texte. Une release contient le tag, le nom, le commit et les assets avec leurs URL.",37
"info.wh.lead": "Un modèle de corps vide envoie ce document JSON. pipeline_success et pipeline_failure contiennent l'id du pipeline, le statut, la ref, le sha, le message de commit, l'auteur et la durée, plus chaque job avec nom, stage, statut, durée, allow_failure, URL web et sortie console. Chaque log contient les 200 dernières lignes, 64 KiB au plus. Les valeurs secrètes d'au moins 8 caractères deviennent [MASKED]. log_truncated vaut true quand le log est coupé. log_url est alors présent et vaut GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Cette requête exige Authorization: Bearer et un jeton personnel qui peut lire le dépôt. Un push contient ref, before, after et 20 commits au plus, chacun avec sha, message, auteur et URL. Une merge request contient id, titre, description, branche source, branche cible, statut, auteur et l'action opened, merged ou commented. Une action commented contient aussi le texte. Une release contient le tag, le nom, le commit et les assets avec leurs URL.",38
"hook.mrMerged": "merge request !%s fusionnée : %s",38
"hook.mrMerged": "merge request !%s fusionnée : %s",39
"hook.mrComment": "commentaire sur la merge request !%s",39
"hook.mrComment": "commentaire sur la merge request !%s",40
}40
}@@ -5,9 +5,11 @@ import (
5
5
"net/http"
6
6
"os"
7
7
"path/filepath"
8
"strconv"
8
9
"strings"
9
10
10
11
"gitlab.com/Timfa/bluegit/internal/access"
12
"gitlab.com/Timfa/bluegit/internal/ci"
11
13
"gitlab.com/Timfa/bluegit/internal/i18n"
12
14
"gitlab.com/Timfa/bluegit/internal/store"
13
15
)
@@ -84,6 +86,53 @@ func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) {
84
86
http.NotFound(w, r)
85
87
}
86
88
89
// jobLogAPI geeft de volledige console-output van een job, met geheimen gemaskeerd.
90
// Een persoonlijk token is verplicht, ook bij een publieke repo. Job-tokens tellen niet.
91
func (s *Server) jobLogAPI(w http.ResponseWriter, r *http.Request) {
92
repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo"))
93
if err != nil {
94
http.NotFound(w, r)
95
return
96
}
97
token := bearer(r)
98
if token == "" {
99
http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized)
100
return
101
}
102
if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") {
103
http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized)
104
return
105
}
106
u, err := s.Store.UserByToken(r.Context(), token)
107
if err != nil {
108
http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized)
109
return
110
}
111
role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID)
112
if err != nil {
113
http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError)
114
return
115
}
116
if !role.CanRead() && repo.Visibility != "public" {
117
http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden)
118
return
119
}
120
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
121
if err != nil || id <= 0 {
122
http.NotFound(w, r)
123
return
124
}
125
job, err := s.Store.JobByID(r.Context(), repo.ID, id)
126
if err != nil {
127
http.NotFound(w, r)
128
return
129
}
130
text := ci.Mask(job.Log, s.maskSecrets(r.Context(), repo))
131
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
132
w.Header().Set("Cache-Control", "no-store")
133
_, _ = w.Write([]byte(text))
134
}
135
87
136
func bearer(r *http.Request) string {
88
137
h := r.Header.Get("Authorization")
89
138
if strings.HasPrefix(strings.ToLower(h), "bearer ") {
@@ -5,9 +5,11 @@ import (5
"net/http"5
"net/http"6
"os"6
"os"7
"path/filepath"7
"path/filepath"8
"strconv"8
"strings"9
"strings"9
10
10
"gitlab.com/Timfa/bluegit/internal/access"11
"gitlab.com/Timfa/bluegit/internal/access"12
"gitlab.com/Timfa/bluegit/internal/ci"11
"gitlab.com/Timfa/bluegit/internal/i18n"13
"gitlab.com/Timfa/bluegit/internal/i18n"12
"gitlab.com/Timfa/bluegit/internal/store"14
"gitlab.com/Timfa/bluegit/internal/store"13
)15
)@@ -84,6 +86,53 @@ func (s *Server) releaseAsset(w http.ResponseWriter, r *http.Request) {84
http.NotFound(w, r)86
http.NotFound(w, r)85
}87
}86
88
89
// jobLogAPI geeft de volledige console-output van een job, met geheimen gemaskeerd.90
// Een persoonlijk token is verplicht, ook bij een publieke repo. Job-tokens tellen niet.91
func (s *Server) jobLogAPI(w http.ResponseWriter, r *http.Request) {92
repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo"))93
if err != nil {94
http.NotFound(w, r)95
return96
}97
token := bearer(r)98
if token == "" {99
http.Error(w, s.tr(r, "err.tokenAbsent"), http.StatusUnauthorized)100
return101
}102
if strings.HasPrefix(token, "bgj_") || !strings.HasPrefix(token, "bg_") {103
http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized)104
return105
}106
u, err := s.Store.UserByToken(r.Context(), token)107
if err != nil {108
http.Error(w, s.tr(r, "err.tokenBad"), http.StatusUnauthorized)109
return110
}111
role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID)112
if err != nil {113
http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError)114
return115
}116
if !role.CanRead() && repo.Visibility != "public" {117
http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden)118
return119
}120
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)121
if err != nil || id <= 0 {122
http.NotFound(w, r)123
return124
}125
job, err := s.Store.JobByID(r.Context(), repo.ID, id)126
if err != nil {127
http.NotFound(w, r)128
return129
}130
text := ci.Mask(job.Log, s.maskSecrets(r.Context(), repo))131
w.Header().Set("Content-Type", "text/plain; charset=utf-8")132
w.Header().Set("Cache-Control", "no-store")133
_, _ = w.Write([]byte(text))134
}135
87
func bearer(r *http.Request) string {136
func bearer(r *http.Request) string {88
h := r.Header.Get("Authorization")137
h := r.Header.Get("Authorization")89
if strings.HasPrefix(strings.ToLower(h), "bearer ") {138
if strings.HasPrefix(strings.ToLower(h), "bearer ") {@@ -74,17 +74,17 @@ func (s *Server) fillPipelineEvent(ctx context.Context, repo store.Repo, ev *web
74
74
secrets := s.maskSecrets(ctx, repo)
75
75
ev.Jobs = make([]webhook.JobInfo, 0, len(jobs))
76
76
for _, j := range jobs {
77
logText, truncated := webhook.TailLog(j.Log, secrets)
77
78
info := webhook.JobInfo{
78
79
Name: j.Name, Stage: j.Stage, Status: j.Status,
79
80
Duration: elapsedSeconds(j.StartedAt, j.FinishedAt),
80
81
AllowFailure: j.AllowFailure,
81
82
WebURL: pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10),
83
Log: logText,
84
LogTruncated: truncated,
82
85
}
83
if j.Status == "failed" {
84
log, truncated := webhook.TailLog(j.Log, secrets)
85
info.Log = log
86
info.LogTruncated = &truncated
87
info.LogURL = info.WebURL + "/log?raw=1"
86
if truncated {
87
info.LogURL = jobLogAPIURL(s.Cfg.BaseURL, repo, j.ID)
88
88
}
89
89
ev.Jobs = append(ev.Jobs, info)
90
90
}
@@ -186,6 +186,10 @@ func (s *Server) releaseEvent(repo store.Repo, tag, name, commit string, assets
186
186
}
187
187
}
188
188
189
func jobLogAPIURL(base string, repo store.Repo, jobID int64) string {
190
return strings.TrimRight(base, "/") + "/api/v1/repos/" + url.PathEscape(repo.OwnerPath) + "/" + url.PathEscape(repo.Name) + "/jobs/" + strconv.FormatInt(jobID, 10) + "/log"
191
}
192
189
193
func refName(ref string) string {
190
194
if b, ok := gitrepo.BranchOfRef(ref); ok {
191
195
return b
@@ -74,17 +74,17 @@ func (s *Server) fillPipelineEvent(ctx context.Context, repo store.Repo, ev *web74
secrets := s.maskSecrets(ctx, repo)74
secrets := s.maskSecrets(ctx, repo)75
ev.Jobs = make([]webhook.JobInfo, 0, len(jobs))75
ev.Jobs = make([]webhook.JobInfo, 0, len(jobs))76
for _, j := range jobs {76
for _, j := range jobs {77
logText, truncated := webhook.TailLog(j.Log, secrets)77
info := webhook.JobInfo{78
info := webhook.JobInfo{78
Name: j.Name, Stage: j.Stage, Status: j.Status,79
Name: j.Name, Stage: j.Stage, Status: j.Status,79
Duration: elapsedSeconds(j.StartedAt, j.FinishedAt),80
Duration: elapsedSeconds(j.StartedAt, j.FinishedAt),80
AllowFailure: j.AllowFailure,81
AllowFailure: j.AllowFailure,81
WebURL: pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10),82
WebURL: pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10),83
Log: logText,84
LogTruncated: truncated,82
}85
}83
if j.Status == "failed" {86
if truncated {84
log, truncated := webhook.TailLog(j.Log, secrets)87
info.LogURL = jobLogAPIURL(s.Cfg.BaseURL, repo, j.ID)85
info.Log = log86
info.LogTruncated = &truncated87
info.LogURL = info.WebURL + "/log?raw=1"88
}88
}89
ev.Jobs = append(ev.Jobs, info)89
ev.Jobs = append(ev.Jobs, info)90
}90
}@@ -186,6 +186,10 @@ func (s *Server) releaseEvent(repo store.Repo, tag, name, commit string, assets186
}186
}187
}187
}188
188
189
func jobLogAPIURL(base string, repo store.Repo, jobID int64) string {190
return strings.TrimRight(base, "/") + "/api/v1/repos/" + url.PathEscape(repo.OwnerPath) + "/" + url.PathEscape(repo.Name) + "/jobs/" + strconv.FormatInt(jobID, 10) + "/log"191
}192
189
func refName(ref string) string {193
func refName(ref string) string {190
if b, ok := gitrepo.BranchOfRef(ref); ok {194
if b, ok := gitrepo.BranchOfRef(ref); ok {191
return b195
return b@@ -180,6 +180,9 @@ func TestWebhookPayloads(t *testing.T) {
180
180
t.Fatal(err)
181
181
}
182
182
default:
183
if err := st.AppendJobLog(ctx, j.ID, "gebouwd\n"); err != nil {
184
t.Fatal(err)
185
}
183
186
if err := st.FinishJob(ctx, j.ID, "success", ""); err != nil {
184
187
t.Fatal(err)
185
188
}
@@ -212,12 +215,19 @@ func TestWebhookPayloads(t *testing.T) {
212
215
if _, ok := detail["duration"]; !ok {
213
216
t.Fatal("duur ontbreekt")
214
217
}
218
var fullLogURL string
215
219
for _, item := range pipe["jobs"].([]any) {
216
220
job := item.(map[string]any)
217
221
raw, _ := json.Marshal(job)
218
222
if strings.Contains(string(raw), "super-secret-token") {
219
223
t.Fatalf("geheim in job %s", raw)
220
224
}
225
if _, ok := job["log"]; !ok {
226
t.Fatalf("elke job heeft een log: %s", raw)
227
}
228
if _, ok := job["log_truncated"]; !ok {
229
t.Fatalf("elke job heeft log_truncated: %s", raw)
230
}
221
231
switch job["name"] {
222
232
case "test":
223
233
if job["log_truncated"] != true || job["allow_failure"] != false {
@@ -227,19 +237,51 @@ func TestWebhookPayloads(t *testing.T) {
227
237
if !strings.Contains(text, "[MASKED]") || len(strings.Split(text, "\n")) != webhook.LogMaxLines {
228
238
t.Fatalf("log %d regels, staart %q", len(strings.Split(text, "\n")), text[max(0, len(text)-40):])
229
239
}
230
if !strings.Contains(job["log_url"].(string), "/log?raw=1") || !strings.Contains(job["web_url"].(string), "/jobs/") {
240
fullLogURL, _ = job["log_url"].(string)
241
if !strings.Contains(fullLogURL, "/api/v1/repos/tim/hooks/jobs/") || !strings.HasSuffix(fullLogURL, "/log") || !strings.Contains(job["web_url"].(string), "/jobs/") {
231
242
t.Fatalf("urls %s", raw)
232
243
}
233
244
case "build":
234
if _, ok := job["log"]; ok || job["status"] != "success" {
245
if job["status"] != "success" || job["log"] != "gebouwd\n" || job["log_truncated"] != false {
235
246
t.Fatalf("build %s", raw)
236
247
}
248
if _, ok := job["log_url"]; ok {
249
t.Fatalf("korte geslaagde log heeft een url: %s", raw)
250
}
237
251
case "soft":
238
if job["allow_failure"] != true || !strings.Contains(job["log"].(string), "[MASKED]") {
252
if job["allow_failure"] != true || job["log_truncated"] != false || !strings.Contains(job["log"].(string), "[MASKED]") {
239
253
t.Fatalf("soft %s", raw)
240
254
}
255
if _, ok := job["log_url"]; ok {
256
t.Fatalf("korte log heeft een url: %s", raw)
257
}
241
258
}
242
259
}
260
if fullLogURL == "" {
261
t.Fatal("geen log_url")
262
}
263
logPath := mustParse(t, fullLogURL).Path
264
noAuth := mustGet(t, http.DefaultClient, ts.URL+logPath)
265
if noAuth.StatusCode != http.StatusUnauthorized {
266
t.Fatalf("zonder token %d", noAuth.StatusCode)
267
}
268
noAuth.Body.Close()
269
full := getBearer(t, ts.URL+logPath, token)
270
if full.StatusCode != http.StatusOK || !strings.HasPrefix(full.Header.Get("Content-Type"), "text/plain") {
271
t.Fatalf("volle log %d %s", full.StatusCode, full.Header.Get("Content-Type"))
272
}
273
fullText := read(t, full)
274
if strings.Contains(fullText, "super-secret-token") || !strings.Contains(fullText, "[MASKED]") {
275
t.Fatal("volle log maskeert het geheim niet")
276
}
277
if n := len(strings.Split(fullText, "\n")); n <= webhook.LogMaxLines {
278
t.Fatalf("volle log heeft %d regels", n)
279
}
280
bad := getBearer(t, ts.URL+logPath, "bgj_geen-job")
281
if bad.StatusCode != http.StatusUnauthorized {
282
t.Fatalf("job-token %d", bad.StatusCode)
283
}
284
bad.Body.Close()
243
285
244
286
rel := srv.releaseEvent(repo, "v1.2.0", "BlueGit 1.2.0", sha, []store.ReleaseAsset{{Name: "blue git", Size: 9}})
245
287
raw, err := webhook.RenderBody("", rel, nil)
@@ -261,7 +303,7 @@ func TestWebhookPayloads(t *testing.T) {
261
303
}
262
304
263
305
infoPage := read(t, mustGet(t, tim, ts.URL+"/-/info"))
264
for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "commit_message", "source_branch"} {
306
for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "/api/v1/repos/{owner}/{repo}/jobs/{id}/log", "commit_message", "source_branch"} {
265
307
if !strings.Contains(infoPage, want) {
266
308
t.Errorf("infopagina mist %q", want)
267
309
}
@@ -271,7 +313,7 @@ func TestWebhookPayloads(t *testing.T) {
271
313
t.Fatal(err)
272
314
}
273
315
spec := read(t, specRes)
274
if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") {
316
if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") || !strings.Contains(spec, "/repos/{owner}/{repo}/jobs/{id}/log") {
275
317
t.Fatalf("spec %d %s", specRes.StatusCode, spec[:min(200, len(spec))])
276
318
}
277
319
}
@@ -307,6 +349,140 @@ func decodeAction(t *testing.T, bodies [][]byte, action string) map[string]any {
307
349
return nil
308
350
}
309
351
352
func TestJobLogAPI(t *testing.T) {
353
storage := t.TempDir()
354
st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
355
if err != nil {
356
t.Fatal(err)
357
}
358
t.Cleanup(func() { _ = st.Close() })
359
ctx := context.Background()
360
tim, err := st.UpsertUser(ctx, 1, "tim", "", false)
361
if err != nil {
362
t.Fatal(err)
363
}
364
ada, err := st.UpsertUser(ctx, 2, "ada", "", false)
365
if err != nil {
366
t.Fatal(err)
367
}
368
priv, err := st.CreateRepo(ctx, "user", tim.ID, "priv", "", "master", "private")
369
if err != nil {
370
t.Fatal(err)
371
}
372
open, err := st.CreateRepo(ctx, "user", tim.ID, "open", "", "master", "public")
373
if err != nil {
374
t.Fatal(err)
375
}
376
if err := st.PutSecret(ctx, "repo", priv.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil {
377
t.Fatal(err)
378
}
379
if err := st.PutSecret(ctx, "repo", open.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil {
380
t.Fatal(err)
381
}
382
privJob := seedJobLog(t, st, priv.ID, "regel\nsuper-secret-token\n")
383
openJob := seedJobLog(t, st, open.ID, "publiek super-secret-token\n")
384
ownerTok, err := st.CreateToken(ctx, tim.ID, "laptop")
385
if err != nil {
386
t.Fatal(err)
387
}
388
adaTok, err := st.CreateToken(ctx, ada.ID, "ada")
389
if err != nil {
390
t.Fatal(err)
391
}
392
cfg := config.Config{
393
BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage,
394
Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},
395
Git: config.Git{Binary: "git", DefaultBranch: "master"},
396
}
397
srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git"}
398
ts := httptest.NewServer(srv.Handler())
399
t.Cleanup(ts.Close)
400
401
privPath := ts.URL + "/api/v1/repos/tim/priv/jobs/" + strconv.FormatInt(privJob, 10) + "/log"
402
openPath := ts.URL + "/api/v1/repos/tim/open/jobs/" + strconv.FormatInt(openJob, 10) + "/log"
403
404
missing := mustGet(t, http.DefaultClient, privPath)
405
if missing.StatusCode != http.StatusUnauthorized {
406
t.Fatalf("zonder token %d", missing.StatusCode)
407
}
408
missing.Body.Close()
409
for _, tok := range []string{"nope", "bg_onbekend", "bgj_job"} {
410
res := getBearer(t, privPath, tok)
411
if res.StatusCode != http.StatusUnauthorized {
412
t.Fatalf("token %s geeft %d", tok, res.StatusCode)
413
}
414
res.Body.Close()
415
}
416
stranger := getBearer(t, privPath, adaTok)
417
if stranger.StatusCode != http.StatusForbidden {
418
t.Fatalf("vreemde op prive %d", stranger.StatusCode)
419
}
420
stranger.Body.Close()
421
other := getBearer(t, ts.URL+"/api/v1/repos/tim/priv/jobs/"+strconv.FormatInt(openJob, 10)+"/log", ownerTok)
422
if other.StatusCode != http.StatusNotFound {
423
t.Fatalf("job van andere repo %d", other.StatusCode)
424
}
425
other.Body.Close()
426
427
own := getBearer(t, privPath, ownerTok)
428
if own.StatusCode != http.StatusOK || !strings.HasPrefix(own.Header.Get("Content-Type"), "text/plain") {
429
t.Fatalf("eigenaar %d %s", own.StatusCode, own.Header.Get("Content-Type"))
430
}
431
body := read(t, own)
432
if body != "regel\n[MASKED]\n" {
433
t.Fatalf("prive log %q", body)
434
}
435
pub := getBearer(t, openPath, adaTok)
436
if pub.StatusCode != http.StatusOK {
437
t.Fatalf("publiek met vreemd token %d", pub.StatusCode)
438
}
439
if got := read(t, pub); got != "publiek [MASKED]\n" {
440
t.Fatalf("publieke log %q", got)
441
}
442
}
443
444
func seedJobLog(t *testing.T, st *store.Store, repoID int64, log string) int64 {
445
t.Helper()
446
ctx := context.Background()
447
id, err := st.CreatePipeline(ctx, repoID, strings.Repeat("a", 40), "refs/heads/master", "push", "tim", "titel", []store.NewJob{
448
{Name: "build", Stage: "build", Script: "echo ok"},
449
})
450
if err != nil {
451
t.Fatal(err)
452
}
453
jobs, err := st.JobsByPipeline(ctx, id)
454
if err != nil || len(jobs) != 1 {
455
t.Fatal(err)
456
}
457
if err := st.AppendJobLog(ctx, jobs[0].ID, log); err != nil {
458
t.Fatal(err)
459
}
460
return jobs[0].ID
461
}
462
463
func getBearer(t *testing.T, raw, token string) *http.Response {
464
t.Helper()
465
req, err := http.NewRequest(http.MethodGet, raw, nil)
466
if err != nil {
467
t.Fatal(err)
468
}
469
req.Header.Set("Authorization", "Bearer "+token)
470
res, err := http.DefaultClient.Do(req)
471
if err != nil {
472
t.Fatal(err)
473
}
474
return res
475
}
476
477
func mustParse(t *testing.T, raw string) *url.URL {
478
t.Helper()
479
u, err := url.Parse(raw)
480
if err != nil {
481
t.Fatal(err)
482
}
483
return u
484
}
485
310
486
func mustJSON(t *testing.T, v any) string {
311
487
t.Helper()
312
488
b, err := json.Marshal(v)
@@ -180,6 +180,9 @@ func TestWebhookPayloads(t *testing.T) {180
t.Fatal(err)180
t.Fatal(err)181
}181
}182
default:182
default:183
if err := st.AppendJobLog(ctx, j.ID, "gebouwd\n"); err != nil {184
t.Fatal(err)185
}183
if err := st.FinishJob(ctx, j.ID, "success", ""); err != nil {186
if err := st.FinishJob(ctx, j.ID, "success", ""); err != nil {184
t.Fatal(err)187
t.Fatal(err)185
}188
}@@ -212,12 +215,19 @@ func TestWebhookPayloads(t *testing.T) {212
if _, ok := detail["duration"]; !ok {215
if _, ok := detail["duration"]; !ok {213
t.Fatal("duur ontbreekt")216
t.Fatal("duur ontbreekt")214
}217
}218
var fullLogURL string215
for _, item := range pipe["jobs"].([]any) {219
for _, item := range pipe["jobs"].([]any) {216
job := item.(map[string]any)220
job := item.(map[string]any)217
raw, _ := json.Marshal(job)221
raw, _ := json.Marshal(job)218
if strings.Contains(string(raw), "super-secret-token") {222
if strings.Contains(string(raw), "super-secret-token") {219
t.Fatalf("geheim in job %s", raw)223
t.Fatalf("geheim in job %s", raw)220
}224
}225
if _, ok := job["log"]; !ok {226
t.Fatalf("elke job heeft een log: %s", raw)227
}228
if _, ok := job["log_truncated"]; !ok {229
t.Fatalf("elke job heeft log_truncated: %s", raw)230
}221
switch job["name"] {231
switch job["name"] {222
case "test":232
case "test":223
if job["log_truncated"] != true || job["allow_failure"] != false {233
if job["log_truncated"] != true || job["allow_failure"] != false {@@ -227,19 +237,51 @@ func TestWebhookPayloads(t *testing.T) {227
if !strings.Contains(text, "[MASKED]") || len(strings.Split(text, "\n")) != webhook.LogMaxLines {237
if !strings.Contains(text, "[MASKED]") || len(strings.Split(text, "\n")) != webhook.LogMaxLines {228
t.Fatalf("log %d regels, staart %q", len(strings.Split(text, "\n")), text[max(0, len(text)-40):])238
t.Fatalf("log %d regels, staart %q", len(strings.Split(text, "\n")), text[max(0, len(text)-40):])229
}239
}230
if !strings.Contains(job["log_url"].(string), "/log?raw=1") || !strings.Contains(job["web_url"].(string), "/jobs/") {240
fullLogURL, _ = job["log_url"].(string)241
if !strings.Contains(fullLogURL, "/api/v1/repos/tim/hooks/jobs/") || !strings.HasSuffix(fullLogURL, "/log") || !strings.Contains(job["web_url"].(string), "/jobs/") {231
t.Fatalf("urls %s", raw)242
t.Fatalf("urls %s", raw)232
}243
}233
case "build":244
case "build":234
if _, ok := job["log"]; ok || job["status"] != "success" {245
if job["status"] != "success" || job["log"] != "gebouwd\n" || job["log_truncated"] != false {235
t.Fatalf("build %s", raw)246
t.Fatalf("build %s", raw)236
}247
}248
if _, ok := job["log_url"]; ok {249
t.Fatalf("korte geslaagde log heeft een url: %s", raw)250
}237
case "soft":251
case "soft":238
if job["allow_failure"] != true || !strings.Contains(job["log"].(string), "[MASKED]") {252
if job["allow_failure"] != true || job["log_truncated"] != false || !strings.Contains(job["log"].(string), "[MASKED]") {239
t.Fatalf("soft %s", raw)253
t.Fatalf("soft %s", raw)240
}254
}255
if _, ok := job["log_url"]; ok {256
t.Fatalf("korte log heeft een url: %s", raw)257
}241
}258
}242
}259
}260
if fullLogURL == "" {261
t.Fatal("geen log_url")262
}263
logPath := mustParse(t, fullLogURL).Path264
noAuth := mustGet(t, http.DefaultClient, ts.URL+logPath)265
if noAuth.StatusCode != http.StatusUnauthorized {266
t.Fatalf("zonder token %d", noAuth.StatusCode)267
}268
noAuth.Body.Close()269
full := getBearer(t, ts.URL+logPath, token)270
if full.StatusCode != http.StatusOK || !strings.HasPrefix(full.Header.Get("Content-Type"), "text/plain") {271
t.Fatalf("volle log %d %s", full.StatusCode, full.Header.Get("Content-Type"))272
}273
fullText := read(t, full)274
if strings.Contains(fullText, "super-secret-token") || !strings.Contains(fullText, "[MASKED]") {275
t.Fatal("volle log maskeert het geheim niet")276
}277
if n := len(strings.Split(fullText, "\n")); n <= webhook.LogMaxLines {278
t.Fatalf("volle log heeft %d regels", n)279
}280
bad := getBearer(t, ts.URL+logPath, "bgj_geen-job")281
if bad.StatusCode != http.StatusUnauthorized {282
t.Fatalf("job-token %d", bad.StatusCode)283
}284
bad.Body.Close()243
285
244
rel := srv.releaseEvent(repo, "v1.2.0", "BlueGit 1.2.0", sha, []store.ReleaseAsset{{Name: "blue git", Size: 9}})286
rel := srv.releaseEvent(repo, "v1.2.0", "BlueGit 1.2.0", sha, []store.ReleaseAsset{{Name: "blue git", Size: 9}})245
raw, err := webhook.RenderBody("", rel, nil)287
raw, err := webhook.RenderBody("", rel, nil)@@ -261,7 +303,7 @@ func TestWebhookPayloads(t *testing.T) {261
}303
}262
304
263
infoPage := read(t, mustGet(t, tim, ts.URL+"/-/info"))305
infoPage := read(t, mustGet(t, tim, ts.URL+"/-/info"))264
for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "commit_message", "source_branch"} {306
for _, want := range []string{"Webhook payloads", "log_truncated", "log_url", "/api/v1/webhooks/openapi.json", "/api/v1/repos/{owner}/{repo}/jobs/{id}/log", "commit_message", "source_branch"} {265
if !strings.Contains(infoPage, want) {307
if !strings.Contains(infoPage, want) {266
t.Errorf("infopagina mist %q", want)308
t.Errorf("infopagina mist %q", want)267
}309
}@@ -271,7 +313,7 @@ func TestWebhookPayloads(t *testing.T) {271
t.Fatal(err)313
t.Fatal(err)272
}314
}273
spec := read(t, specRes)315
spec := read(t, specRes)274
if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") {316
if specRes.StatusCode != 200 || !strings.Contains(spec, `"openapi": "3.1.0"`) || !strings.Contains(spec, "log_truncated") || !strings.Contains(spec, "/repos/{owner}/{repo}/jobs/{id}/log") {275
t.Fatalf("spec %d %s", specRes.StatusCode, spec[:min(200, len(spec))])317
t.Fatalf("spec %d %s", specRes.StatusCode, spec[:min(200, len(spec))])276
}318
}277
}319
}@@ -307,6 +349,140 @@ func decodeAction(t *testing.T, bodies [][]byte, action string) map[string]any {307
return nil349
return nil308
}350
}309
351
352
func TestJobLogAPI(t *testing.T) {353
storage := t.TempDir()354
st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())355
if err != nil {356
t.Fatal(err)357
}358
t.Cleanup(func() { _ = st.Close() })359
ctx := context.Background()360
tim, err := st.UpsertUser(ctx, 1, "tim", "", false)361
if err != nil {362
t.Fatal(err)363
}364
ada, err := st.UpsertUser(ctx, 2, "ada", "", false)365
if err != nil {366
t.Fatal(err)367
}368
priv, err := st.CreateRepo(ctx, "user", tim.ID, "priv", "", "master", "private")369
if err != nil {370
t.Fatal(err)371
}372
open, err := st.CreateRepo(ctx, "user", tim.ID, "open", "", "master", "public")373
if err != nil {374
t.Fatal(err)375
}376
if err := st.PutSecret(ctx, "repo", priv.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil {377
t.Fatal(err)378
}379
if err := st.PutSecret(ctx, "repo", open.ID, "DEPLOY_TOKEN", "super-secret-token", true); err != nil {380
t.Fatal(err)381
}382
privJob := seedJobLog(t, st, priv.ID, "regel\nsuper-secret-token\n")383
openJob := seedJobLog(t, st, open.ID, "publiek super-secret-token\n")384
ownerTok, err := st.CreateToken(ctx, tim.ID, "laptop")385
if err != nil {386
t.Fatal(err)387
}388
adaTok, err := st.CreateToken(ctx, ada.ID, "ada")389
if err != nil {390
t.Fatal(err)391
}392
cfg := config.Config{393
BaseURL: "http://git.example", Listen: "127.0.0.1:0", StoragePath: storage,394
Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"},395
Git: config.Git{Binary: "git", DefaultBranch: "master"},396
}397
srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git"}398
ts := httptest.NewServer(srv.Handler())399
t.Cleanup(ts.Close)400
401
privPath := ts.URL + "/api/v1/repos/tim/priv/jobs/" + strconv.FormatInt(privJob, 10) + "/log"402
openPath := ts.URL + "/api/v1/repos/tim/open/jobs/" + strconv.FormatInt(openJob, 10) + "/log"403
404
missing := mustGet(t, http.DefaultClient, privPath)405
if missing.StatusCode != http.StatusUnauthorized {406
t.Fatalf("zonder token %d", missing.StatusCode)407
}408
missing.Body.Close()409
for _, tok := range []string{"nope", "bg_onbekend", "bgj_job"} {410
res := getBearer(t, privPath, tok)411
if res.StatusCode != http.StatusUnauthorized {412
t.Fatalf("token %s geeft %d", tok, res.StatusCode)413
}414
res.Body.Close()415
}416
stranger := getBearer(t, privPath, adaTok)417
if stranger.StatusCode != http.StatusForbidden {418
t.Fatalf("vreemde op prive %d", stranger.StatusCode)419
}420
stranger.Body.Close()421
other := getBearer(t, ts.URL+"/api/v1/repos/tim/priv/jobs/"+strconv.FormatInt(openJob, 10)+"/log", ownerTok)422
if other.StatusCode != http.StatusNotFound {423
t.Fatalf("job van andere repo %d", other.StatusCode)424
}425
other.Body.Close()426
427
own := getBearer(t, privPath, ownerTok)428
if own.StatusCode != http.StatusOK || !strings.HasPrefix(own.Header.Get("Content-Type"), "text/plain") {429
t.Fatalf("eigenaar %d %s", own.StatusCode, own.Header.Get("Content-Type"))430
}431
body := read(t, own)432
if body != "regel\n[MASKED]\n" {433
t.Fatalf("prive log %q", body)434
}435
pub := getBearer(t, openPath, adaTok)436
if pub.StatusCode != http.StatusOK {437
t.Fatalf("publiek met vreemd token %d", pub.StatusCode)438
}439
if got := read(t, pub); got != "publiek [MASKED]\n" {440
t.Fatalf("publieke log %q", got)441
}442
}443
444
func seedJobLog(t *testing.T, st *store.Store, repoID int64, log string) int64 {445
t.Helper()446
ctx := context.Background()447
id, err := st.CreatePipeline(ctx, repoID, strings.Repeat("a", 40), "refs/heads/master", "push", "tim", "titel", []store.NewJob{448
{Name: "build", Stage: "build", Script: "echo ok"},449
})450
if err != nil {451
t.Fatal(err)452
}453
jobs, err := st.JobsByPipeline(ctx, id)454
if err != nil || len(jobs) != 1 {455
t.Fatal(err)456
}457
if err := st.AppendJobLog(ctx, jobs[0].ID, log); err != nil {458
t.Fatal(err)459
}460
return jobs[0].ID461
}462
463
func getBearer(t *testing.T, raw, token string) *http.Response {464
t.Helper()465
req, err := http.NewRequest(http.MethodGet, raw, nil)466
if err != nil {467
t.Fatal(err)468
}469
req.Header.Set("Authorization", "Bearer "+token)470
res, err := http.DefaultClient.Do(req)471
if err != nil {472
t.Fatal(err)473
}474
return res475
}476
477
func mustParse(t *testing.T, raw string) *url.URL {478
t.Helper()479
u, err := url.Parse(raw)480
if err != nil {481
t.Fatal(err)482
}483
return u484
}485
310
func mustJSON(t *testing.T, v any) string {486
func mustJSON(t *testing.T, v any) string {311
t.Helper()487
t.Helper()312
b, err := json.Marshal(v)488
b, err := json.Marshal(v)@@ -12,7 +12,6 @@ import (
12
12
"gitlab.com/Timfa/bluegit/internal/i18n"
13
13
"gitlab.com/Timfa/bluegit/internal/names"
14
14
"gitlab.com/Timfa/bluegit/internal/store"
15
"gitlab.com/Timfa/bluegit/internal/webhook"
16
15
)
17
16
18
17
func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) {
@@ -12,7 +12,6 @@ import (12
"gitlab.com/Timfa/bluegit/internal/i18n"12
"gitlab.com/Timfa/bluegit/internal/i18n"13
"gitlab.com/Timfa/bluegit/internal/names"13
"gitlab.com/Timfa/bluegit/internal/names"14
"gitlab.com/Timfa/bluegit/internal/store"14
"gitlab.com/Timfa/bluegit/internal/store"15
"gitlab.com/Timfa/bluegit/internal/webhook"16
)15
)17
16
18
func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) {17
func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) {@@ -4,7 +4,6 @@ import (
4
4
"context"
5
5
"net/http"
6
6
"os"
7
"strconv"
8
7
"strings"
9
8
10
9
"gitlab.com/Timfa/bluegit/internal/access"
@@ -4,7 +4,6 @@ import (4
"context"4
"context"5
"net/http"5
"net/http"6
"os"6
"os"7
"strconv"8
"strings"7
"strings"9
8
10
"gitlab.com/Timfa/bluegit/internal/access"9
"gitlab.com/Timfa/bluegit/internal/access"@@ -116,6 +116,7 @@ func (s *Server) Handler() http.Handler {
116
116
mux.HandleFunc("POST /{owner}/{repo}/git-upload-pack", s.gitHTTP)
117
117
mux.HandleFunc("POST /{owner}/{repo}/git-receive-pack", s.gitHTTP)
118
118
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/releases", s.createRelease)
119
mux.HandleFunc("GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log", s.jobLogAPI)
119
120
mux.HandleFunc("GET /api/v1/repos/{owner}/{repo}/webhook-secrets", s.repoWebhookSecrets)
120
121
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/webhooks", s.repoWebhookAPI)
121
122
mux.HandleFunc("PUT /api/v1/repos/{owner}/{repo}/webhooks/{id}", s.repoWebhookUpdate)
@@ -116,6 +116,7 @@ func (s *Server) Handler() http.Handler {116
mux.HandleFunc("POST /{owner}/{repo}/git-upload-pack", s.gitHTTP)116
mux.HandleFunc("POST /{owner}/{repo}/git-upload-pack", s.gitHTTP)117
mux.HandleFunc("POST /{owner}/{repo}/git-receive-pack", s.gitHTTP)117
mux.HandleFunc("POST /{owner}/{repo}/git-receive-pack", s.gitHTTP)118
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/releases", s.createRelease)118
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/releases", s.createRelease)119
mux.HandleFunc("GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log", s.jobLogAPI)119
mux.HandleFunc("GET /api/v1/repos/{owner}/{repo}/webhook-secrets", s.repoWebhookSecrets)120
mux.HandleFunc("GET /api/v1/repos/{owner}/{repo}/webhook-secrets", s.repoWebhookSecrets)120
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/webhooks", s.repoWebhookAPI)121
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/webhooks", s.repoWebhookAPI)121
mux.HandleFunc("PUT /api/v1/repos/{owner}/{repo}/webhooks/{id}", s.repoWebhookUpdate)122
mux.HandleFunc("PUT /api/v1/repos/{owner}/{repo}/webhooks/{id}", s.repoWebhookUpdate)@@ -858,6 +858,19 @@ func (s *Store) JobByToken(ctx context.Context, token string) (Job, error) {
858
858
return scanJob(rows)
859
859
}
860
860
861
// JobByID leest een job van deze repository.
862
func (s *Store) JobByID(ctx context.Context, repoID, id int64) (Job, error) {
863
rows, err := s.db.QueryContext(ctx, jobSelect+` WHERE j.repo_id = ? AND j.id = ?`, repoID, id)
864
if err != nil {
865
return Job{}, err
866
}
867
defer rows.Close()
868
if !rows.Next() {
869
return Job{}, errNotFound
870
}
871
return scanJob(rows)
872
}
873
861
874
// Artifact verwijst naar een bestand uit een job.
862
875
type Artifact struct {
863
876
ID int64
@@ -858,6 +858,19 @@ func (s *Store) JobByToken(ctx context.Context, token string) (Job, error) {858
return scanJob(rows)858
return scanJob(rows)859
}859
}860
860
861
// JobByID leest een job van deze repository.862
func (s *Store) JobByID(ctx context.Context, repoID, id int64) (Job, error) {863
rows, err := s.db.QueryContext(ctx, jobSelect+` WHERE j.repo_id = ? AND j.id = ?`, repoID, id)864
if err != nil {865
return Job{}, err866
}867
defer rows.Close()868
if !rows.Next() {869
return Job{}, errNotFound870
}871
return scanJob(rows)872
}873
861
// Artifact verwijst naar een bestand uit een job.874
// Artifact verwijst naar een bestand uit een job.862
type Artifact struct {875
type Artifact struct {863
ID int64876
ID int64@@ -42,12 +42,36 @@ func OpenAPI() []byte {
42
42
"version": "1",
43
43
"description": "JSON body of a BlueGit webhook when the body template is empty. " +
44
44
"pipeline_success and pipeline_failure include the pipeline and every job. " +
45
"A failed job adds the last 200 log lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +
46
"log_truncated is true when the log was cut. log_url is the full log. " +
45
"Every job includes its console output: the last 200 lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +
46
"log_truncated is true when the log was cut. log_url is then GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log and needs Authorization Bearer with a personal token that can read the repo. " +
47
47
"A push lists ref, before, after and up to 20 commits. " +
48
48
"A merge request uses action opened, merged or commented, and commented includes the comment text. " +
49
49
"A release lists the tag, name, commit and asset URLs.",
50
50
},
51
"servers": []any{map[string]any{"url": "/api/v1"}},
52
"paths": map[string]any{
53
"/repos/{owner}/{repo}/jobs/{id}/log": map[string]any{
54
"get": map[string]any{
55
"summary": "Full job log",
56
"description": "Returns the full console output as text/plain. Secret values of at least 8 characters are replaced with [MASKED]. Requires a personal access token. A token that can read the repo is enough, and on a public repo any valid personal token is enough. Job tokens are rejected.",
57
"security": []any{map[string]any{"bearer": []any{}}},
58
"parameters": []any{
59
map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
60
map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},
61
map[string]any{"name": "id", "in": "path", "required": true, "schema": map[string]any{"type": "integer"}},
62
},
63
"responses": map[string]any{
64
"200": map[string]any{
65
"description": "Full log.",
66
"content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},
67
},
68
"401": map[string]any{"description": "Missing or invalid token."},
69
"403": map[string]any{"description": "The token cannot read this private repo."},
70
"404": map[string]any{"description": "Unknown repo or job."},
71
},
72
},
73
},
74
},
51
75
"webhooks": map[string]any{
52
76
"pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"),
53
77
"push": post("Push", "#/components/schemas/PushEvent"),
@@ -55,6 +79,9 @@ func OpenAPI() []byte {
55
79
"release": post("Release published", "#/components/schemas/ReleaseEvent"),
56
80
},
57
81
"components": map[string]any{
82
"securitySchemes": map[string]any{
83
"bearer": map[string]any{"type": "http", "scheme": "bearer", "description": "Personal access token (bg_)."},
84
},
58
85
"schemas": map[string]any{
59
86
"PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{
60
87
"object_kind": str("Always pipeline."),
@@ -77,17 +104,17 @@ func OpenAPI() []byte {
77
104
"duration": num("Seconds from start to finish."),
78
105
"url": str("Pipeline page."),
79
106
}, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),
80
"Job": obj("One job. log, log_truncated and log_url are present only when status is failed.", map[string]any{
107
"Job": obj("One job. log is always the console tail. log_url is present only when log_truncated is true.", map[string]any{
81
108
"name": str("Job name."),
82
109
"stage": str("Stage name."),
83
110
"status": str("Job status."),
84
111
"duration": num("Seconds the job ran."),
85
112
"allow_failure": boolean("True when a failure does not fail the pipeline."),
86
113
"web_url": str("Job page."),
87
"log": str("Tail of the console output."),
88
"log_truncated": boolean("True when log is not the full console output."),
89
"log_url": str("Plain-text URL of the full log."),
90
}, []string{"name", "stage", "status", "duration", "allow_failure", "web_url"}),
114
"log": str("Last 200 lines of the console output, at most 64 KiB. Secrets of at least 8 characters are [MASKED]."),
115
"log_truncated": boolean("True when log is shorter than the stored console output."),
116
"log_url": str("GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Present only when log_truncated is true. Requires Authorization Bearer and a personal token that can read the repo. The response is the full log as text/plain, with secrets masked."),
117
}, []string{"name", "stage", "status", "duration", "allow_failure", "web_url", "log", "log_truncated"}),
91
118
"PushEvent": obj("A branch or tag update.", map[string]any{
92
119
"object_kind": str("Always push."),
93
120
"type": str("Always push."),
@@ -42,12 +42,36 @@ func OpenAPI() []byte {42
"version": "1",42
"version": "1",43
"description": "JSON body of a BlueGit webhook when the body template is empty. " +43
"description": "JSON body of a BlueGit webhook when the body template is empty. " +44
"pipeline_success and pipeline_failure include the pipeline and every job. " +44
"pipeline_success and pipeline_failure include the pipeline and every job. " +45
"A failed job adds the last 200 log lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +45
"Every job includes its console output: the last 200 lines, at most 64 KiB. Secret values of at least 8 characters are replaced with [MASKED]. " +46
"log_truncated is true when the log was cut. log_url is the full log. " +46
"log_truncated is true when the log was cut. log_url is then GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log and needs Authorization Bearer with a personal token that can read the repo. " +47
"A push lists ref, before, after and up to 20 commits. " +47
"A push lists ref, before, after and up to 20 commits. " +48
"A merge request uses action opened, merged or commented, and commented includes the comment text. " +48
"A merge request uses action opened, merged or commented, and commented includes the comment text. " +49
"A release lists the tag, name, commit and asset URLs.",49
"A release lists the tag, name, commit and asset URLs.",50
},50
},51
"servers": []any{map[string]any{"url": "/api/v1"}},52
"paths": map[string]any{53
"/repos/{owner}/{repo}/jobs/{id}/log": map[string]any{54
"get": map[string]any{55
"summary": "Full job log",56
"description": "Returns the full console output as text/plain. Secret values of at least 8 characters are replaced with [MASKED]. Requires a personal access token. A token that can read the repo is enough, and on a public repo any valid personal token is enough. Job tokens are rejected.",57
"security": []any{map[string]any{"bearer": []any{}}},58
"parameters": []any{59
map[string]any{"name": "owner", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},60
map[string]any{"name": "repo", "in": "path", "required": true, "schema": map[string]any{"type": "string"}},61
map[string]any{"name": "id", "in": "path", "required": true, "schema": map[string]any{"type": "integer"}},62
},63
"responses": map[string]any{64
"200": map[string]any{65
"description": "Full log.",66
"content": map[string]any{"text/plain": map[string]any{"schema": map[string]any{"type": "string"}}},67
},68
"401": map[string]any{"description": "Missing or invalid token."},69
"403": map[string]any{"description": "The token cannot read this private repo."},70
"404": map[string]any{"description": "Unknown repo or job."},71
},72
},73
},74
},51
"webhooks": map[string]any{75
"webhooks": map[string]any{52
"pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"),76
"pipeline": post("Pipeline finished", "#/components/schemas/PipelineEvent"),53
"push": post("Push", "#/components/schemas/PushEvent"),77
"push": post("Push", "#/components/schemas/PushEvent"),@@ -55,6 +79,9 @@ func OpenAPI() []byte {55
"release": post("Release published", "#/components/schemas/ReleaseEvent"),79
"release": post("Release published", "#/components/schemas/ReleaseEvent"),56
},80
},57
"components": map[string]any{81
"components": map[string]any{82
"securitySchemes": map[string]any{83
"bearer": map[string]any{"type": "http", "scheme": "bearer", "description": "Personal access token (bg_)."},84
},58
"schemas": map[string]any{85
"schemas": map[string]any{59
"PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{86
"PipelineEvent": obj("pipeline_success or pipeline_failure.", map[string]any{60
"object_kind": str("Always pipeline."),87
"object_kind": str("Always pipeline."),@@ -77,17 +104,17 @@ func OpenAPI() []byte {77
"duration": num("Seconds from start to finish."),104
"duration": num("Seconds from start to finish."),78
"url": str("Pipeline page."),105
"url": str("Pipeline page."),79
}, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),106
}, []string{"id", "status", "ref", "sha", "commit_message", "author", "duration"}),80
"Job": obj("One job. log, log_truncated and log_url are present only when status is failed.", map[string]any{107
"Job": obj("One job. log is always the console tail. log_url is present only when log_truncated is true.", map[string]any{81
"name": str("Job name."),108
"name": str("Job name."),82
"stage": str("Stage name."),109
"stage": str("Stage name."),83
"status": str("Job status."),110
"status": str("Job status."),84
"duration": num("Seconds the job ran."),111
"duration": num("Seconds the job ran."),85
"allow_failure": boolean("True when a failure does not fail the pipeline."),112
"allow_failure": boolean("True when a failure does not fail the pipeline."),86
"web_url": str("Job page."),113
"web_url": str("Job page."),87
"log": str("Tail of the console output."),114
"log": str("Last 200 lines of the console output, at most 64 KiB. Secrets of at least 8 characters are [MASKED]."),88
"log_truncated": boolean("True when log is not the full console output."),115
"log_truncated": boolean("True when log is shorter than the stored console output."),89
"log_url": str("Plain-text URL of the full log."),116
"log_url": str("GET /api/v1/repos/{owner}/{repo}/jobs/{id}/log. Present only when log_truncated is true. Requires Authorization Bearer and a personal token that can read the repo. The response is the full log as text/plain, with secrets masked."),90
}, []string{"name", "stage", "status", "duration", "allow_failure", "web_url"}),117
}, []string{"name", "stage", "status", "duration", "allow_failure", "web_url", "log", "log_truncated"}),91
"PushEvent": obj("A branch or tag update.", map[string]any{118
"PushEvent": obj("A branch or tag update.", map[string]any{92
"object_kind": str("Always push."),119
"object_kind": str("Always push."),93
"type": str("Always push."),120
"type": str("Always push."),@@ -58,7 +58,8 @@ type PipelineInfo struct {
58
58
}
59
59
60
60
// JobInfo is één job in een pipeline-event.
61
// Log, log_truncated en log_url staan er alleen bij een gefaalde job.
61
// Elke job stuurt de console-output mee. log_url staat er alleen als de log
62
// is afgekapt, en wijst naar de API voor de volledige tekst.
62
63
type JobInfo struct {
63
64
Name string `json:"name"`
64
65
Stage string `json:"stage"`
@@ -66,8 +67,8 @@ type JobInfo struct {
66
67
Duration float64 `json:"duration"`
67
68
AllowFailure bool `json:"allow_failure"`
68
69
WebURL string `json:"web_url"`
69
Log string `json:"log,omitempty"`
70
LogTruncated *bool `json:"log_truncated,omitempty"`
70
Log string `json:"log"`
71
LogTruncated bool `json:"log_truncated"`
71
72
LogURL string `json:"log_url,omitempty"`
72
73
}
73
74
@@ -202,7 +203,6 @@ func cutUTF8Tail(s string, max int) string {
202
203
203
204
// Example is het voorbeeld op de infopagina.
204
205
func Example() string {
205
trunc := true
206
206
samples := []Event{
207
207
{
208
208
Type: EventPipelineFailure, Project: "tim/app", Actor: "tim",
@@ -218,12 +218,13 @@ func Example() string {
218
218
{
219
219
Name: "test", Stage: "test", Status: "failed", Duration: 4.1,
220
220
AllowFailure: false, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/7",
221
Log: "go: token [MASKED]\nFAIL", LogTruncated: &trunc,
222
LogURL: "https://git.example/tim/app/-/pipelines/26/jobs/7/log?raw=1",
221
Log: "go: token [MASKED]\nFAIL", LogTruncated: true,
222
LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log",
223
223
},
224
224
{
225
Name: "build", Stage: "build", Status: "skipped", Duration: 0,
225
Name: "build", Stage: "build", Status: "success", Duration: 8,
226
226
WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8",
227
Log: "built bluegit",
227
228
},
228
229
},
229
230
},
@@ -58,7 +58,8 @@ type PipelineInfo struct {58
}58
}59
59
60
// JobInfo is één job in een pipeline-event.60
// JobInfo is één job in een pipeline-event.61
// Log, log_truncated en log_url staan er alleen bij een gefaalde job.61
// Elke job stuurt de console-output mee. log_url staat er alleen als de log62
// is afgekapt, en wijst naar de API voor de volledige tekst.62
type JobInfo struct {63
type JobInfo struct {63
Name string `json:"name"`64
Name string `json:"name"`64
Stage string `json:"stage"`65
Stage string `json:"stage"`@@ -66,8 +67,8 @@ type JobInfo struct {66
Duration float64 `json:"duration"`67
Duration float64 `json:"duration"`67
AllowFailure bool `json:"allow_failure"`68
AllowFailure bool `json:"allow_failure"`68
WebURL string `json:"web_url"`69
WebURL string `json:"web_url"`69
Log string `json:"log,omitempty"`70
Log string `json:"log"`70
LogTruncated *bool `json:"log_truncated,omitempty"`71
LogTruncated bool `json:"log_truncated"`71
LogURL string `json:"log_url,omitempty"`72
LogURL string `json:"log_url,omitempty"`72
}73
}73
74
@@ -202,7 +203,6 @@ func cutUTF8Tail(s string, max int) string {202
203
203
// Example is het voorbeeld op de infopagina.204
// Example is het voorbeeld op de infopagina.204
func Example() string {205
func Example() string {205
trunc := true206
samples := []Event{206
samples := []Event{207
{207
{208
Type: EventPipelineFailure, Project: "tim/app", Actor: "tim",208
Type: EventPipelineFailure, Project: "tim/app", Actor: "tim",@@ -218,12 +218,13 @@ func Example() string {218
{218
{219
Name: "test", Stage: "test", Status: "failed", Duration: 4.1,219
Name: "test", Stage: "test", Status: "failed", Duration: 4.1,220
AllowFailure: false, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/7",220
AllowFailure: false, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/7",221
Log: "go: token [MASKED]\nFAIL", LogTruncated: &trunc,221
Log: "go: token [MASKED]\nFAIL", LogTruncated: true,222
LogURL: "https://git.example/tim/app/-/pipelines/26/jobs/7/log?raw=1",222
LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log",223
},223
},224
{224
{225
Name: "build", Stage: "build", Status: "skipped", Duration: 0,225
Name: "build", Stage: "build", Status: "success", Duration: 8,226
WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8",226
WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8",227
Log: "built bluegit",227
},228
},228
},229
},229
},230
},@@ -38,13 +38,12 @@ func TestTailLogMasksAndCuts(t *testing.T) {
38
38
}
39
39
40
40
func TestDocumentShapes(t *testing.T) {
41
trunc := false
42
41
pipe := Event{
43
42
Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9",
44
43
Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5},
45
44
Jobs: []JobInfo{
46
{Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: &trunc, LogURL: "https://git.example/log"},
47
{Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build"},
45
{Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: true, LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log"},
46
{Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build", Log: "ok"},
48
47
},
49
48
}
50
49
body, err := RenderBody("", pipe, nil)
@@ -60,15 +59,15 @@ func TestDocumentShapes(t *testing.T) {
60
59
}
61
60
jobs := doc["jobs"].([]any)
62
61
failed := jobs[0].(map[string]any)
63
if failed["log"] != "boom" || failed["log_truncated"] != false || failed["allow_failure"] != false || failed["log_url"] == "" {
62
if failed["log"] != "boom" || failed["log_truncated"] != true || failed["allow_failure"] != false || failed["log_url"] == "" {
64
63
t.Fatalf("gefaalde job %v", failed)
65
64
}
66
65
ok := jobs[1].(map[string]any)
67
if _, has := ok["log"]; has {
68
t.Fatalf("geslaagde job heeft een log: %v", ok)
66
if ok["log"] != "ok" || ok["log_truncated"] != false {
67
t.Fatalf("geslaagde job %v", ok)
69
68
}
70
if _, has := ok["log_truncated"]; has {
71
t.Fatalf("geslaagde job heeft log_truncated: %v", ok)
69
if _, has := ok["log_url"]; has {
70
t.Fatalf("korte log heeft een url: %v", ok)
72
71
}
73
72
74
73
push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b"}
@@ -139,6 +138,7 @@ func TestOpenAPIMatchesExample(t *testing.T) {
139
138
for _, want := range []string{
140
139
"PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent",
141
140
"commit_message", "allow_failure", "log_truncated", "log_url",
141
"/repos/{owner}/{repo}/jobs/{id}/log",
142
142
"source_branch", "target_branch", "before", "after", "assets",
143
143
} {
144
144
if !strings.Contains(raw, want) {
@@ -38,13 +38,12 @@ func TestTailLogMasksAndCuts(t *testing.T) {38
}38
}39
39
40
func TestDocumentShapes(t *testing.T) {40
func TestDocumentShapes(t *testing.T) {41
trunc := false42
pipe := Event{41
pipe := Event{43
Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9",42
Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9",44
Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5},43
Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5},45
Jobs: []JobInfo{44
Jobs: []JobInfo{46
{Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: &trunc, LogURL: "https://git.example/log"},45
{Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: true, LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log"},47
{Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build"},46
{Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build", Log: "ok"},48
},47
},49
}48
}50
body, err := RenderBody("", pipe, nil)49
body, err := RenderBody("", pipe, nil)@@ -60,15 +59,15 @@ func TestDocumentShapes(t *testing.T) {60
}59
}61
jobs := doc["jobs"].([]any)60
jobs := doc["jobs"].([]any)62
failed := jobs[0].(map[string]any)61
failed := jobs[0].(map[string]any)63
if failed["log"] != "boom" || failed["log_truncated"] != false || failed["allow_failure"] != false || failed["log_url"] == "" {62
if failed["log"] != "boom" || failed["log_truncated"] != true || failed["allow_failure"] != false || failed["log_url"] == "" {64
t.Fatalf("gefaalde job %v", failed)63
t.Fatalf("gefaalde job %v", failed)65
}64
}66
ok := jobs[1].(map[string]any)65
ok := jobs[1].(map[string]any)67
if _, has := ok["log"]; has {66
if ok["log"] != "ok" || ok["log_truncated"] != false {68
t.Fatalf("geslaagde job heeft een log: %v", ok)67
t.Fatalf("geslaagde job %v", ok)69
}68
}70
if _, has := ok["log_truncated"]; has {69
if _, has := ok["log_url"]; has {71
t.Fatalf("geslaagde job heeft log_truncated: %v", ok)70
t.Fatalf("korte log heeft een url: %v", ok)72
}71
}73
72
74
push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b"}73
push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b"}@@ -139,6 +138,7 @@ func TestOpenAPIMatchesExample(t *testing.T) {139
for _, want := range []string{138
for _, want := range []string{140
"PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent",139
"PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent",141
"commit_message", "allow_failure", "log_truncated", "log_url",140
"commit_message", "allow_failure", "log_truncated", "log_url",141
"/repos/{owner}/{repo}/jobs/{id}/log",142
"source_branch", "target_branch", "before", "after", "assets",142
"source_branch", "target_branch", "before", "after", "assets",143
} {143
} {144
if !strings.Contains(raw, want) {144
if !strings.Contains(raw, want) {