-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfile-provider.ts
More file actions
78 lines (69 loc) · 2.11 KB
/
file-provider.ts
File metadata and controls
78 lines (69 loc) · 2.11 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {File} from "@actions/workflow-parser/workflows/file";
import {FileProvider} from "@actions/workflow-parser/workflows/file-provider";
import {fileIdentifier} from "@actions/workflow-parser/workflows/file-reference";
import {Octokit} from "@octokit/rest";
import {TTLCache} from "./utils/cache.js";
import * as vscodeURI from "vscode-uri";
export function getFileProvider(
client: Octokit | undefined,
cache: TTLCache,
workspace: string | undefined,
readFile: (path: string) => Promise<string>
): FileProvider | undefined {
if (!client && !workspace) {
return undefined;
}
return {
getFileContent: async (ref): Promise<File> => {
if ("repository" in ref) {
if (!client) {
throw new Error("Remote file references are not supported with this configuration");
}
return await cache.get(`file-content-${fileIdentifier(ref)}`, undefined, () =>
fetchWorkflowFile(client, ref.owner, ref.repository, ref.path, ref.version)
);
}
if (!workspace) {
throw new Error("Local file references are not supported with this configuration");
}
const workspaceURI = vscodeURI.URI.parse(workspace);
const refURI = vscodeURI.Utils.joinPath(workspaceURI, ref.path);
const file = await readFile(refURI.toString());
if (!file) {
throw new Error(`File not found: ${ref.path}`);
}
return {
name: ref.path,
content: file
};
}
};
}
async function fetchWorkflowFile(
client: Octokit,
owner: string,
repo: string,
path: string,
version: string
): Promise<File> {
const resp = await client.repos.getContent({
owner,
repo,
path,
ref: version
});
// https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28
// Ignore directories (array of files) and non-file content
if (
resp.data === undefined ||
Array.isArray(resp.data) ||
resp.data.type !== "file" ||
resp.data.content === undefined
) {
throw new Error("Not a file");
}
return {
name: path,
content: Buffer.from(resp.data.content, "base64").toString("utf8")
};
}