BlueGit

timfa bluegit

Files

bluegit / internal / webhook / payload.go

payload.go

fix: stuur de console-output van elke job mee in pipeline-webhooks
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
6080426e History
payload.go 7.6 KiB Download Raw
1package webhook
2
3import (
4 "encoding/json"
5 "strings"
6
7 "gitlab.com/Timfa/bluegit/internal/ci"
8)
9
10// Grenzen voor wat een webhook van een gefaalde job meestuurt.
11const (
12 LogMaxLines = 200
13 LogMaxBytes = 64 << 10
14)
15
16// MaxPushCommits is het aantal commits in een push-payload.
17// Dat zijn de nieuwste in het bereik, oudste eerst.
18const MaxPushCommits = 20
19
20// Document is de JSON-body van een webhook zonder eigen sjabloon.
21type Document struct {
22 ObjectKind string `json:"object_kind"`
23 Type string `json:"type"`
24 Project string `json:"project"`
25 Group string `json:"group,omitempty"`
26 Actor string `json:"actor,omitempty"`
27 Summary string `json:"summary,omitempty"`
28 URL string `json:"url,omitempty"`
29 Branch string `json:"branch,omitempty"`
30 Commit string `json:"commit,omitempty"`
31 PipelineID string `json:"pipeline_id,omitempty"`
32 ReleaseTag string `json:"release_tag,omitempty"`
33
34 Pipeline *PipelineInfo `json:"pipeline,omitempty"`
35 Jobs *[]JobInfo `json:"jobs,omitempty"`
36
37 Ref string `json:"ref,omitempty"`
38 Before string `json:"before,omitempty"`
39 After string `json:"after,omitempty"`
40 Commits *[]CommitInfo `json:"commits,omitempty"`
41
42 MergeRequest *MRInfo `json:"merge_request,omitempty"`
43 Comment string `json:"comment,omitempty"`
44
45 Release *ReleaseInfo `json:"release,omitempty"`
46}
47
48// PipelineInfo is de pipeline waar het event over gaat.
49type PipelineInfo struct {
50 ID int64 `json:"id"`
51 Status string `json:"status"`
52 Ref string `json:"ref"`
53 SHA string `json:"sha"`
54 CommitMessage string `json:"commit_message"`
55 Author string `json:"author"`
56 Duration float64 `json:"duration"`
57 URL string `json:"url,omitempty"`
58}
59
60// JobInfo is één job in een pipeline-event.
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.
63type JobInfo struct {
64 Name string `json:"name"`
65 Stage string `json:"stage"`
66 Status string `json:"status"`
67 Duration float64 `json:"duration"`
68 AllowFailure bool `json:"allow_failure"`
69 WebURL string `json:"web_url"`
70 Log string `json:"log"`
71 LogTruncated bool `json:"log_truncated"`
72 LogURL string `json:"log_url,omitempty"`
73}
74
75// CommitInfo is één commit in een push.
76type CommitInfo struct {
77 SHA string `json:"sha"`
78 Message string `json:"message"`
79 Author string `json:"author"`
80 URL string `json:"url"`
81}
82
83// MRInfo is de merge request van het event.
84type MRInfo struct {
85 ID int64 `json:"id"`
86 Title string `json:"title"`
87 Description string `json:"description"`
88 SourceBranch string `json:"source_branch"`
89 TargetBranch string `json:"target_branch"`
90 Status string `json:"status"`
91 Author string `json:"author"`
92 Action string `json:"action"`
93 URL string `json:"url,omitempty"`
94}
95
96// ReleaseInfo is een gepubliceerde release.
97type ReleaseInfo struct {
98 Tag string `json:"tag"`
99 Name string `json:"name"`
100 Commit string `json:"commit"`
101 Assets []AssetInfo `json:"assets"`
102}
103
104// AssetInfo is een bestand bij een release.
105type AssetInfo struct {
106 Name string `json:"name"`
107 URL string `json:"url"`
108 Size int64 `json:"size"`
109}
110
111// Document bouwt de JSON-structuur. Lege deelobjecten blijven weg.
112func (ev Event) Document() Document {
113 d := Document{
114 ObjectKind: objectKind(ev.Type),
115 Type: ev.Type,
116 Project: ev.Project,
117 Group: ev.Group,
118 Actor: ev.Actor,
119 Summary: ev.Summary,
120 URL: ev.URL,
121 Branch: ev.Branch,
122 Commit: ev.Commit,
123 PipelineID: ev.PipelineID,
124 ReleaseTag: ev.ReleaseTag,
125 Pipeline: ev.Pipeline,
126 Ref: ev.Ref,
127 Before: ev.Before,
128 After: ev.After,
129 MergeRequest: ev.MergeRequest,
130 Release: ev.Release,
131 }
132 switch ev.Type {
133 case EventPush:
134 commits := ev.Commits
135 if commits == nil {
136 commits = []CommitInfo{}
137 }
138 d.Commits = &commits
139 case EventPipelineSuccess, EventPipelineFailure:
140 jobs := ev.Jobs
141 if jobs == nil {
142 jobs = []JobInfo{}
143 }
144 d.Jobs = &jobs
145 case EventMergeRequest:
146 if ev.MergeRequest != nil && ev.MergeRequest.Action == "commented" {
147 d.Comment = ev.Comment
148 }
149 }
150 if d.Release != nil && d.Release.Assets == nil {
151 rel := *d.Release
152 rel.Assets = []AssetInfo{}
153 d.Release = &rel
154 }
155 return d
156}
157
158func objectKind(eventType string) string {
159 switch eventType {
160 case EventPipelineSuccess, EventPipelineFailure:
161 return "pipeline"
162 case EventRelease:
163 return "release"
164 case EventMergeRequest:
165 return "merge_request"
166 default:
167 return eventType
168 }
169}
170
171// TailLog houdt de laatste LogMaxLines regels en hoogstens LogMaxBytes.
172// Geheimwaarden van minstens 8 tekens worden [MASKED], net als in de joblog.
173// truncated is true zodra er regels of bytes af zijn.
174func TailLog(text string, secrets []string) (string, bool) {
175 text = ci.Mask(text, secrets)
176 if text == "" {
177 return "", false
178 }
179 truncated := false
180 lines := strings.Split(text, "\n")
181 if len(lines) > LogMaxLines {
182 lines = lines[len(lines)-LogMaxLines:]
183 truncated = true
184 }
185 out := strings.Join(lines, "\n")
186 if len(out) > LogMaxBytes {
187 out = cutUTF8Tail(out, LogMaxBytes)
188 truncated = true
189 }
190 return out, truncated
191}
192
193func cutUTF8Tail(s string, max int) string {
194 if len(s) <= max {
195 return s
196 }
197 s = s[len(s)-max:]
198 for len(s) > 0 && s[0]&0xC0 == 0x80 {
199 s = s[1:]
200 }
201 return s
202}
203
204// Example is het voorbeeld op de infopagina.
205func Example() string {
206 samples := []Event{
207 {
208 Type: EventPipelineFailure, Project: "tim/app", Actor: "tim",
209 Summary: "pipeline #26 voor tim/app is mislukt",
210 URL: "https://git.example/tim/app/-/pipelines/26",
211 Branch: "master", Commit: "725e6947", PipelineID: "26",
212 Pipeline: &PipelineInfo{
213 ID: 26, Status: "failed", Ref: "refs/heads/master", SHA: "725e6947",
214 CommitMessage: "fix de build", Author: "tim", Duration: 12.4,
215 URL: "https://git.example/tim/app/-/pipelines/26",
216 },
217 Jobs: []JobInfo{
218 {
219 Name: "test", Stage: "test", Status: "failed", Duration: 4.1,
220 AllowFailure: false, WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/7",
221 Log: "go: token [MASKED]\nFAIL", LogTruncated: true,
222 LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log",
223 },
224 {
225 Name: "build", Stage: "build", Status: "success", Duration: 8,
226 WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8",
227 Log: "built bluegit",
228 },
229 },
230 },
231 {
232 Type: EventPush, Project: "tim/app", Actor: "tim", Branch: "master",
233 Ref: "refs/heads/master", Before: strings.Repeat("0", 40), After: "abc123",
234 Commits: []CommitInfo{{
235 SHA: "abc123", Message: "fix de build", Author: "Tim <tim@example.com>",
236 URL: "https://git.example/tim/app/-/commit/abc123",
237 }},
238 },
239 {
240 Type: EventMergeRequest, Project: "tim/app", Actor: "ada",
241 MergeRequest: &MRInfo{
242 ID: 4, Title: "Toon de kloon-URL", Description: "SSH en HTTPS.",
243 SourceBranch: "feature", TargetBranch: "master", Status: "open",
244 Author: "ada", Action: "opened",
245 URL: "https://git.example/tim/app/-/merge-requests/4",
246 },
247 },
248 {
249 Type: EventRelease, Project: "tim/app", ReleaseTag: "v1.2.0", Commit: "abc123",
250 Release: &ReleaseInfo{
251 Tag: "v1.2.0", Name: "BlueGit 1.2.0", Commit: "abc123",
252 Assets: []AssetInfo{{
253 Name: "bluegit", Size: 8123456,
254 URL: "https://git.example/tim/app/-/releases/v1.2.0/assets/bluegit",
255 }},
256 },
257 },
258 }
259 var b strings.Builder
260 for i, ev := range samples {
261 if i > 0 {
262 b.WriteString("\n\n")
263 }
264 raw, err := json.MarshalIndent(ev.Document(), "", " ")
265 if err != nil {
266 continue
267 }
268 b.Write(raw)
269 }
270 return b.String()
271}