Merge validation code into a single traversal
This commit is contained in:
@@ -117,7 +117,7 @@ async function getValues(
|
|||||||
(parent.definition?.key && defaultValueProviders[parent.definition.key]);
|
(parent.definition?.key && defaultValueProviders[parent.definition.key]);
|
||||||
|
|
||||||
if (valueProvider) {
|
if (valueProvider) {
|
||||||
const values = valueProvider(workflowContext);
|
const values = await valueProvider(workflowContext);
|
||||||
return filterAndSortCompletionOptions(values, existingValues);
|
return filterAndSortCompletionOptions(values, existingValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {Diagnostic} from "vscode-languageserver-types";
|
import {Diagnostic, DiagnosticSeverity} from "vscode-languageserver-types";
|
||||||
import {createDocument} from "./test-utils/document";
|
import {createDocument} from "./test-utils/document";
|
||||||
import {validate} from "./validate";
|
import {validate} from "./validate";
|
||||||
|
import {defaultValueProviders} from "./value-providers/default";
|
||||||
|
|
||||||
describe("validation", () => {
|
describe("validation", () => {
|
||||||
it("valid workflow", async () => {
|
it("valid workflow", async () => {
|
||||||
@@ -57,4 +58,68 @@ jobs:
|
|||||||
}
|
}
|
||||||
} as Diagnostic);
|
} as Diagnostic);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("single value not returned by value provider", async () => {
|
||||||
|
const result = await validate(
|
||||||
|
createDocument(
|
||||||
|
"wf.yaml",
|
||||||
|
`on: push
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: does-not-exist
|
||||||
|
steps:
|
||||||
|
- run: echo`
|
||||||
|
),
|
||||||
|
defaultValueProviders
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
expect(result[0]).toEqual({
|
||||||
|
message: "Value 'does-not-exist' is not valid",
|
||||||
|
severity: DiagnosticSeverity.Error,
|
||||||
|
range: {
|
||||||
|
end: {
|
||||||
|
character: 27,
|
||||||
|
line: 3
|
||||||
|
},
|
||||||
|
start: {
|
||||||
|
character: 13,
|
||||||
|
line: 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as Diagnostic);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("value in sequence not returned by value provider", async () => {
|
||||||
|
const result = await validate(
|
||||||
|
createDocument(
|
||||||
|
"wf.yaml",
|
||||||
|
`on: push
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on:
|
||||||
|
- ubuntu-latest
|
||||||
|
- does-not-exist
|
||||||
|
steps:
|
||||||
|
- run: echo`
|
||||||
|
),
|
||||||
|
defaultValueProviders
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
expect(result[0]).toEqual({
|
||||||
|
message: "Value 'does-not-exist' is not valid",
|
||||||
|
severity: DiagnosticSeverity.Error,
|
||||||
|
range: {
|
||||||
|
end: {
|
||||||
|
character: 20,
|
||||||
|
line: 5
|
||||||
|
},
|
||||||
|
start: {
|
||||||
|
character: 6,
|
||||||
|
line: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as Diagnostic);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import {
|
|||||||
WorkflowTemplate
|
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 {BasicExpressionToken} from "@github/actions-workflow-parser/templates/tokens/basic-expression-token";
|
||||||
|
import {StringToken} from "@github/actions-workflow-parser/templates/tokens/string-token";
|
||||||
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";
|
||||||
@@ -50,13 +52,15 @@ export async function validate(
|
|||||||
// Errors will be updated in the context
|
// Errors will be updated in the context
|
||||||
const template = convertWorkflowTemplate(result.context, result.value);
|
const template = convertWorkflowTemplate(result.context, result.value);
|
||||||
|
|
||||||
// Validate with value providers
|
// Validate expressions and value providers
|
||||||
if (valueProviderConfig) {
|
await additionalValidations(
|
||||||
await validateValueProviders(diagnostics, textDocument.uri, template, result, valueProviderConfig);
|
diagnostics,
|
||||||
}
|
textDocument.uri,
|
||||||
|
template,
|
||||||
// Validate expressions
|
result.value,
|
||||||
validateExpressions(diagnostics, result, contextProviderConfig);
|
valueProviderConfig,
|
||||||
|
contextProviderConfig
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now map parser errors directly to diagnostics
|
// For now map parser errors directly to diagnostics
|
||||||
@@ -101,86 +105,34 @@ function mapRange(range: TokenRange | undefined): Range {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateExpressions(
|
async function additionalValidations(
|
||||||
diagnostics: Diagnostic[],
|
|
||||||
result: ParseWorkflowResult,
|
|
||||||
contextProviderConfig: ContextProviderConfig | undefined
|
|
||||||
) {
|
|
||||||
if (!result.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate over the parsed workflow
|
|
||||||
for (const token of TemplateToken.traverse(result.value)) {
|
|
||||||
if (isBasicExpression(token)) {
|
|
||||||
// Validate the expression
|
|
||||||
for (const expression of token.originalExpressions || [token]) {
|
|
||||||
const allowedContexts = token.definition?.readerContext || [];
|
|
||||||
const {namedContexts, functions} = splitAllowedContext(allowedContexts);
|
|
||||||
|
|
||||||
let expr: Expr | undefined;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const l = new Lexer(expression.expression);
|
|
||||||
const lr = l.lex();
|
|
||||||
|
|
||||||
const p = new Parser(lr.tokens, namedContexts, functions);
|
|
||||||
expr = p.parse();
|
|
||||||
} catch {
|
|
||||||
// Ignore any error here, we should've caught this earlier in the parsing process
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const context = getContext(namedContexts, contextProviderConfig);
|
|
||||||
|
|
||||||
const e = new Evaluator(expr, wrapDictionary(context));
|
|
||||||
e.evaluate();
|
|
||||||
|
|
||||||
// Any invalid context access would've thrown an error via the `ErrorDictionary`, for now we don't have to check the actual
|
|
||||||
// result of the evaluation.
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof AccessError) {
|
|
||||||
diagnostics.push({
|
|
||||||
message: `Context access might be invalid: ${e.keyName}`,
|
|
||||||
severity: DiagnosticSeverity.Warning,
|
|
||||||
range: mapRange(expression.range)
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Ignore error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function validateValueProviders(
|
|
||||||
diagnostics: Diagnostic[],
|
diagnostics: Diagnostic[],
|
||||||
documentUri: URI,
|
documentUri: URI,
|
||||||
template: WorkflowTemplate,
|
template: WorkflowTemplate,
|
||||||
result: ParseWorkflowResult,
|
root: TemplateToken,
|
||||||
valueProviderConfig: ValueProviderConfig
|
valueProviderConfig: ValueProviderConfig | undefined,
|
||||||
|
contextProviderConfig: ContextProviderConfig | undefined
|
||||||
) {
|
) {
|
||||||
if (!result.value) {
|
for (const token of TemplateToken.traverse(root)) {
|
||||||
return;
|
// If this is an expression, validate it
|
||||||
}
|
if (isBasicExpression(token)) {
|
||||||
|
validateExpression(diagnostics, token, contextProviderConfig);
|
||||||
|
}
|
||||||
|
|
||||||
// Allowed values coming from the schema have already been validated. Only check if
|
// 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.
|
// 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 (valueProviderConfig && token.range && token.definition?.key) {
|
||||||
if (token.range && token.definition?.key) {
|
|
||||||
const defKey = token.definition.key;
|
const defKey = token.definition.key;
|
||||||
|
|
||||||
let customValues: Value[] | undefined;
|
let customValues: Value[] | undefined;
|
||||||
|
|
||||||
const customValueProvider = valueProviderConfig[defKey];
|
const customValueProvider = valueProviderConfig[defKey];
|
||||||
if (customValueProvider) {
|
if (customValueProvider) {
|
||||||
customValues = await customValueProvider(getProviderContext(documentUri, template, result.value, token));
|
customValues = await customValueProvider(getProviderContext(documentUri, template, root, token));
|
||||||
} else {
|
} else {
|
||||||
const defaultValueProvider = defaultValueProviders[defKey];
|
const defaultValueProvider = defaultValueProviders[defKey];
|
||||||
if (defaultValueProvider) {
|
if (defaultValueProvider) {
|
||||||
customValues = defaultValueProvider(getProviderContext(documentUri, template, result.value, token));
|
customValues = await defaultValueProvider(getProviderContext(documentUri, template, root, token));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,11 +143,7 @@ async function validateValueProviders(
|
|||||||
|
|
||||||
if (isString(entry)) {
|
if (isString(entry)) {
|
||||||
if (!customValues.map(x => x.label).includes(entry.value)) {
|
if (!customValues.map(x => x.label).includes(entry.value)) {
|
||||||
diagnostics.push({
|
invalidValue(diagnostics, entry);
|
||||||
message: `Value '${entry.value}' is not allowed`,
|
|
||||||
severity: DiagnosticSeverity.Error,
|
|
||||||
range: mapRange(entry.range)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,11 +151,7 @@ async function validateValueProviders(
|
|||||||
|
|
||||||
if (isString(token)) {
|
if (isString(token)) {
|
||||||
if (!customValues.map(x => x.label).includes(token.value)) {
|
if (!customValues.map(x => x.label).includes(token.value)) {
|
||||||
diagnostics.push({
|
invalidValue(diagnostics, token);
|
||||||
message: `Value '${token.value}' is not allowed`,
|
|
||||||
severity: DiagnosticSeverity.Error,
|
|
||||||
range: mapRange(token.range)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,6 +159,14 @@ async function validateValueProviders(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function invalidValue(diagnostics: Diagnostic[], token: StringToken) {
|
||||||
|
diagnostics.push({
|
||||||
|
message: `Value '${token.value}' is not valid`,
|
||||||
|
severity: DiagnosticSeverity.Error,
|
||||||
|
range: mapRange(token.range)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getProviderContext(
|
function getProviderContext(
|
||||||
documentUri: URI,
|
documentUri: URI,
|
||||||
template: WorkflowTemplate,
|
template: WorkflowTemplate,
|
||||||
@@ -230,3 +182,48 @@ function getProviderContext(
|
|||||||
);
|
);
|
||||||
return getWorkflowContext(documentUri, template, path);
|
return getWorkflowContext(documentUri, template, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateExpression(
|
||||||
|
diagnostics: Diagnostic[],
|
||||||
|
token: BasicExpressionToken,
|
||||||
|
contextProviderConfig: ContextProviderConfig | undefined
|
||||||
|
) {
|
||||||
|
// Validate the expression
|
||||||
|
for (const expression of token.originalExpressions || [token]) {
|
||||||
|
const allowedContexts = token.definition?.readerContext || [];
|
||||||
|
const {namedContexts, functions} = splitAllowedContext(allowedContexts);
|
||||||
|
|
||||||
|
let expr: Expr | undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const l = new Lexer(expression.expression);
|
||||||
|
const lr = l.lex();
|
||||||
|
|
||||||
|
const p = new Parser(lr.tokens, namedContexts, functions);
|
||||||
|
expr = p.parse();
|
||||||
|
} catch {
|
||||||
|
// Ignore any error here, we should've caught this earlier in the parsing process
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const context = getContext(namedContexts, contextProviderConfig);
|
||||||
|
|
||||||
|
const e = new Evaluator(expr, wrapDictionary(context));
|
||||||
|
e.evaluate();
|
||||||
|
|
||||||
|
// Any invalid context access would've thrown an error via the `ErrorDictionary`, for now we don't have to check the actual
|
||||||
|
// result of the evaluation.
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof AccessError) {
|
||||||
|
diagnostics.push({
|
||||||
|
message: `Context access might be invalid: ${e.keyName}`,
|
||||||
|
severity: DiagnosticSeverity.Warning,
|
||||||
|
range: mapRange(expression.range)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import {WorkflowContext} from "../context/workflow-context";
|
import {WorkflowContext} from "../context/workflow-context";
|
||||||
import {Value} from "./config";
|
import {Value, ValueProviderConfig} from "./config";
|
||||||
import {needs} from "./needs";
|
import {needs} from "./needs";
|
||||||
|
|
||||||
export const defaultValueProviders: {[key: string]: (workflowContext: WorkflowContext) => Value[]} = {
|
export const defaultValueProviders: ValueProviderConfig = {
|
||||||
needs,
|
needs,
|
||||||
"runs-on": () =>
|
"runs-on": async (_: WorkflowContext) =>
|
||||||
stringsToValues([
|
stringsToValues([
|
||||||
"ubuntu-latest",
|
"ubuntu-latest",
|
||||||
"ubuntu-18.04",
|
"ubuntu-18.04",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {WorkflowContext} from "../context/workflow-context";
|
import {WorkflowContext} from "../context/workflow-context";
|
||||||
import {Value} from "./config";
|
import {Value} from "./config";
|
||||||
|
|
||||||
export function needs(context: WorkflowContext): Value[] {
|
export async function needs(context: WorkflowContext): Promise<Value[]> {
|
||||||
if (!context.template) {
|
if (!context.template) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user