diff --git a/src/api.ts b/src/api.ts index 0cbcf12..4cd5df5 100644 --- a/src/api.ts +++ b/src/api.ts @@ -2,26 +2,11 @@ import browser from "webextension-polyfill"; export async function readCoverageData( path: string, + reference: string, ): Promise { const pathParts = window.location.pathname.split("/"); const [workspace, project] = pathParts.slice(1, 3); - let commitShaIndex = pathParts.indexOf("pull"); - let reference: string | null = null; - if (commitShaIndex >= 0) { - reference = `refs/pull/${pathParts[commitShaIndex + 1]}`; - } else { - commitShaIndex = pathParts.indexOf("commit"); - if (commitShaIndex >= 0) { - reference = pathParts[commitShaIndex + 1]; - } - } - - if (!reference) { - console.error("[qlty] No commit SHA found in the URL."); - return null; - } - try { const response = await browser.runtime.sendMessage({ command: "getFileCoverage", diff --git a/src/github.ts b/src/github.ts index 2fae66f..b05e915 100644 --- a/src/github.ts +++ b/src/github.ts @@ -31,12 +31,57 @@ export function tryInjectDiffUI(): void { } } +function getRef() { + const commitSha = getPullRequestCommitSha(); + if (commitSha) { + return commitSha; + } + + const ref = getRefFromUrl(); + if (ref) { + return ref; + } + + return null; +} + +function getPullRequestCommitSha() { + // Retrieve the SHA from the "changes from ..." picker + const finalCommitElement = document.querySelector("#files_bucket details-menu [data-commit]:last-child"); + + return finalCommitElement?.getAttribute('data-commit') ?? null; +} + +function getRefFromUrl() { + const pathParts = window.location.pathname.split("/"); + + let commitShaIndex = pathParts.indexOf("pull"); + let reference: string | null = null; + if (commitShaIndex >= 0) { + reference = `refs/pull/${pathParts[commitShaIndex + 1]}`; + } else { + commitShaIndex = pathParts.indexOf("commit"); + if (commitShaIndex >= 0) { + reference = pathParts[commitShaIndex + 1]; + } + } + + return reference; +} + async function loadCoverageForPath(path: string): Promise { path = normalizePath(path); let cached = coverageData.get(path); if (cached) return cached; - const data = await readCoverageData(path); + const ref = getRef(); + if (!ref) { + console.warn("[qlty] No reference found for coverage data"); + return null; + } + + const data = await readCoverageData(path, ref); + const coverage = data?.files.find( (file) => normalizePath(file.path) === path, );