Skip to content

Commit 10d6d76

Browse files
authored
Merge pull request #4473 from TrymBeast/Nuget-CredentialsProviders
GH2028: Adding credential support for inprocess nuget client
2 parents fddc1d9 + fd72093 commit 10d6d76

File tree

7 files changed

+84
-11
lines changed

7 files changed

+84
-11
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ jobs:
4747
id: build-cake
4848
uses: cake-build/cake-action@v3.0.1
4949
env:
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5051
AZURE_DEVOPS_NUGET_API_KEY: ${{ secrets.AZURE_DEVOPS_NUGET_API_KEY }}
5152
AZURE_DEVOPS_NUGET_API_URL: ${{ secrets.AZURE_DEVOPS_NUGET_API_URL }}
5253
with:

src/Cake.NuGet/Cake.NuGet.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</ItemGroup>
1919
<!-- Global packages -->
2020
<ItemGroup>
21+
<PackageReference Include="NuGet.Credentials"/>
2122
<PackageReference Include="NuGet.Frameworks"/>
2223
<PackageReference Include="NuGet.Versioning" />
2324
<PackageReference Include="NuGet.Protocol" />

src/Cake.NuGet/Client/NuGetLogger.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ public NuGetLogger(ICakeLog log)
5050
_log = log ?? throw new ArgumentNullException(nameof(log));
5151
}
5252

53-
public void LogDebug(string data) => _log.Debug(data);
53+
public void LogDebug(string data) => _log.Debug("{0}", data);
5454

55-
public void LogVerbose(string data) => _log.Debug(data);
55+
public void LogVerbose(string data) => _log.Debug("{0}", data);
5656

57-
public void LogInformation(string data) => _log.Debug(data);
57+
public void LogInformation(string data) => _log.Debug("{0}", data);
5858

59-
public void LogMinimal(string data) => _log.Information(data);
59+
public void LogMinimal(string data) => _log.Information("{0}", data);
6060

61-
public void LogWarning(string data) => _log.Warning(data);
61+
public void LogWarning(string data) => _log.Warning("{0}", data);
6262

63-
public void LogError(string data) => _log.Error(data);
63+
public void LogError(string data) => _log.Error("{0}", data);
6464

65-
public void LogInformationSummary(string data) => _log.Information(data);
65+
public void LogInformationSummary(string data) => _log.Information("{0}", data);
6666

67-
public void LogErrorSummary(string data) => _log.Error(data);
67+
public void LogErrorSummary(string data) => _log.Error("{0}", data);
6868

69-
public void Log(LogLevel level, string data) => _log.Write(GetVerbosity(level), GetLogLevel(level), data);
69+
public void Log(LogLevel level, string data) => _log.Write(GetVerbosity(level), GetLogLevel(level), "{0}", data);
7070

7171
public Task LogAsync(LogLevel level, string data)
7272
{

src/Cake.NuGet/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static class NuGet
2727
/// The config key name for overriding the default NuGet config file.
2828
/// </summary>
2929
public const string ConfigFile = "NuGet_ConfigFile";
30+
31+
/// <summary>
32+
/// The config key name for non-interactive mode.
33+
/// </summary>
34+
public const string NonInteractive = "NuGet_NonInteractive";
3035
}
3136

3237
public static class Paths

src/Cake.NuGet/Installers/InProcessInstaller.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Cake.Core.IO;
1313
using NuGet.Common;
1414
using NuGet.Configuration;
15+
using NuGet.Credentials;
1516
using NuGet.Frameworks;
1617
using NuGet.Packaging;
1718
using NuGet.Packaging.Core;
@@ -114,6 +115,14 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType
114115
allRepositories.AddRange(localAndPrimaryRepositories);
115116
allRepositories.AddRange(sourceRepositoryProvider.Repositories);
116117

118+
var nonInteractiveString = _config.GetValue(Constants.NuGet.NonInteractive) ?? bool.TrueString;
119+
if (!bool.TryParse(nonInteractiveString, out bool nonInteractive))
120+
{
121+
// If there is no explicit preference, use non interactive.
122+
nonInteractive = true;
123+
}
124+
DefaultCredentialServiceUtility.SetupDefaultCredentialService(_nugetLogger, nonInteractive);
125+
117126
var packageIdentity = GetPackageId(package, localAndPrimaryRepositories, targetFramework, _sourceCacheContext, _nugetLogger);
118127
if (packageIdentity == null)
119128
{

src/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PackageVersion Include="NuGet.Protocol" Version="$(NuGetVersion)" />
2929
<PackageVersion Include="NuGet.Resolver" Version="$(NuGetVersion)" />
3030
<PackageVersion Include="NuGet.Versioning" Version="$(NuGetVersion)" />
31+
<PackageVersion Include="NuGet.Credentials" Version="$(NuGetVersion)" />
3132
<PackageVersion Include="Spectre.Console" Version="0.54.0" />
3233
<PackageVersion Include="Spectre.Console.Cli" Version="0.53.1" />
3334
<PackageVersion Include="Spectre.Console.Testing" Version="0.54.0" />
@@ -59,4 +60,4 @@
5960
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="8.0.1" />
6061
</ItemGroup>
6162

62-
</Project>
63+
</Project>

tests/integration/Cake.NuGet/InProcessInstaller.cake

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,65 @@ Task("Cake.NuGet.InProcessInstaller.VersionPinRange.Wildcard")
133133
});
134134
});
135135

