diff --git a/src/content/github/common/fetchers.ts b/src/content/github/common/fetchers.ts index 1759efa..e8f3983 100644 --- a/src/content/github/common/fetchers.ts +++ b/src/content/github/common/fetchers.ts @@ -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({ @@ -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 || {}; } diff --git a/src/content/github/pr/main.tsx b/src/content/github/pr/main.tsx index c80f16c..06d4212 100644 --- a/src/content/github/pr/main.tsx +++ b/src/content/github/pr/main.tsx @@ -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); @@ -64,7 +71,8 @@ function createContainer() { } function getMetadataFromURL(): { [key: string]: string } | null { - const regexp = /\/(?.+?)\/(?.+?)\/pull\/(?\d+?)\/files/; + const regexp = + /\/(?.+?)\/(?.+?)\/pull\/(?\d+)(\/(?files))?/; const matches = regexp.exec(window.location.pathname); const groups = matches?.groups; if (!groups) { diff --git a/src/service.ts b/src/service.ts index 594f516..902099e 100644 --- a/src/service.ts +++ b/src/service.ts @@ -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({ @@ -104,7 +105,7 @@ export class Codecov { } static async fetchPRComparison(payload: any, referrer: string): Promise { - const { service, owner, repo, pullid } = payload; + const { service, owner, repo, pullid, isDiff } = payload; const url = new URL( `/api/v2/${service}/${owner}/repos/${repo}/compare`, @@ -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(), }; }