-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathbqrs-utils.ts
More file actions
60 lines (53 loc) · 1.82 KB
/
bqrs-utils.ts
File metadata and controls
60 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { createRemoteFileRef } from "../common/location-link-utils";
import type { UrlValue } from "./raw-result-types";
import { isUrlValueResolvable } from "./raw-result-types";
/**
* Checks whether the file path is empty. If so, we do not want to render this location
* as a link.
*/
export function isEmptyPath(uriStr: string) {
return !uriStr || uriStr === "file:/" || uriStr === "file:///";
}
export function tryGetRemoteLocation(
loc: UrlValue | undefined,
fileLinkPrefix: string,
sourceLocationPrefix: string | undefined,
): string | undefined {
if (!loc || !isUrlValueResolvable(loc)) {
return undefined;
}
let trimmedLocation: string;
// Remote locations have the following format:
// "file:${sourceLocationPrefix}/relative/path/to/file"
// So we need to strip off the first part to get the relative path.
if (sourceLocationPrefix) {
if (!loc.uri.startsWith(`file:${sourceLocationPrefix}/`)) {
return undefined;
}
trimmedLocation = loc.uri.replace(`file:${sourceLocationPrefix}/`, "");
} else {
// If the source location prefix is empty (e.g. for older remote queries), we assume that the database
// was created on a Linux actions runner and has the format:
// "file:/home/runner/work/<repo>/<repo>/relative/path/to/file"
// So we need to drop the first 6 parts of the path.
if (!loc.uri.startsWith("file:/home/runner/work/")) {
return undefined;
}
const locationParts = loc.uri.split("/");
trimmedLocation = locationParts.slice(6, locationParts.length).join("/");
}
const fileLink = {
fileLinkPrefix,
filePath: trimmedLocation,
};
if (loc.type === "wholeFileLocation") {
return createRemoteFileRef(fileLink);
}
return createRemoteFileRef(
fileLink,
loc.startLine,
loc.endLine,
loc.startColumn,
loc.endColumn,
);
}