@@ -10,8 +10,66 @@ export function tryInjectDiffUI(): boolean {
1010 }
1111}
1212
13+ function getRef ( ) {
14+ const commitSha = getPullRequestCommitSha ( ) ;
15+ if ( commitSha ) {
16+ return commitSha ;
17+ }
18+
19+ const ref = getRefFromUrl ( ) ;
20+ if ( ref ) {
21+ return ref ;
22+ }
23+
24+ return null ;
25+ }
26+
27+ function getPullRequestCommitSha ( ) {
28+ const getRepoElement = document . querySelector ( "get-repo" ) ;
29+ if ( ! getRepoElement ) {
30+ return null ;
31+ }
32+
33+ const detailsMenu = getRepoElement . querySelector ( 'details-menu' ) ;
34+ if ( ! detailsMenu ) {
35+ return null ;
36+ }
37+
38+ // src="/{org}/{repo}/pull/{number}/open_with_menu?ref={sha}"
39+ const src = detailsMenu . getAttribute ( 'src' ) ;
40+ const match = src ?. match ( / r e f = ( [ a - z 0 - 9 ] + ) / ) ;
41+ if ( ! match || match . length < 2 ) {
42+ return null ;
43+ }
44+
45+ return match [ 1 ] ;
46+ }
47+
48+ function getRefFromUrl ( ) {
49+ const pathParts = window . location . pathname . split ( "/" ) ;
50+
51+ let commitShaIndex = pathParts . indexOf ( "pull" ) ;
52+ let reference : string | null = null ;
53+ if ( commitShaIndex >= 0 ) {
54+ reference = `refs/pull/${ pathParts [ commitShaIndex + 1 ] } ` ;
55+ } else {
56+ commitShaIndex = pathParts . indexOf ( "commit" ) ;
57+ if ( commitShaIndex >= 0 ) {
58+ reference = pathParts [ commitShaIndex + 1 ] ;
59+ }
60+ }
61+
62+ return reference ;
63+ }
64+
1365async function loadCoverageForPath ( path : string ) : Promise < FileCoverage | null > {
14- const data = await readCoverageData ( path ) ;
66+ const ref = getRef ( ) ;
67+ if ( ! ref ) {
68+ console . warn ( "[qlty] No reference found for coverage data" ) ;
69+ return null ;
70+ }
71+
72+ const data = await readCoverageData ( path , ref ) ;
1573 const coverage = data ?. files . find (
1674 ( file ) => normalizePath ( file . path ) === normalizePath ( path ) ,
1775 ) ;
0 commit comments