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 ) ;
6
11
7
12
/**
8
13
* Simple CSV line parser that handles quoted fields
@@ -187,7 +192,7 @@ const generateJacocoReport = (csvFilePath, outputPath) => {
187
192
// Generate markdown content
188
193
const markdownContent = `# JaCoCo Code Coverage Report
189
194
190
- ## Package Coverage
195
+ ## Coverage Report
191
196
192
197
${ generateMarkdownTable ( packageReport , {
193
198
'package' : 'Package' ,
@@ -196,14 +201,17 @@ ${generateMarkdownTable(packageReport, {
196
201
'lines total' : 'Total Lines'
197
202
} ) }
198
203
199
- ## Class Coverage
200
204
205
+ <details>
206
+ <summary>Class Coverage</summary>
201
207
${ generateMarkdownTable ( classReport , {
202
208
'class' : 'Class' ,
203
209
'coverage' : 'Coverage' ,
204
210
'lines covered' : 'Lines Covered' ,
205
211
'lines total' : 'Total Lines'
206
212
} ) }
213
+
214
+ </details>
207
215
` ;
208
216
209
217
// Write to file
@@ -214,6 +222,7 @@ ${generateMarkdownTable(classReport, {
214
222
} ) ;
215
223
} ;
216
224
225
+
217
226
/**
218
227
* Posts or updates a comment on a GitHub PR
219
228
* @param {string } token - GitHub token with repo:write permission
@@ -378,42 +387,47 @@ if (!repo) {
378
387
379
388
const [ owner , repository ] = repo . split ( '/' ) ;
380
389
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
+ }
413
424
}
425
+ } catch ( error ) {
426
+ console . error ( 'An error occurred:' ) ;
427
+ console . error ( error . message ) ;
428
+ process . exit ( 1 ) ;
414
429
}
415
- } catch ( error ) {
416
- console . error ( 'An error occurred:' ) ;
417
- console . error ( error . message ) ;
418
- process . exit ( 1 ) ;
419
430
}
431
+
432
+ // Run the main function
433
+ main ( ) ;
0 commit comments