Skip to content

Commit 1e44746

Browse files
created auto-generated-catalog to allow commit-push.
no longer tries to retrieve the environment variable within the code but expects it as input. will use the catalog identifier from the auto-generated-catalog if it exists.
1 parent b68e402 commit 1e44746

File tree

5 files changed

+58
-163
lines changed

5 files changed

+58
-163
lines changed

CICD.Tools.GitHubToCatalogYamlTests/CatalogManagerTests.cs

+2-23
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public async Task ProcessCatalogYamlAsync_ShouldCreateNewCatalogYaml_WhenNoFileE
5555

5656
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(false); // No catalog.yml exists
5757
mockFileSystem.Setup(fs => fs.File.Exists(manifestFilePath)).Returns(false); // No manifest.yml exists
58-
mockGitHubService.Setup(s => s.GetCatalogIdentifierAsync()).ReturnsAsync("newCatalogId");
5958
mockGitHubService.Setup(s => s.GetRepositoryDescriptionAsync()).ReturnsAsync("new description");
6059
mockGitHubService.Setup(s => s.GetRepositoryTopicsAsync()).ReturnsAsync(new List<string> { "newTopic" });
6160

@@ -107,10 +106,9 @@ public async Task ProcessCatalogYamlAsync_ShouldAssignNewId_WhenIdIsMissing()
107106
var yamlContent = "short_description: test description\ntags: [testTag]";
108107
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(true); // catalog.yml exists
109108
mockFileSystem.Setup(fs => fs.File.ReadAllText(catalogFilePath)).Returns(yamlContent);
110-
mockGitHubService.Setup(s => s.GetCatalogIdentifierAsync()).ReturnsAsync("newCatalogId");
111109

112110
// Act
113-
await catalogManager.ProcessCatalogYamlAsync(repoName);
111+
await catalogManager.ProcessCatalogYamlAsync(repoName, "newCatalogId");
114112

115113
// Assert
116114
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => s.Contains("id: newCatalogId"))), Times.Once);
@@ -240,8 +238,6 @@ public async Task ProcessCatalogYamlAsync_ShouldSaveYamlFileToCorrectPath_WhenNo
240238
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(false); // catalog.yml does not exist
241239
mockFileSystem.Setup(fs => fs.File.Exists(manifestFilePath)).Returns(false); // manifest.yml does not exist
242240

243-
mockGitHubService.Setup(s => s.GetCatalogIdentifierAsync()).ReturnsAsync("newCatalogId");
244-
245241
// Act
246242
await catalogManager.ProcessCatalogYamlAsync(repoName);
247243

@@ -283,7 +279,7 @@ public async Task ProcessCatalogYamlAsync_ShouldLogInformationMessage_WhenProces
283279
It.Is<It.IsAnyType>((v, t) => true),
284280
It.IsAny<Exception>(),
285281
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true))
286-
, Times.Exactly(2));
282+
, Times.AtLeast(2));
287283
}
288284

289285
[TestMethod]
@@ -300,23 +296,6 @@ public async Task ProcessCatalogYamlAsync_ShouldThrowException_WhenFileSystemThr
300296
await act.Should().ThrowAsync<Exception>().WithMessage("File system failure");
301297
}
302298

303-
[TestMethod]
304-
public async Task ProcessCatalogYamlAsync_ShouldThrowException_WhenGitHubServiceFailsToGetIdentifier()
305-
{
306-
// Arrange
307-
var repoName = "SLC-AS-testRepo";
308-
var yamlContent = "short_description: test description";
309-
mockFileSystem.Setup(fs => fs.File.Exists(catalogFilePath)).Returns(true);
310-
mockFileSystem.Setup(fs => fs.File.ReadAllText(catalogFilePath)).Returns(yamlContent);
311-
mockGitHubService.Setup(s => s.GetCatalogIdentifierAsync()).ThrowsAsync(new Exception("GitHub service failure"));
312-
313-
// Act
314-
Func<Task> act = async () => await catalogManager.ProcessCatalogYamlAsync(repoName);
315-
316-
// Assert
317-
await act.Should().ThrowAsync<Exception>().WithMessage("GitHub service failure");
318-
}
319-
320299
[TestMethod]
321300
public async Task ProcessCatalogYamlAsync_ShouldThrowInvalidOperationException_WhenTypeCannotBeInferred()
322301
{

CICD.Tools.GitHubToCatalogYamlTests/GitHubServiceTests.cs

-23
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,6 @@ public void Setup()
6666
_service = new GitHubService(httpClient, logger, _githubToken, _githubRepository);
6767
}
6868

