@@ -117,6 +117,45 @@ srv.post('/github', async (req, res, next) => {
117117
118118///////////////////// STATUS DETAILS /////////////////////
119119
120+ /**
121+ * Serve the test records for requested commit id. Returns JSON data for the commit.
122+ * @param {string } id - A commit SHA of any length, or branch name.
123+ * @param {boolean|null } [isBranch] - If true, id treated as a branch name. Inferred from id by default.
124+ * @param {string } [module] - (Sub)module name. REPO_NAME by default.
125+ * @return {Promise } - Resolved to full commit SHA.
126+ */
127+ function fetchCommit ( id , isBranch = null , module ) {
128+ isBranch = isBranch === null ? ! lib . isSHA ( id ) : isBranch
129+ const data = {
130+ owner : process . env [ 'REPO_OWNER' ] ,
131+ repo : module || process . env . REPO_NAME ,
132+ id : id
133+ } ;
134+ let endpoint = `GET /repos/:owner/:repo/${ isBranch ? 'branches' : 'commits' } /:id` ;
135+ return request ( endpoint , data ) . then ( response => {
136+ return isBranch ? response . data . commit . sha : response . data . sha ;
137+ } ) ;
138+ }
139+
140+ /**
141+ * Parse the short SHA or branch name and redirect to static reports directory.
142+ */
143+ srv . get ( `/coverage/:id` , ( req , res ) => {
144+ let id = lib . shortID ( req . params . id ) ;
145+ let isSHA = ( req . query . branch || ! lib . isSHA ( req . params . id ) ) === false ;
146+ console . log ( 'Request for test coverage for ' + ( isSHA ? `commit ${ id } ` : `branch ${ req . params . id } ` ) ) ;
147+ fetchCommit ( req . params . id , ! isSHA , req . query . module )
148+ . then ( id => {
149+ log ( 'Commit ID found: %s' , id ) ;
150+ res . redirect ( 301 , `/${ ENDPOINT } /coverage/${ id } ` ) ;
151+ } )
152+ . catch ( err => {
153+ log ( '%s' , err . message ) ;
154+ res . statusCode = 404 ;
155+ res . send ( `Coverage for ${ isSHA ? 'commit' : 'branch' } ${ req . params . id } not found` ) ;
156+ } ) ;
157+ } )
158+
120159/**
121160 * Serve the reports tree as a static resource; allows users to inspect the HTML coverage reports.
122161 * We will add a link to the reports in the check details.
@@ -130,23 +169,16 @@ srv.get(`/${ENDPOINT}/records/:id`, function (req, res) {
130169 let id = lib . shortID ( req . params . id ) ;
131170 let isSHA = ( req . query . branch || ! lib . isSHA ( req . params . id ) ) === false ;
132171 console . log ( 'Request for test records for ' + ( isSHA ? `commit ${ id } ` : `branch ${ req . params . id } ` ) ) ;
133- const data = {
134- owner : process . env [ 'REPO_OWNER' ] ,
135- repo : req . query . module || process . env . REPO_NAME ,
136- id : req . params . id
137- } ;
138- let endpoint = `GET /repos/:owner/:repo/${ isSHA ? 'commits' : 'branches' } /:id` ;
139- request ( endpoint , data )
140- . then ( response => {
141- let id = isSHA ? response . data . sha : response . data . commit . sha ;
172+ fetchCommit ( req . params . id , ! isSHA , req . query . module )
173+ . then ( id => {
142174 log ( 'Commit ID found: %s' , id ) ;
143175 let record = lib . loadTestRecords ( id ) ;
144176 if ( record ) {
145177 res . setHeader ( 'Content-Type' , 'application/json' ) ;
146178 res . end ( JSON . stringify ( record ) ) ;
147179 } else {
148180 res . statusCode = 404 ;
149- res . send ( `${ isSHA ? 'Commit' : 'Branch' } ${ req . params . id } not recognized.` ) ;
181+ res . send ( `${ isSHA ? 'Commit' : 'Branch' } ${ id } not recognized.` ) ;
150182 }
151183 } )
152184 . catch ( err => {
@@ -163,27 +195,35 @@ srv.get(`/${ENDPOINT}/records/:id`, function (req, res) {
163195 */
164196srv . get ( `/${ ENDPOINT } /:id` , function ( req , res ) {
165197 let id = lib . shortID ( req . params . id ) ;
166- let isSHA = lib . isSHA ( req . params . id ) ;
167198 let log_only = ( req . query . type || '' ) . startsWith ( 'log' )
199+ let isSHA = ( req . query . branch || ! lib . isSHA ( req . params . id ) ) === false ;
168200 console . log (
169201 `Request for test ${ log_only ? 'log' : 'stdout' } for ` +
170202 ( isSHA ? `commit ${ id } ` : `branch ${ req . params . id } ` )
171203 ) ;
172- let filename = log_only ? `test_output.log` : `std_output-${ id } .log` ;
173- let logFile = path . join ( config . dataPath , 'reports' , req . params . id , filename ) ;
174- fs . readFile ( logFile , 'utf8' , ( err , data ) => {
175- if ( err ) {
204+ fetchCommit ( req . params . id , ! isSHA , req . query . module )
205+ . then ( id => {
206+ let filename = log_only ? `test_output.log` : `std_output-${ lib . shortID ( req . params . id ) } .log` ;
207+ let logFile = path . join ( config . dataPath , 'reports' , id , filename ) ;
208+ fs . readFile ( logFile , 'utf8' , ( err , data ) => {
209+ if ( err ) {
210+ log ( '%s' , err . message ) ;
211+ res . statusCode = 404 ;
212+ res . send ( `Record for ${ isSHA ? 'commit' : 'branch' } ${ id } not found` ) ;
213+ } else {
214+ res . statusCode = 200 ;
215+ // Wrap in HTML tags so that the formatting is a little nicer.
216+ let preText = '<html lang="en-GB"><body><pre>' ;
217+ let postText = '</pre></body></html>' ;
218+ res . send ( preText + data + postText ) ;
219+ }
220+ } ) ;
221+ } )
222+ . catch ( err => {
176223 log ( '%s' , err . message ) ;
177224 res . statusCode = 404 ;
178225 res . send ( `Record for ${ isSHA ? 'commit' : 'branch' } ${ req . params . id } not found` ) ;
179- } else {
180- res . statusCode = 200 ;
181- // Wrap in HTML tags so that the formatting is a little nicer.
182- let preText = '<html lang="en-GB"><body><pre>' ;
183- let postText = '</pre></body></html>' ;
184- res . send ( preText + data + postText ) ;
185- }
186- } ) ;
226+ } ) ;
187227} ) ;
188228
189229
@@ -367,15 +407,15 @@ async function updateStatus(data, targetURL = '') {
367407 await setAccessToken ( ) ;
368408 return request ( "POST /repos/:owner/:repo/statuses/:sha" , {
369409 owner : data [ 'owner' ] || process . env [ 'REPO_OWNER' ] ,
370- repo : data [ 'repo' ] ,
410+ repo : data [ 'repo' ] || process . env [ 'REPO_NAME' ] ,
371411 headers : {
372412 authorization : `token ${ token [ 'token' ] } ` ,
373413 accept : "application/vnd.github.machine-man-preview+json"
374414 } ,
375415 sha : data [ 'sha' ] ,
376416 state : data [ 'status' ] ,
377417 target_url : targetURL ,
378- description : data [ 'description' ] . substring ( 0 , maxN ) ,
418+ description : ( data [ 'description' ] || '' ) . substring ( 0 , maxN ) ,
379419 context : data [ 'context' ]
380420 } ) ;
381421}
@@ -547,5 +587,5 @@ queue.on('finish', (err, job) => { // On job end post result to API
547587} ) ;
548588
549589module . exports = {
550- updateStatus, srv, handler, setAccessToken, prepareEnv, runTests, eventCallback
590+ updateStatus, srv, handler, setAccessToken, prepareEnv, runTests, eventCallback, fetchCommit
551591}
0 commit comments