File tree Expand file tree Collapse file tree 4 files changed +36
-8
lines changed
Expand file tree Collapse file tree 4 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,11 @@ import {
66} from "src/types" ;
77
88export async function getMetadata ( url : string ) : Promise < FileMetadata > {
9- const response = await fetch ( url ) . then ( ( response ) => response . json ( ) ) ;
9+ const response = await fetch ( url , {
10+ headers : {
11+ "Accept" : "application/json" ,
12+ } ,
13+ } ) . then ( ( response ) => response . json ( ) ) ;
1014 let branch = undefined ;
1115 if ( response . payload . refInfo . refType === "branch" ) {
1216 branch = response . payload . refInfo . name ;
Original file line number Diff line number Diff line change 1+ // Note that we're guaranteed to be on a github page due to manifest.json,
2+ // so these checks don't need to include that.
3+
4+ // This one matches PR files pages. Something like:
5+ // /codecov/gazebo/pull/2435/files
6+ const prUrlRegex = / \/ [ ^ \/ ] + \/ [ ^ \/ ] + \/ p u l l \/ \d + \/ f i l e s .* /
7+
8+ // And this one matches file view pages - which look like:
9+ // /codecov/gazebo/blob/main/src/App.jsx
10+ const fileUrlRegex = / \/ [ ^ \/ ] + \/ [ ^ \/ ] + \/ b l o b \/ [ ^ \/ ] + \/ .* /
11+
12+ export function isFileUrl ( url : string ) : boolean {
13+ return fileUrlRegex . test ( url ) ;
14+ }
15+
16+ export function isPrUrl ( url : string ) : boolean {
17+ return prUrlRegex . test ( url ) ;
18+ }
Original file line number Diff line number Diff line change @@ -12,7 +12,6 @@ import {
1212 FileCoverageReport ,
1313 FileCoverageReportResponse ,
1414 FileMetadata ,
15- MessageType ,
1615} from "src/types" ;
1716import {
1817 componentsStorageKey ,
@@ -34,6 +33,7 @@ import {
3433 getBranchReport ,
3534} from "../common/fetchers" ;
3635import { print } from "src/utils" ;
36+ import { isFileUrl } from "../common/utils" ;
3737
3838const globals : {
3939 coverageReport ?: FileCoverageReport ;
@@ -60,15 +60,14 @@ function init(): Promise<void> {
6060}
6161
6262async function main ( ) : Promise < void > {
63- let metadata : FileMetadata ;
64-
65- try {
66- metadata = await getMetadata ( document . URL ) ;
67- } catch ( e ) {
63+ if ( ! isFileUrl ( document . URL ) ) {
6864 print ( "file not detected at current URL" ) ;
6965 return ;
7066 }
7167
68+ let metadata : FileMetadata ;
69+ metadata = await getMetadata ( document . URL ) ;
70+
7271 globals . coverageButton = createCoverageButton ( ) ;
7372
7473 process ( metadata ) . catch ( ( e ) => {
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import _ from "lodash";
44
55import "src/basscss.css" ;
66import { displayChange } from "src/utils" ;
7- import { CoverageStatus , MessageType , PullCoverageReport } from "src/types" ;
7+ import { CoverageStatus , PullCoverageReport } from "src/types" ;
88import {
99 animateAndAnnotateLines ,
1010 clearAnimation ,
@@ -14,6 +14,7 @@ import { lineSelector } from "./constants";
1414import { colors } from "../common/constants" ;
1515import { print } from "src/utils" ;
1616import { getPRReport } from "../common/fetchers" ;
17+ import { isPrUrl } from "../common/utils" ;
1718
1819const globals : {
1920 coverageReport ?: PullCoverageReport ;
@@ -26,7 +27,13 @@ async function main() {
2627}
2728
2829async function execute ( ) {
30+ if ( ! isPrUrl ( document . URL ) ) {
31+ print ( "PR not detected at current URL" ) ;
32+ return ;
33+ }
34+
2935 const urlMetadata = getMetadataFromURL ( ) ;
36+
3037 if ( ! urlMetadata ) {
3138 print ( "PR not detected at current URL" ) ;
3239 return ;
You can’t perform that action at this time.
0 commit comments