69-
/// <summary>
70-
/// Integration test for creating and getting a catalog identifier variable in the GitHub repository.
71-
/// </summary>
72-
[TestMethod]
73-
public async Task CreateAndGetCatalogIdentifierAsyncTest()
74-
{
75-
// Arrange
76-
var guid = Guid.NewGuid().ToString();
77-
78-
// Act
79-
var createdOk = await _service.CreateCatalogIdentifierAsync(guid);
80-
Thread.Sleep(1000);
81-
var returnedGuid = await _service.GetCatalogIdentifierAsync();
82-
bool deleted = await _service.DeleteCatalogIdentifierAsync();
83-
84-
// Assert
85-
Assert.IsTrue(createdOk, "The catalog identifier was not successfully created in the GitHub repository.");
86-
Assert.IsNotNull(returnedGuid, "Failed to retrieve the catalog identifier from the GitHub repository.");
87-
returnedGuid.Should().Be(guid);
88-
deleted.Should().BeTrue();
89-
}
90-
91-
9269
/// <summary>
9370
/// Integration test for retrieving the repository description from the GitHub repository.
9471
/// </summary>

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

+56-16
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,9 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
5656
.Build();
5757

5858
CatalogYaml catalogYaml = CreateCatalogYaml(deserializer, out string filePath);
59+
CatalogYaml autoGeneratedCatalogYaml = CreateAutoGeneratedCatalogYaml(deserializer, out string autoGenFilePath);
5960

60-
if (String.IsNullOrWhiteSpace(catalogIdentifier))
61-
{
62-
await CheckId(catalogYaml);
63-
}
64-
else
65-
{
66-
catalogYaml.Id = catalogIdentifier;
67-
}
61+
await CheckId(catalogYaml, autoGeneratedCatalogYaml, catalogIdentifier);
6862

6963
await CheckShortDescription(catalogYaml);
7064

@@ -76,8 +70,13 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
7670
CheckType(catalogYaml, parsedRepoName);
7771

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

75+
// Save both the catalog.yml and the auto-generated-catalog.yml
76+
// The auto-generated-catalog.yml will be committed & pushed by the pipeline when changed.
8077
SaveFile(catalogYaml, serializer, outputPath);
78+
SaveFile(catalogYaml, serializer, autoGeneratedOutputPath);
79+
logger.LogInformation($"Finished. Updated or Created auto-generated file with path: {outputPath}");
8180
logger.LogInformation($"Finished. Updated or Created file with path: {outputPath}");
8281
}
8382

