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

azp: context filetable / wip improve error messages #259

Merged
merged 3 commits into from
Nov 2, 2023
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
9 changes: 7 additions & 2 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,23 +461,28 @@ public static string RelativeTo(string cwd, string filename) {

public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.Context context, string filenameAndRef, Dictionary<string, TemplateToken> cparameters = null, string schemaName = null) {
var variables = context.VariablesProvider?.GetVariablesForEnvironment("");
var templateContext = AzureDevops.CreateTemplateContext(context.TraceWriter ?? new EmptyTraceWriter(), new List<string>(), context.Flags);
var afilenameAndRef = filenameAndRef.Split("@", 2);
var filename = afilenameAndRef[0];
var fileId = templateContext.GetFileId(filename);
// Read the file
var finalRepository = afilenameAndRef.Length == 1 ? context.RepositoryAndRef : string.Equals(afilenameAndRef[1], "self", StringComparison.OrdinalIgnoreCase) ? null : (context.Repositories?.TryGetValue(afilenameAndRef[1], out var ralias) ?? false) ? ralias : throw new Exception($"Couldn't find repository with alias {afilenameAndRef[1]} in repository resources");
var finalFileName = RelativeTo(context.RepositoryAndRef == finalRepository ? context.CWD ?? "." : "/", filename);
if(finalFileName == null) {
throw new Exception($"Couldn't find template location {filenameAndRef}");
}

var fileContent = await context.FileProvider.ReadFile(finalRepository, finalFileName);
if(fileContent == null) {
throw new Exception($"Couldn't read template {filenameAndRef} resolved to {finalFileName} ({finalRepository ?? "self"})");
}
context.TraceWriter?.Info("{0}", $"Parsing template {filenameAndRef} resolved to {finalFileName} ({finalRepository ?? "self"}) using Schema {schemaName ?? "pipeline-root"}");
context.TraceWriter?.Verbose("{0}", fileContent);

var errorTemplateFileName = $"({finalRepository ?? "self"})/{finalFileName}";
context.FileTable ??= new List<string>();
context.FileTable.Add(errorTemplateFileName);
var templateContext = AzureDevops.CreateTemplateContext(context.TraceWriter ?? new EmptyTraceWriter(), context.FileTable, context.Flags);
var fileId = templateContext.GetFileId(errorTemplateFileName);

TemplateToken token;
using (var stringReader = new StringReader(fileContent))
{
Expand Down
1 change: 1 addition & 0 deletions src/Sdk/AzurePipelines/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Context {

public ITaskByNameAndVersionProvider TaskByNameAndVersion { get; set; }
public IRequiredParametersProvider RequiredParametersProvider { get; set; }
public List<string> FileTable { get; set; } = new List<string>();

public Context Clone() {
return MemberwiseClone() as Context;
Expand Down