Files
languageservices/actions-languageservice/src/validate.ts
T

239 lines
7.1 KiB
TypeScript
Raw Normal View History

2022-11-21 17:55:24 -08:00
import {Evaluator, Lexer, Parser} from "@github/actions-expressions";
import {Expr} from "@github/actions-expressions/ast";
import {
convertWorkflowTemplate,
isBasicExpression,
2022-11-28 15:47:05 -08:00
isSequence,
isString,
2022-11-21 17:55:24 -08:00
parseWorkflow,
2022-11-28 15:47:05 -08:00
ParseWorkflowResult,
WorkflowTemplate
2022-11-21 17:55:24 -08:00
} from "@github/actions-workflow-parser";
2022-11-22 10:36:17 -08:00
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";
2022-11-21 17:55:24 -08:00
import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/template-token";
2022-11-15 14:08:12 -05:00
import {TokenRange} from "@github/actions-workflow-parser/templates/tokens/token-range";
import {File} from "@github/actions-workflow-parser/workflows/file";
import {TextDocument} from "vscode-languageserver-textdocument";
2022-11-28 15:47:05 -08:00
import {Diagnostic, DiagnosticSeverity, Range, URI} from "vscode-languageserver-types";
2022-11-21 17:55:24 -08:00
import {ContextProviderConfig} from "./context-providers/config";
import {getContext} from "./context-providers/default";
2022-11-28 15:47:05 -08:00
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
2022-11-21 17:55:24 -08:00
import {AccessError, wrapDictionary} from "./expression-validation/error-dictionary";
2022-11-15 14:08:12 -05:00
import {nullTrace} from "./nulltrace";
2022-11-28 15:47:05 -08:00
import {findToken} from "./utils/find-token";
2022-11-28 16:15:57 -08:00
import {ValueProviderConfig, ValueProviderKind} from "./value-providers/config";
2022-11-28 15:47:05 -08:00
import {defaultValueProviders} from "./value-providers/default";
2022-11-08 17:00:59 -08:00
/**
* Validates a workflow file
*
* @param textDocument Document to validate
* @returns Array of diagnostics
*/
export async function validate(
2022-11-21 17:55:24 -08:00
textDocument: TextDocument,
2022-11-08 17:00:59 -08:00
// TODO: Support multiple files, context for API calls
2022-11-21 17:55:24 -08:00
valueProviderConfig?: ValueProviderConfig,
contextProviderConfig?: ContextProviderConfig
2022-11-08 17:00:59 -08:00
): Promise<Diagnostic[]> {
const file: File = {
name: textDocument.uri,
2022-11-15 14:08:12 -05:00
content: textDocument.getText()
2022-11-08 17:00:59 -08:00
};
2022-11-21 17:55:24 -08:00
const diagnostics: Diagnostic[] = [];
2022-11-08 17:00:59 -08:00
try {
2022-11-15 14:08:12 -05:00
const result: ParseWorkflowResult = parseWorkflow(file.name, [file], nullTrace);
2022-11-08 17:00:59 -08:00
if (result.value) {
// Errors will be updated in the context
2022-11-28 15:47:05 -08:00
const template = convertWorkflowTemplate(result.context, result.value);
2022-11-08 17:00:59 -08:00
// Validate expressions and value providers
await additionalValidations(
diagnostics,
textDocument.uri,
template,
result.value,
valueProviderConfig,
contextProviderConfig
);
2022-11-28 15:47:05 -08:00
}
2022-11-08 17:00:59 -08:00
2022-11-21 17:55:24 -08:00
// For now map parser errors directly to diagnostics
for (const error of result.context.errors.getErrors()) {
let range = mapRange(error.range);
diagnostics.push({
2022-11-08 17:00:59 -08:00
message: error.rawMessage,
2022-11-15 14:08:12 -05:00
range
2022-11-21 17:55:24 -08:00
});
}
2022-11-08 17:00:59 -08:00
} catch (e) {
// TODO: Handle error here
}
2022-11-21 17:55:24 -08:00
return diagnostics;
2022-11-08 17:00:59 -08:00
}
2022-11-21 17:55:24 -08:00
function mapRange(range: TokenRange | undefined): Range {
2022-11-08 17:00:59 -08:00
if (!range) {
2022-11-21 17:55:24 -08:00
return {
start: {
line: 1,
character: 1
},
end: {
line: 1,
character: 1
}
};
2022-11-08 17:00:59 -08:00
}
return {
start: {
line: range.start[0] - 1,
2022-11-15 14:08:12 -05:00
character: range.start[1] - 1
2022-11-08 17:00:59 -08:00
},
end: {
line: range.end[0] - 1,
2022-11-15 14:08:12 -05:00
character: range.end[1] - 1
}
2022-11-08 17:00:59 -08:00
};
}
2022-11-21 17:55:24 -08:00
async function additionalValidations(
2022-11-28 15:47:05 -08:00
diagnostics: Diagnostic[],
documentUri: URI,
template: WorkflowTemplate,
root: TemplateToken,
valueProviderConfig: ValueProviderConfig | undefined,
contextProviderConfig: ContextProviderConfig | undefined
2022-11-28 15:47:05 -08:00
) {
for (const token of TemplateToken.traverse(root)) {
// If this is an expression, validate it
if (isBasicExpression(token)) {
validateExpression(diagnostics, token, contextProviderConfig);
}
2022-11-28 15:47:05 -08:00
// 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.
if (valueProviderConfig && token.range && token.definition?.key) {
2022-11-28 15:47:05 -08:00
const defKey = token.definition.key;
2022-11-28 16:15:57 -08:00
// Try a custom value provider first
let valueProvider = valueProviderConfig[defKey];
if (!valueProvider) {
// fall back to default
valueProvider = defaultValueProviders[defKey];
2022-11-28 15:47:05 -08:00
}
2022-11-28 16:15:57 -08:00
if (valueProvider) {
const customValues = await valueProvider.get(getProviderContext(documentUri, template, root, token));
2022-11-28 15:47:05 -08:00
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)) {
2022-11-28 16:15:57 -08:00
invalidValue(diagnostics, entry, valueProvider.kind);
2022-11-28 15:47:05 -08:00
}
}
}
}
if (isString(token)) {
if (!customValues.map(x => x.label).includes(token.value)) {
2022-11-28 16:15:57 -08:00
invalidValue(diagnostics, token, valueProvider.kind);
2022-11-28 15:47:05 -08:00
}
}
}
}
}
}
2022-11-28 16:15:57 -08:00
function invalidValue(diagnostics: Diagnostic[], token: StringToken, kind: ValueProviderKind) {
switch (kind) {
case ValueProviderKind.AllowedValues:
diagnostics.push({
message: `Value '${token.value}' is not valid`,
severity: DiagnosticSeverity.Error,
range: mapRange(token.range)
});
break;
case ValueProviderKind.SuggestedValues:
diagnostics.push({
message: `Value '${token.value}' might not be valid`,
severity: DiagnosticSeverity.Warning,
range: mapRange(token.range)
});
2022-11-29 07:56:07 -08:00
break;
}
}
2022-11-28 15:47:05 -08:00
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);
}
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
}
}
}
}