コード例 #1
0
        public async Task <IRemote> GetRemoteAsync(string repoUrl, ILogger logger)
        {
            using (logger.BeginScope($"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);

                await ExponentialRetry.RetryAsync(
                    async() => await EnsureLocalGit(logger),
                    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(_gitExecutable, await GitHubTokenProvider.GetTokenForInstallationAsync(installationId),
                                                 logger, temporaryRepositoryRoot, Cache.Cache);
                    break;

                case "dev.azure.com":
                    gitClient = new AzureDevOpsClient(_gitExecutable, await AzureDevOpsTokenProvider.GetTokenForRepository(normalizedUrl),
                                                      logger, temporaryRepositoryRoot);
                    break;

                default:
                    throw new NotImplementedException($"Unknown repo url type {normalizedUrl}");
                }
                ;

                return(new Remote(gitClient, new MaestroBarClient(Context), logger));
            }
        }
コード例 #2
0
        public async Task <IRemote> GetRemoteAsync(string repoUrl, ILogger logger)
        {
            // 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 = Path.GetTempPath();
            }
            IGitRepo gitClient;

            long installationId = await Context.GetInstallationId(normalizedUrl);

            switch (normalizedRepoUri.Host)
            {
            case "github.com":
                if (installationId == default)
                {
                    throw new SubscriptionException($"No installation is avaliable for repository '{normalizedUrl}'");
                }

                gitClient = new GitHubClient(await GitHubTokenProvider.GetTokenForInstallation(installationId),
                                             logger, temporaryRepositoryRoot);
                break;

            case "dev.azure.com":
                gitClient = new AzureDevOpsClient(await AzureDevOpsTokenProvider.GetTokenForRepository(normalizedUrl),
                                                  logger, temporaryRepositoryRoot);
                break;

            default:
                throw new NotImplementedException($"Unknown repo url type {normalizedUrl}");
            }
            ;

            return(new Remote(gitClient, new MaestroBarClient(Context), logger));
        }