Skip to content

Commit 066af40

Browse files
committed
Fix test path and codeowners aggregation
1 parent de11cba commit 066af40

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

tools/flakeguard/cmd/aggregate_results.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ var AggregateResultsCmd = &cobra.Command{
5656
// Load test reports from JSON files and aggregate them
5757
aggregatedReport, err := reports.LoadAndAggregate(
5858
resultsPath,
59+
reports.WithRepoPath(repoPath),
60+
reports.WithCodeOwnersPath(codeOwnersPath),
5961
reports.WithReportID(reportID),
6062
reports.WithSplunk(splunkURL, splunkToken, splunkEvent),
6163
reports.WithBranchName(branchName),
@@ -75,36 +77,9 @@ var AggregateResultsCmd = &cobra.Command{
7577

7678
// Start spinner for mapping test results to paths
7779
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
78-
s.Suffix = " Mapping test results to paths..."
80+
s.Suffix = " Filter failed tests..."
7981
s.Start()
8082

81-
// Map test results to test paths
82-
err = reports.MapTestResultsToPaths(aggregatedReport, repoPath)
83-
if err != nil {
84-
s.Stop()
85-
log.Error().Stack().Err(err).Msg("Error mapping test results to paths")
86-
os.Exit(ErrorExitCode)
87-
}
88-
s.Stop()
89-
log.Debug().Msg("Successfully mapped paths to test results")
90-
91-
// Map test results to code owners if codeOwnersPath is provided
92-
if codeOwnersPath != "" {
93-
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
94-
s.Suffix = " Mapping test results to code owners..."
95-
s.Start()
96-
fmt.Println()
97-
98-
err = reports.MapTestResultsToOwners(aggregatedReport, codeOwnersPath)
99-
if err != nil {
100-
s.Stop()
101-
log.Error().Stack().Err(err).Msg("Error mapping test results to code owners")
102-
os.Exit(ErrorExitCode)
103-
}
104-
s.Stop()
105-
log.Debug().Msg("Successfully mapped code owners to test results")
106-
}
107-
10883
failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool {
10984
return !tr.Skipped && tr.PassRatio < maxPassRatio
11085
})

tools/flakeguard/reports/io.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type aggregateOptions struct {
5151
baseSha string
5252
headSha string
5353
repoURL string
54+
repoPath string
55+
codeownersPath string
5456
gitHubWorkflowName string
5557
gitHubWorkflowRunURL string
5658
}
@@ -101,6 +103,18 @@ func WithRepoURL(repoURL string) AggregateOption {
101103
}
102104
}
103105

106+
func WithRepoPath(repoPath string) AggregateOption {
107+
return func(opts *aggregateOptions) {
108+
opts.repoURL = repoPath
109+
}
110+
}
111+
112+
func WithCodeOwnersPath(codeOwnersPath string) AggregateOption {
113+
return func(opts *aggregateOptions) {
114+
opts.codeownersPath = codeOwnersPath
115+
}
116+
}
117+
104118
// WithGitHubWorkflowName sets the GitHub workflow name for the aggregated report.
105119
func WithGitHubWorkflowName(githubWorkflowName string) AggregateOption {
106120
return func(opts *aggregateOptions) {
@@ -176,6 +190,28 @@ func LoadAndAggregate(resultsPath string, options ...AggregateOption) (*TestRepo
176190
return nil, fmt.Errorf("error aggregating reports: %w", err)
177191
}
178192

193+
// Map test results to test paths
194+
err = MapTestResultsToPaths(aggregatedReport, opts.repoPath)
195+
if err != nil {
196+
return nil, fmt.Errorf("error mapping test results to paths: %w", err)
197+
}
198+
199+
// Map test results to code owners if codeOwnersPath is provided
200+
if opts.codeownersPath != "" {
201+
err := MapTestResultsToOwners(aggregatedReport, opts.codeownersPath)
202+
if err != nil {
203+
return nil, fmt.Errorf("error mapping test results to code owners: %w", err)
204+
}
205+
}
206+
207+
sendToSplunk := opts.splunkURL != ""
208+
if sendToSplunk {
209+
err = sendDataToSplunk(&opts, *aggregatedReport)
210+
if err != nil {
211+
return aggregatedReport, fmt.Errorf("error sending data to Splunk: %w", err)
212+
}
213+
}
214+
179215
return aggregatedReport, nil
180216
}
181217

@@ -444,7 +480,6 @@ func aggregate(reportChan <-chan *TestReport, errChan <-chan error, opts *aggreg
444480
testMap = make(map[string]TestResult)
445481
excludedTests = map[string]struct{}{}
446482
selectedTests = map[string]struct{}{}
447-
sendToSplunk = opts.splunkURL != ""
448483
)
449484

450485
for report := range reportChan {
@@ -497,12 +532,6 @@ func aggregate(reportChan <-chan *TestReport, errChan <-chan error, opts *aggreg
497532
fullReport.Results = aggregatedResults
498533
fullReport.GenerateSummaryData()
499534

500-
if sendToSplunk {
501-
err = sendDataToSplunk(opts, *fullReport)
502-
if err != nil {
503-
return fullReport, fmt.Errorf("error sending data to Splunk: %w", err)
504-
}
505-
}
506535
return fullReport, err
507536
}
508537

@@ -633,6 +662,7 @@ func sendDataToSplunk(opts *aggregateOptions, report TestReport) error {
633662
}
634663
log.Info().Msgf("Example Run. See '%s' for the results that would be sent to splunk", exampleSplunkReportFileName)
635664
} else {
665+
fmt.Printf("Final sent: %v", reportData)
636666
resp, err := client.R().SetBody(reportData).Post("")
637667
if err != nil {
638668
splunkErrs = append(splunkErrs, fmt.Errorf("error sending report '%s' to Splunk: %w", report.ID, err))
@@ -680,6 +710,8 @@ func batchSplunkResults(results []SplunkTestResult) (batchData bytes.Buffer, res
680710
}
681711
resultTestNames = append(resultTestNames, result.Event.Data.TestName)
682712
}
713+
fmt.Println("Formatted Batch Data:")
714+
fmt.Println(batchData.String())
683715
return batchData, resultTestNames, nil
684716
}
685717

0 commit comments

Comments
 (0)