@@ -86,18 +85,25 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
8685
/// </summary>
8786
/// <param name="catalogYaml">The catalog YAML object to check and update.</param>
8887
/// <returns>A task that represents the asynchronous operation.</returns>
89-
private async Task CheckId(CatalogYaml catalogYaml)
88+
private async Task CheckId(CatalogYaml catalogYaml, CatalogYaml autoGeneratedCatalogYaml, string catalogIdentifier)
9089
{
91-
logger.LogDebug("Checking if ID exists, otherwise retrieve or create it...");
90+
logger.LogDebug("Checking if ID was user provided, otherwise retrieve or create it...");
9291
if (String.IsNullOrWhiteSpace(catalogYaml.Id))
9392
{
94-
var catalogId = await service.GetCatalogIdentifierAsync();
93+
var catalogId = catalogIdentifier;
9594
if (String.IsNullOrWhiteSpace(catalogId))
9695
{
97-
logger.LogDebug("Creating new ID...");
98-
catalogId = Guid.NewGuid().ToString();
99-
await service.CreateCatalogIdentifierAsync(catalogId);
100-
logger.LogDebug("New Catalog ID created and stored in GitHub Variable.");
96+
if (String.IsNullOrWhiteSpace(autoGeneratedCatalogYaml.Id))
97+
{
98+
logger.LogDebug("Creating new ID...");
99+
catalogId = Guid.NewGuid().ToString();
100+
logger.LogDebug("New Catalog ID created.");
101+
}
102+
else
103+
{
104+
logger.LogDebug("ID was previously generated and retrieved from auto-generated-catalog.yml.");
105+
catalogId = autoGeneratedCatalogYaml.Id;
106+
}
101107
}
102108

103109
catalogYaml.Id = catalogId;
@@ -249,6 +255,40 @@ private CatalogYaml CreateCatalogYaml(IDeserializer deserializer, out string fou
249255
return deserializer.Deserialize<CatalogYaml>(yamlContent) ?? new CatalogYaml();
250256
}
251257

258+
/// <summary>
259+
/// Creates a new catalog YAML object by reading the existing catalog.yml or manifest.yml file in the workspace.
260+
/// If no file is found, it creates an empty catalog YAML object.
261+
/// </summary>
262+
/// <param name="deserializer">The YAML deserializer to parse the existing YAML file.</param>
263+
/// <param name="foundFile">The path to the found YAML file, or null if no file is found.</param>
264+
/// <returns>The deserialized <see cref="CatalogYaml"/> object, or a new object if no file is found.</returns>
265+
private CatalogYaml CreateAutoGeneratedCatalogYaml(IDeserializer deserializer, out string foundFile)
266+
{
267+
logger.LogDebug("Checking if ");
268+
foundFile = null;
269+
270+
string filePath = fs.Path.Combine(workspace, ".githubtocatalog", "auto-generated-catalog.yml");
271+
if (!fs.File.Exists(filePath))
272+
{
273+
logger.LogDebug("No existing auto-generated configuration file found.");
274+
}
275+
else
276+
{
277+
foundFile = filePath;
278+
logger.LogDebug($"Found auto-generated file at: {filePath}");
279+
}
280+
281+
if (String.IsNullOrWhiteSpace(foundFile))
282+
{
283+
return new CatalogYaml();
284+
}
285+
286+
var yamlContent = fs.File.ReadAllText(filePath);
287+
logger.LogDebug("Existing Configuration File Parsed.");
288+
return deserializer.Deserialize<CatalogYaml>(yamlContent) ?? new CatalogYaml();
289+
}
290+
291+
252292
/// <summary>
253293
/// Infers the artifact content type based on the provided keyword. It checks if the keyword is in the artifact type map and returns the corresponding content type.
254294
/// </summary>
@@ -262,7 +302,7 @@ private static string InferArtifactContentType(string keyword)
262302
return contentType;
263303
}
264304

265-
return Constants.ArtifactTypeMap.FirstOrDefault(pair => pair.Value.Equals(keyword, StringComparison.OrdinalIgnoreCase)).Key ??
305+
return Constants.ArtifactTypeMap.FirstOrDefault(pair => pair.Value.Equals(keyword, StringComparison.OrdinalIgnoreCase)).Value ??
266306
String.Empty;
267307
}
268308

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

-82
Original file line numberDiff line numberDiff line change
@@ -32,88 +32,6 @@ public GitHubService(HttpClient httpClient, ILogger logger, string key, string g
3232
githubRoot = $"https://api.github.com/repos/{githubRepository}";
3333
}
3434

