Use value providers for validation

This commit is contained in:
Christopher Schleiden
2022-11-28 15:53:02 -08:00
parent a217edfbc6
commit a32295a09a
7 changed files with 164 additions and 67 deletions
+6 -2
View File
@@ -19,6 +19,7 @@ import {
} from "./initializationOptions";
import { onCompletion } from "./on-completion";
import { TTLCache } from "./utils/cache";
import { valueProviders } from "./value-providers";
// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
@@ -90,7 +91,10 @@ documents.onDidChangeContent((change) => {
});
async function validateTextDocument(textDocument: TextDocument): Promise<void> {
const result = await validate(textDocument);
const result = await validate(
textDocument,
valueProviders(sessionToken, repoWorkspaceMap)
);
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: result });
}
@@ -111,7 +115,7 @@ connection.onCompletion(
documents.get(textDocument.uri)!,
sessionToken,
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
cache,
cache
);
}
);
+7 -38
View File
@@ -1,51 +1,20 @@
import { complete } from "@github/actions-languageservice/complete";
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
import { Value, ValueProviderConfig } from "@github/actions-languageservice/value-providers/config";
import { Octokit } from "@octokit/rest";
import { CompletionItem, Position } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { RepositoryContext } from "./initializationOptions";
import { TTLCache } from "./utils/cache";
import { getEnvironments } from "./value-providers/job-environment";
import { getRunnerLabels } from "./value-providers/runs-on";
import { valueProviders } from "./value-providers";
export async function onCompletion(
position: Position,
document: TextDocument,
sessionToken: string | undefined,
repoContext: RepositoryContext | undefined,
cache: TTLCache,
cache: TTLCache
): Promise<CompletionItem[]> {
const config: ValueProviderConfig = {
getCustomValues: async (key: string, context: WorkflowContext) =>
getCustomValues(key, context, sessionToken, repoContext, cache),
};
return await complete(document, position, config);
}
async function getCustomValues(
key: string,
_: WorkflowContext,
sessionToken: string | undefined,
repo: RepositoryContext | undefined,
cache: TTLCache,
): Promise<Value[] | undefined> {
if (!sessionToken || !repo) {
return;
}
const octokit = new Octokit({
auth: sessionToken,
});
switch (key) {
case "job-environment": {
return await getEnvironments(octokit, cache, repo.owner, repo.name);
}
case "runs-on": {
return await getRunnerLabels(octokit, cache, repo.owner, repo.name);
}
}
return await complete(
document,
position,
repoContext && valueProviders(sessionToken, repoContext, cache)
);
}
@@ -0,0 +1,48 @@
import { ValueProviderConfig } from "@github/actions-languageservice";
import { WorkflowContext } from "@github/actions-languageservice/context/workflow-context";
import { Value } from "@github/actions-languageservice/value-providers/config";
import { Octokit } from "@octokit/rest";
import { RepositoryContext } from "./initializationOptions";
import { TTLCache } from "./utils/cache";
import { getEnvironments } from "./value-providers/job-environment";
import { getRunnerLabels } from "./value-providers/runs-on";
export function valueProviders(
sessionToken: string | undefined,
repoContext: RepositoryContext,
cache: TTLCache
): ValueProviderConfig {
return {
"job-environment": passOctoKit(
sessionToken,
repoContext,
cache,
getEnvironments
),
"runs-on": passOctoKit(sessionToken, repoContext, cache, getRunnerLabels),
};
}
function passOctoKit(
sessionToken: string | undefined,
repo: RepositoryContext,
cache: TTLCache,
f: (
octokit: Octokit,
cache: TTLCache,
owner: string,
name: string
) => Promise<Value[]>
) {
return async function (context: WorkflowContext): Promise<Value[]> {
if (!sessionToken) {
return [];
}
const octokit = new Octokit({
auth: sessionToken,
});
return f(octokit, cache, repo.owner, repo.name);
};
}