File tree Expand file tree Collapse file tree 2 files changed +46
-18
lines changed
Expand file tree Collapse file tree 2 files changed +46
-18
lines changed Original file line number Diff line number Diff line change 11export async function readCoverageData (
22 path : string ,
3+ reference : string ,
34) : Promise < GetFileCoverageResponse | null > {
45 const pathParts = window . location . pathname . split ( "/" ) ;
56 const [ workspace , project ] = pathParts . slice ( 1 , 3 ) ;
6-
7- let commitShaIndex = pathParts . indexOf ( "pull" ) ;
8- let reference : string | null = null ;
9- if ( commitShaIndex >= 0 ) {
10- reference = `refs/pull/${ pathParts [ commitShaIndex + 1 ] } ` ;
11- } else {
12- commitShaIndex = pathParts . indexOf ( "commit" ) ;
13- if ( commitShaIndex >= 0 ) {
14- reference = pathParts [ commitShaIndex + 1 ] ;
15- }
16- }
17-
18- if ( ! reference ) {
19- console . error ( "[qlty] No commit SHA found in the URL." ) ;
20- return null ;
21- }
22-
237 return new Promise ( ( resolve , reject ) => {
248 chrome . runtime . sendMessage (
259 {
Original file line number Diff line number Diff line change @@ -10,8 +10,52 @@ 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+ // Retrieve the SHA from the "changes from ..." picker
29+ const finalCommitElement = document . querySelector ( "#files_bucket details-menu [data-commit]:last-child" ) ;
30+
31+ return finalCommitElement ?. getAttribute ( 'data-commit' ) ?? null ;
32+ }
33+
34+ function getRefFromUrl ( ) {
35+ const pathParts = window . location . pathname . split ( "/" ) ;
36+
37+ let commitShaIndex = pathParts . indexOf ( "pull" ) ;
38+ let reference : string | null = null ;
39+ if ( commitShaIndex >= 0 ) {
40+ reference = `refs/pull/${ pathParts [ commitShaIndex + 1 ] } ` ;
41+ } else {
42+ commitShaIndex = pathParts . indexOf ( "commit" ) ;
43+ if ( commitShaIndex >= 0 ) {
44+ reference = pathParts [ commitShaIndex + 1 ] ;
45+ }
46+ }
47+
48+ return reference ;
49+ }
50+
1351async function loadCoverageForPath ( path : string ) : Promise < FileCoverage | null > {
14- const data = await readCoverageData ( path ) ;
52+ const ref = getRef ( ) ;
53+ if ( ! ref ) {
54+ console . warn ( "[qlty] No reference found for coverage data" ) ;
55+ return null ;
56+ }
57+
58+ const data = await readCoverageData ( path , ref ) ;
1559 const coverage = data ?. files . find (
1660 ( file ) => normalizePath ( file . path ) === normalizePath ( path ) ,
1761 ) ;
You can’t perform that action at this time.
0 commit comments