Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit Syntax Validation for pipelines #373

Merged
merged 5 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading;
using System.Threading.Tasks;
using GitHub.DistributedTask.Expressions2;
using GitHub.DistributedTask.Expressions2.Sdk;
using GitHub.DistributedTask.Expressions2.Sdk.Functions;
using GitHub.DistributedTask.ObjectTemplating;
using GitHub.DistributedTask.ObjectTemplating.Schema;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
Expand Down Expand Up @@ -655,11 +657,117 @@ public static string RelativeTo(string cwd, string filename) {
token = TemplateReader.Read(templateContext, schemaName ?? "pipeline-root", yamlObjectReader, fileId, out _);
}

foreach(var stepCond in token.TraverseByPattern(new [] { "steps", "*", "condition" })
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "steps", "*", "condition" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "steps", "*", "condition" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "strategy", "", "steps", "*", "condition" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "strategy", "on", "", "steps", "*", "condition" }))
) {
CheckConditionalExpressions(templateContext.Errors, stepCond, Level.Step);
}
foreach(var jobCond in token.TraverseByPattern(new [] { "jobs", "*", "condition" })
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "condition" }))
) {
CheckConditionalExpressions(templateContext.Errors, jobCond, Level.Job);
}
foreach(var stageCond in token.TraverseByPattern(new [] { "stages", "*", "condition" })
) {
CheckConditionalExpressions(templateContext.Errors, stageCond, Level.Stage);
}
foreach(var runtimeExpr in token.TraverseByPattern(new [] { "variables", "*", "value" })
.Concat(token.TraverseByPattern(new [] { "variables", "" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "variables", "*", "value" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "variables", "" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "variables", "*", "value" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "variables", "" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "continueOnError" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "timeoutInMinutes" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "cancelTimeoutInMinutes" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "container" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "container", "alias" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "container", "image" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "strategy", "matrix" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "strategy", "maxParallel" }))
.Concat(token.TraverseByPattern(new [] { "stages", "*", "jobs", "*", "strategy", "parallel" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "variables", "*", "value" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "variables", "" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "continueOnError" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "timeoutInMinutes" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "cancelTimeoutInMinutes" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "container" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "container", "alias" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "container", "image" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "strategy", "matrix" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "strategy", "maxParallel" }))
.Concat(token.TraverseByPattern(new [] { "jobs", "*", "strategy", "parallel" }))
) {
CheckSingleRuntimeExpression(templateContext.Errors, runtimeExpr);
}

templateContext.Errors.Check();

return (errorTemplateFileName, token);
}

private static void CheckSingleRuntimeExpression(TemplateValidationErrors errors, TemplateToken rawVal)
{
if(rawVal == null || rawVal.Type == TokenType.Null || !(rawVal is LiteralToken) || rawVal.Type == TokenType.BasicExpression) {
return;
}
var val = rawVal.AssertLiteralString("runtime expression");
if (val == null || !(val.StartsWith("$[") && val.EndsWith("]")))
{
return;
}
try {
var parser = new ExpressionParser() { Flags = ExpressionFlags.DTExpressionsV1 | ExpressionFlags.ExtendedDirectives | ExpressionFlags.AllowAnyForInsert };
var node = parser.CreateTree(val.Substring(2, val.Length - 3), new EmptyTraceWriter().ToExpressionTraceWriter(),
new[] { "variables", "resources", "pipeline", "dependencies", "stageDependencies" }.Select(n => new NamedValueInfo<NoOperationNamedValue>(n)),
ExpressionConstants.AzureWellKnownFunctions.Where(kv => kv.Key != "split").Select(kv => kv.Value).Append(new FunctionInfo<NoOperation>("counter", 0, 2)));
} catch (Exception ex) {
errors.Add($"{GitHub.DistributedTask.ObjectTemplating.Tokens.TemplateTokenExtensions.GetAssertPrefix(rawVal)}{ex.Message}");
}
}

private enum Level {
Stage,
Job,
Step
}

