Skip to content

Commit

Permalink
Make local darc settings available in DI (#3658)
Browse files Browse the repository at this point in the history
  • Loading branch information
premun authored Jun 20, 2024
1 parent 2ad03e5 commit 6660885
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Darc/Darc/Helpers/LocalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static LocalSettings GetSettings(ICommandLineOptions options, ILogger log
{
if (!options.IsCi && options.OutputFormat != DarcOutputType.json)
{
logger.LogWarning(e, $"Failed to load the darc settings file, may be corrupted");
logger.LogInformation(e, $"Failed to load the darc settings file, may be corrupted");
}
}

Expand Down
17 changes: 6 additions & 11 deletions src/Microsoft.DotNet.Darc/Darc/Helpers/RemoteFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ public static IRemote GetRemote(ICommandLineOptions options, string repoUrl, ILo
}

public static IBarApiClient GetBarClient(ICommandLineOptions options, ILogger logger)
{
var settings = LocalSettings.GetSettings(options, logger);
return new BarApiClient(
settings?.BuildAssetRegistryToken,
options?.FederatedToken,
=> new BarApiClient(
options.BuildAssetRegistryToken,
options.FederatedToken,
managedIdentityId: null,
options.IsCi,
settings?.BuildAssetRegistryBaseUri);
}
options.BuildAssetRegistryBaseUri);

public Task<IRemote> GetRemoteAsync(string repoUrl, ILogger logger)
=> Task.FromResult(GetRemote(_options, repoUrl, logger));
Expand All @@ -47,8 +44,6 @@ public Task<IDependencyFileManager> GetDependencyFileManagerAsync(string repoUrl

private static IRemoteGitRepo GetRemoteGitClient(ICommandLineOptions options, string repoUrl, ILogger logger)
{
var darcSettings = LocalSettings.GetSettings(options, logger);

string temporaryRepositoryRoot = Path.GetTempPath();

var repoType = GitRepoUrlParser.ParseTypeFromUri(repoUrl);
Expand All @@ -58,7 +53,7 @@ private static IRemoteGitRepo GetRemoteGitClient(ICommandLineOptions options, st
GitRepoType.GitHub =>
new GitHubClient(
options.GitLocation,
darcSettings.GitHubToken,
options.GitHubPat,
logger,
temporaryRepositoryRoot,
// Caching not in use for Darc local client.
Expand All @@ -67,7 +62,7 @@ private static IRemoteGitRepo GetRemoteGitClient(ICommandLineOptions options, st
GitRepoType.AzureDevOps =>
new AzureDevOpsClient(
options.GitLocation,
darcSettings.AzureDevOpsToken,
options.AzureDevOpsPat,
logger,
temporaryRepositoryRoot),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ private async Task<int> PromoteBuildAsync(Build build, List<Channel> targetChann
return Constants.SuccessCode;
}

LocalSettings localSettings = LocalSettings.GetSettings(_options, Logger);
_options.AzureDevOpsPat = (string.IsNullOrEmpty(_options.AzureDevOpsPat)) ? localSettings.AzureDevOpsToken : _options.AzureDevOpsPat;

if (string.IsNullOrEmpty(_options.AzureDevOpsPat))
{
Console.WriteLine($"Promoting build {build.Id} with the given parameters would require starting the Build Promotion pipeline, however an AzDO PAT was not found.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,6 @@ private async Task<string> DownloadAssetFromAzureDevOpsFeedAsync(HttpClient clie

var packageContentUrl = $"https://pkgs.dev.azure.com/{feedAccount}/{feedProject}_apis/packaging/feeds/{feedName}/nuget/packages/{assetName}/versions/{asset.Version}/content";

if (string.IsNullOrEmpty(_options.AzureDevOpsPat))
{
var localSettings = LocalSettings.GetSettings(_options, Logger);
_options.AzureDevOpsPat = localSettings.AzureDevOpsToken;
}

var authHeader = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _options.AzureDevOpsPat))));
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.DotNet.Darc/Darc/Operations/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected Operation(ICommandLineOptions options, IServiceCollection? services =

Provider = services.BuildServiceProvider();
Logger = Provider.GetRequiredService<ILogger<Operation>>();
options.InitializeFromSettings(Logger);
}

public abstract Task<int> ExecuteAsync();
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.DotNet.Darc/Darc/Options/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using CommandLine;
using Microsoft.DotNet.Darc.Helpers;
using Microsoft.DotNet.Darc.Operations;
using Microsoft.DotNet.DarcLib;
using Microsoft.Extensions.Logging;

namespace Microsoft.DotNet.Darc.Options;

Expand Down Expand Up @@ -57,4 +59,13 @@ public RemoteConfiguration GetRemoteConfiguration()
{
return new RemoteConfiguration(GitHubPat, AzureDevOpsPat);
}

public void InitializeFromSettings(ILogger logger)
{
var localSettings = LocalSettings.GetSettings(this, logger);
AzureDevOpsPat ??= localSettings.AzureDevOpsToken;
GitHubPat ??= localSettings.GitHubToken;
BuildAssetRegistryBaseUri ??= localSettings.BuildAssetRegistryBaseUri;
BuildAssetRegistryToken ??= localSettings.BuildAssetRegistryToken;
}
}
6 changes: 6 additions & 0 deletions src/Microsoft.DotNet.Darc/Darc/Options/ICommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.DotNet.Darc.Operations;
using Microsoft.DotNet.DarcLib;
using Microsoft.Extensions.Logging;

namespace Microsoft.DotNet.Darc.Options;
public interface ICommandLineOptions
Expand All @@ -20,4 +21,9 @@ public interface ICommandLineOptions

Operation GetOperation();
RemoteConfiguration GetRemoteConfiguration();

/// <summary>
/// Reads missing options from the local settings.
/// </summary>
void InitializeFromSettings(ILogger logger);
}

0 comments on commit 6660885

Please sign in to comment.