Skip to content

Commit 251c735

Browse files
committed
Update send-to-splunk cmd
1 parent b7cc9ee commit 251c735

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

tools/flakeguard/cmd/send_to_splunk.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ var SendToSplunkCmd = &cobra.Command{
1919
splunkToken, _ := cmd.Flags().GetString("splunk-token")
2020
splunkEvent, _ := cmd.Flags().GetString("splunk-event")
2121
failLogsURL, _ := cmd.Flags().GetString("failed-logs-url")
22+
repoURL, _ := cmd.Flags().GetString("repo-url")
23+
branchName, _ := cmd.Flags().GetString("branch-name")
24+
headSHA, _ := cmd.Flags().GetString("head-sha")
25+
baseSHA, _ := cmd.Flags().GetString("base-sha")
26+
githubWorkflowName, _ := cmd.Flags().GetString("github-workflow-name")
27+
githubWorkflowRunURL, _ := cmd.Flags().GetString("github-workflow-run-url")
28+
reportID, _ := cmd.Flags().GetString("report-id")
29+
genReportID, _ := cmd.Flags().GetBool("gen-report-id")
30+
repoPath, _ := cmd.Flags().GetString("repo-path")
31+
codeownersPath, _ := cmd.Flags().GetString("codeowners-path")
2232

2333
// Read the report file.
2434
data, err := os.ReadFile(reportPath)
@@ -33,12 +43,49 @@ var SendToSplunkCmd = &cobra.Command{
3343
log.Error().Err(err).Msg("Error unmarshalling report JSON")
3444
os.Exit(1)
3545
}
46+
testReport.GenerateSummaryData()
3647

48+
// Override report fields with flags if provided.
49+
if repoURL != "" {
50+
testReport.RepoURL = repoURL
51+
}
52+
if branchName != "" {
53+
testReport.BranchName = branchName
54+
}
55+
if headSHA != "" {
56+
testReport.HeadSHA = headSHA
57+
}
58+
if baseSHA != "" {
59+
testReport.BaseSHA = baseSHA
60+
}
61+
if githubWorkflowName != "" {
62+
testReport.GitHubWorkflowName = githubWorkflowName
63+
}
64+
if githubWorkflowRunURL != "" {
65+
testReport.GitHubWorkflowRunURL = githubWorkflowRunURL
66+
}
67+
if reportID != "" {
68+
testReport.SetReportID(reportID)
69+
}
70+
if genReportID {
71+
testReport.SetRandomReportID()
72+
}
73+
if repoPath != "" {
74+
err = reports.MapTestResultsToPaths(&testReport, repoPath)
75+
if err != nil {
76+
log.Error().Err(err).Msg("Error mapping test results to paths")
77+
os.Exit(1)
78+
}
79+
}
80+
if codeownersPath != "" && repoPath != "" {
81+
reports.MapTestResultsToOwners(&testReport, codeownersPath)
82+
}
3783
if failLogsURL != "" {
3884
testReport.FailedLogsURL = failLogsURL
3985
}
4086

41-
err = reports.SendReportToSplunk(splunkURL, splunkToken, splunkEvent, testReport)
87+
// Send the test report to Splunk.
88+
err = reports.SendTestReportToSplunk(splunkURL, splunkToken, splunkEvent, testReport)
4289
if err != nil {
4390
log.Error().Err(err).Msg("Error sending test report to Splunk")
4491
os.Exit(1)
@@ -55,6 +102,16 @@ func init() {
55102
SendToSplunkCmd.Flags().String("splunk-url", "", "Optional URL to send the test results to Splunk")
56103
SendToSplunkCmd.Flags().String("splunk-token", "", "Optional Splunk HEC token to send the test results")
57104
SendToSplunkCmd.Flags().String("splunk-event", "", "Optional Splunk event to send as the triggering event for the test results")
105+
SendToSplunkCmd.Flags().String("repo-url", "", "The repository URL")
106+
SendToSplunkCmd.Flags().String("branch-name", "", "Branch name for the test report")
107+
SendToSplunkCmd.Flags().String("head-sha", "", "Head commit SHA for the test report")
108+
SendToSplunkCmd.Flags().String("base-sha", "", "Base commit SHA for the test report")
109+
SendToSplunkCmd.Flags().String("github-workflow-name", "", "GitHub workflow name for the test report")
110+
SendToSplunkCmd.Flags().String("github-workflow-run-url", "", "GitHub workflow run URL for the test report")
111+
SendToSplunkCmd.Flags().String("report-id", "", "Optional identifier for the test report. Will be generated if not provided")
112+
SendToSplunkCmd.Flags().StringP("repo-path", "", ".", "The path to the root of the repository/project")
113+
SendToSplunkCmd.Flags().StringP("codeowners-path", "", "", "Path to the CODEOWNERS file")
114+
SendToSplunkCmd.Flags().Bool("gen-report-id", false, "Generate a random report ID")
58115

59116
// Mark required flags.
60117
if err := SendToSplunkCmd.MarkFlagRequired("report-path"); err != nil {

tools/flakeguard/reports/data.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"sort"
1111
"strings"
1212
"time"
13+
14+
"github.com/google/uuid"
1315
)
1416

1517
// TestReport reports on the parameters and results of one to many test runs
@@ -33,6 +35,22 @@ type TestReport struct {
3335
MaxPassRatio float64 `json:"max_pass_ratio,omitempty"`
3436
}
3537

38+
func (r *TestReport) SetRandomReportID() {
39+
uuid, err := uuid.NewRandom()
40+
if err != nil {
41+
panic(fmt.Errorf("error generating random report id: %w", err))
42+
}
43+
r.SetReportID(uuid.String())
44+
}
45+
46+
func (r *TestReport) SetReportID(reportID string) {
47+
r.ID = reportID
48+
// Set the report ID in all test results
49+
for i := range r.Results {
50+
r.Results[i].ReportID = r.ID
51+
}
52+
}
53+
3654
// SaveToFile saves the test report to a JSON file at the given path.
3755
// It returns an error if there's any issue with marshaling the report or writing to the file.
3856
func (testReport *TestReport) SaveToFile(outputPath string) error {

tools/flakeguard/reports/io.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"path/filepath"
1010

11-
"github.com/google/uuid"
1211
"github.com/rs/zerolog/log"
1312
)
1413

@@ -157,19 +156,14 @@ func LoadAndAggregate(resultsPath string, options ...AggregateOption) (*TestRepo
157156
}
158157
}()
159158

160-
if opts.reportID == "" {
161-
uuid, err := uuid.NewRandom()
162-
if err != nil {
163-
return nil, fmt.Errorf("error generating UUID: %w", err)
164-
}
165-
opts.reportID = uuid.String()
166-
}
167-
168159
// Aggregate results as they are being loaded
169160
aggregatedReport, err := aggregate(reportChan, errChan, &opts)
170161
if err != nil {
171162
return nil, fmt.Errorf("error aggregating reports: %w", err)
172163
}
164+
if opts.reportID == "" {
165+
aggregatedReport.SetRandomReportID()
166+
}
173167

174168
// Map test results to test paths
175169
err = MapTestResultsToPaths(aggregatedReport, opts.repoPath)

tools/flakeguard/reports/io_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestAggregateResultFilesSplunk(t *testing.T) {
6969
report, err := LoadAndAggregate("./testdata", WithReportID(reportID))
7070
require.NoError(t, err, "LoadAndAggregate failed")
7171

72-
err = SendReportToSplunk(srv.URL, splunkToken, splunkEvent, *report)
72+
err = SendTestReportToSplunk(srv.URL, splunkToken, splunkEvent, *report)
7373
require.NoError(t, err, "SendReportToSplunk failed")
7474
verifyAggregatedReport(t, report)
7575
assert.Equal(t, 1, reportRequestsReceived, "unexpected number of report requests")

tools/flakeguard/reports/splunk.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"github.com/rs/zerolog/log"
1717
)
1818

19-
// SendReportToSplunk sends a truncated TestReport and each individual TestResults to Splunk as events
20-
func SendReportToSplunk(splunkURL, splunkToken, splunkEvent string, report TestReport) error {
19+
// SendTestReportToSplunk sends a truncated TestReport and each individual TestResults to Splunk as events
20+
func SendTestReportToSplunk(splunkURL, splunkToken, splunkEvent string, report TestReport) error {
2121
start := time.Now()
2222
results := report.Results
2323
report.Results = nil // Don't send results to Splunk, doing that individually
@@ -117,6 +117,9 @@ func SendReportToSplunk(splunkURL, splunkToken, splunkEvent string, report TestR
117117
log.Info().Msg("Example Run. See 'example_results/splunk_results' for the results that would be sent to splunk")
118118
}
119119

120+
// Sanitize report fields
121+
report.BranchName = strings.TrimSpace(report.BranchName)
122+
120123
reportData := SplunkTestReport{
121124
Event: SplunkTestReportEvent{
122125
Event: splunkEvent,

0 commit comments

Comments
 (0)