Skip to content

Commit 33dfa46

Browse files
Fix broken file coverage due to unexpected HTML response from GitHub
2 parents 0a395bf + 9cde8e2 commit 33dfa46

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/content/github/common/fetchers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
} from "src/types";
77

88
export 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;

src/content/github/common/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 = /\/[^\/]+\/[^\/]+\/pull\/\d+\/files.*/
7+
8+
// And this one matches file view pages - which look like:
9+
// /codecov/gazebo/blob/main/src/App.jsx
10+
const fileUrlRegex = /\/[^\/]+\/[^\/]+\/blob\/[^\/]+\/.*/
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+
}

src/content/github/file/main.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
FileCoverageReport,
1313
FileCoverageReportResponse,
1414
FileMetadata,
15-
MessageType,
1615
} from "src/types";
1716
import {
1817
componentsStorageKey,
@@ -34,6 +33,7 @@ import {
3433
getBranchReport,
3534
} from "../common/fetchers";
3635
import { print } from "src/utils";
36+
import { isFileUrl } from "../common/utils";
3737

3838
const globals: {
3939
coverageReport?: FileCoverageReport;
@@ -60,15 +60,14 @@ function init(): Promise<void> {
6060
}
6161

6262
async 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) => {

src/content/github/pr/main.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from "lodash";
44

55
import "src/basscss.css";
66
import { displayChange } from "src/utils";
7-
import { CoverageStatus, MessageType, PullCoverageReport } from "src/types";
7+
import { CoverageStatus, PullCoverageReport } from "src/types";
88
import {
99
animateAndAnnotateLines,
1010
clearAnimation,
@@ -14,6 +14,7 @@ import { lineSelector } from "./constants";
1414
import { colors } from "../common/constants";
1515
import { print } from "src/utils";
1616
import { getPRReport } from "../common/fetchers";
17+
import { isPrUrl } from "../common/utils";
1718

1819
const globals: {
1920
coverageReport?: PullCoverageReport;
@@ -26,7 +27,13 @@ async function main() {
2627
}
2728

2829
async 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;

0 commit comments

Comments
 (0)