@@ -31,6 +31,7 @@ import {
3131 getComponents ,
3232 getCommitReport ,
3333 getFlags ,
34+ getBranchReport ,
3435} from "../common/fetchers" ;
3536import { print } from "src/utils" ;
3637
@@ -41,6 +42,7 @@ const globals: {
4142 flagsDrop ?: Drop ;
4243 componentsButton ?: HTMLElement ;
4344 componentsDrop ?: Drop ;
45+ prompt ?: HTMLElement ;
4446} = { } ;
4547
4648init ( ) . catch ( ( e ) => print ( "unexpected error" , e ) ) ;
@@ -221,6 +223,8 @@ async function process(metadata: FileMetadata): Promise<void> {
221223
222224 if ( _ . isEmpty ( coverageReport ) ) {
223225 updateButton ( `Coverage: N/A` ) ;
226+ globals . coverageReport = { } ;
227+ await promptPastReport ( metadata ) ;
224228 return ;
225229 }
226230
@@ -231,6 +235,42 @@ async function process(metadata: FileMetadata): Promise<void> {
231235 animateAndAnnotateLines ( lineSelector , annotateLine ) ;
232236}
233237
238+ async function promptPastReport ( metadata : FileMetadata ) : Promise < void > {
239+ if ( ! metadata . branch ) {
240+ return ;
241+ }
242+ const response = await getBranchReport ( metadata ) ;
243+ const regexp = / a p p .c o d e c o v .i o \/ g i t h u b \/ .* \/ .* \/ c o m m i t \/ (?< commit > .* ) \/ b l o b / ;
244+ const matches = regexp . exec ( response . commit_file_url ) ;
245+ const commit = matches ?. groups ?. commit ;
246+ if ( ! commit ) {
247+ print ( "could not parse commit hash from response for past coverage report" ) ;
248+ return ;
249+ }
250+ const link = document . URL . replace (
251+ `blob/${ metadata . branch } ` ,
252+ `blob/${ commit } `
253+ ) ;
254+ globals . prompt = createPrompt (
255+ < span >
256+ Coverage report not available for branch HEAD (
257+ { metadata . commit . substr ( 0 , 7 ) } ), most recent coverage report for this
258+ branch available at commit < a href = { link } > { commit . substr ( 0 , 7 ) } </ a >
259+ </ span >
260+ ) ;
261+ }
262+
263+ function createPrompt ( child : any ) {
264+ const ref = document . querySelector ( '[data-testid="latest-commit"]' )
265+ ?. parentElement ?. parentElement ;
266+ if ( ! ref ) {
267+ print ( "could not find reference element to render prompt" ) ;
268+ return ;
269+ }
270+ const prompt = < div className = "codecov-mb2 codecov-mx1" > { child } </ div > ;
271+ return ref . insertAdjacentElement ( "afterend" , prompt ) as HTMLElement ;
272+ }
273+
234274function createCoverageButton ( ) {
235275 const rawButton = document . querySelector ( '[data-testid="raw-button"]' ) ;
236276 if ( ! rawButton ) {
@@ -309,12 +349,13 @@ function annotateLine(line: HTMLElement) {
309349 }
310350}
311351
312- function clearButtons ( ) {
352+ function clearElements ( ) {
313353 globals . coverageButton ?. remove ( ) ;
314354 globals . flagsButton ?. remove ( ) ;
315355 globals . flagsDrop ?. remove ( ) ;
316356 globals . componentsButton ?. remove ( ) ;
317357 globals . componentsDrop ?. remove ( ) ;
358+ globals . prompt ?. remove ( ) ;
318359}
319360
320361function clearAnimationAndAnnotations ( ) {
@@ -325,6 +366,6 @@ function clearAnimationAndAnnotations() {
325366}
326367
327368function clear ( ) {
328- clearButtons ( ) ;
369+ clearElements ( ) ;
329370 clearAnimationAndAnnotations ( ) ;
330371}
0 commit comments