Skip to content

Commit

Permalink
Fallback to check for the catalog.yml starting from workspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
janstaelensskyline committed Oct 17, 2024
1 parent 7be20c7 commit 37fe6b0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
22 changes: 20 additions & 2 deletions CICD.Tools.CatalogUpload.Lib/CatalogMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public bool IsPreRelease()
/// <param name="fs">The file system interface to use for file operations.</param>
/// <param name="startPath">The starting directory or file path for the search.</param>
/// <returns>True if a catalog YAML file is found and applied, otherwise false.</returns>
public bool SearchAndApplyCatalogYaml(IFileSystem fs, string startPath)
public bool SearchAndApplyCatalogYamlAndReadMe(IFileSystem fs, string startPath)
{
string foundYaml;
if (fs.Directory.IsDirectory(startPath))
Expand All @@ -224,6 +224,22 @@ public bool SearchAndApplyCatalogYaml(IFileSystem fs, string startPath)
foundYaml = RecursiveFindClosestCatalogYaml(fs, directory, 5);
}

if (foundYaml == null)
{
// Last fallback. Check starting from working directory and down.
var currentDirectory = Directory.GetCurrentDirectory();
var searchPattern = "*.yml";

foundYaml = fs.Directory
.EnumerateFiles(currentDirectory, searchPattern, SearchOption.AllDirectories)
.FirstOrDefault(p =>
fs.Path.GetFileNameWithoutExtension(p)
.Equals("catalog", StringComparison.InvariantCultureIgnoreCase) ||
fs.Path.GetFileNameWithoutExtension(p)
.Equals("manifest", StringComparison.InvariantCultureIgnoreCase)
);
}

if (foundYaml == null) return false;

// use a yaml parser to grab the yaml file.
Expand All @@ -249,6 +265,8 @@ public bool SearchAndApplyCatalogYaml(IFileSystem fs, string startPath)
p.Tags?.ForEach(Tags.Add);
}

SearchAndApplyReadMe(fs, foundYaml);

return true;
}

Expand Down Expand Up @@ -360,7 +378,7 @@ public async Task<byte[]> ToCatalogZipAsync(IFileSystem fs, ISerializer serializ
}

