Skip to content

Commit

Permalink
Maestro Logger registration, git location, token retrieval fix (#3706)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkurepa authored Jul 9, 2024
1 parent 9159489 commit 2946d9c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Maestro/FeedCleanerService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.DotNet.ServiceFabric.ServiceHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace FeedCleanerService;

Expand Down Expand Up @@ -52,6 +53,9 @@ public static void Configure(IServiceCollection services)
services.AddAzureDevOpsTokenProvider();
services.Configure<AzureDevOpsTokenProviderOptions>("AzureDevOps", (o, s) => s.Bind(o));
services.AddTransient<IAzureDevOpsClient, AzureDevOpsClient>();
services.AddTransient<IProcessManager>(sp => ActivatorUtilities.CreateInstance<ProcessManager>(sp, "git"));
services.AddTransient<IProcessManager>(sp =>
new ProcessManager(
sp.GetRequiredService<ILogger<ProcessManager>>(),
"git"));
}
}
5 changes: 4 additions & 1 deletion src/Maestro/Maestro.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ public override void ConfigureServices(IServiceCollection services)
// in such a way that will work with sizing.
services.AddSingleton<DarcRemoteMemoryCache>();

services.AddTransient<IProcessManager>(sp => ActivatorUtilities.CreateInstance<ProcessManager>(sp, "git"));
services.AddTransient<IProcessManager>(sp =>
new ProcessManager(
sp.GetRequiredService<ILogger<ProcessManager>>(),
"git"));
services.AddTransient<IVersionDetailsParser, VersionDetailsParser>();
services.AddScoped<IRemoteFactory, DarcRemoteFactory>();
services.AddTransient<IBasicBarClient, SqlBarClient>();
Expand Down
2 changes: 0 additions & 2 deletions src/Maestro/SubscriptionActorService/DarcRemoteFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ private async Task<IRemoteGitRepo> GetRemoteGitClient(string repoUrl, ILogger lo
throw new GithubApplicationInstallationException($"No installation is available for repository '{normalizedUrl}'");
}

var gitExe = _localGit.GetPathToLocalGit();

