BlueGit

timfa bluegit

Files

bluegit / internal / webhook / payload.go

payload.go

feat: stuur volledige details mee in webhook-payloads
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
3ca4d219 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// Log, log_truncated en log_url staan er alleen bij een gefaalde job.
62type JobInfo struct {
63 Name string `json:"name"`
64 Stage string `json:"stage"`
65 Status string `json:"status"`
66 Duration float64 `json:"duration"`
67 AllowFailure bool `json:"allow_failure"`
68 WebURL string `json:"web_url"`
69 Log string `json:"log,omitempty"`
70 LogTruncated *bool `json:"log_truncated,omitempty"`
71 LogURL string `json:"log_url,omitempty"`
72}
73
74// CommitInfo is één commit in een push.
75type CommitInfo struct {
76 SHA string `json:"sha"`
77 Message string `json:"message"`
78 Author string `json:"author"`
79 URL string `json:"url"`
80}
81
82// MRInfo is de merge request van het event.
83type MRInfo struct {
84 ID int64 `json:"id"`
85 Title string `json:"title"`
86 Description string `json:"description"`
87 SourceBranch string `json:"source_branch"`
88 TargetBranch string `json:"target_branch"`
89 Status string `json:"status"`
90 Author string `json:"author"`
91 Action string `json:"action"`
92 URL string `json:"url,omitempty"`
93}
94
95// ReleaseInfo is een gepubliceerde release.
96type ReleaseInfo struct {
97 Tag string `json:"tag"`
98 Name string `json:"name"`
99 Commit string `json:"commit"`
100 Assets []AssetInfo `json:"assets"`
101}
102
103// AssetInfo is een bestand bij een release.
104type AssetInfo struct {
105 Name string `json:"name"`
106 URL string `json:"url"`
107 Size int64 `json:"size"`
108}
109
110// Document bouwt de JSON-structuur. Lege deelobjecten blijven weg.
111func (ev Event) Document() Document {
112 d := Document{
113 ObjectKind: objectKind(ev.Type),
114 Type: ev.Type,
115 Project: ev.Project,
116 Group: ev.Group,
117 Actor: ev.Actor,
118 Summary: ev.Summary,
119 URL: ev.URL,
120 Branch: ev.Branch,
121 Commit: ev.Commit,
122 PipelineID: ev.PipelineID,
123 ReleaseTag: ev.ReleaseTag,
124 Pipeline: ev.Pipeline,
125 Ref: ev.Ref,
126 Before: ev.Before,
127 After: ev.After,
128 MergeRequest: ev.MergeRequest,
129 Release: ev.Release,
130 }
131 switch ev.Type {
132 case EventPush:
133 commits := ev.Commits
134 if commits == nil {
135 commits = []CommitInfo{}
136 }
137 d.Commits = &commits
138 case EventPipelineSuccess, EventPipelineFailure:
139 jobs := ev.Jobs
140 if jobs == nil {
141 jobs = []JobInfo{}
142 }
143 d.Jobs = &jobs
144 case EventMergeRequest:
145 if ev.MergeRequest != nil && ev.MergeRequest.Action == "commented" {
146 d.Comment = ev.Comment
147 }
148 }
149 if d.Release != nil && d.Release.Assets == nil {
150 rel := *d.Release
151 rel.Assets = []AssetInfo{}
152 d.Release = &rel
153 }
154 return d
155}
156
157func objectKind(eventType string) string {
158 switch eventType {
159 case EventPipelineSuccess, EventPipelineFailure:
160 return "pipeline"
161 case EventRelease:
162 return "release"
163 case EventMergeRequest:
164 return "merge_request"
165 default:
166 return eventType
167 }
168}
169
170// TailLog houdt de laatste LogMaxLines regels en hoogstens LogMaxBytes.
171// Geheimwaarden van minstens 8 tekens worden [MASKED], net als in de joblog.
172// truncated is true zodra er regels of bytes af zijn.
173func TailLog(text string, secrets []string) (string, bool) {
174 text = ci.Mask(text, secrets)
175 if text == "" {
176 return "", false
177 }
178 truncated := false
179 lines := strings.Split(text, "\n")
180 if len(lines) > LogMaxLines {
181 lines = lines[len(lines)-LogMaxLines:]
182 truncated = true
183 }
184 out := strings.Join(lines, "\n")
185 if len(out) > LogMaxBytes {
186 out = cutUTF8Tail(out, LogMaxBytes)
187 truncated = true
188 }
189 return out, truncated
190}
191
192func cutUTF8Tail(s string, max int) string {
193 if len(s) <= max {
194 return s
195 }
196 s = s[len(s)-max:]
197 for len(s) > 0 && s[0]&0xC0 == 0x80 {
198 s = s[1:]
199 }
200 return s
201}
202
203// Example is het voorbeeld op de infopagina.
204func Example() string {
205 trunc := true
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: &trunc,
222 LogURL: "https://git.example/tim/app/-/pipelines/26/jobs/7/log?raw=1",
223 },
224 {
225 Name: "build", Stage: "build", Status: "skipped", Duration: 0,
226 WebURL: "https://git.example/tim/app/-/pipelines/26/jobs/8",
227 },
228 },
229 },
230 {
231 Type: EventPush, Project: "tim/app", Actor: "tim", Branch: "master",
232 Ref: "refs/heads/master", Before: strings.Repeat("0", 40), After: "abc123",
233 Commits: []CommitInfo{{
234 SHA: "abc123", Message: "fix de build", Author: "Tim <tim@example.com>",
235 URL: "https://git.example/tim/app/-/commit/abc123",
236 }},
237 },
238 {
239 Type: EventMergeRequest, Project: "tim/app", Actor: "ada",
240 MergeRequest: &MRInfo{
241 ID: 4, Title: "Toon de kloon-URL", Description: "SSH en HTTPS.",
242 SourceBranch: "feature", TargetBranch: "master", Status: "open",
243 Author: "ada", Action: "opened",
244 URL: "https://git.example/tim/app/-/merge-requests/4",
245 },
246 },
247 {
248 Type: EventRelease, Project: "tim/app", ReleaseTag: "v1.2.0", Commit: "abc123",
249 Release: &ReleaseInfo{
250 Tag: "v1.2.0", Name: "BlueGit 1.2.0", Commit: "abc123",
251 Assets: []AssetInfo{{
252 Name: "bluegit", Size: 8123456,
253 URL: "https://git.example/tim/app/-/releases/v1.2.0/assets/bluegit",
254 }},
255 },
256 },
257 }
258 var b strings.Builder
259 for i, ev := range samples {
260 if i > 0 {
261 b.WriteString("\n\n")
262 }
263 raw, err := json.MarshalIndent(ev.Document(), "", " ")
264 if err != nil {
265 continue
266 }
267 b.Write(raw)
268 }
269 return b.String()
270}