Skip to content

Commit

Permalink
Fixes bug where topics dont get filtered if a type is defined in a cu…
Browse files Browse the repository at this point in the history
…stom yaml file.
  • Loading branch information
janstaelensskyline committed Jan 8, 2025
1 parent 98e2af6 commit 9175247
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
34 changes: 34 additions & 0 deletions CICD.Tools.GitHubToCatalogYamlTests/CatalogManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,40 @@ public async Task ProcessCatalogYamlAsync_ShouldFilterTagsThatMatchTypes()
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => Regex.IsMatch(s, @"tags:\s*\n\s*-\s*newTag\s*\n\s*-\s*otherTag\s*(\n\s*#.*|\s*)*$", RegexOptions.Multiline))), Times.Once);
}

[TestMethod]
public async Task ProcessCatalogYamlAsync_ShouldFilterTagsThatMatchTypes_NoNameType()
{
// Arrange
var repoName = "SLC-testRepo";
var yamlContent = "id: testId\nshort_description: test description";
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(true); // catalog.yml exists
mockFileSystem.Setup(fs => fs.File.ReadAllText(catalogFilePath)).Returns(yamlContent);
mockGitHubService.Setup(s => s.GetRepositoryTopicsAsync()).ReturnsAsync(new List<string> { "dataminer-automation-script", "newTag", "dataminer", "otherTag", "dataminer-connector" });

// Act
await catalogManager.ProcessCatalogYamlAsync(repoName);

// Assert
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => Regex.IsMatch(s, @"tags:\s*\n\s*-\s*newTag\s*\n\s*-\s*otherTag\s*(\n\s*#.*|\s*)*$", RegexOptions.Multiline))), Times.Once);
}

[TestMethod]
public async Task ProcessCatalogYamlAsync_ShouldFilterTagsThatMatchTypes_TypeInYaml()
{
// Arrange
var repoName = "SLC-testRepo";
var yamlContent = "type: Automation Script\nid: testId\nshort_description: test description";
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(true); // catalog.yml exists
mockFileSystem.Setup(fs => fs.File.ReadAllText(catalogFilePath)).Returns(yamlContent);
mockGitHubService.Setup(s => s.GetRepositoryTopicsAsync()).ReturnsAsync(new List<string> { "dataminer-automation-script", "newTag", "dataminer", "otherTag", "dataminer-connector" });

// Act
await catalogManager.ProcessCatalogYamlAsync(repoName);

// Assert
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => Regex.IsMatch(s, @"tags:\s*\n\s*-\s*newTag\s*\n\s*-\s*otherTag\s*(\n\s*#.*|\s*)*$", RegexOptions.Multiline))), Times.Once);
}

[TestMethod]
public async Task ProcessCatalogYamlAsync_ShouldUseRepositoryNameAsTitle_WhenTitleIsMissing()
{
Expand Down
29 changes: 22 additions & 7 deletions Skyline.DataMiner.CICD.Tools.GitHubToCatalogYaml/CatalogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
// Perform this after CheckTags.
CheckType(catalogYaml, parsedRepoName);

// Cleanup tags, only after checking type
RemoveTagsThatDealWithType(catalogYaml);

string outputPath = filePath ?? fs.Path.Combine(workspace, "catalog.yml");
string autoGeneratedOutputPath = autoGenFilePath ?? fs.Path.Combine(workspace, ".githubtocatalog", "auto-generated-catalog.yml");

Expand Down Expand Up @@ -245,7 +248,6 @@ private void CheckType(CatalogYaml catalogYaml, CleanTitle parsedRepoName)
// Always check the tags
if (catalogYaml.Tags != null)
{
List<string> filteredTopics = new List<string>();
foreach (var topic in catalogYaml.Tags)
{
var inferredType = InferArtifactContentType(topic);
Expand All @@ -254,13 +256,7 @@ private void CheckType(CatalogYaml catalogYaml, CleanTitle parsedRepoName)
catalogYaml.Type = inferredType;
logger.LogDebug($"Item Type could be inferred from repository topics {catalogYaml.Type}.");
}
else
{
filteredTopics.Add(topic);
}
}

catalogYaml.Tags = filteredTopics;
}

if (String.IsNullOrWhiteSpace(catalogYaml.Type))
Expand All @@ -270,6 +266,25 @@ private void CheckType(CatalogYaml catalogYaml, CleanTitle parsedRepoName)
}
}

private void RemoveTagsThatDealWithType(CatalogYaml catalogYaml)
{
// Always check the tags
if (catalogYaml.Tags != null)
{
List<string> filteredTopics = new List<string>();
foreach (var topic in catalogYaml.Tags)
{
var inferredType = InferArtifactContentType(topic);
if (String.IsNullOrWhiteSpace(inferredType))
{
filteredTopics.Add(topic);
}
}

catalogYaml.Tags = filteredTopics;
}
}

/// <summary>
/// Creates a new catalog YAML object by reading the existing catalog.yml or manifest.yml file in the workspace.
/// If no file is found, it creates an empty catalog YAML object.
Expand Down

0 comments on commit 9175247

Please sign in to comment.