From 82898928cd25ac87bdc3af1f0a786ccf3fb67ea6 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Thu, 16 Jun 2022 20:04:32 -0500 Subject: [PATCH] (#508) Push switch to new NuGet library methods This switches the nuget push to use the methods used by the updated nuget libraries. It uses the same general codepath as the NuGet CLI does (via the NuGet.Commands PushRunner) --- .../infrastructure.app/nuget/NugetPush.cs | 35 ++++++++++--------- .../services/NugetService.cs | 5 +-- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/chocolatey/infrastructure.app/nuget/NugetPush.cs b/src/chocolatey/infrastructure.app/nuget/NugetPush.cs index a80acf19dc..9a09ee146e 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetPush.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetPush.cs @@ -17,19 +17,17 @@ namespace chocolatey.infrastructure.app.nuget { using System; - using System.Collections; using System.Collections.Generic; - using System.IO; + using System.Threading.Tasks; using configuration; using logging; - using NuGet.Protocol; - using NuGet.Commands; + using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol.Core.Types; public class NugetPush { - public static void push_package(ChocolateyConfiguration config, string nupkgFilePath) + public static void push_package(ChocolateyConfiguration config, string nupkgFilePath, ILogger nugetLogger, string nupkgFileName) { var timeout = TimeSpan.FromSeconds(Math.Abs(config.CommandExecutionTimeoutSeconds)); if (timeout.Seconds <= 0) @@ -38,23 +36,28 @@ public static void push_package(ChocolateyConfiguration config, string nupkgFile } const bool disableBuffering = false; - var packageServer = new PackageServer(config.Sources, ApplicationParameters.UserAgent); + // Controls adding /api/v2/packages to the end of the source url + const bool noServiceEndpoint = true; - packageServer.SendingRequest += (sender, e) => { if (config.Verbose) "chocolatey".Log().Info(ChocolateyLoggers.Verbose, "{0} {1}".format_with(e.Request.Method, e.Request.RequestUri)); }; - - var package = new OptimizedZipPackage(nupkgFilePath); + List> providers = new List>(); + providers.AddRange(Repository.Provider.GetCoreV3()); + PackageSource packageSource = new PackageSource(config.Sources); + SourceRepository sourceRepository = new SourceRepository(packageSource, providers); + PackageUpdateResource packageUpdateResource = sourceRepository.GetResource(); + var nupkgFilePaths = new List() { nupkgFilePath }; + UserAgent.SetUserAgentString(new UserAgentStringBuilder("{0}/{1} via NuGet Client".format_with(ApplicationParameters.UserAgent, config.Information.ChocolateyProductVersion))); try { - packageServer.PushPackage( - config.PushCommand.Key, - package, - new FileInfo(nupkgFilePath).Length, - Convert.ToInt32(timeout.TotalMilliseconds), - disableBuffering); + var pushTask = Task.Run(async () => + { + await packageUpdateResource.Push(nupkgFilePaths, "", Convert.ToInt32(timeout.TotalSeconds), disableBuffering, endpoint => config.PushCommand.Key, null, noServiceEndpoint, true, null, nugetLogger); + }); + pushTask.Wait(); } catch (InvalidOperationException ex) { + var message = ex.Message; if (!string.IsNullOrWhiteSpace(message)) { @@ -73,7 +76,7 @@ public static void push_package(ChocolateyConfiguration config, string nupkgFile throw; } - "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(package.GetFullName(), config.Sources)); + "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} was pushed successfully to {1}".format_with(nupkgFileName, config.Sources)); } } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index f41f2999c6..85ec6e0acb 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -332,9 +332,10 @@ public void push_noop(ChocolateyConfiguration config) public virtual void push_run(ChocolateyConfiguration config) { string nupkgFilePath = validate_and_return_package_file(config, NuGetConstants.PackageExtension); - if (config.RegularOutput) this.Log().Info(() => "Attempting to push {0} to {1}".format_with(_fileSystem.get_file_name(nupkgFilePath), config.Sources)); + string nupkgFileName = _fileSystem.get_file_name(nupkgFilePath); + if (config.RegularOutput) this.Log().Info(() => "Attempting to push {0} to {1}".format_with(nupkgFileName, config.Sources)); - NugetPush.push_package(config, _fileSystem.get_full_path(nupkgFilePath)); + NugetPush.push_package(config, _fileSystem.get_full_path(nupkgFilePath), _nugetLogger, nupkgFileName); if (config.RegularOutput && (config.Sources.is_equal_to(ApplicationParameters.ChocolateyCommunityFeedPushSource) || config.Sources.is_equal_to(ApplicationParameters.ChocolateyCommunityFeedPushSourceOld)))