35-
/// <summary>
36-
/// Creates a GitHub repository variable named 'catalogIdentifier'.
37-
/// </summary>
38-
/// <param name="catalogIdentifier">The catalog identifier to store as a repository variable.</param>
39-
/// <returns>A task representing the asynchronous operation, containing a boolean indicating whether the operation was successful.</returns>
40-
public async Task<bool> CreateCatalogIdentifierAsync(string catalogIdentifier)
41-
{
42-
var requestUrl = $"{githubRoot}/actions/variables";
43-
var content = new StringContent(JsonSerializer.Serialize(new
44-
{
45-
name = "catalogIdentifier",
46-
value = catalogIdentifier
47-
}), System.Text.Encoding.UTF8, "application/json");
48-
49-
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
50-
{
51-
Content = content
52-
};
53-
request.Headers.Add("Authorization", $"Bearer {key}");
54-
request.Headers.Add("User-Agent", "GitHubToCatalogYaml");
55-
56-
var response = await _httpClient.SendAsync(request);
57-
if (response.IsSuccessStatusCode)
58-
{
59-
_logger.LogInformation("Successfully created catalogIdentifier in GitHub repository.");
60-
return true;
61-
}
62-
63-
_logger.LogError($"Failed to create catalogIdentifier in GitHub repository: {response.StatusCode}");
64-
return false;
65-
}
66-
67-
/// <summary>
68-
/// Retrieves the 'catalogIdentifier' variable from the GitHub repository.
69-
/// </summary>
70-
/// <returns>A task representing the asynchronous operation, containing the catalog identifier as a string, or null if the retrieval fails.</returns>
71-
public async Task<string> GetCatalogIdentifierAsync()
72-
{
73-
var requestUrl = $"{githubRoot}/actions/variables/catalogIdentifier";
74-
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
75-
request.Headers.Add("Authorization", $"Bearer {key}");
76-
request.Headers.Add("User-Agent", "GitHubToCatalogYaml");
77-
78-
var response = await _httpClient.SendAsync(request);
79-
if (response.IsSuccessStatusCode)
80-
{
81-
var jsonResponse = await response.Content.ReadAsStringAsync();
82-
var jsonDocument = JsonDocument.Parse(jsonResponse);
83-
if (jsonDocument.RootElement.TryGetProperty("value", out var catalogIdentifier))
84-
{
85-
return catalogIdentifier.GetString();
86-
}
87-
}
88-
89-
_logger.LogError($"Failed to retrieve catalogIdentifier from GitHub: {response.StatusCode}");
90-
return null;
91-
}
92-
93-
/// <summary>
94-
/// Deletes a GitHub repository variable named 'catalogIdentifier'.
95-
/// </summary>
96-
/// <returns>A task representing the asynchronous operation, containing a boolean indicating whether the operation was successful.</returns>
97-
public async Task<bool> DeleteCatalogIdentifierAsync()
98-
{
99-
var requestUrl = $"{githubRoot}/actions/variables/catalogIdentifier";
100-
101-
var request = new HttpRequestMessage(HttpMethod.Delete, requestUrl);
102-
request.Headers.Add("Authorization", $"Bearer {key}");
103-
request.Headers.Add("User-Agent", "GitHubToCatalogYaml");
104-
105-
var response = await _httpClient.SendAsync(request);
106-
if (response.IsSuccessStatusCode)
107-
{
108-
_logger.LogInformation("Successfully deleted catalogIdentifier in GitHub repository.");
109-
return true;
110-
}
111-
112-
_logger.LogError($"Failed to delete catalogIdentifier in GitHub repository: {response.StatusCode}");
113-
return false;
114-
}
115-
116-
11735
/// <summary>
11836
/// Retrieves the repository description from the GitHub repository.
11937
/// </summary>

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

-19
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@
88
/// </summary>
99
internal interface IGitHubService
1010
{
11-
/// <summary>
12-
/// Creates a GitHub repository variable named 'catalogIdentifier'.
13-
/// </summary>
14-
/// <param name="catalogIdentifier">The catalog identifier to store as a repository variable.</param>
15-
/// <returns>A task representing the asynchronous operation, containing a boolean indicating whether the operation was successful.</returns>
16-
Task<bool> CreateCatalogIdentifierAsync(string catalogIdentifier);
17-
18-
/// <summary>
19-
/// Deletes a GitHub repository variable named 'catalogIdentifier'.
20-
/// </summary>
21-
/// <returns>A task representing the asynchronous operation, containing a boolean indicating whether the operation was successful.</returns>
22-
Task<bool> DeleteCatalogIdentifierAsync();
23-
24-
/// <summary>
25-
/// Retrieves the 'catalogIdentifier' variable from the GitHub repository.
26-
/// </summary>
27-
/// <returns>A task representing the asynchronous operation, containing the catalog identifier as a string, or null if the retrieval fails.</returns>
28-
Task<string> GetCatalogIdentifierAsync();
29-
3011
/// <summary>
3112
/// Retrieves the repository description from the GitHub repository.
3213
/// </summary>

0 commit comments

Comments
 (0)