private static void CheckConditionalExpressions(TemplateValidationErrors errors, TemplateToken rawCondition, Level level)
{

if(rawCondition == null || rawCondition.Type == TokenType.Null || !(rawCondition is LiteralToken) || rawCondition.Type == TokenType.BasicExpression) {
return;
}
var val = rawCondition.AssertLiteralString("condition");
IEnumerable<string> names = new[] {"variables"};
switch(level) {
case Level.Stage:
names = names.Concat(new[] {"pipeline", "dependencies"});
break;
case Level.Job:
names = names.Concat(new[] {"pipeline", "dependencies", "stageDependencies"});
break;
default:
break;
}
var funcs = new List<IFunctionInfo>();
funcs.Add(new FunctionInfo<NoOperation>(PipelineTemplateConstants.Always, 0, 0));
funcs.Add(new FunctionInfo<NoOperation>("Canceled", 0, 0));
funcs.Add(new FunctionInfo<NoOperation>("Failed", 0, Int32.MaxValue));
funcs.Add(new FunctionInfo<NoOperation>("Succeeded", 0, Int32.MaxValue));
funcs.Add(new FunctionInfo<NoOperation>("SucceededOrFailed", 0, Int32.MaxValue));
try {
var parser = new ExpressionParser() { Flags = ExpressionFlags.DTExpressionsV1 | ExpressionFlags.ExtendedDirectives | ExpressionFlags.AllowAnyForInsert };
var node = parser.CreateTree(val, new EmptyTraceWriter().ToExpressionTraceWriter(),
names.Select(n => new NamedValueInfo<NoOperationNamedValue>(n)),
ExpressionConstants.AzureWellKnownFunctions.Where(kv => kv.Key != "split").Select(kv => kv.Value).Concat(funcs));
} catch (Exception ex) {
errors.Add($"{GitHub.DistributedTask.ObjectTemplating.Tokens.TemplateTokenExtensions.GetAssertPrefix(rawCondition)}{ex.Message}");
}
}

