|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "slices" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type RawLogEntry struct { |
| 12 | + TestName string `json:"test_name"` |
| 13 | + Branch string `json:"branch"` |
| 14 | + WorkflowRunURL string `json:"workflow_run_url"` |
| 15 | + WorkflowJobName string `json:"workflow_job_name"` |
| 16 | + WorkflowRunAttempt int `json:"workflow_run_attempt"` |
| 17 | +} |
| 18 | + |
| 19 | +func AggregateFlakyTestsFromResponse(lokiResp *LokiResponse) ([]FlakyTest, error) { |
| 20 | + var rawEntries []RawLogEntry |
| 21 | + for _, result := range lokiResp.Data.Result { |
| 22 | + testName := result.Stream["parent_test_name"] |
| 23 | + branch := result.Stream["ci_github_workflow_run_head_branch"] |
| 24 | + workflowRunURL := result.Stream["ci_github_workflow_run_html_url"] |
| 25 | + workflowJobName := result.Stream["ci_github_workflow_job_name"] |
| 26 | + workflowRunAttempt, _ := strconv.Atoi(result.Stream["ci_github_workflow_run_run_attempt"]) |
| 27 | + |
| 28 | + if testName == "" || branch == "" { |
| 29 | + continue |
| 30 | + } |
| 31 | + entry := RawLogEntry{ |
| 32 | + TestName: testName, |
| 33 | + Branch: branch, |
| 34 | + WorkflowRunURL: workflowRunURL, |
| 35 | + WorkflowJobName: workflowJobName, |
| 36 | + WorkflowRunAttempt: workflowRunAttempt, |
| 37 | + } |
| 38 | + rawEntries = append(rawEntries, entry) |
| 39 | + } |
| 40 | + |
| 41 | + log.Printf("🔄 Processed %d log lines, extracted %d valid test failure entries", len(lokiResp.Data.Result), len(rawEntries)) |
| 42 | + |
| 43 | + return detectFlakyTestsFromRawEntries(rawEntries), nil |
| 44 | +} |
| 45 | + |
| 46 | +func detectFlakyTestsFromRawEntries(rawEntries []RawLogEntry) []FlakyTest { |
| 47 | + testMap := make(map[string]map[string]int) |
| 48 | + exampleWorkflows := make(map[string]map[GithubActionsWorkflow]bool) |
| 49 | + |
| 50 | + for _, entry := range rawEntries { |
| 51 | + if entry.TestName == "" || entry.Branch == "" { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + if testMap[entry.TestName] == nil { |
| 56 | + testMap[entry.TestName] = make(map[string]int) |
| 57 | + exampleWorkflows[entry.TestName] = make(map[GithubActionsWorkflow]bool) |
| 58 | + } |
| 59 | + |
| 60 | + testMap[entry.TestName][entry.Branch]++ |
| 61 | + |
| 62 | + workflow := GithubActionsWorkflow{ |
| 63 | + RunURL: entry.WorkflowRunURL, |
| 64 | + JobName: entry.WorkflowJobName, |
| 65 | + Attempt: entry.WorkflowRunAttempt, |
| 66 | + } |
| 67 | + |
| 68 | + if workflow != (GithubActionsWorkflow{}) && len(exampleWorkflows[entry.TestName]) < 3 { |
| 69 | + exampleWorkflows[entry.TestName][workflow] = true |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + var flakyTests []FlakyTest |
| 74 | + |
| 75 | + for testName, branches := range testMap { |
| 76 | + isFlaky := false |
| 77 | + totalFailures := 0 |
| 78 | + |
| 79 | + for branch, count := range branches { |
| 80 | + totalFailures += count |
| 81 | + |
| 82 | + if branch == "main" || branch == "master" { |
| 83 | + isFlaky = true |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if len(branches) > 1 { |
| 88 | + isFlaky = true |
| 89 | + } |
| 90 | + |
| 91 | + if !isFlaky { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + var branchSummary []string |
| 96 | + for branch, count := range branches { |
| 97 | + branchSummary = append(branchSummary, fmt.Sprintf("%s:%d", branch, count)) |
| 98 | + } |
| 99 | + |
| 100 | + var workflows []GithubActionsWorkflow |
| 101 | + for workflowURL := range exampleWorkflows[testName] { |
| 102 | + workflows = append(workflows, workflowURL) |
| 103 | + } |
| 104 | + |
| 105 | + flakyTests = append(flakyTests, FlakyTest{ |
| 106 | + TestName: testName, |
| 107 | + TotalFailures: totalFailures, |
| 108 | + BranchCounts: branches, |
| 109 | + ExampleWorkflows: workflows, |
| 110 | + }) |
| 111 | + |
| 112 | + log.Printf("🔍 Detected flaky test: %s (%d total failures) - branches: %s", |
| 113 | + testName, totalFailures, strings.Join(branchSummary, ", ")) |
| 114 | + } |
| 115 | + |
| 116 | + log.Printf("📈 Test analysis stats:") |
| 117 | + log.Printf(" - Total unique tests with failures: %d", len(testMap)) |
| 118 | + log.Printf(" - Tests classified as flaky: %d", len(flakyTests)) |
| 119 | + |
| 120 | + return sortFlakyTests(flakyTests) |
| 121 | +} |
| 122 | + |
| 123 | +func sortFlakyTests(tests []FlakyTest) []FlakyTest { |
| 124 | + slices.SortFunc(tests, func(a, b FlakyTest) int { |
| 125 | + branchesDelta := len(b.BranchCounts) - len(a.BranchCounts) |
| 126 | + if branchesDelta != 0 { |
| 127 | + return branchesDelta |
| 128 | + } |
| 129 | + if a.TestName < b.TestName { |
| 130 | + return -1 |
| 131 | + } else if a.TestName > b.TestName { |
| 132 | + return 1 |
| 133 | + } |
| 134 | + return 0 |
| 135 | + }) |
| 136 | + return tests |
| 137 | +} |
0 commit comments