Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preload PR coverage report #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/content/github/common/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export async function getPRReport(url: any) {
owner: url.owner,
repo: url.repo,
pullid: url.id,
isDiff: url.isDiff,
};

const response = await browser.runtime.sendMessage({
Expand All @@ -114,5 +115,6 @@ export async function getPRReport(url: any) {
referrer: window.location.href,
});

return response.data;
// does not await response when called with isDiff: false
return response?.data || {};
}
10 changes: 9 additions & 1 deletion src/content/github/pr/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ async function execute() {
return;
}

if (!urlMetadata.isDiff) {
// if not on diff view, dispatch API request
// promise will be resolved when needed
await getPRReport(urlMetadata);
return;
}

createContainer();

const coverageReport = await getPRReport(urlMetadata);
Expand Down Expand Up @@ -64,7 +71,8 @@ function createContainer() {
}

function getMetadataFromURL(): { [key: string]: string } | null {
const regexp = /\/(?<owner>.+?)\/(?<repo>.+?)\/pull\/(?<id>\d+?)\/files/;
const regexp =
/\/(?<owner>.+?)\/(?<repo>.+?)\/pull\/(?<id>\d+)(\/(?<isDiff>files))?/;
const matches = regexp.exec(window.location.pathname);
const groups = matches?.groups;
if (!groups) {
Expand Down
27 changes: 18 additions & 9 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
export class Codecov {
static baseUrl = "https://api.codecov.io";
static checkAuthPath = "/api/v2/github/codecov";
static cache: { [pullUrl: string]: any } = {};

static _init() {
fetchIntercept.register({
Expand Down Expand Up @@ -104,7 +105,7 @@ export class Codecov {
}

static async fetchPRComparison(payload: any, referrer: string): Promise<any> {
const { service, owner, repo, pullid } = payload;
const { service, owner, repo, pullid, isDiff } = payload;

const url = new URL(
`/api/v2/${service}/${owner}/repos/${repo}/compare`,
Expand All @@ -113,16 +114,24 @@ export class Codecov {
const params = { pullid };
url.search = new URLSearchParams(params).toString();

const response = await fetch(url.toString(), {
headers: {
Referrer: referrer,
},
});
const data = await response.json();

let response = await Promise.resolve(this.cache[url.toString()]);
if (!response?.ok) {
this.cache[url.toString()] = fetch(url.toString(), {
headers: {
Referrer: referrer,
},
});
}

if (!isDiff) {
return;
}

// await promise immediately if requested from diff view
response = await Promise.resolve(this.cache[url.toString()]);
return {
ok: response.ok,
data,
data: await response.clone().json(),
};
}

Expand Down