private static string RecursiveFindClosestCatalogYaml(IFileSystem fs, string directory, int maxRecurse)
{
{
if (maxRecurse-- <= 0)
{
return null;
Expand Down
11 changes: 4 additions & 7 deletions CICD.Tools.CatalogUpload.Lib/CatalogMetaDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,19 @@ public CatalogMetaData FromArtifact(string pathToArtifact, string pathToReadme =
public CatalogMetaData FromCatalogYaml(IFileSystem fs, string startPath, string pathToReadme = null, string pathToImages = null)
{
var meta = new CatalogMetaData();
if (!meta.SearchAndApplyCatalogYaml(fs, startPath))
if (!meta.SearchAndApplyCatalogYamlAndReadMe(fs, startPath))
throw new InvalidOperationException("Unable to locate a catalog.yml or manifest.yml file within the provided directory/file or up to 5 parent directories.");

if (pathToReadme == null)
{
meta.SearchAndApplyReadMe(fs, startPath);
}
else
if (pathToReadme != null)
{
meta.PathToReadme = pathToReadme;
meta.PathToReadme = pathToReadme; ;
}

if (pathToImages != null)
{
meta.PathToImages = pathToImages;
}

return meta;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void SearchAndApplyCatalogYaml_ValidFile_ShouldApplyYamlData()
var metaData = new CatalogMetaData();

// Act
var success = metaData.SearchAndApplyCatalogYaml(mockFileSystem.Object, "test/path");
var success = metaData.SearchAndApplyCatalogYamlAndReadMe(mockFileSystem.Object, "test/path");

// Assert
success.Should().BeTrue();
Expand Down Expand Up @@ -265,7 +265,7 @@ public void SearchAndApplyCatalogYaml_ValidFile_ShouldOverwriteOrPreserveExistin
};

// Act
var success = metaData.SearchAndApplyCatalogYaml(mockFileSystem.Object, "test/path");
var success = metaData.SearchAndApplyCatalogYamlAndReadMe(mockFileSystem.Object, "test/path");

// Assert
success.Should().BeTrue();
Expand Down Expand Up @@ -293,7 +293,7 @@ public void SearchAndApplyCatalogYaml_NoYamlFile_ShouldReturnFalse()
var metaData = new CatalogMetaData();

// Act
var success = metaData.SearchAndApplyCatalogYaml(mockFileSystem.Object, "test/path");
var success = metaData.SearchAndApplyCatalogYamlAndReadMe(mockFileSystem.Object, "test/path");

// Assert
success.Should().BeFalse();
Expand All @@ -314,7 +314,7 @@ public void RecursiveFindClosestCatalogYaml_FoundInParentDirectory_ShouldReturnF
var metaData = new CatalogMetaData();

// Act
var foundFile = metaData.SearchAndApplyCatalogYaml(mockFileSystem.Object, "test/path");
var foundFile = metaData.SearchAndApplyCatalogYamlAndReadMe(mockFileSystem.Object, "test/path");

// Assert
foundFile.Should().BeTrue();
Expand Down
17 changes: 8 additions & 9 deletions CICD.Tools.CatalogUpload/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<int> ProcessVolatile(string pathToArtifact, string dmCatalogTo
{
// Order of priority first the content of the artifact. Then the provided yml file. Finally any arguments from the tool.
CatalogMetaData metaData = catalogMetaDataFactory.FromArtifact(pathToArtifact);
metaData.SearchAndApplyCatalogYaml(fs, pathToArtifact);
metaData.SearchAndApplyCatalogYamlAndReadMe(fs, pathToArtifact);

var artifact = new CatalogArtifact(pathToArtifact, service, fs, logger, metaData);

Expand Down Expand Up @@ -98,8 +98,7 @@ public async Task<int> ProcessWithRegistrationAsync(string dmCatalogToken, strin
{
// Order of priority first the content of the artifact. Then the provided yml file. Finally any arguments from the tool.
CatalogMetaData metaData = catalogMetaDataFactory.FromArtifact(pathToArtifact);
metaData.SearchAndApplyCatalogYaml(fs, pathToArtifact);
metaData.SearchAndApplyReadMe(fs, pathToArtifact);
metaData.SearchAndApplyCatalogYamlAndReadMe(fs, pathToArtifact);
ApplyOptionalArguments(optionalArguments, metaData);

var artifact = new CatalogArtifact(pathToArtifact, service, fs, logger, metaData);
Expand Down Expand Up @@ -212,37 +211,37 @@ private void ApplyOptionalArguments(OptionalRegistrationArguments optionalArgume
if (optionalArguments.UriSourceCode != null)
{
logger.LogDebug($"Overriding SourceCodeUri from '{metaData.SourceCodeUri}' to '{optionalArguments.UriSourceCode}'");
metaData.SourceCodeUri = optionalArguments.UriSourceCode;
metaData.SourceCodeUri = optionalArguments.UriSourceCode.Trim();
}

if (optionalArguments.OverrideVersion != null)
{
logger.LogDebug($"Overriding Version from '{metaData.Version.Value}' to '{optionalArguments.OverrideVersion}'");
metaData.Version.Value = optionalArguments.OverrideVersion;
metaData.Version.Value = optionalArguments.OverrideVersion.Trim();
}

if (optionalArguments.Branch != null)
{
logger.LogDebug($"Overriding Branch from '{metaData.Version.Branch}' to '{optionalArguments.Branch}'");
metaData.Version.Branch = optionalArguments.Branch;
metaData.Version.Branch = optionalArguments.Branch.Trim();
}

if (optionalArguments.CommitterMail != null)
{
logger.LogDebug($"Overriding CommitterMail from '{metaData.Version.CommitterMail}' to '{optionalArguments.CommitterMail}'");
metaData.Version.CommitterMail = optionalArguments.CommitterMail;
metaData.Version.CommitterMail = optionalArguments.CommitterMail.Trim();
}

if (optionalArguments.ReleaseUri != null)
{
logger.LogDebug($"Overriding ReleaseUri from '{metaData.Version.ReleaseUri}' to '{optionalArguments.ReleaseUri}'");
metaData.Version.ReleaseUri = optionalArguments.ReleaseUri;
metaData.Version.ReleaseUri = optionalArguments.ReleaseUri.Trim();
}

if (optionalArguments.CatalogIdentifier != null)
{
logger.LogDebug($"Overriding CatalogIdentifier from '{metaData.CatalogIdentifier}' to '{optionalArguments.CatalogIdentifier}'");
metaData.CatalogIdentifier = optionalArguments.CatalogIdentifier;
metaData.CatalogIdentifier = optionalArguments.CatalogIdentifier.Trim();
}
}
}
Expand Down

0 comments on commit 37fe6b0

Please sign in to comment.