Skip to content
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
17 changes: 1 addition & 16 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@ import browser from "webextension-polyfill";

export async function readCoverageData(
path: string,
reference: string,
): Promise<GetFileCoverageResponse | null> {
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",
Expand Down
47 changes: 46 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileCoverage | null> {
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,
);
Expand Down