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

52 lines
1.6 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 { Value, ValueProviderConfig } from "@github/actions-languageservice/value-providers/config";
2022-11-08 17:00:59 -08:00
import { Octokit } from "@octokit/rest";
2022-11-28 15:49:56 -05:00
import { CompletionItem, Position } from "vscode-languageserver";
2022-11-08 17:00:59 -08:00
import { TextDocument } from "vscode-languageserver-textdocument";
import { RepositoryContext } from "./initializationOptions";
import { TTLCache } from "./utils/cache";
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,
document: TextDocument,
sessionToken: string | undefined,
repoContext: RepositoryContext | undefined,
cache: TTLCache,
2022-11-08 17:00:59 -08:00
): Promise<CompletionItem[]> {
const config: ValueProviderConfig = {
2022-11-14 22:37:34 +00:00
getCustomValues: async (key: string, context: WorkflowContext) =>
2022-11-28 18:08:11 -05:00
getCustomValues(key, context, sessionToken, repoContext, cache),
2022-11-08 17:00:59 -08:00
};
return await complete(document, position, config);
}
async function getCustomValues(
key: string,
_: WorkflowContext,
2022-11-28 18:08:11 -05:00
sessionToken: string | undefined,
repo: RepositoryContext | undefined,
cache: TTLCache,
): Promise<Value[] | undefined> {
2022-11-28 18:08:11 -05:00
if (!sessionToken || !repo) {
return;
}
2022-11-16 19:21:36 +00:00
const octokit = new Octokit({
auth: sessionToken,
});
switch (key) {
case "job-environment": {
2022-11-28 18:08:11 -05:00
return await getEnvironments(octokit, cache, repo.owner, repo.name);
2022-11-16 19:21:36 +00:00
}
2022-11-28 10:15:17 -08:00
2022-11-16 19:21:36 +00:00
case "runs-on": {
2022-11-28 18:08:11 -05:00
return await getRunnerLabels(octokit, cache, repo.owner, repo.name);
2022-11-16 19:21:36 +00:00
}
2022-11-08 17:00:59 -08:00
}
}