Skip to content

Commit dbf3ef5

Browse files
Catalog Publish Initial support added.
1 parent bc9d266 commit dbf3ef5

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

Sdk/CatalogService/HttpCatalogService.cs

+57-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,56 @@ private sealed class CatalogUploadResult
2828
public string AzureStorageId { get; set; }
2929
}
3030

31+
/// <summary>
32+
/// Artifact information returned from registering an artifact to the catalog.
33+
/// </summary>
34+
private sealed class CatalolRegisterResult
35+
{
36+
[JsonProperty("catalogId")]
37+
public string CatalogId { get; set; }
38+
}
39+
40+
/// <summary>
41+
/// Represents an exception that is thrown when a version already exists.
42+
/// </summary>
43+
internal sealed class VersionAlreadyExistsException : Exception
44+
{
45+
/// <summary>
46+
/// Gets the version that caused the exception.
47+
/// </summary>
48+
public string Version { get; }
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class.
52+
/// </summary>
53+
public VersionAlreadyExistsException()
54+
: base("The specified version already exists.")
55+
{
56+
}
57+
58+
/// <summary>
59+
/// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class
60+
/// with a specified error message.
61+
/// </summary>
62+
/// <param name="version">The conflicting version.</param>
63+
public VersionAlreadyExistsException(string version)
64+
: base($"The specified version '{version}' already exists.")
65+
{
66+
Version = version;
67+
}
68+
69+
/// <summary>
70+
/// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class
71+
/// with a specified error message and a reference to the inner exception that is the cause of this exception.
72+
/// </summary>
73+
/// <param name="version">The conflicting version.</param>
74+
/// <param name="innerException">The exception that is the cause of the current exception.</param>
75+
public VersionAlreadyExistsException(string version, Exception innerException)
76+
: base($"The specified version '{version}' already exists.")
77+
{
78+
Version = version;
79+
}
80+
}
3181

3282
private const string RegistrationPath = "api/key-catalog/v2-0/catalogs/register";
3383
private const string VersionUploadPathEnd = "/register/version";
@@ -66,8 +116,8 @@ public async Task<ArtifactUploadResult> RegisterCatalogAsync(byte[] catalogDetai
66116

67117
if (response.IsSuccessStatusCode)
68118
{
69-
var returnedResult = JsonConvert.DeserializeObject<CatalogUploadResult>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
70-
return new ArtifactUploadResult() { ArtifactId = returnedResult.AzureStorageId };
119+
var returnedResult = JsonConvert.DeserializeObject<CatalolRegisterResult>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
120+
return new ArtifactUploadResult() { ArtifactId = returnedResult.CatalogId };
71121
}
72122

73123
if (response.StatusCode is HttpStatusCode.Forbidden || response.StatusCode is HttpStatusCode.Unauthorized)
@@ -129,6 +179,11 @@ public async Task<ArtifactUploadResult> UploadVersionAsync(byte[] package, strin
129179
throw new AuthenticationException($"The version upload api returned a {response.StatusCode} response. Body: {body}");
130180
}
131181

182+
if(response.StatusCode is HttpStatusCode.Conflict && body.Contains("already exists."))
183+
{
184+
throw new VersionAlreadyExistsException(version);
185+
}
186+
132187
throw new InvalidOperationException($"The version upload api returned a {response.StatusCode} response. Body: {body}");
133188
}
134189
}

Sdk/Sdk.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<BuildOutputTargetFolder>Sdk</BuildOutputTargetFolder>
2424
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2525
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
26-
<PackageVersion>0.2.4</PackageVersion>
26+
<PackageVersion>0.2.8</PackageVersion>
2727
</PropertyGroup>
2828

2929
<ItemGroup>

Sdk/Tasks/PublishToCatalog.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Skyline.DataMiner.Sdk.Tasks
1818
using Skyline.DataMiner.Net.SLConfiguration;
1919
using Skyline.DataMiner.Sdk.CatalogService;
2020

21+
using static Skyline.DataMiner.Sdk.CatalogService.HttpCatalogService;
22+
2123
using Task = Microsoft.Build.Utilities.Task;
2224

2325
public class PublishToCatalog : Task, ICancelableTask
@@ -116,14 +118,21 @@ public override bool Execute()
116118
var cts = new CancellationTokenSource();
117119
var catalogService = CatalogServiceFactory.CreateWithHttp(new System.Net.Http.HttpClient());
118120
byte[] catalogData = fs.File.ReadAllBytes(catalogInfoPath);
119-
Log.LogMessage(MessageImportance.Low, $"Registering Catalog Metadata...");
121+
Log.LogMessage(MessageImportance.High, $"Registering Catalog Metadata...");
120122
var catalogResult = catalogService.RegisterCatalogAsync(catalogData, organizationKey, cts.Token).WaitAndUnwrapException();
121-
Log.LogMessage(MessageImportance.Low, $"Done");
123+
Log.LogMessage(MessageImportance.High, $"Done");
122124

123-
Log.LogMessage(MessageImportance.Low, $"Uploading new version...");
124-
byte[] packageData = fs.File.ReadAllBytes(packagePath);
125-
catalogService.UploadVersionAsync(packageData, fs.Path.GetFileName(packagePath), organizationKey, catalogResult.ArtifactId, PackageVersion, VersionComment, cts.Token).WaitAndUnwrapException();
126-
Log.LogMessage(MessageImportance.Low, $"Done");
125+
try
126+
{
127+
Log.LogMessage(MessageImportance.High, $"Uploading new version to {catalogResult.ArtifactId}...");
128+
byte[] packageData = fs.File.ReadAllBytes(packagePath);
129+
catalogService.UploadVersionAsync(packageData, fs.Path.GetFileName(packagePath), organizationKey, catalogResult.ArtifactId, PackageVersion, VersionComment, cts.Token).WaitAndUnwrapException();
130+
Log.LogMessage(MessageImportance.High, $"Done");
131+
}
132+
catch (VersionAlreadyExistsException)
133+
{
134+
Log.LogWarning("Version already exists! Only catalog details were updated.");
135+
}
127136

128137
if (cancel)
129138
{

0 commit comments

Comments
 (0)