return GitRepoUrlParser.ParseTypeFromUri(normalizedUrl) switch
{
GitRepoType.GitHub => installationId == default
Expand Down
3 changes: 2 additions & 1 deletion src/Maestro/SubscriptionActorService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ private static void Main()
public static void Configure(IServiceCollection services)
{
services.TryAddTransient<ILogger>(sp => sp.GetRequiredService<ILogger<SubscriptionActor>>());
services.AddTransient<IProcessManager>(sp => ActivatorUtilities.CreateInstance<ProcessManager>(sp, "git"));
services.AddTransient<IProcessManager>(sp =>
ActivatorUtilities.CreateInstance<ProcessManager>(sp, sp.GetRequiredService<ILocalGit>().GetPathToLocalGit()));
services.AddSingleton<IActionRunner, ActionRunner>();
services.AddSingleton<IMergePolicyEvaluator, MergePolicyEvaluator>();
services.AddTransient<ICoherencyUpdateResolver, CoherencyUpdateResolver>();
Expand Down
24 changes: 19 additions & 5 deletions src/Microsoft.DotNet.Darc/DarcLib/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private async Task<string> GetFileContentsAsync(string owner, string repo, strin
{
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/contents/{filePath}?ref={branch}",
_logger,
logFailure: false))
Expand Down Expand Up @@ -160,6 +161,7 @@ public async Task CreateBranchAsync(string repoUri, string newBranch, string bas

using (await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/branches/{newBranch}",
_logger,
retryCount: 0)) { }
Expand All @@ -168,6 +170,7 @@ public async Task CreateBranchAsync(string repoUri, string newBranch, string bas
body = JsonConvert.SerializeObject(githubRef, _serializerSettings);
using (await ExecuteRemoteGitCommandAsync(
new HttpMethod("PATCH"),
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/git/{gitRef}",
_logger,
body)) { }
Expand All @@ -181,6 +184,7 @@ public async Task CreateBranchAsync(string repoUri, string newBranch, string bas
body = JsonConvert.SerializeObject(githubRef, _serializerSettings);
using (await ExecuteRemoteGitCommandAsync(
HttpMethod.Post,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/git/refs",
_logger,
body)) { }
Expand Down Expand Up @@ -277,6 +281,7 @@ public async Task<IEnumerable<int>> SearchPullRequestsAsync(
JObject responseContent;
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"search/issues?q={query}",
_logger))
{
Expand All @@ -300,8 +305,11 @@ public async Task<PrStatus> GetPullRequestStatusAsync(string pullRequestUrl)
(string owner, string repo, int id) = ParsePullRequestUri(pullRequestUrl);

JObject responseContent;
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(HttpMethod.Get,
$"repos/{owner}/{repo}/pulls/{id}", _logger))
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/pulls/{id}",
_logger))
{
responseContent = JObject.Parse(await response.Content.ReadAsStringAsync());
}
Expand Down Expand Up @@ -748,6 +756,7 @@ private async Task<GitFile> GetGitItemImpl(string path, TreeItem treeItem, strin
/// <returns></returns>
private async Task<HttpResponseMessage> ExecuteRemoteGitCommandAsync(
HttpMethod method,
string repoUri,
string requestUri,
ILogger logger,
string body = null,
Expand All @@ -759,7 +768,7 @@ private async Task<HttpResponseMessage> ExecuteRemoteGitCommandAsync(
{
retryCount = 0;
}
using (HttpClient client = CreateHttpClient())
using (HttpClient client = CreateHttpClient(repoUri))
{
var requestManager = new HttpRequestManager(client, method, requestUri, logger, body, versionOverride, logFailure);
try
Expand All @@ -783,14 +792,14 @@ private async Task<HttpResponseMessage> ExecuteRemoteGitCommandAsync(
/// Create a new http client for talking to github.
/// </summary>
/// <returns>New http client</returns
private HttpClient CreateHttpClient()
private HttpClient CreateHttpClient(string repoUri)
{
var client = new HttpClient(new HttpClientHandler { CheckCertificateRevocationList = true })
{
BaseAddress = new Uri(GitHubApiUri)
};

var token = _tokenProvider.GetTokenForRepository(GitHubApiUri);
var token = _tokenProvider.GetTokenForRepository(repoUri);
if (token != null)
{
client.DefaultRequestHeaders.Add("Authorization", $"Token {token}");
Expand Down Expand Up @@ -820,6 +829,7 @@ public async Task<string> CheckIfFileExistsAsync(string repoUri, string filePath
JObject content;
using (response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/contents/{filePath}?ref={branch}",
_logger))
{
Expand Down Expand Up @@ -891,6 +901,7 @@ private async Task<string> GetLastCommitShaAsync(string owner, string repo, stri
JObject content;
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/commits/{branch}",
_logger))
{
Expand Down Expand Up @@ -1102,6 +1113,7 @@ private async Task GetCommitMapForPathAsync(

using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/contents/{path}?ref={assetsProducedInCommit}",
_logger))
{
Expand Down Expand Up @@ -1207,6 +1219,7 @@ public async Task<GitDiff> GitDiffAsync(string repoUri, string baseVersion, stri
JObject content;
using (HttpResponseMessage response = await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}/compare/{baseVersion}...{targetVersion}",
_logger))
{
Expand Down Expand Up @@ -1241,6 +1254,7 @@ public async Task<bool> RepoExistsAsync(string repoUri)
{
using (await ExecuteRemoteGitCommandAsync(
HttpMethod.Get,
$"https://github.com/{owner}/{repo}",
$"repos/{owner}/{repo}",
_logger,
logFailure: false)) { }
Expand Down

0 comments on commit 2946d9c

Please sign in to comment.