Skip to content

Commit b0c2ef7

Browse files
committed
Refactor GitHub report generation command to use separate paths for summary and PR comment markdowns
1 parent 6273597 commit b0c2ef7

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

tools/flakeguard/cmd/generate_github_report.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ var GenerateReportCmd = &cobra.Command{
1919

2020
// Get flag values
2121
flakeguardReportPath, _ := cmd.Flags().GetString("flakeguard-report")
22-
outputDir, _ := cmd.Flags().GetString("output-dir")
22+
summaryReportPath, _ := cmd.Flags().GetString("summary-report-md-path")
23+
prCommentPath, _ := cmd.Flags().GetString("pr-comment-md-path")
2324
maxPassRatio, _ := cmd.Flags().GetFloat64("max-pass-ratio")
24-
generatePRComment, _ := cmd.Flags().GetBool("generate-pr-comment")
2525
failedLogsURL, _ := cmd.Flags().GetString("failed-logs-url")
2626

2727
failedLogsArtifactName := "failed-test-results-with-logs.json"
2828

29+
// Load the test report from file
2930
testReport := &reports.TestReport{}
3031
reportFile, err := os.Open(flakeguardReportPath)
3132
if err != nil {
@@ -43,28 +44,28 @@ var GenerateReportCmd = &cobra.Command{
4344
fmt.Println()
4445
log.Info().Msg("Successfully loaded aggregated test report")
4546

46-
// Create output directory if it doesn't exist
47-
if err := fs.MkdirAll(outputDir, 0755); err != nil {
48-
log.Error().Err(err).Msg("Error creating output directory")
47+
// Create directory for summary markdown if needed
48+
summaryDir := filepath.Dir(summaryReportPath)
49+
if err := fs.MkdirAll(summaryDir, 0755); err != nil {
50+
log.Error().Err(err).Msg("Error creating directory for summary report markdown")
4951
os.Exit(ErrorExitCode)
5052
}
5153

5254
// Generate GitHub summary markdown
53-
54-
summaryPath := filepath.Join(outputDir, "all-tests-summary.md")
55-
err = generateGitHubSummaryMarkdown(*testReport, summaryPath, failedLogsURL, failedLogsArtifactName)
55+
err = generateGitHubSummaryMarkdown(*testReport, summaryReportPath, failedLogsURL, failedLogsArtifactName)
5656
if err != nil {
5757
fmt.Println()
5858
log.Error().Err(err).Msg("Error generating GitHub summary markdown")
5959
os.Exit(ErrorExitCode)
6060
}
6161
fmt.Println()
6262
log.Info().
63-
Str("path", summaryPath).
63+
Str("path", summaryReportPath).
6464
Msg("GitHub summary markdown generated successfully")
6565

66-
if generatePRComment {
67-
// Retrieve required flags
66+
// Generate PR comment markdown if prCommentPath is provided
67+
if prCommentPath != "" {
68+
// Retrieve required flags for PR comment
6869
currentBranch, _ := cmd.Flags().GetString("current-branch")
6970
currentCommitSHA, _ := cmd.Flags().GetString("current-commit-sha")
7071
baseBranch, _ := cmd.Flags().GetString("base-branch")
@@ -86,14 +87,20 @@ var GenerateReportCmd = &cobra.Command{
8687
missingFlags = append(missingFlags, "--action-run-id")
8788
}
8889
if len(missingFlags) > 0 {
89-
log.Error().Strs("missing flags", missingFlags).Msg("Not all required flags are provided for --generate-pr-comment")
90+
log.Error().Strs("missing flags", missingFlags).Msg("Not all required flags are provided for PR comment generation")
91+
os.Exit(ErrorExitCode)
92+
}
93+
94+
// Create directory for PR comment markdown if needed
95+
prCommentDir := filepath.Dir(prCommentPath)
96+
if err := fs.MkdirAll(prCommentDir, 0755); err != nil {
97+
log.Error().Err(err).Msg("Error creating directory for PR comment markdown")
9098
os.Exit(ErrorExitCode)
9199
}
92100

93-
prCommentPath := filepath.Join(outputDir, "all-tests-pr-comment.md")
94101
err = generatePRCommentMarkdown(
95102
*testReport,
96-
filepath.Join(outputDir, prCommentPath),
103+
prCommentPath,
97104
baseBranch,
98105
currentBranch,
99106
currentCommitSHA,
@@ -118,14 +125,14 @@ var GenerateReportCmd = &cobra.Command{
118125

119126
func init() {
120127
GenerateReportCmd.Flags().StringP("flakeguard-report", "i", "", "Path to the flakeguard test report JSON file (required)")
121-
GenerateReportCmd.Flags().StringP("output-dir", "o", "./report", "Path to output the generated report files")
128+
GenerateReportCmd.Flags().String("summary-report-md-path", "", "Path to output the generated summary markdown file (required)")
129+
GenerateReportCmd.Flags().String("pr-comment-md-path", "", "Path to output the generated PR comment markdown file (optional)")
122130
GenerateReportCmd.Flags().Float64P("max-pass-ratio", "", 1.0, "The maximum pass ratio threshold for a test to be considered flaky")
123-
GenerateReportCmd.Flags().Bool("generate-pr-comment", false, "Set to true to generate PR comment markdown")
124131
GenerateReportCmd.Flags().String("base-branch", "develop", "The base branch to compare against (used in PR comment)")
125-
GenerateReportCmd.Flags().String("current-branch", "", "The current branch name (required if generate-pr-comment is set)")
126-
GenerateReportCmd.Flags().String("current-commit-sha", "", "The current commit SHA (required if generate-pr-comment is set)")
127-
GenerateReportCmd.Flags().String("repo-url", "", "The repository URL (required if generate-pr-comment is set)")
128-
GenerateReportCmd.Flags().String("action-run-id", "", "The GitHub Actions run ID (required if generate-pr-comment is set)")
132+
GenerateReportCmd.Flags().String("current-branch", "", "The current branch name (required if pr-comment-md-path is provided)")
133+
GenerateReportCmd.Flags().String("current-commit-sha", "", "The current commit SHA (required if pr-comment-md-path is provided)")
134+
GenerateReportCmd.Flags().String("repo-url", "", "The repository URL (required if pr-comment-md-path is provided)")
135+
GenerateReportCmd.Flags().String("action-run-id", "", "The GitHub Actions run ID (required if pr-comment-md-path is provided)")
129136
GenerateReportCmd.Flags().String("github-repository", "", "The GitHub repository in the format owner/repo (required)")
130137
GenerateReportCmd.Flags().Int64("github-run-id", 0, "The GitHub Actions run ID (required)")
131138
GenerateReportCmd.Flags().String("failed-logs-url", "", "Optional URL linking to additional logs for failed tests")
@@ -134,6 +141,10 @@ func init() {
134141
log.Error().Err(err).Msg("Error marking flag as required")
135142
os.Exit(ErrorExitCode)
136143
}
144+
if err := GenerateReportCmd.MarkFlagRequired("summary-report-md-path"); err != nil {
145+
log.Error().Err(err).Msg("Error marking flag as required")
146+
os.Exit(ErrorExitCode)
147+
}
137148
}
138149

139150
func generateGitHubSummaryMarkdown(report reports.TestReport, outputPath, artifactLink, artifactName string) error {

0 commit comments

Comments
 (0)