Skip to content

Commit a47ba72

Browse files
committed
Retrieve the commit SHA from the details-menu element for pull requests, as refs/pull/{number} does not yield results from the qlty.sh API
1 parent 2508991 commit a47ba72

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/api.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,11 @@ import browser from "webextension-polyfill";
22

33
export async function readCoverageData(
44
path: string,
5+
reference: string,
56
): Promise<GetFileCoverageResponse | null> {
67
const pathParts = window.location.pathname.split("/");
78
const [workspace, project] = pathParts.slice(1, 3);
89

9-
let commitShaIndex = pathParts.indexOf("pull");
10-
let reference: string | null = null;
11-
if (commitShaIndex >= 0) {
12-
reference = `refs/pull/${pathParts[commitShaIndex + 1]}`;
13-
} else {
14-
commitShaIndex = pathParts.indexOf("commit");
15-
if (commitShaIndex >= 0) {
16-
reference = pathParts[commitShaIndex + 1];
17-
}
18-
}
19-
20-
if (!reference) {
21-
console.error("[qlty] No commit SHA found in the URL.");
22-
return null;
23-
}
24-
2510
try {
2611
const response = await browser.runtime.sendMessage({
2712
command: "getFileCoverage",

src/github.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,57 @@ export function tryInjectDiffUI(): void {
3131
}
3232
}
3333

34+
function getRef() {
35+
const commitSha = getPullRequestCommitSha();
36+
if (commitSha) {
37+
return commitSha;
38+
}
39+
40+
const ref = getRefFromUrl();
41+
if (ref) {
42+
return ref;
43+
}
44+
45+
return null;
46+
}
47+
48+
function getPullRequestCommitSha() {
49+
// Retrieve the SHA from the "changes from ..." picker
50+
const finalCommitElement = document.querySelector("#files_bucket details-menu [data-commit]:last-child");
51+
52+
return finalCommitElement?.getAttribute('data-commit') ?? null;
53+
}
54+
55+
function getRefFromUrl() {
56+
const pathParts = window.location.pathname.split("/");
57+
58+
let commitShaIndex = pathParts.indexOf("pull");
59+
let reference: string | null = null;
60+
if (commitShaIndex >= 0) {
61+
reference = `refs/pull/${pathParts[commitShaIndex + 1]}`;
62+
} else {
63+
commitShaIndex = pathParts.indexOf("commit");
64+
if (commitShaIndex >= 0) {
65+
reference = pathParts[commitShaIndex + 1];
66+
}
67+
}
68+
69+
return reference;
70+
}
71+
3472
async function loadCoverageForPath(path: string): Promise<FileCoverage | null> {
3573
path = normalizePath(path);
3674
let cached = coverageData.get(path);
3775
if (cached) return cached;
3876

39-
const data = await readCoverageData(path);
77+
const ref = getRef();
78+
if (!ref) {
79+
console.warn("[qlty] No reference found for coverage data");
80+
return null;
81+
}
82+
83+
const data = await readCoverageData(path, ref);
84+
4085
const coverage = data?.files.find(
4186
(file) => normalizePath(file.path) === path,
4287
);

0 commit comments

Comments
 (0)