Use value providers for validation
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
|||||||
} from "./initializationOptions";
|
} from "./initializationOptions";
|
||||||
import { onCompletion } from "./on-completion";
|
import { onCompletion } from "./on-completion";
|
||||||
import { TTLCache } from "./utils/cache";
|
import { TTLCache } from "./utils/cache";
|
||||||
|
import { valueProviders } from "./value-providers";
|
||||||
|
|
||||||
// Create a connection for the server, using Node's IPC as a transport.
|
// Create a connection for the server, using Node's IPC as a transport.
|
||||||
// Also include all preview / proposed LSP features.
|
// Also include all preview / proposed LSP features.
|
||||||
@@ -90,7 +91,10 @@ documents.onDidChangeContent((change) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function validateTextDocument(textDocument: TextDocument): Promise<void> {
|
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 });
|
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: result });
|
||||||
}
|
}
|
||||||
@@ -111,7 +115,7 @@ connection.onCompletion(
|
|||||||
documents.get(textDocument.uri)!,
|
documents.get(textDocument.uri)!,
|
||||||
sessionToken,
|
sessionToken,
|
||||||
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
|
repos.find((repo) => textDocument.uri.startsWith(repo.workspaceUri)),
|
||||||
cache,
|
cache
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,51 +1,20 @@
|
|||||||
import { complete } from "@github/actions-languageservice/complete";
|
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 { CompletionItem, Position } from "vscode-languageserver";
|
||||||
import { TextDocument } from "vscode-languageserver-textdocument";
|
import { TextDocument } from "vscode-languageserver-textdocument";
|
||||||
import { RepositoryContext } from "./initializationOptions";
|
import { RepositoryContext } from "./initializationOptions";
|
||||||
import { TTLCache } from "./utils/cache";
|
import { TTLCache } from "./utils/cache";
|
||||||
import { getEnvironments } from "./value-providers/job-environment";
|
import { valueProviders } from "./value-providers";
|
||||||
import { getRunnerLabels } from "./value-providers/runs-on";
|
|
||||||
|
|
||||||
export async function onCompletion(
|
export async function onCompletion(
|
||||||
position: Position,
|
position: Position,
|
||||||
document: TextDocument,
|
document: TextDocument,
|
||||||
sessionToken: string | undefined,
|
sessionToken: string | undefined,
|
||||||
repoContext: RepositoryContext | undefined,
|
repoContext: RepositoryContext | undefined,
|
||||||
cache: TTLCache,
|
cache: TTLCache
|
||||||
): Promise<CompletionItem[]> {
|
): Promise<CompletionItem[]> {
|
||||||
const config: ValueProviderConfig = {
|
return await complete(
|
||||||
getCustomValues: async (key: string, context: WorkflowContext) =>
|
document,
|
||||||
getCustomValues(key, context, sessionToken, repoContext, cache),
|
position,
|
||||||
};
|
repoContext && valueProviders(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import {complete} from "./complete";
|
import {complete} from "./complete";
|
||||||
import {WorkflowContext} from "./context/workflow-context";
|
import {WorkflowContext} from "./context/workflow-context";
|
||||||
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
import {getPositionFromCursor} from "./test-utils/cursor-position";
|
||||||
import {Value, ValueProviderConfig} from "./value-providers/config";
|
import {ValueProviderConfig} from "./value-providers/config";
|
||||||
|
|
||||||
describe("completion", () => {
|
describe("completion", () => {
|
||||||
it("runs-on", async () => {
|
it("runs-on", async () => {
|
||||||
@@ -179,14 +179,10 @@ jobs:
|
|||||||
it("custom value providers override defaults", async () => {
|
it("custom value providers override defaults", async () => {
|
||||||
const input = "on: push\njobs:\n build:\n runs-on: |";
|
const input = "on: push\njobs:\n build:\n runs-on: |";
|
||||||
|
|
||||||
const getCustomValues = async (key: string, _: WorkflowContext): Promise<Value[] | undefined> => {
|
const config: ValueProviderConfig = {
|
||||||
if (key === "runs-on") {
|
"runs-on": async (_: WorkflowContext) => {
|
||||||
return [{label: "my-custom-label"}];
|
return [{label: "my-custom-label"}];
|
||||||
}
|
}
|
||||||
return [];
|
|
||||||
};
|
|
||||||
const config: ValueProviderConfig = {
|
|
||||||
getCustomValues: getCustomValues
|
|
||||||
};
|
};
|
||||||
const result = await complete(...getPositionFromCursor(input), config);
|
const result = await complete(...getPositionFromCursor(input), config);
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ async function getValues(
|
|||||||
const existingValues = getExistingValues(token, parent);
|
const existingValues = getExistingValues(token, parent);
|
||||||
|
|
||||||
if (token?.definition?.key) {
|
if (token?.definition?.key) {
|
||||||
const customValues = await valueProviderConfig?.getCustomValues(token.definition.key, workflowContext);
|
const customValues = await valueProviderConfig?.[token.definition.key]?.(workflowContext);
|
||||||
|
|
||||||
if (customValues) {
|
if (customValues) {
|
||||||
return filterAndSortCompletionOptions(customValues, existingValues);
|
return filterAndSortCompletionOptions(customValues, existingValues);
|
||||||
@@ -131,7 +131,7 @@ async function getValues(
|
|||||||
return filterAndSortCompletionOptions(values, existingValues);
|
return filterAndSortCompletionOptions(values, existingValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExistingValues(token: TemplateToken | null, parent: TemplateToken) {
|
export function getExistingValues(token: TemplateToken | null, parent: TemplateToken) {
|
||||||
// For incomplete YAML, we may only have a parent token
|
// For incomplete YAML, we may only have a parent token
|
||||||
if (token) {
|
if (token) {
|
||||||
if (!isString(token)) {
|
if (!isString(token)) {
|
||||||
|
|||||||
@@ -3,21 +3,27 @@ import {Expr} from "@github/actions-expressions/ast";
|
|||||||
import {
|
import {
|
||||||
convertWorkflowTemplate,
|
convertWorkflowTemplate,
|
||||||
isBasicExpression,
|
isBasicExpression,
|
||||||
|
isSequence,
|
||||||
|
isString,
|
||||||
parseWorkflow,
|
parseWorkflow,
|
||||||
ParseWorkflowResult
|
ParseWorkflowResult,
|
||||||
|
WorkflowTemplate
|
||||||
} from "@github/actions-workflow-parser";
|
} from "@github/actions-workflow-parser";
|
||||||
import {splitAllowedContext} from "@github/actions-workflow-parser/templates/allowed-context";
|
import {splitAllowedContext} from "@github/actions-workflow-parser/templates/allowed-context";
|
||||||
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
|
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
|
||||||
import {TokenRange} from "@github/actions-workflow-parser/templates/tokens/token-range";
|
import {TokenRange} from "@github/actions-workflow-parser/templates/tokens/token-range";
|
||||||
import {File} from "@github/actions-workflow-parser/workflows/file";
|
import {File} from "@github/actions-workflow-parser/workflows/file";
|
||||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||||
import {Diagnostic, DiagnosticSeverity, Range} from "vscode-languageserver-types";
|
import {Diagnostic, DiagnosticSeverity, Range, URI} from "vscode-languageserver-types";
|
||||||
|
|
||||||
import {ContextProviderConfig} from "./context-providers/config";
|
import {ContextProviderConfig} from "./context-providers/config";
|
||||||
import {getContext} from "./context-providers/default";
|
import {getContext} from "./context-providers/default";
|
||||||
|
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
|
||||||
import {AccessError, wrapDictionary} from "./expression-validation/error-dictionary";
|
import {AccessError, wrapDictionary} from "./expression-validation/error-dictionary";
|
||||||
import {nullTrace} from "./nulltrace";
|
import {nullTrace} from "./nulltrace";
|
||||||
import {ValueProviderConfig} from "./value-providers/config";
|
import {findToken} from "./utils/find-token";
|
||||||
|
import {Value, ValueProviderConfig} from "./value-providers/config";
|
||||||
|
import {defaultValueProviders} from "./value-providers/default";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates a workflow file
|
* Validates a workflow file
|
||||||
@@ -42,11 +48,16 @@ export async function validate(
|
|||||||
const result: ParseWorkflowResult = parseWorkflow(file.name, [file], nullTrace);
|
const result: ParseWorkflowResult = parseWorkflow(file.name, [file], nullTrace);
|
||||||
if (result.value) {
|
if (result.value) {
|
||||||
// Errors will be updated in the context
|
// Errors will be updated in the context
|
||||||
convertWorkflowTemplate(result.context, result.value);
|
const template = convertWorkflowTemplate(result.context, result.value);
|
||||||
|
|
||||||
|
// Validate with value providers
|
||||||
|
if (valueProviderConfig) {
|
||||||
|
await validateValueProviders(diagnostics, textDocument.uri, template, result, valueProviderConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate expressions
|
// Validate expressions
|
||||||
validateExpressions(diagnostics, result, contextProviderConfig);
|
validateExpressions(diagnostics, result, contextProviderConfig);
|
||||||
|
}
|
||||||
|
|
||||||
// For now map parser errors directly to diagnostics
|
// For now map parser errors directly to diagnostics
|
||||||
for (const error of result.context.errors.getErrors()) {
|
for (const error of result.context.errors.getErrors()) {
|
||||||
@@ -91,7 +102,7 @@ function mapRange(range: TokenRange | undefined): Range {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function validateExpressions(
|
function validateExpressions(
|
||||||
diagnotics: Diagnostic[],
|
diagnostics: Diagnostic[],
|
||||||
result: ParseWorkflowResult,
|
result: ParseWorkflowResult,
|
||||||
contextProviderConfig: ContextProviderConfig | undefined
|
contextProviderConfig: ContextProviderConfig | undefined
|
||||||
) {
|
) {
|
||||||
@@ -130,7 +141,7 @@ function validateExpressions(
|
|||||||
// result of the evaluation.
|
// result of the evaluation.
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof AccessError) {
|
if (e instanceof AccessError) {
|
||||||
diagnotics.push({
|
diagnostics.push({
|
||||||
message: `Context access might be invalid: ${e.keyName}`,
|
message: `Context access might be invalid: ${e.keyName}`,
|
||||||
severity: DiagnosticSeverity.Warning,
|
severity: DiagnosticSeverity.Warning,
|
||||||
range: mapRange(expression.range)
|
range: mapRange(expression.range)
|
||||||
@@ -143,3 +154,79 @@ function validateExpressions(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function validateValueProviders(
|
||||||
|
diagnostics: Diagnostic[],
|
||||||
|
documentUri: URI,
|
||||||
|
template: WorkflowTemplate,
|
||||||
|
result: ParseWorkflowResult,
|
||||||
|
valueProviderConfig: ValueProviderConfig
|
||||||
|
) {
|
||||||
|
if (!result.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allowed values coming from the schema have already been validated. Only check if
|
||||||
|
// a value provider is defined for a token and if it is, validate the values match.
|
||||||
|
for (const token of TemplateToken.traverse(result.value)) {
|
||||||
|
if (token.range && token.definition?.key) {
|
||||||
|
const defKey = token.definition.key;
|
||||||
|
|
||||||
|
let customValues: Value[] | undefined;
|
||||||
|
|
||||||
|
const customValueProvider = valueProviderConfig[defKey];
|
||||||
|
if (customValueProvider) {
|
||||||
|
customValues = await customValueProvider(getProviderContext(documentUri, template, result.value, token));
|
||||||
|
} else {
|
||||||
|
const defaultValueProvider = defaultValueProviders[defKey];
|
||||||
|
if (defaultValueProvider) {
|
||||||
|
customValues = defaultValueProvider(getProviderContext(documentUri, template, result.value, token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (customValues) {
|
||||||
|
if (isSequence(token)) {
|
||||||
|
for (let i = 0; i < token.count; ++i) {
|
||||||
|
const entry = token.get(i);
|
||||||
|
|
||||||
|
if (isString(entry)) {
|
||||||
|
if (!customValues.map(x => x.label).includes(entry.value)) {
|
||||||
|
diagnostics.push({
|
||||||
|
message: `Value '${entry.value}' is not allowed`,
|
||||||
|
severity: DiagnosticSeverity.Error,
|
||||||
|
range: mapRange(entry.range)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isString(token)) {
|
||||||
|
if (!customValues.map(x => x.label).includes(token.value)) {
|
||||||
|
diagnostics.push({
|
||||||
|
message: `Value '${token.value}' is not allowed`,
|
||||||
|
severity: DiagnosticSeverity.Error,
|
||||||
|
range: mapRange(token.range)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getProviderContext(
|
||||||
|
documentUri: URI,
|
||||||
|
template: WorkflowTemplate,
|
||||||
|
root: TemplateToken,
|
||||||
|
token: TemplateToken
|
||||||
|
): WorkflowContext {
|
||||||
|
const {parent, path} = findToken(
|
||||||
|
{
|
||||||
|
line: token.range!.start[0],
|
||||||
|
character: token.range!.start[1]
|
||||||
|
},
|
||||||
|
root
|
||||||
|
);
|
||||||
|
return getWorkflowContext(documentUri, template, path);
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,15 +5,8 @@ export interface Value {
|
|||||||
description?: string;
|
description?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ValueProvider = () => Value[];
|
export type ValueProvider = (context: WorkflowContext) => Promise<Value[]>;
|
||||||
|
|
||||||
export interface ValueProviderConfig {
|
export interface ValueProviderConfig {
|
||||||
getCustomValues: (key: string, context: WorkflowContext) => Promise<Value[] | undefined>;
|
[definitionKey: string]: ValueProvider;
|
||||||
getActionInputs?: (owner: string, name: string, ref: string, path?: string) => Promise<ActionInput[]>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ActionInput {
|
|
||||||
name: string;
|
|
||||||
description?: string;
|
|
||||||
required?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user