Skip to content

Commit 36471ca

Browse files
Added SourceCodeUrl
1 parent 5727ad5 commit 36471ca

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

CICD.Tools.GitHubToCatalogYamlTests/CatalogManagerTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ public async Task ProcessCatalogYamlAsync_ShouldCreateNewManifestFile_WhenCatalo
106106
mockFileSystem.Verify(fs => fs.File.WriteAllText(manifestFilePath, It.IsAny<string>()), Times.Once); // Ensure it saves to catalog.yml
107107
}
108108

109+
[TestMethod]
110+
public async Task ProcessCatalogYamlAsync_ShouldSetSourceCodeUrl_WhenMissing()
111+
{
112+
// Arrange
113+
var repoName = "SkylineCommunications/KPN-AS-OutageReport";
114+
var yamlContent = "id: testId\nshort_description: test description"; // No SourceCodeUrl in the initial YAML
115+
var expectedSourceCodeUrl = $"https://github.com/{repoName}";
116+
117+
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(true);
118+
mockFileSystem.Setup(fs => fs.File.ReadAllText(catalogFilePath)).Returns(yamlContent); // Simulate existing catalog.yml
119+
120+
// Act
121+
await catalogManager.ProcessCatalogYamlAsync(repoName);
122+
123+
// Assert
124+
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => s.Contains($"source_code_url: {expectedSourceCodeUrl}"))), Times.Once);
125+
}
126+
127+
109128
[TestMethod]
110129
public async Task ProcessCatalogYamlAsync_ShouldAssignNewId_WhenIdIsMissing()
111130
{

Skyline.DataMiner.CICD.Tools.GitHubToCatalogYaml/CatalogManager.cs

+28-7
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
5959
CatalogYaml catalogYaml = CreateCatalogYaml(deserializer, out string filePath);
6060
CatalogYaml autoGeneratedCatalogYaml = CreateAutoGeneratedCatalogYaml(deserializer, out string autoGenFilePath);
6161

62-
await CheckId(catalogYaml, autoGeneratedCatalogYaml, catalogIdentifier);
62+
CheckId(catalogYaml, autoGeneratedCatalogYaml, catalogIdentifier);
6363

6464
await CheckShortDescription(catalogYaml);
6565

66+
CheckSourceCodeUrl(catalogYaml, repoName);
67+
6668
await CheckTags(catalogYaml);
6769

6870
CleanTitle parsedRepoName = CheckTitle(repoName, catalogYaml);
@@ -76,17 +78,19 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
7678
// Save both the catalog.yml and the auto-generated-catalog.yml
7779
// The auto-generated-catalog.yml will be committed & pushed by the pipeline when changed.
7880
SaveFile(catalogYaml, serializer, outputPath);
79-
SaveFile(catalogYaml, serializer, autoGeneratedOutputPath);
80-
logger.LogInformation($"Finished. Updated or Created auto-generated file with path: {outputPath}");
8181
logger.LogInformation($"Finished. Updated or Created file with path: {outputPath}");
82+
SaveFile(catalogYaml, serializer, autoGeneratedOutputPath);
83+
logger.LogInformation($"Finished. Updated or Created auto-generated file with path: {autoGeneratedOutputPath}");
8284
}
8385

8486
/// <summary>
85-
/// Checks if the catalog ID exists in the YAML file, and if not, it retrieves or generates a new catalog ID from GitHub and updates the file accordingly.
87+
/// Checks and sets the ID field in the provided catalog YAML.
88+
/// If the ID is not present or is empty, it attempts to retrieve or create a new ID using the provided catalog identifier, or the ID from the auto-generated catalog YAML.
8689
/// </summary>
8790
/// <param name="catalogYaml">The catalog YAML object to check and update.</param>
88-
/// <returns>A task that represents the asynchronous operation.</returns>
89-
private async Task CheckId(CatalogYaml catalogYaml, CatalogYaml autoGeneratedCatalogYaml, string catalogIdentifier)
91+
/// <param name="autoGeneratedCatalogYaml">The auto-generated catalog YAML object used to retrieve the ID if necessary.</param>
92+
/// <param name="catalogIdentifier">The catalog identifier to be used if no ID is present in the catalog YAML.</param>
93+
private void CheckId(CatalogYaml catalogYaml, CatalogYaml autoGeneratedCatalogYaml, string catalogIdentifier)
9094
{
9195
logger.LogDebug("Checking if ID was user provided, otherwise retrieve or create it...");
9296
if (String.IsNullOrWhiteSpace(catalogYaml.Id))
@@ -129,6 +133,23 @@ private async Task CheckShortDescription(CatalogYaml catalogYaml)
129133
}
130134
}
131135

136+
/// <summary>
137+
/// Checks and sets the SourceCodeUrl field in the provided catalog YAML.
138+
/// If the SourceCodeUrl is not present or is empty, it assigns a GitHub repository URL based on the provided repository name.
139+
/// </summary>
140+
/// <param name="catalogYaml">The catalog YAML object to check and update.</param>
141+
/// <param name="repoName">The name of the GitHub repository to generate the SourceCodeUrl.</param>
142+
private void CheckSourceCodeUrl(CatalogYaml catalogYaml, string repoName)
143+
{
144+
logger.LogDebug("Checking if SourceCodeUrl exists, otherwise create sourcecode url from repository name...");
145+
if (String.IsNullOrWhiteSpace(catalogYaml.SourceCodeUrl))
146+
{
147+
var sourceCodeUrl = "https://github.com/" + repoName;
148+
catalogYaml.SourceCodeUrl = sourceCodeUrl;
149+
logger.LogDebug($"Sourcecode url applied: {sourceCodeUrl}");
150+
}
151+
}
152+
132153
/// <summary>
133154
/// Checks if the tags exist in the YAML file. If they are missing, it retrieves repository topics from GitHub and adds them to the YAML file, ensuring that the tags are distinct.
134155
/// </summary>
@@ -321,7 +342,7 @@ private void SaveFile(CatalogYaml catalogYaml, ISerializer serializer, string ou
321342
Thread.Sleep(500);
322343
var updatedYaml = serializer.Serialize(catalogYaml);
323344

324-
var parentDir = fs.Path.GetDirectoryName(outputPath);
345+
var parentDir = fs.Path.GetDirectoryName(outputPath);
325346
fs.Directory.CreateDirectory(parentDir);
326347
fs.Directory.TryAllowWritesOnDirectory(parentDir);
327348

0 commit comments

Comments
 (0)