@@ -21,12 +21,13 @@ const id = process.env.GITHUB_APP_IDENTIFIER;
2121const secret = process . env . GITHUB_WEBHOOK_SECRET ;
2222
2323// Configure ssh tunnel
24- const cmd = 'ssh -tt -R 80:localhost:3000 serveo.net' ;
25- const sh = String ( cp . execFileSync ( 'where' , [ 'git' ] ) ) . replace ( ' cmd\\git.exe' , 'bin\\sh.exe' ) ;
24+ const cmd = 'ssh -tt -R gladius: 80:localhost:3000 serveo.net' ;
25+ const sh = String ( cp . execFileSync ( 'where' , [ 'git' ] ) ) . replace ( / c m d \\ g i t .e x e \s * / gi , 'bin\\sh.exe' ) ;
2626const tunnel = ( ) => {
2727 let ssh = cp . spawn ( sh , [ '-c' , cmd ] )
2828 ssh . stdout . on ( 'data' , ( data ) => { console . log ( `stdout: ${ data } ` ) ; } ) ;
2929 ssh . on ( 'exit' , ( ) => { console . log ( 'Reconnecting to Serveo' ) ; tunnel ( ) ; } ) ;
30+ ssh . on ( 'error' , ( e ) => { console . error ( e ) } ) ;
3031}
3132
3233// Create handler to verify posts signed with webhook secret. Content type must be application/json
@@ -65,7 +66,7 @@ srv.post('/github', async (req, res, next) => {
6566 const installationId = data . id ;
6667 installationAccessToken = await app . getInstallationAccessToken ( { installationId } ) ;
6768 handler ( req , res , ( ) => res . end ( 'ok' ) )
68- next ( ) ;
69+ // next();
6970 } catch ( error ) {
7071 next ( error ) ;
7172 }
@@ -89,31 +90,31 @@ srv.get('/github/:id', function (req, res) {
8990} ) ;
9091
9192// Serve the coverage results
92- srv . get ( '/coverage/:repo/:branch' , function ( req , res ) {
93+ srv . get ( '/coverage/:repo/:branch' , async ( req , res ) => {
9394 // Find head commit of branch
9495 try {
95- const result = await request ( 'GET /repos/:owner/:repo/git/refs/heads/:branch' , {
96+ const { data } = await request ( 'GET /repos/:owner/:repo/git/refs/heads/:branch' , {
9697 owner : 'cortex-lab' , // @todo Generalize repo owner
9798 repo : req . params . repo ,
9899 branch : req . params . branch
99100 } ) ;
100- if result . data . ref . endsWith ( '/' + req . params . branch ) {
101+ if ( data . ref . endsWith ( '/' + req . params . branch ) ) {
101102 console . log ( 'Request for ' + req . params . branch + ' coverage' )
102- let id = result . data . object . sha ;
103+ let id = data . object . sha ;
103104 var report = { 'schemaVersion' : 1 , 'label' : 'coverage' } ;
104105 try { // Try to load coverage record
105106 record = await loadTestRecords ( id ) ;
106107 if ( typeof record == 'undefined' || record [ 'coverage' ] == [ ] ) { throw 404 } ; // Test not found for commit
107108 if ( record [ 'status' ] === 'error' ) { throw 500 } ; // Test found for commit but errored
108- report [ 'message' ] = record [ 'coverage' ] + '%' ;
109+ report [ 'message' ] = Math . round ( record [ 'coverage' ] * 100 ) / 100 + '%' ;
109110 report [ 'color' ] = ( record [ 'coverage' ] > 75 ? 'green' : 'red' ) ;
110111 } catch ( err ) { // No coverage value
111112 report [ 'message' ] = ( err === 404 ? 'pending' : 'unknown' ) ;
112113 report [ 'color' ] = 'orange' ;
113114 // Check test isn't already on the pile
114115 let onPile = false ;
115- for ( let job of queue . pile ) { if job . id === id { onPile = true ; break ; } } ;
116- if ! onPile { // Add test to queue
116+ for ( let job of queue . pile ) { if ( job . id === id ) { onPile = true ; break ; } } ;
117+ if ( ! onPile ) { // Add test to queue
117118 queue . add ( {
118119 sha : id ,
119120 owner : 'cortex-lab' , // @todo Generalize repo owner
@@ -123,12 +124,10 @@ srv.get('/coverage/:repo/:branch', function (req, res) {
123124 }
124125 } finally { // Send report
125126 res . setHeader ( 'Content-Type' , 'application/json' ) ;
126- console . log ( report )
127127 res . end ( JSON . stringify ( report ) ) ; }
128- }
129128 } else { throw 404 } ; // Specified repo or branch not found
130- catch ( error ) {
131- let msg = ( error === 404 ? `${ req . params . repo } /${ req . params . branch } not found` : error ) ;
129+ } catch ( error ) {
130+ let msg = ( error === 404 ? `${ req . params . repo } /${ req . params . branch } not found` : error ) ; // @fixme error thrown by request not 404
132131 console . error ( msg )
133132 res . statusCode = 401 ; // If not found, send 401 for security reasons
134133 res . send ( msg ) ;
@@ -210,18 +209,18 @@ queue.on('finish', job => { // On job end post result to API
210209 * @todo Save full coverage object for future inspection
211210 */
212211queue . on ( 'complete' , job => { // On job end post result to API
213- console . log ( 'Updating coverage for ' + job . id )
212+ console . log ( 'Updating coverage for job # ' + job . id )
214213 Coverage ( './CoverageResults.xml' , job . data . repo , job . data . id , obj => {
215214 // Digest and save percentage coverage
216215 let misses = 0 , hits = 0 ;
217- for ( let file of job . source_files ) {
218- misses += file . coverage . filter ( x => x == 0 ) . length ;
216+ for ( let file of obj . source_files ) {
217+ misses += file . coverage . filter ( x => x === 0 ) . length ;
219218 hits += file . coverage . filter ( x => x > 0 ) . length ;
220219 }
221220 // Load data and save
222221 let records = JSON . parse ( fs . readFileSync ( './db.json' , 'utf8' ) ) ;
223222 if ( ! Array . isArray ( records ) ) records = [ records ] ; // Ensure array
224- for ( let o of records ) { if o . commit === job . data . id { o . coverage = hits / ( hits + misses ) } ; break ; } // Add percentage
223+ for ( let o of records ) { if ( o . commit === job . data . id ) { o . coverage = hits / ( hits + misses ) * 100 } ; break ; } // Add percentage
225224 // Save object
226225 fs . writeFile ( './db.json' , JSON . stringify ( records ) , function ( err ) {
227226 if ( err ) { console . log ( err ) ; }
0 commit comments