Skip to content

Commit

Permalink
(#508) Push switch to new NuGet library methods
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
TheCakeIsNaOH committed Jun 17, 2022
1 parent 764efa6 commit 8289892
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
35 changes: 19 additions & 16 deletions src/chocolatey/infrastructure.app/nuget/NugetPush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();
providers.AddRange(Repository.Provider.GetCoreV3());
PackageSource packageSource = new PackageSource(config.Sources);
SourceRepository sourceRepository = new SourceRepository(packageSource, providers);
PackageUpdateResource packageUpdateResource = sourceRepository.GetResource<PackageUpdateResource>();
var nupkgFilePaths = new List<string>() { 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))
{
Expand All @@ -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));
}
}
}
5 changes: 3 additions & 2 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 8289892

Please sign in to comment.