diff --git a/actions-languageserver/src/connection.ts b/actions-languageserver/src/connection.ts index 0a047f0..cb35490 100644 --- a/actions-languageserver/src/connection.ts +++ b/actions-languageserver/src/connection.ts @@ -20,6 +20,7 @@ import {getClient} from "./client"; import {Commands} from "./commands"; import {contextProviders} from "./context-providers"; import {descriptionProvider} from "./description-provider"; +import {getFileProvider} from "./file-provider"; import {InitializationOptions, RepositoryContext} from "./initializationOptions"; import {onCompletion} from "./on-completion"; import {TTLCache} from "./utils/cache"; @@ -106,7 +107,8 @@ export function initConnection(connection: Connection) { } return undefined; - } + }, + fileProvider: getFileProvider(client, cache) }; const result = await validate(textDocument, config); diff --git a/actions-languageserver/src/file-provider.ts b/actions-languageserver/src/file-provider.ts new file mode 100644 index 0000000..7c01cd1 --- /dev/null +++ b/actions-languageserver/src/file-provider.ts @@ -0,0 +1,54 @@ +import {File} from "@github/actions-workflow-parser/workflows/file"; +import {FileProvider} from "@github/actions-workflow-parser/workflows/file-provider"; +import {fileIdentifier} from "@github/actions-workflow-parser/workflows/file-reference"; +import {Octokit} from "@octokit/rest"; +import {TTLCache} from "./utils/cache"; + +export function getFileProvider(client: Octokit | undefined, cache: TTLCache): FileProvider | undefined { + if (!client) { + return undefined; + } + + return { + getFileContent: async (ref): Promise => { + if (!("repository" in ref)) { + throw new Error("Only remote file references are supported right now"); + } + + return await cache.get(`file-content-${fileIdentifier(ref)}`, undefined, () => + fetchWorkflowFile(client, ref.owner, ref.repository, ref.path, ref.version) + ); + } + }; +} + +async function fetchWorkflowFile( + client: Octokit, + owner: string, + repo: string, + path: string, + version: string +): Promise { + 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") + }; +}