136+
Task("Cake.NuGet.InProcessInstaller.DotNetTool.AuthenticatedFeed")
137+
.IsDependentOn("Cake.NuGet.InProcessInstaller.Setup")
138+
.WithCriteria(() => BuildSystem.GitHubActions.IsRunningOnGitHubActions || Argument<string>("target", "Default") == "Cake.NuGet.InProcessInstaller.DotNetTool.AuthenticatedFeed")
139+
.Does(()=>
140+
{
141+
var scriptPath = Paths.Temp.Combine("./Cake.NuGet.InProcessInstaller/").CombineWithFilePath("build.cake");
142+
var scriptToolsPath = Paths.Temp.Combine("./Cake.NuGet.InProcessInstaller/tools");
143+
var nugetConfigPath = Paths.Temp.Combine("./Cake.NuGet.InProcessInstaller/").CombineWithFilePath("nuget.config");
144+
145+
// Create NuGet.config without credentials
146+
var githubToken = EnvironmentVariable("GITHUB_TOKEN");
147+
148+
var nugetConfig =
149+
"""
150+
<?xml version="1.0" encoding="utf-8"?>
151+
<configuration>
152+
<packageSources>
153+
<clear />
154+
<add key="github" value="https://nuget.pkg.github.com/devlead/index.json" />
155+
</packageSources>
156+
</configuration>
157+
""";
158+
System.IO.File.WriteAllText(nugetConfigPath.FullPath, nugetConfig);
159+
160+
// If githubToken is provided, update the source with credentials using DotNetNuGetUpdateSource
161+
if (!string.IsNullOrEmpty(githubToken))
162+
{
163+
var sourceSettings = new DotNetNuGetSourceSettings
164+
{
165+
Source = "https://nuget.pkg.github.com/devlead/index.json",
166+
UserName = GitHubActions.Environment.Workflow.RepositoryOwner,
167+
Password = githubToken,
168+
StorePasswordInClearText = true,
169+
ConfigFile = nugetConfigPath
170+
};
171+
DotNetNuGetUpdateSource("github", sourceSettings);
172+
}
173+
174+
var script = @"#addin nuget:https://nuget.pkg.github.com/devlead/index.json?package=litjson&version=0.19.0-alpha0044&prerelease
175+
var result = LitJson.JsonMapper.ToJson(new { Name = ""John"", Age = 30 });
176+
";
177+
178+
System.IO.File.WriteAllText(scriptPath.FullPath, script);
179+
180+
CakeExecuteScript(scriptPath,
181+
new CakeSettings
182+
{
183+
EnvironmentVariables = {
184+
{ "CAKE_PATHS_TOOLS", scriptToolsPath.FullPath },
185+
{ "CAKE_PATHS_ADDINS", scriptToolsPath.Combine("Addins").FullPath },
186+
{ "CAKE_PATHS_MODULES", scriptToolsPath.Combine("Modules").FullPath }
187+
}
188+
});
189+
});
190+
136191
Task("Cake.NuGet.InProcessInstaller")
137192
.IsDependentOn("Cake.NuGet.InProcessInstaller.Setup")
138193
.IsDependentOn("Cake.NuGet.InProcessInstaller.VersionPinExact")
139194
.IsDependentOn("Cake.NuGet.InProcessInstaller.VersionPinRange.InclusiveExclusive")
140195
.IsDependentOn("Cake.NuGet.InProcessInstaller.VersionPinRange.InclusiveInclusive")
141-
.IsDependentOn("Cake.NuGet.InProcessInstaller.VersionPinRange.Wildcard");
196+
.IsDependentOn("Cake.NuGet.InProcessInstaller.VersionPinRange.Wildcard")
197+
.IsDependentOn("Cake.NuGet.InProcessInstaller.DotNetTool.AuthenticatedFeed");

0 commit comments

Comments
 (0)