1- const fs = require ( 'fs' ) ;
2- const path = require ( 'path' ) ;
3- const readline = require ( 'readline' ) ;
4- const https = require ( 'https' ) ;
5- const { execSync } = require ( 'child_process' ) ;
1+ import { createReadStream , existsSync , mkdirSync , writeFileSync } from 'fs' ;
2+ import { dirname } from 'path' ;
3+ import { createInterface } from 'readline' ;
4+ import { request } from 'https' ;
5+ import { execSync } from 'child_process' ;
6+ import { fileURLToPath } from 'url' ;
7+
8+ // Get __dirname equivalent in ES modules
9+ const __filename = fileURLToPath ( import . meta. url ) ;
10+ const __dirname = dirname ( __filename ) ;
611
712/**
813 * Simple CSV line parser that handles quoted fields
@@ -187,7 +192,7 @@ const generateJacocoReport = (csvFilePath, outputPath) => {
187192 // Generate markdown content
188193 const markdownContent = `# JaCoCo Code Coverage Report
189194
190- ## Package Coverage
195+ ## Coverage Report
191196
192197${ generateMarkdownTable ( packageReport , {
193198 'package' : 'Package' ,
@@ -196,14 +201,17 @@ ${generateMarkdownTable(packageReport, {
196201 'lines total' : 'Total Lines'
197202 } ) }
198203
199- ## Class Coverage
200204
205+ <details>
206+ <summary>Class Coverage</summary>
201207${ generateMarkdownTable ( classReport , {
202208 'class' : 'Class' ,
203209 'coverage' : 'Coverage' ,
204210 'lines covered' : 'Lines Covered' ,
205211 'lines total' : 'Total Lines'
206212 } ) }
213+
214+ </details>
207215` ;
208216
209217 // Write to file
@@ -214,6 +222,7 @@ ${generateMarkdownTable(classReport, {
214222 } ) ;
215223} ;
216224
225+
217226/**
218227 * Posts or updates a comment on a GitHub PR
219228 * @param {string } token - GitHub token with repo:write permission
@@ -378,42 +387,47 @@ if (!repo) {
378387
379388const [ owner , repository ] = repo . split ( '/' ) ;
380389
381- // Generate the report and optionally post to GitHub
382- try {
383- const reportContent = await generateJacocoReport ( inputFile , outputFile ) ;
384-
385- if ( prNumber && githubToken ) {
386- try {
387- const commentMarker = '<!-- jacoco-coverage-report -->' ;
388- const comment = `${ commentMarker } \n# JaCoCo Coverage Report\n\n${ reportContent } ` ;
389-
390- // Find existing comment to update
391- const existingComment = await findExistingComment (
392- githubToken ,
393- owner ,
394- repository ,
395- prNumber ,
396- commentMarker
397- ) ;
398-
399- // Post or update comment
400- const result = await postOrUpdateComment (
401- githubToken ,
402- owner ,
403- repository ,
404- prNumber ,
405- comment ,
406- existingComment ?. id
407- ) ;
408-
409- console . log ( `✅ Coverage report ${ existingComment ? 'updated' : 'posted' } to PR #${ prNumber } ` ) ;
410- } catch ( error ) {
411- console . error ( 'Error posting to GitHub:' , error . message ) ;
412- process . exit ( 1 ) ;
390+ // Main function to run the script
391+ async function main ( ) {
392+ try {
393+ const reportContent = await generateJacocoReport ( inputFile , outputFile ) ;
394+
395+ if ( prNumber && githubToken ) {
396+ try {
397+ const commentMarker = '<!-- jacoco-coverage-report -->' ;
398+ const comment = `${ commentMarker } \n# JaCoCo Coverage Report\n\n${ reportContent } ` ;
399+
400+ // Find existing comment to update
401+ const existingComment = await findExistingComment (
402+ githubToken ,
403+ owner ,
404+ repository ,
405+ prNumber ,
406+ commentMarker
407+ ) ;
408+
409+ // Post or update comment
410+ const result = await postOrUpdateComment (
411+ githubToken ,
412+ owner ,
413+ repository ,
414+ prNumber ,
415+ comment ,
416+ existingComment ?. id
417+ ) ;
418+
419+ console . log ( `✅ Coverage report ${ existingComment ? 'updated' : 'posted' } to PR #${ prNumber } ` ) ;
420+ } catch ( error ) {
421+ console . error ( 'Error posting to GitHub:' , error . message ) ;
422+ process . exit ( 1 ) ;
423+ }
413424 }
425+ } catch ( error ) {
426+ console . error ( 'An error occurred:' ) ;
427+ console . error ( error . message ) ;
428+ process . exit ( 1 ) ;
414429 }
415- } catch ( error ) {
416- console . error ( 'An error occurred:' ) ;
417- console . error ( error . message ) ;
418- process . exit ( 1 ) ;
419430}
431+
432+ // Run the main function
433+ main ( ) ;
0 commit comments