@@ -84,7 +84,7 @@ function loadTestRecords(id) {
8484
8585// Serve the test results for requested commit id
8686srv . get ( '/github/:id' , function ( req , res ) {
87- console . log ( req . params . id )
87+ console . log ( 'Request for test results for commit ' + req . params . id . substring ( 0 , 6 ) )
8888 const record = loadTestRecords ( req . params . id ) ;
8989 res . send ( record [ 'results' ] ) ;
9090} ) ;
@@ -134,6 +134,50 @@ srv.get('/coverage/:repo/:branch', async (req, res) => {
134134 }
135135} ) ;
136136
137+ // Serve the build status
138+ srv . get ( '/status/:repo/:branch' , async ( req , res ) => {
139+ // Find head commit of branch
140+ try {
141+ const { data } = await request ( 'GET /repos/:owner/:repo/git/refs/heads/:branch' , {
142+ owner : 'cortex-lab' , // @todo Generalize repo owner
143+ repo : req . params . repo ,
144+ branch : req . params . branch
145+ } ) ;
146+ if ( data . ref . endsWith ( '/' + req . params . branch ) ) {
147+ console . log ( 'Request for ' + req . params . branch + ' build status' )
148+ let id = data . object . sha ;
149+ var report = { 'schemaVersion' : 1 , 'label' : 'build' } ;
150+ try { // Try to load coverage record
151+ record = await loadTestRecords ( id ) ;
152+ if ( typeof record == 'undefined' || record [ 'status' ] == '' ) { throw 404 } ; // Test not found for commit
153+ report [ 'message' ] = ( record [ 'status' ] === 'success' ? 'passing' : 'failing' ) ;
154+ report [ 'color' ] = ( record [ 'status' ] === 'success' ? 'green' : 'red' ) ;
155+ } catch ( err ) { // No coverage value
156+ report [ 'message' ] = ( err === 404 ? 'pending' : 'unknown' ) ;
157+ report [ 'color' ] = 'orange' ;
158+ // Check test isn't already on the pile
159+ let onPile = false ;
160+ for ( let job of queue . pile ) { if ( job . id === id ) { onPile = true ; break ; } } ;
161+ if ( ! onPile ) { // Add test to queue
162+ queue . add ( {
163+ sha : id ,
164+ owner : 'cortex-lab' , // @todo Generalize repo owner
165+ repo : req . params . repo ,
166+ status : '' ,
167+ context : '' } ) ;
168+ }
169+ } finally { // Send report
170+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
171+ res . end ( JSON . stringify ( report ) ) ; }
172+ } else { throw 404 } ; // Specified repo or branch not found
173+ } catch ( error ) {
174+ let msg = ( error === 404 ? `${ req . params . repo } /${ req . params . branch } not found` : error ) ; // @fixme error thrown by request not 404
175+ console . error ( msg )
176+ res . statusCode = 401 ; // If not found, send 401 for security reasons
177+ res . send ( msg ) ;
178+ }
179+ } ) ;
180+
137181// Define how to process tests. Here we checkout git and call MATLAB
138182queue . process ( async ( job , done ) => {
139183 // job.data contains the custom data passed when the job was created
0 commit comments