public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.Context context, string filenameAndRef, Dictionary<string, TemplateToken> cparameters = null, string schemaName = null)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,15 @@ private ExpressionToken ParseExpression(
{
return null;
}
else if (extendedDirectives && MatchesDirective(trimmed, "if", 1, out parameters, out ex))
else if (extendedDirectives && MatchesDirective(trimmed, "if", 1, out parameters, out ex) && ExpressionToken.IsValidExpression(parameters[0], allowedContext, out ex, m_context.Flags))
{
return new IfExpressionToken(m_fileId, line, column, parameters[0]);
}
else if (ex != null)
{
return null;
}
else if (extendedDirectives && MatchesDirective(trimmed, "elseif", 1, out parameters, out ex))
else if (extendedDirectives && MatchesDirective(trimmed, "elseif", 1, out parameters, out ex) && ExpressionToken.IsValidExpression(parameters[0], allowedContext, out ex, m_context.Flags))
{
return new ElseIfExpressionToken(m_fileId, line, column, parameters[0]);
}
Expand All @@ -726,7 +726,7 @@ private ExpressionToken ParseExpression(
{
return null;
}
else if (extendedDirectives && MatchesDirective(trimmed, "each", 3, out parameters, out ex) && parameters[1] == "in")
else if (extendedDirectives && MatchesDirective(trimmed, "each", 3, out parameters, out ex) && parameters[1] == "in" && ExpressionToken.IsValidExpression(parameters[2], allowedContext, out ex, m_context.Flags))
{
return new EachExpressionToken(m_fileId, line, column, parameters[0], parameters[2]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,53 @@ public static IEnumerable<string> CheckUnknownParameters(
}
}

/// <summary>
/// Returns all tokens (depth first)
/// </summary>
public static IEnumerable<TemplateToken> TraverseByPattern(
this TemplateToken token,
string[] pattern)
{
int level = 0;
if (token != null)
{
if (token is SequenceToken && pattern[level] == "*" || token is MappingToken)
{
var state = new TraversalState(null, token);
while (state != null)
{
if (state.MoveNext(false))
{
token = state.Current;
bool skip = false;
if (state.IsMapping && pattern[level] != "" && !(token is ExpressionToken) && token.AssertLiteralString("") != pattern[level]) {
skip = true;
}
if(state.IsMapping) {
state.MoveNext(false);
token = state.Current;

if(!skip && level + 1 == pattern.Length) {
yield return token;
}
}

if (!skip && level + 1 < pattern.Length && (token is SequenceToken && pattern[level + 1] == "*" || token is MappingToken))
{
state = new TraversalState(state, token);
level++;
}
}
else
{
state = state.Parent;
level--;
}
}
}
}
}

/// <summary>
/// Returns all tokens (depth first)
/// </summary>
Expand Down Expand Up @@ -328,6 +375,8 @@ public TraversalState(
m_token = token;
}

public bool IsMapping { get => m_token.Type == TokenType.Mapping; }

public bool MoveNext(bool omitKeys)
{
switch (m_token.Type)
Expand Down
6 changes: 6 additions & 0 deletions src/azure-pipelines-vscode-ext/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### v0.0.23 (Preview)
- check for syntax errors by default as soon as the current file is detected as a pipeline
- file type azure-pipelines is always checked
- file type yaml once it is valid yaml syntax and contains some azure pipelines structure
- can be disabled in settings

### v0.0.22 (Preview)
- add lost string value support for task steps like continueOnError, retryCount and timeoutInMinutes

Expand Down
8 changes: 6 additions & 2 deletions src/azure-pipelines-vscode-ext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ The `azure-pipelines-vscode-ext.repositories` settings maps the external Reposit

Syntax `[<owner>/]<repo>@<ref>=<uri>` per line. `<uri>` can be formed like `file:///<folder>` (raw file paths are not supported (yet?)), `vscode-vfs://github/<owner>/<repository>` and `vscode-vfs://azurerepos/<owner>/<project>/<repository>`

### Check Syntax Azure Pipeline

`> Check Syntax Azure Pipeline`

This command explicitly checks for syntax errors in the yaml structure and expression syntaxes. These are necessary but not sufficient checks for a successful Validation of the Azure Pipeline File.

### Validate Azure Pipeline

`> Validate Azure Pipeline`

This command tries to evaluate your current open Azure Pipeline including templates and notifies you about the result.

_Once this extension has been activated by any command, you can validate your pipeline via a statusbar button with the same name on all yaml or azure-pipelines documents_

### Expand Azure Pipeline

`> Expand Azure Pipeline`
Expand Down
82 changes: 73 additions & 9 deletions src/azure-pipelines-vscode-ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const vscode = require('vscode');
import { basePaths, customImports } from "./config.js"
import { AzurePipelinesDebugSession } from "./azure-pipelines-debug";
import { integer } from "vscode-languageclient";
import jsYaml from "js-yaml";

/**
* @param {vscode.ExtensionContext} context
Expand Down Expand Up @@ -463,22 +464,72 @@ function activate(context) {

context.subscriptions.push(vscode.commands.registerCommand('azure-pipelines-vscode-ext.validateAzurePipeline', () => validateAzurePipelineCommand()));

context.subscriptions.push(vscode.commands.registerCommand('azure-pipelines-vscode-ext.checkSyntaxAzurePipeline', () => checkSyntaxAzurePipelineCommand()));

context.subscriptions.push(vscode.commands.registerCommand('extension.expandAzurePipeline', () => expandAzurePipelineCommand()));

context.subscriptions.push(vscode.commands.registerCommand('extension.validateAzurePipeline', () => validateAzurePipelineCommand()));

var statusbar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
context.subscriptions.push(statusbar);
statusbar.tooltip = "Configure this Icon in Settings";

statusbar.text = "Validate Azure Pipeline";
var update = function(status) {
statusbar.text = `$(${status}) Azure Pipelines Tools`;
}
update("pass");
var syntaxChecks = vscode.languages.createDiagnosticCollection("Syntax Checks");
statusbar.command = {
command: 'azure-pipelines-vscode-ext.checkSyntaxAzurePipeline',
arguments: [null, syntaxChecks]
};

statusbar.command = 'azure-pipelines-vscode-ext.validateAzurePipeline';
context.subscriptions.push(syntaxChecks);
context.subscriptions.push(vscode.commands.registerCommand(statusbar.command.command, (file, collection) => {
if(collection) {
var hasError = false;
update("sync~spin");
expandAzurePipeline(false, null, null, null, () => {
if(!hasError) {
update("pass");
}
}, null, () => {
hasError = true;
update("error");
}, null, collection, null, true, true);
} else {
checkSyntaxAzurePipelineCommand();
}
}));

var checkIsPipeline = () => {
try {
var obj = jsYaml.load(vscode.window.activeTextEditor.document.getText());
return ((obj.trigger || obj.pr || obj.resources && (obj.resources.builds instanceof Array || obj.resources.containers instanceof Array || obj.resources.pipelines instanceof Array || obj.resources.repositories instanceof Array || obj.resources.webhooks instanceof Array || obj.resources.packages instanceof Array) || obj.schedules instanceof Array || obj.lockBehavior instanceof String || obj.variables instanceof Object || obj.variables instanceof Array || obj.parameters instanceof Object || obj.parameters instanceof Array) && (obj.extends && obj.extends.template || obj.stages instanceof Array || obj.jobs instanceof Array || obj.steps instanceof Array)
|| obj.steps instanceof Array && obj.steps.find(x => x.task || x.script !== undefined || x.bash !== undefined || x.pwsh !== undefined || x.powershell !== undefined || x.template !== undefined)
|| obj.jobs.find(x => x.job !== undefined || x.deployment !== undefined || x.template !== undefined)
|| obj.stages.find(x => x.stage !== undefined || x.template !== undefined)
);
} catch {

}
return false;
}

var onLanguageChanged = languageId => {
if(languageId === "azure-pipelines" || languageId === "yaml") {
statusbar.show();
var conf = vscode.workspace.getConfiguration("azure-pipelines-vscode-ext");
if(languageId === "azure-pipelines" || languageId === "yaml" && checkIsPipeline()) {

if(conf.get("disable-status-bar")) {
statusbar.hide();
} else {
statusbar.show();
}
if(!conf.get("disable-auto-syntax-check")) {
vscode.commands.executeCommand(statusbar.command.command, null, syntaxChecks);
}
} else {
if(vscode.window.activeTextEditor && vscode.window.activeTextEditor.document) {
syntaxChecks.set(vscode.window.activeTextEditor.document.uri, []);
}
statusbar.hide();
}
};
Expand All @@ -490,9 +541,22 @@ function activate(context) {
});

var onTextEditChanged = texteditor => onLanguageChanged(texteditor && texteditor.document && texteditor.document.languageId ? texteditor.document.languageId : null);
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(onTextEditChanged))
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(document => onLanguageChanged(document && document.languageId ? document.languageId : null)));
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(document => onLanguageChanged(document && document.languageId ? document.languageId : null)));
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(onTextEditChanged));
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(ev => {
if(vscode.window.activeTextEditor.document === ev.document) {
onLanguageChanged(ev.document && ev.document.languageId ? ev.document.languageId : null);
}
}));
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(document => {
if(vscode.window.activeTextEditor.document === document) {
onLanguageChanged(null);
}
}));
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(document => {
if(vscode.window.activeTextEditor.document === document) {
onLanguageChanged(document && document.languageId ? document.languageId : null);
}
}));
onTextEditChanged(vscode.window.activeTextEditor);
var executor = new vscode.CustomExecution(async def => {
const writeEmitter = new vscode.EventEmitter();
Expand Down
Loading
Loading