Skip to content

Commit 2f84357

Browse files
authored
Merge pull request #46 from codecov/show-prompt
show prompt for past commit report
2 parents f1c9086 + e21ca0b commit 2f84357

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/content/github/common/fetchers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import {
77

88
export async function getMetadata(url: string): Promise<FileMetadata> {
99
const response = await fetch(url).then((response) => response.json());
10+
let branch = undefined;
11+
if (response.payload.refInfo.refType === "branch") {
12+
branch = response.payload.refInfo.name;
13+
}
1014
return {
1115
owner: response.payload.repo.ownerLogin,
1216
repo: response.payload.repo.name,
1317
path: response.payload.path,
1418
commit: response.payload.refInfo.currentOid,
19+
branch: branch,
1520
};
1621
}
1722

@@ -75,6 +80,26 @@ export async function getCommitReport(
7580
return response.data;
7681
}
7782

83+
export async function getBranchReport(
84+
metadata: FileMetadata
85+
): Promise<FileCoverageReportResponse> {
86+
const payload = {
87+
service: "github",
88+
owner: metadata.owner,
89+
repo: metadata.repo,
90+
path: metadata.path,
91+
branch: metadata.branch,
92+
};
93+
94+
const response = await browser.runtime.sendMessage({
95+
type: MessageType.FETCH_COMMIT_REPORT,
96+
payload,
97+
referrer: window.location.href,
98+
});
99+
100+
return response.data;
101+
}
102+
78103
export async function getPRReport(url: any) {
79104
const payload = {
80105
service: "github",

src/content/github/file/main.tsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
getComponents,
3232
getCommitReport,
3333
getFlags,
34+
getBranchReport,
3435
} from "../common/fetchers";
3536
import { 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

4648
init().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 = /app.codecov.io\/github\/.*\/.*\/commit\/(?<commit>.*)\/blob/;
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+
234274
function 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

320361
function clearAnimationAndAnnotations() {
@@ -325,6 +366,6 @@ function clearAnimationAndAnnotations() {
325366
}
326367

327368
function clear() {
328-
clearButtons();
369+
clearElements();
329370
clearAnimationAndAnnotations();
330371
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type FileMetadata = {
33
repo: string;
44
path: string;
55
commit: string;
6+
branch: string | undefined;
67
};
78

89
export enum CoverageStatus {
@@ -15,6 +16,7 @@ export type FileCoverageReportResponse = {
1516
files?: Array<{
1617
line_coverage: Array<[number, CoverageStatus]>;
1718
}>;
19+
commit_file_url: string;
1820
};
1921

2022
export type FileCoverageReport = {

0 commit comments

Comments
 (0)