Files
languageservices/actions-languageserver/src/on-completion.ts
T

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-11-08 17:00:59 -08:00
import { complete } from "@github/actions-languageservice/complete";
2022-11-28 10:15:17 -08:00
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
import { ValueProviderConfig } from "@github/actions-languageservice/value-providers/config";
2022-11-08 17:00:59 -08:00
import { Octokit } from "@octokit/rest";
import { CompletionItem, DocumentUri, Position } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { RepositoryContext } from "./initializationOptions";
2022-11-16 16:21:36 -05:00
import { getEnvironments } from "./value-providers/job-environment";
2022-11-08 17:00:59 -08:00
import { getRunnerLabels } from "./value-providers/runs-on";
export async function onCompletion(
position: Position,
uri: DocumentUri,
document: TextDocument,
sessionToken: string | undefined,
repoWorkflowMap: Map<string, RepositoryContext>
): Promise<CompletionItem[]> {
const config: ValueProviderConfig = {
2022-11-14 22:37:34 +00:00
getCustomValues: async (key: string, context: WorkflowContext) =>
getCustomValues(key, context, sessionToken, repoWorkflowMap),
2022-11-08 17:00:59 -08:00
};
return await complete(document, position, config);
}
async function getCustomValues(
key: string,
context: WorkflowContext,
sessionToken: string | undefined,
2022-11-14 22:37:34 +00:00
repoWorkspaceMap: Map<string, RepositoryContext>
2022-11-08 17:00:59 -08:00
) {
if (!sessionToken) {
return;
}
// TODO: Parse workflow URI and look up repo for workspace
const [repo] = repoWorkspaceMap.values();
if (!repo) {
return;
}
2022-11-16 19:21:36 +00:00
const octokit = new Octokit({
auth: sessionToken,
});
switch (key) {
case "job-environment": {
return await getEnvironments(octokit, repo.owner, repo.name);
}
2022-11-28 10:15:17 -08:00
2022-11-16 19:21:36 +00:00
case "runs-on": {
return await getRunnerLabels(octokit, repo.owner, repo.name);
}
2022-11-08 17:00:59 -08:00
}
}