@@ -20,13 +20,10 @@ var ReportCmd = &cobra.Command{
2020 // Get flag values
2121 reportResultsPath , _ := cmd .Flags ().GetString ("results-path" )
2222 reportOutputPath , _ := cmd .Flags ().GetString ("output-path" )
23- reportFormats , _ := cmd .Flags ().GetString ("format" )
2423 reportMaxPassRatio , _ := cmd .Flags ().GetFloat64 ("max-pass-ratio" )
2524 reportCodeOwnersPath , _ := cmd .Flags ().GetString ("codeowners-path" )
2625 reportRepoPath , _ := cmd .Flags ().GetString ("repo-path" )
27-
28- // Split the formats into a slice
29- formats := strings .Split (reportFormats , "," )
26+ generatePRComment , _ := cmd .Flags ().GetBool ("generate-pr-comment" )
3027
3128 // Start spinner for loading test reports
3229 s := spinner .New (spinner .CharSets [11 ], 100 * time .Millisecond )
@@ -104,19 +101,57 @@ var ReportCmd = &cobra.Command{
104101 }
105102 fmt .Printf ("All tests report saved to %s\n " , allTestsReportPath )
106103
107- // Generate and save the summary reports (all tests) in specified formats
108- for _ , format := range formats {
109- format = strings .ToLower (strings .TrimSpace (format ))
104+ // Generate GitHub summary markdown
105+ s = spinner .New (spinner .CharSets [11 ], 100 * time .Millisecond )
106+ s .Suffix = " Generating GitHub summary markdown..."
107+ s .Start ()
108+
109+ err = generateGitHubSummaryMarkdown (aggregatedReport , filepath .Join (outputDir , "all-tests" ))
110+ if err != nil {
111+ s .Stop ()
112+ return fmt .Errorf ("error generating GitHub summary markdown: %w" , err )
113+ }
114+ s .Stop ()
115+ fmt .Println ("GitHub summary markdown generated successfully." )
116+
117+ if generatePRComment {
118+ // Retrieve required flags
119+ currentBranch , _ := cmd .Flags ().GetString ("current-branch" )
120+ currentCommitSHA , _ := cmd .Flags ().GetString ("current-commit-sha" )
121+ baseBranch , _ := cmd .Flags ().GetString ("base-branch" )
122+ repoURL , _ := cmd .Flags ().GetString ("repo-url" )
123+ actionRunID , _ := cmd .Flags ().GetString ("action-run-id" )
124+
125+ // Validate that required flags are provided
126+ missingFlags := []string {}
127+ if currentBranch == "" {
128+ missingFlags = append (missingFlags , "--current-branch" )
129+ }
130+ if currentCommitSHA == "" {
131+ missingFlags = append (missingFlags , "--current-commit-sha" )
132+ }
133+ if repoURL == "" {
134+ missingFlags = append (missingFlags , "--repo-url" )
135+ }
136+ if actionRunID == "" {
137+ missingFlags = append (missingFlags , "--action-run-id" )
138+ }
139+ if len (missingFlags ) > 0 {
140+ return fmt .Errorf ("the following flags are required when --generate-pr-comment is set: %s" , strings .Join (missingFlags , ", " ))
141+ }
142+
143+ // Generate PR comment markdown
110144 s = spinner .New (spinner .CharSets [11 ], 100 * time .Millisecond )
111- s .Suffix = fmt . Sprintf ( " Generating all tests summary in format %s ..." , format )
145+ s .Suffix = " Generating PR comment markdown ..."
112146 s .Start ()
113147
114- if err := generateReport (aggregatedReport , format , filepath .Join (outputDir , "all-tests" )); err != nil {
148+ err = generatePRCommentMarkdown (aggregatedReport , filepath .Join (outputDir , "all-tests" ), baseBranch , currentBranch , currentCommitSHA , repoURL , actionRunID )
149+ if err != nil {
115150 s .Stop ()
116- return fmt .Errorf ("error generating all tests summary in format %s : %w" , format , err )
151+ return fmt .Errorf ("error generating PR comment markdown : %w" , err )
117152 }
118153 s .Stop ()
119- fmt .Printf ( "All tests summary in format %s generated successfully.\n " , format )
154+ fmt .Println ( "PR comment markdown generated successfully." )
120155 }
121156
122157 // Filter failed tests (PassRatio < maxPassRatio and not skipped)
@@ -163,38 +198,40 @@ var ReportCmd = &cobra.Command{
163198func init () {
164199 ReportCmd .Flags ().StringP ("results-path" , "p" , "" , "Path to the folder containing JSON test result files (required)" )
165200 ReportCmd .Flags ().StringP ("output-path" , "o" , "./report" , "Path to output the generated report files" )
166- ReportCmd .Flags ().StringP ("format" , "f" , "markdown,json" , "Comma-separated list of summary report formats (markdown,json)" )
167201 ReportCmd .Flags ().Float64P ("max-pass-ratio" , "" , 1.0 , "The maximum pass ratio threshold for a test to be considered flaky" )
168202 ReportCmd .Flags ().StringP ("codeowners-path" , "" , "" , "Path to the CODEOWNERS file" )
169203 ReportCmd .Flags ().StringP ("repo-path" , "" , "." , "The path to the root of the repository/project" )
204+ ReportCmd .Flags ().Bool ("generate-pr-comment" , false , "Set to true to generate PR comment markdown" )
205+ ReportCmd .Flags ().String ("base-branch" , "develop" , "The base branch to compare against (used in PR comment)" )
206+ ReportCmd .Flags ().String ("current-branch" , "" , "The current branch name (required if generate-pr-comment is set)" )
207+ ReportCmd .Flags ().String ("current-commit-sha" , "" , "The current commit SHA (required if generate-pr-comment is set)" )
208+ ReportCmd .Flags ().String ("repo-url" , "" , "The repository URL (required if generate-pr-comment is set)" )
209+ ReportCmd .Flags ().String ("action-run-id" , "" , "The GitHub Actions run ID (required if generate-pr-comment is set)" )
210+
170211 ReportCmd .MarkFlagRequired ("results-path" )
171212}
172213
173- func generateReport (report * reports.TestReport , format , outputPath string ) error {
214+ func generateGitHubSummaryMarkdown (report * reports.TestReport , outputPath string ) error {
174215 fs := reports.OSFileSystem {}
175- format = strings .ToLower (strings .TrimSpace (format ))
176-
177- switch format {
178- case "markdown" :
179- // Adjust the markdown filename to include "-summary"
180- mdFileName := outputPath + "-summary.md"
181- mdFile , err := fs .Create (mdFileName )
182- if err != nil {
183- return fmt .Errorf ("error creating markdown file: %w" , err )
184- }
185- defer mdFile .Close ()
186- reports .GenerateMarkdownSummary (mdFile , report , 1.0 )
187- case "json" :
188- // Generate summary JSON
189- summaryData := reports .GenerateSummaryData (report .Results , 1.0 )
190- summaryFileName := outputPath + "-summary.json"
191- if err := reports .SaveSummaryAsJSON (fs , summaryFileName , summaryData ); err != nil {
192- return fmt .Errorf ("error saving summary JSON: %w" , err )
193- }
194- default :
195- return fmt .Errorf ("unsupported summary report format: %s" , format )
216+ mdFileName := outputPath + "-summary.md"
217+ mdFile , err := fs .Create (mdFileName )
218+ if err != nil {
219+ return fmt .Errorf ("error creating GitHub summary markdown file: %w" , err )
196220 }
221+ defer mdFile .Close ()
222+ reports .GenerateGitHubSummaryMarkdown (mdFile , report , 1.0 )
223+ return nil
224+ }
197225
226+ func generatePRCommentMarkdown (report * reports.TestReport , outputPath , baseBranch , currentBranch , currentCommitSHA , repoURL , actionRunID string ) error {
227+ fs := reports.OSFileSystem {}
228+ mdFileName := outputPath + "-pr-comment.md"
229+ mdFile , err := fs .Create (mdFileName )
230+ if err != nil {
231+ return fmt .Errorf ("error creating PR comment markdown file: %w" , err )
232+ }
233+ defer mdFile .Close ()
234+ reports .GeneratePRCommentMarkdown (mdFile , report , 1.0 , baseBranch , currentBranch , currentCommitSHA , repoURL , actionRunID )
198235 return nil
199236}
200237
0 commit comments