2022-11-08 17:00:59 -08:00
|
|
|
import { complete } from "@github/actions-languageservice/complete";
|
|
|
|
|
import {
|
|
|
|
|
ValueProviderConfig,
|
|
|
|
|
WorkflowContext,
|
|
|
|
|
} from "@github/actions-languageservice/value-providers/config";
|
|
|
|
|
import { Octokit } from "@octokit/rest";
|
|
|
|
|
import { CompletionItem, DocumentUri, Position } from "vscode-languageserver";
|
|
|
|
|
import { TextDocument } from "vscode-languageserver-textdocument";
|
|
|
|
|
import { RepositoryContext } from "./initializationOptions";
|
2022-11-14 22:34:57 +00:00
|
|
|
import { getJobNames } from "./value-providers/needs";
|
2022-11-08 17:00:59 -08:00
|
|
|
import { getRunnerLabels } from "./value-providers/runs-on";
|
2022-11-14 22:34:57 +00:00
|
|
|
import { WorkflowTemplate } from "@github/actions-workflow-parser";
|
|
|
|
|
|
2022-11-08 17:00:59 -08:00
|
|
|
|
|
|
|
|
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:34:57 +00:00
|
|
|
getCustomValues: async (key: string, context: WorkflowContext, template: WorkflowTemplate | undefined) =>
|
|
|
|
|
getCustomValues(key, context, sessionToken, repoWorkflowMap, template),
|
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:34:57 +00:00
|
|
|
repoWorkspaceMap: Map<string, RepositoryContext>,
|
|
|
|
|
template: WorkflowTemplate | undefined
|
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;
|
|
|
|
|
}
|
|
|
|
|
if (key === "runs-on") {
|
|
|
|
|
const octokit = new Octokit({
|
|
|
|
|
auth: sessionToken,
|
|
|
|
|
});
|
|
|
|
|
return await getRunnerLabels(octokit, repo.owner, repo.name);
|
|
|
|
|
}
|
2022-11-14 22:34:57 +00:00
|
|
|
if (key === "needs" && template) {
|
|
|
|
|
return await getJobNames(template);
|
|
|
|
|
}
|
2022-11-08 17:00:59 -08:00
|
|
|
}
|