public async Task <IRemote> GetRemoteAsync(string repoUrl, ILogger logger) { using (_operations.BeginOperation($"Getting remote for repo {repoUrl}.")) { // Normalize the url with the AzDO client prior to attempting to // get a token. When we do coherency updates we build a repo graph and // may end up traversing links to classic azdo uris. string normalizedUrl = AzureDevOpsClient.NormalizeUrl(repoUrl); Uri normalizedRepoUri = new Uri(normalizedUrl); // Look up the setting for where the repo root should be held. Default to empty, // which will use the temp directory. string temporaryRepositoryRoot = _configuration.GetValue <string>("DarcTemporaryRepoRoot", null); if (string.IsNullOrEmpty(temporaryRepositoryRoot)) { temporaryRepositoryRoot = _tempFiles.GetFilePath("repos"); } IGitRepo gitClient; long installationId = await _context.GetInstallationId(normalizedUrl); var gitExe = await ExponentialRetry.RetryAsync( async() => await _localGit.GetPathToLocalGitAsync(), ex => logger.LogError(ex, $"Failed to install git to local temporary directory."), ex => true); switch (normalizedRepoUri.Host) { case "github.com": if (installationId == default) { throw new GithubApplicationInstallationException($"No installation is avaliable for repository '{normalizedUrl}'"); } gitClient = new GitHubClient(gitExe, await _gitHubTokenProvider.GetTokenForInstallationAsync(installationId), logger, temporaryRepositoryRoot, _cache.Cache); break; case "dev.azure.com": gitClient = new AzureDevOpsClient(gitExe, await _azureDevOpsTokenProvider.GetTokenForRepository(normalizedUrl), logger, temporaryRepositoryRoot); break; default: throw new NotImplementedException($"Unknown repo url type {normalizedUrl}"); } ; return(new Remote(gitClient, new MaestroBarClient(_context), logger)); } }
/// <summary> /// Github authentication. /// </summary> /// <param name="issueRepo">Repository where gitHub issue is created.</param> /// <returns>Authenticated GithubClient</returns> private async Task <GitHubClient> AuthenticateGitHubClient(string issueRepo) { IGitHubTokenProvider gitHubTokenProvider = _context.GetService <IGitHubTokenProvider>(); long installationId = await _context.GetInstallationId(issueRepo); string gitHubToken = await gitHubTokenProvider.GetTokenForInstallationAsync(installationId); _logger.LogInformation($"GitHub token acquired for '{issueRepo}'"); string version = Assembly.GetExecutingAssembly() .GetCustomAttribute <AssemblyInformationalVersionAttribute>() .InformationalVersion; ProductHeaderValue product = new ProductHeaderValue("Maestro", version); return(new GitHubClient(product) { Credentials = new Credentials(gitHubToken), }); }