-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
790 lines (715 loc) · 21 KB
/
Copy pathmain.go
File metadata and controls
790 lines (715 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"go.yaml.in/yaml/v3"
)
// ansiEscape matches ANSI escape sequences for Github logs
var ansiEscape = regexp.MustCompile(`\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])`)
type Config struct {
Settings struct {
SourceRepo string `yaml:"source_repo"`
MaxRunsPerWorkflow int `yaml:"max_runs_per_workflow"`
RecentRunsInOutput int `yaml:"recent_runs_in_output"`
} `yaml:"settings"`
Notify NotifyConfig `yaml:"notify"`
LogAnalysis LogAnalysisConfig `yaml:"log_analysis"`
Workflows []struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Critical bool `yaml:"critical"`
Required bool `yaml:"required"`
} `yaml:"workflows"`
}
type LogAnalysisConfig struct {
MaxSignalsPerJob int `yaml:"max_signals_per_job"`
NoisePatterns []string `yaml:"noise_patterns"`
Categories []FailureCategoryConfig `yaml:"categories"`
}
type FailureCategoryConfig struct {
Name string `yaml:"name"`
Priority int `yaml:"priority"`
Patterns []string `yaml:"patterns"`
}
type SignalLine struct {
Line string
Category string
Priority int
}
type LogSummary struct {
Signals []SignalLine
ByCategory map[string][]string
TopCategory string
Empty bool
}
type Workflow struct {
ID int `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
}
type WorkflowsListResponse struct {
Workflows []Workflow `json:"workflows"`
}
type WorkflowsResponse struct {
WorkflowRuns []Run `json:"workflow_runs"`
}
type Step struct {
Name string `json:"name"`
Conclusion string `json:"conclusion"`
Number int `json:"number"`
StartedAt time.Time `json:"started_at"`
CompletedAt time.Time `json:"completed_at"`
}
type FailedJob struct {
ID int `json:"id"`
Name string `json:"name"`
Conclusion string `json:"conclusion"`
HTMLURL string `json:"html_url"`
LogSnippet string `json:"log_snippet"`
RawLog string `json:"raw_log"`
}
type Run struct {
ID int `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Conclusion string `json:"conclusion"`
RunNumber int `json:"run_number"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
RunStartedAt time.Time `json:"run_started_at"`
HTMLURL string `json:"html_url"`
Jobs []JobSummary `json:"jobs,omitempty"`
FailedJobs []FailedJob `json:"failed_jobs,omitempty"`
RunAttempt int `json:"run_attempt"`
}
type Job struct {
ID int `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Conclusion string `json:"conclusion"`
HTMLURL string `json:"html_url"`
StartedAt time.Time `json:"started_at"`
CompletedAt time.Time `json:"completed_at"`
Steps []Step `json:"steps"`
}
type JobSummary struct {
Name string `json:"name"`
Conclusion string `json:"conclusion"`
DurationSec float64 `json:"duration_sec"`
HTMLURL string `json:"html_url"`
}
type JobsResponse struct {
Jobs []Job `json:"jobs"`
}
type WorkflowSummary struct {
Name string `json:"name"`
Description string `json:"description"`
Critical bool `json:"critical"`
Required bool `json:"required"`
TotalRuns int `json:"total_runs"`
FailedRuns int `json:"failed_runs"`
FailureRate float64 `json:"failure_rate"`
AvgDurationSecs float64 `json:"avg_duration_secs"`
WeatherHistory []string `json:"weather_history"`
LastRun *Run `json:"last_run"`
RecentRuns []Run `json:"recent_runs"`
}
type DashboardData struct {
GeneratedAt time.Time `json:"generated_at"`
Repo string `json:"repo"`
OverallHealth float64 `json:"overall_health"`
Workflows []WorkflowSummary `json:"workflows"`
}
type Client struct {
token string
repo string
httpClient *http.Client
logClient *http.Client
logAnalysis LogAnalysisConfig
}
func NewClient(token, repo string, la LogAnalysisConfig) *Client {
return &Client{
token: token,
repo: repo,
httpClient: &http.Client{Timeout: 20 * time.Second},
logClient: &http.Client{Timeout: 2 * time.Minute},
logAnalysis: la,
}
}
func (c *Client) setAuthHeaders(req *http.Request) {
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("Accept", "application/vnd.github+json")
}
// get performs an authenticated GET request against the GitHub API
func (c *Client) get(url string, v interface{}) error {
for attempt := 0; attempt < 3; attempt++ {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
c.setAuthHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests {
wait := 60 * time.Second
if reset := resp.Header.Get("X-RateLimit-Reset"); reset != "" {
if ts, err := strconv.ParseInt(reset, 10, 64); err == nil {
wait = time.Until(time.Unix(ts, 0)) + 2*time.Second
}
}
log.Printf("rate limited, waiting %s (attempt %d)...", wait, attempt+1)
time.Sleep(wait)
continue
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("GitHub API returned HTTP %d for %s", resp.StatusCode, url)
}
return json.NewDecoder(resp.Body).Decode(v)
}
return fmt.Errorf("still rate limited after retries: %s", url)
}
func parseGHTimestamp(line string) (time.Time, error) {
if len(line) < 28 {
return time.Time{}, fmt.Errorf("line too short")
}
return time.Parse("2006-01-02T15:04:05.9999999Z", line[:28])
}
func stripGHTimestamp(line string) string {
if len(line) > 29 && line[10] == 'T' {
line = line[29:]
}
line = ansiEscape.ReplaceAllString(line, "")
return strings.TrimSpace(line)
}
// analyseLog scans log lines and extracts meaningful failure signals based on the category patterns defined in config.yaml
func analyseLog(scanner *bufio.Scanner, cfg LogAnalysisConfig) LogSummary {
maxSignals := cfg.MaxSignalsPerJob
if maxSignals <= 0 {
maxSignals = 20
}
var signals []SignalLine
seen := make(map[string]bool)
for scanner.Scan() {
if len(signals) >= maxSignals {
break
}
clean := stripGHTimestamp(scanner.Text())
if clean == "" {
continue
}
lower := strings.ToLower(clean)
if matchesAny(lower, cfg.NoisePatterns) {
continue
}
for _, cat := range cfg.Categories {
if matchesAny(lower, cat.Patterns) && !seen[clean] {
seen[clean] = true
signals = append(signals, SignalLine{
Line: clean,
Category: cat.Name,
Priority: cat.Priority,
})
break
}
}
}
if len(signals) == 0 {
return LogSummary{Empty: true}
}
sort.SliceStable(signals, func(i, j int) bool {
return signals[i].Priority < signals[j].Priority
})
topCategory := signals[0].Category
byCategory := make(map[string][]string)
for _, s := range signals {
byCategory[s.Category] = append(byCategory[s.Category], s.Line)
}
return LogSummary{
Signals: signals,
ByCategory: byCategory,
TopCategory: topCategory,
}
}
// renderSummary formats a LogSummary into a human-readable plain-text block grouped by category
func renderSummary(s LogSummary) string {
if s.Empty {
return "(no actionable failure signal found in log)"
}
total := len(s.Signals)
var sb strings.Builder
fmt.Fprintf(&sb, "[%s] — %d signal(s) found\n", s.TopCategory, total)
sb.WriteString(strings.Repeat("─", 48) + "\n")
seen := make(map[string]bool)
var orderedCats []string
for _, sig := range s.Signals {
if !seen[sig.Category] {
seen[sig.Category] = true
orderedCats = append(orderedCats, sig.Category)
}
}
for _, cat := range orderedCats {
fmt.Fprintf(&sb, "-> %s\n", cat)
for _, line := range s.ByCategory[cat] {
fmt.Fprintf(&sb, " %s\n", line)
}
}
return strings.TrimRight(sb.String(), "\n")
}
func matchesAny(lower string, patterns []string) bool {
for _, p := range patterns {
if strings.Contains(lower, strings.ToLower(p)) {
return true
}
}
return false
}
// fetchAndAnalyseLog downloads the raw log for a single job
func (c *Client) fetchAndAnalyseLog(logURL string, failedSteps []Step) (snippet, raw string, err error) {
var resp *http.Response
for attempt := 0; attempt < 2; attempt++ {
req, _ := http.NewRequest("GET", logURL, nil)
c.setAuthHeaders(req)
resp, err = c.logClient.Do(req)
if err == nil {
break
}
if attempt == 0 {
log.Printf("log fetch attempt 1 failed (%v), retrying...", err)
time.Sleep(3 * time.Second)
}
}
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound ||
resp.StatusCode == http.StatusGone || // 410
resp.StatusCode == http.StatusForbidden { // 403
return "", "", fmt.Errorf("log expired (%d)", resp.StatusCode)
}
// Build a time window that covers all failed steps, with a small buffer
// before the first step and after the last, so we capture context lines
var windowStart, windowEnd time.Time
hasWindow := false
for _, step := range failedSteps {
if step.StartedAt.IsZero() {
continue
}
s := step.StartedAt.Add(-time.Second)
e := step.CompletedAt.Add(5 * time.Second)
if !hasWindow {
windowStart, windowEnd = s, e
hasWindow = true
} else {
if s.Before(windowStart) {
windowStart = s
}
if e.After(windowEnd) {
windowEnd = e
}
}
}
if hasWindow {
log.Printf("window: %s -> %s (%d failed step(s))",
windowStart.Format(time.RFC3339),
windowEnd.Format(time.RFC3339),
len(failedSteps))
} else {
log.Printf("no step timestamps - capturing full job log")
}
scanner := bufio.NewScanner(resp.Body)
scanner.Buffer(make([]byte, 256*1024), 256*1024)
// Slice the log to the computed window (or keep all lines if no window)
var sliced []string
for scanner.Scan() {
line := scanner.Text()
if hasWindow {
ts, parseErr := parseGHTimestamp(line)
if parseErr != nil || ts.Before(windowStart) {
continue
}
if ts.After(windowEnd) {
break
}
}
sliced = append(sliced, line)
}
if scanErr := scanner.Err(); scanErr != nil {
return "", "", scanErr
}
log.Printf("sliced: %d lines", len(sliced))
// Tighten the window further by finding the last line that contains a
// known precise error signal and discarding everything after it. This
// removes trailing teardown noise that follows the actual failure
if hasWindow && len(sliced) > 0 {
preciseSignals := []string{
"##[error]",
"--- fail:",
"fail!",
"[fail]",
"panic:",
"✖",
"timed out after",
}
var preciseEnd time.Time
for _, line := range sliced {
lower := strings.ToLower(stripGHTimestamp(line))
for _, sig := range preciseSignals {
if strings.Contains(lower, sig) {
if ts, parseErr := parseGHTimestamp(line); parseErr == nil {
preciseEnd = ts
}
break
}
}
}
if !preciseEnd.IsZero() {
log.Printf("precise end: %s", preciseEnd.Format("2006-01-02T15:04:05.9999999Z"))
var precise []string
for _, line := range sliced {
ts, parseErr := parseGHTimestamp(line)
if parseErr != nil {
precise = append(precise, line)
continue
}
if ts.After(preciseEnd) {
break
}
precise = append(precise, line)
}
sliced = precise
}
}
var captured []string
for _, line := range sliced {
clean := stripGHTimestamp(line)
if clean == "" {
continue
}
if matchesAny(strings.ToLower(clean), c.logAnalysis.NoisePatterns) {
continue
}
captured = append(captured, clean)
}
log.Printf("captured: %d lines after noise filter", len(captured))
lineReader := strings.NewReader(strings.Join(captured, "\n"))
summary := analyseLog(bufio.NewScanner(lineReader), c.logAnalysis)
snippet = renderSummary(summary)
raw = strings.Join(captured, "\n")
if len(raw) > 50000 {
raw = raw[:50000] + "\n\n[truncated at 50KB — see full log at: " + logURL + "]"
}
return snippet, raw, nil
}
// fetchAnnotationFallback is called when the job log has expired or is otherwise unavailable
func (c *Client) fetchAnnotationFallback(jobID int, htmlURL string) (snippet, raw string) {
url := fmt.Sprintf(
"https://api.github.com/repos/%s/check-runs/%d/annotations",
c.repo, jobID)
var annotations []struct {
Level string `json:"annotation_level"`
Message string `json:"message"`
Title string `json:"title"`
}
if err := c.get(url, &annotations); err != nil {
log.Printf("annotation fallback failed for job %d: %v", jobID, err)
snippet = "(log expired — no annotations available)"
raw = fmt.Sprintf("(log expired — see: %s)", htmlURL)
return
}
var failures []string
for _, a := range annotations {
if strings.EqualFold(a.Level, "failure") {
msg := a.Message
if a.Title != "" {
msg = a.Title + ": " + msg
}
failures = append(failures, msg)
}
}
if len(failures) == 0 {
snippet = "(log expired — no failure annotations found)"
raw = fmt.Sprintf("(log expired — see: %s)", htmlURL)
return
}
snippet = strings.Join(failures, "\n")
raw = fmt.Sprintf("(log expired — annotations only)\n\n%s\n\nSee: %s",
snippet, htmlURL)
return
}
// fetchJobsAndEnrich fetches all jobs for a run, appends their summaries to run.Jobs,
// and for each failed job downloads and analyses the log (falling back to annotations if the log is unavailable)
func (c *Client) fetchJobsAndEnrich(run *Run) {
url := fmt.Sprintf(
"https://api.github.com/repos/%s/actions/runs/%d/jobs",
c.repo, run.ID)
var resp JobsResponse
if err := c.get(url, &resp); err != nil {
log.Printf("warn: could not fetch jobs for run %d: %v", run.ID, err)
return
}
for _, job := range resp.Jobs {
dur := job.CompletedAt.Sub(job.StartedAt).Seconds()
if dur < 0 {
dur = 0
}
run.Jobs = append(run.Jobs, JobSummary{
Name: job.Name,
Conclusion: job.Conclusion,
DurationSec: dur,
HTMLURL: job.HTMLURL,
})
if job.Conclusion != "failure" {
continue
}
var failedSteps []Step
for _, step := range job.Steps {
if step.Conclusion == "failure" {
failedSteps = append(failedSteps, step)
log.Printf(" failed step #%d %q | %s → %s",
step.Number, step.Name,
step.StartedAt.Format(time.RFC3339),
step.CompletedAt.Format(time.RFC3339))
}
}
if len(failedSteps) == 0 {
log.Printf("job %d (%s): no step-level data — will capture full job log",
job.ID, job.Name)
}
logURL := fmt.Sprintf(
"https://api.github.com/repos/%s/actions/jobs/%d/logs",
c.repo, job.ID)
log.Printf("fetching logs for failed job %d (%s)...", job.ID, job.Name)
snippet, raw, fetchErr := c.fetchAndAnalyseLog(logURL, failedSteps)
if fetchErr != nil {
log.Printf("warn: log fetch failed for job %d: %v", job.ID, fetchErr)
snippet, raw = c.fetchAnnotationFallback(job.ID, job.HTMLURL)
}
run.FailedJobs = append(run.FailedJobs, FailedJob{
ID: job.ID,
Name: job.Name,
Conclusion: job.Conclusion,
HTMLURL: job.HTMLURL,
LogSnippet: snippet,
RawLog: raw,
})
}
}
func normalize(s string) string {
s = strings.ToLower(s)
s = strings.ReplaceAll(s, "-", "")
s = strings.ReplaceAll(s, "_", "")
s = strings.ReplaceAll(s, " ", "")
return s
}
func stemPath(path string) string {
parts := strings.Split(path, "/")
file := parts[len(parts)-1]
file = strings.TrimSuffix(file, ".yml")
file = strings.TrimSuffix(file, ".yaml")
return normalize(file)
}
// findWorkflow locates a GitHub Workflow in the list by matching keyword
// against file stems and display names using three passes in increasing fuzziness
func findWorkflow(workflows []Workflow, keyword string) *Workflow {
key := normalize(keyword)
for i, wf := range workflows {
if stemPath(wf.Path) == key {
return &workflows[i]
}
}
for i, wf := range workflows {
stem := stemPath(wf.Path)
if strings.Contains(stem, key) || strings.Contains(key, stem) {
return &workflows[i]
}
}
for i, wf := range workflows {
name := normalize(wf.Name)
if strings.Contains(name, key) || strings.Contains(key, name) {
return &workflows[i]
}
}
return nil
}
// buildWeatherHistory returns a fixed-width slice of the 7 most recent run conclusions
func buildWeatherHistory(runs []Run) []string {
const slots = 7
history := make([]string, slots)
for i := range history {
history[i] = "unknown"
}
take := runs
if len(take) > slots {
take = runs[:slots]
}
offset := slots - len(take)
for i, r := range take {
idx := len(take) - 1 - i
c := r.Conclusion
if c == "" {
c = "unknown"
}
switch c {
case "success", "failure", "skipped", "action_required":
default:
c = "unknown"
}
history[offset+idx] = c
}
return history
}
// buildSummary computes aggregate statistics (failure rate, average duration, weather history) for the given runs and returns a WorkflowSummary
func buildSummary(runs []Run, name, desc string, critical, required bool) WorkflowSummary {
var failed int
var totalDuration float64
for _, r := range runs {
if r.Conclusion == "failure" {
failed++
}
start := r.RunStartedAt
if start.IsZero() {
start = r.CreatedAt
}
totalDuration += r.UpdatedAt.Sub(start).Seconds()
}
total := len(runs)
var failureRate, avg float64
if total > 0 {
failureRate = float64(failed) / float64(total) * 100
avg = totalDuration / float64(total)
}
var lastRun *Run
if len(runs) > 0 {
r := runs[0]
lastRun = &r
}
return WorkflowSummary{
Name: name,
Description: desc,
Critical: critical,
Required: required,
TotalRuns: total,
FailedRuns: failed,
FailureRate: failureRate,
AvgDurationSecs: avg,
WeatherHistory: buildWeatherHistory(runs),
LastRun: lastRun,
RecentRuns: runs,
}
}
// main loads config.yaml, workflows_raw.json, and runs_raw.json, then for each
// configured workflow: matches it to a GitHub workflow ID, fetches job/log data
// for recent runs, builds a WorkflowSummary
func main() {
cfgBytes, err := os.ReadFile("config.yaml")
if err != nil {
log.Fatalf("cannot read config.yaml: %v", err)
}
var cfg Config
if err := yaml.Unmarshal(cfgBytes, &cfg); err != nil {
log.Fatalf("cannot parse config.yaml: %v", err)
}
recentLimit := cfg.Settings.RecentRunsInOutput
if recentLimit <= 0 {
recentLimit = cfg.Settings.MaxRunsPerWorkflow
}
workflowsBytes, err := os.ReadFile("workflows_raw.json")
if err != nil {
log.Fatalf("cannot read workflows_raw.json: %v", err)
}
var workflowsResp WorkflowsListResponse
if err := json.Unmarshal(workflowsBytes, &workflowsResp); err != nil {
log.Fatalf("cannot parse workflows_raw.json: %v", err)
}
runsBytes, err := os.ReadFile("runs_raw.json")
if err != nil {
log.Fatalf("cannot read runs_raw.json: %v", err)
}
var allRuns map[string]WorkflowsResponse
if err := json.Unmarshal(runsBytes, &allRuns); err != nil {
log.Fatalf("cannot parse runs_raw.json: %v", err)
}
client := NewClient(os.Getenv("GITHUB_TOKEN"), cfg.Settings.SourceRepo, cfg.LogAnalysis)
var notifier *Notifier
if cfg.Notify.Enabled {
if os.Getenv("GITHUB_TOKEN") == "" {
log.Println("warn: notify.enabled = true but GITHUB_TOKEN not set — skipping notifications")
} else {
notifier = NewNotifier(os.Getenv("GITHUB_TOKEN"), cfg.Settings.SourceRepo, cfg.Notify)
log.Printf("Notifier enabled -> target: %s, label: %s",
notifier.targetRepo, notifier.label)
}
}
var summaries []WorkflowSummary
var totalHealth float64
for _, w := range cfg.Workflows {
wf := findWorkflow(workflowsResp.Workflows, w.Name)
if wf == nil {
log.Println("Not found:", w.Name)
continue
}
log.Printf("Matched: %s -> %s", w.Name, wf.Name)
runsData, ok := allRuns[fmt.Sprintf("%d", wf.ID)]
if !ok {
log.Printf("warn: no runs found for workflow %s", w.Name)
continue
}
runs := runsData.WorkflowRuns
sort.Slice(runs, func(i, j int) bool {
return runs[i].CreatedAt.After(runs[j].CreatedAt)
})
recent := runs
if len(recent) > recentLimit {
recent = runs[:recentLimit]
}
log.Printf("fetching jobs and logs for %s...", w.Name)
for i := range recent {
client.fetchJobsAndEnrich(&recent[i])
time.Sleep(300 * time.Millisecond)
}
summary := buildSummary(runs, w.Name, w.Description, w.Critical, w.Required)
summary.RecentRuns = recent
if len(recent) > 0 && summary.LastRun != nil && recent[0].ID == summary.LastRun.ID {
r := recent[0]
summary.LastRun = &r
}
summaries = append(summaries, summary)
totalHealth += (100 - summary.FailureRate)
if notifier != nil {
notifier.Process(summary)
}
}
// Overall health is the mean of (100 - failureRate) across all workflows
health := 0.0
if len(summaries) > 0 {
health = totalHealth / float64(len(summaries))
}
data := DashboardData{
GeneratedAt: time.Now().UTC(),
Repo: cfg.Settings.SourceRepo,
OverallHealth: health,
Workflows: summaries,
}
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatalf("cannot marshal stats: %v", err)
}
if err := os.WriteFile("stats.json", out, 0644); err != nil {
log.Fatalf("cannot write stats.json: %v", err)
}
}