Skip to content

Commit 27946e3

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 901ec91 commit 27946e3

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

src/api.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
export async function readCoverageData(
22
path: string,
3+
reference: string,
34
): Promise<GetFileCoverageResponse | null> {
45
const pathParts = window.location.pathname.split("/");
56
const [workspace, project] = pathParts.slice(1, 3);
6-
7-
let commitShaIndex = pathParts.indexOf("pull");
8-
let reference: string | null = null;
9-
if (commitShaIndex >= 0) {
10-
reference = `refs/pull/${pathParts[commitShaIndex + 1]}`;
11-
} else {
12-
commitShaIndex = pathParts.indexOf("commit");
13-
if (commitShaIndex >= 0) {
14-
reference = pathParts[commitShaIndex + 1];
15-
}
16-
}
17-
18-
if (!reference) {
19-
console.error("[qlty] No commit SHA found in the URL.");
20-
return null;
21-
}
22-
237
return new Promise((resolve, reject) => {
248
chrome.runtime.sendMessage(
259
{

src/github.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,66 @@ export function tryInjectDiffUI(): boolean {
1010
}
1111
}
1212

13+
function getRef() {
14+
const commitSha = getPullRequestCommitSha();
15+
if (commitSha) {
16+
return commitSha;
17+
}
18+
19+
const ref = getRefFromUrl();
20+
if (ref) {
21+
return ref;
22+
}
23+
24+
return null;
25+
}
26+
27+
function getPullRequestCommitSha() {
28+
const getRepoElement = document.querySelector("get-repo");
29+
if (!getRepoElement) {
30+
return null;
31+
}
32+
33+
const detailsMenu = getRepoElement.querySelector('details-menu');
34+
if (!detailsMenu) {
35+
return null;
36+
}
37+
38+
// src="/{org}/{repo}/pull/{number}/open_with_menu?ref={sha}"
39+
const src = detailsMenu.getAttribute('src');
40+
const match = src?.match(/ref=([a-z0-9]+)/);
41+
if (!match || match.length < 2) {
42+
return null;
43+
}
44+
45+
return match[1];
46+
}
47+
48+
function getRefFromUrl() {
49+
const pathParts = window.location.pathname.split("/");
50+
51+
let commitShaIndex = pathParts.indexOf("pull");
52+
let reference: string | null = null;
53+
if (commitShaIndex >= 0) {
54+
reference = `refs/pull/${pathParts[commitShaIndex + 1]}`;
55+
} else {
56+
commitShaIndex = pathParts.indexOf("commit");
57+
if (commitShaIndex >= 0) {
58+
reference = pathParts[commitShaIndex + 1];
59+
}
60+
}
61+
62+
return reference;
63+
}
64+
1365
async function loadCoverageForPath(path: string): Promise<FileCoverage | null> {
14-
const data = await readCoverageData(path);
66+
const ref = getRef();
67+
if (!ref) {
68+
console.warn("[qlty] No reference found for coverage data");
69+
return null;
70+
}
71+
72+
const data = await readCoverageData(path, ref);
1573
const coverage = data?.files.find(
1674
(file) => normalizePath(file.path) === normalizePath(path),
1775
);

0 commit comments

Comments
 (0)