From a7e8b97e20f32154bf5bc0fedca2df9d060ccede Mon Sep 17 00:00:00 2001 From: Anukul Sangwan Date: Wed, 27 Sep 2023 23:51:22 +0400 Subject: [PATCH 1/2] preload PR coverage report --- src/content/github/common/fetchers.ts | 4 +++- src/content/github/pr/main.tsx | 10 +++++++++- src/service.ts | 27 ++++++++++++++++++--------- 3 files changed, 30 insertions(+), 11 deletions(-) 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..d8bebab 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 + 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(), }; } From e636e09b7ab5b744b8c965340e67fb111afa6099 Mon Sep 17 00:00:00 2001 From: Anukul Sangwan Date: Thu, 28 Sep 2023 01:25:05 +0400 Subject: [PATCH 2/2] nit --- src/content/github/pr/main.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/github/pr/main.tsx b/src/content/github/pr/main.tsx index d8bebab..06d4212 100644 --- a/src/content/github/pr/main.tsx +++ b/src/content/github/pr/main.tsx @@ -35,7 +35,7 @@ async function execute() { if (!urlMetadata.isDiff) { // if not on diff view, dispatch API request // promise will be resolved when needed - getPRReport(urlMetadata); + await getPRReport(urlMetadata); return; }