Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/GprTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ class PackageInfo
[Command(Description = "Publish a package")]
public class PushCommand : GprCommandBase
{
[Option(Description = "Don't fail if there's already a package on the server with the same version")]
public bool IgnoreDuplicate { get; set; } = false;

static IAsyncPolicy<IRestResponse> BuildRetryAsyncPolicy(int retryNumber, int retrySleepSeconds, int timeoutSeconds)
{
if (retryNumber <= 0)
Expand Down Expand Up @@ -491,14 +494,14 @@ protected override async Task<int> OnExecuteAsyncImpl(CommandLineApplication app
var retryPolicy = BuildRetryAsyncPolicy(Math.Max(0, Retries), 10, 300);

await packageFiles.ForEachAsync(
(packageFile, packageCancellationToken) => UploadPackageAsync(packageFile, nuGetVersion, token, retryPolicy, packageCancellationToken),
(packageFile, packageCancellationToken) => UploadPackageAsync(packageFile, nuGetVersion, token, retryPolicy, packageCancellationToken, IgnoreDuplicate),
(packageFile, exception) =>
{
Console.WriteLine($"[{packageFile.Filename}]: {exception.Message}");
}, cancellationToken, Math.Max(1, Concurrency));

static async Task UploadPackageAsync(PackageFile packageFile,
NuGetVersion nuGetVersion, string token, IAsyncPolicy<IRestResponse> retryPolicy, CancellationToken cancellationToken)
NuGetVersion nuGetVersion, string token, IAsyncPolicy<IRestResponse> retryPolicy, CancellationToken cancellationToken, bool ignoreDuplicates)
{
if (packageFile == null) throw new ArgumentNullException(nameof(packageFile));

Expand Down Expand Up @@ -529,11 +532,11 @@ static async Task UploadPackageAsync(PackageFile packageFile,
$"Size: {packageStream.Length} bytes. ");

await retryPolicy.ExecuteAndCaptureAsync(retryCancellationToken =>
UploadPackageAsyncImpl(packageFile, packageStream, token, retryCancellationToken), cancellationToken);
UploadPackageAsyncImpl(packageFile, packageStream, token, retryCancellationToken, ignoreDuplicates), cancellationToken);
}

static async Task<IRestResponse> UploadPackageAsyncImpl(PackageFile packageFile, MemoryStream packageStream, string token,
CancellationToken cancellationToken)
CancellationToken cancellationToken, bool ignoreDuplicates)
{
if (packageFile == null) throw new ArgumentNullException(nameof(packageFile));
if (packageStream == null) throw new ArgumentNullException(nameof(packageStream));
Expand All @@ -556,14 +559,25 @@ static async Task<IRestResponse> UploadPackageAsyncImpl(PackageFile packageFile,

var response = await client.ExecuteAsync(request, cancellationToken);

packageFile.IsUploaded = response.StatusCode == HttpStatusCode.OK;
packageFile.IsUploaded = false;

if (packageFile.IsUploaded)
if (response.StatusCode == HttpStatusCode.OK)
{
packageFile.IsUploaded = true;

Console.WriteLine($"[{packageFile.Filename}]: {response.Content}");
return response;
}

if (response.StatusCode == HttpStatusCode.Conflict && ignoreDuplicates)
{
packageFile.IsUploaded = true;

Console.WriteLine($"[{packageFile.Filename}]: {FindWarning(response)}");
Console.WriteLine($"[{packageFile.Filename}]: Ignoring duplicate package error.");
return response;
}

if (FindWarning(response) is { } warning)
{
Console.WriteLine($"[{packageFile.Filename}]: {warning}");
Expand Down