Esempio n. 1
0
        private async Task <int> InvokeAsync(IEnumerable <string> arguments)
        {
            if (arguments.Count() == 0)
            {
                Utilities.WriteError($"Invalid number of arguments ({arguments.Count()} provided to command 'upload'; must specify at least one CSV to upload");
                return(1);
            }

            // Get the GitHub PAT and storage account connection string from key vault
            AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider();
            SecretBundle githubPat;
            SecretBundle storageAccountConnectionString;

            using (KeyVaultClient kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback)))
            {
                Console.WriteLine("Fetching PAT and connection string from key vault.");
                githubPat = await kv.GetSecretAsync(Utilities.KeyVaultUri, Utilities.GitHubPatSecretName);

                storageAccountConnectionString = await kv.GetSecretAsync(Utilities.KeyVaultUri, ScorecardsStorageAccount.KeySecretName);
            }

            return(await RolloutUploader.UploadResultsAsync(arguments.ToList(), Utilities.GetGithubClient(githubPat.Value), storageAccountConnectionString.Value));
        }
Esempio n. 2
0
        public async Task <int> InvokeAsync(IEnumerable <string> arguments)
        {
            Options.Parse(arguments);

            if (_showHelp)
            {
                Options.WriteOptionDescriptions(CommandSet.Out);
                return(0);
            }

            if (string.IsNullOrEmpty(_rolloutScorer.OutputFile))
            {
                _rolloutScorer.OutputFile = Path.Combine(Directory.GetCurrentDirectory(),
                                                         $"{_rolloutScorer.Repo}-{_rolloutScorer.RolloutStartDate.Date.ToShortDateString().Replace("/","-")}-scorecard.csv");
            }

            _rolloutScorer.RolloutWeightConfig = StandardConfig.DefaultConfig.RolloutWeightConfig;
            _rolloutScorer.GithubConfig        = StandardConfig.DefaultConfig.GithubConfig;

            // If they haven't told us to upload but they also haven't specified a repo & rollout start date, we need to throw
            if (string.IsNullOrEmpty(_rolloutScorer.Repo) || (_rolloutScorer.RolloutStartDate == null))
            {
                Utilities.WriteError($"ERROR: One or both of required parameters 'repo' and 'rollout-start-date' were not specified.");
                return(1);
            }

            _rolloutScorer.RepoConfig = StandardConfig.DefaultConfig.RepoConfigs.Find(r => r.Repo == _rolloutScorer.Repo);
            if (_rolloutScorer.RepoConfig == null)
            {
                Utilities.WriteError($"ERROR: Provided repo '{_rolloutScorer.Repo}' does not exist in config file");
                return(1);
            }

            _rolloutScorer.AzdoConfig = StandardConfig.DefaultConfig.AzdoInstanceConfigs.Find(a => a.Name == _rolloutScorer.RepoConfig.AzdoInstance);
            if (_rolloutScorer.AzdoConfig == null)
            {
                Utilities.WriteError($"ERROR: Configuration file is invalid; repo '{_rolloutScorer.RepoConfig.Repo}' " +
                                     $"references unknown AzDO instance '{_rolloutScorer.RepoConfig.AzdoInstance}'");
                return(1);
            }

            // Get the AzDO & GitHub PATs from key vault
            AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider();
            SecretBundle githubPat;
            SecretBundle storageAccountConnectionString;

            using (KeyVaultClient kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback)))
            {
                Console.WriteLine("Fetching PATs from key vault.");
                _rolloutScorer.SetupHttpClient((await kv.GetSecretAsync(_rolloutScorer.AzdoConfig.KeyVaultUri, _rolloutScorer.AzdoConfig.PatSecretName)).Value);
                githubPat = await kv.GetSecretAsync(Utilities.KeyVaultUri, Utilities.GitHubPatSecretName);

                _rolloutScorer.SetupGithubClient(githubPat.Value);
                storageAccountConnectionString = await kv.GetSecretAsync(Utilities.KeyVaultUri, ScorecardsStorageAccount.KeySecretName);
            }

            try
            {
                await _rolloutScorer.InitAsync();
            }
            catch (ArgumentException e)
            {
                Utilities.WriteError(e.Message);
                return(1);
            }

            Scorecard scorecard = await Scorecard.CreateScorecardAsync(_rolloutScorer);

            string expectedTimeToRollout = TimeSpan.FromMinutes(_rolloutScorer.RepoConfig.ExpectedTime).ToString();

            Console.WriteLine($"The {_rolloutScorer.Repo} {_rolloutScorer.RolloutStartDate.Date.ToShortDateString()} rollout score is {scorecard.TotalScore}.\n");
            Console.WriteLine($"|              Metric              |   Value  |  Target  |   Score   |");
            Console.WriteLine($"|:--------------------------------:|:--------:|:--------:|:---------:|");
            Console.WriteLine($"| Time to Rollout                  | {scorecard.TimeToRollout} | {expectedTimeToRollout} |     {scorecard.TimeToRolloutScore}     |");
            Console.WriteLine($"| Critical/blocking issues created |     {scorecard.CriticalIssues}    |    0     |     {scorecard.CriticalIssueScore}     |");
            Console.WriteLine($"| Hotfixes                         |     {scorecard.Hotfixes}    |    0     |     {scorecard.HotfixScore}     |");
            Console.WriteLine($"| Rollbacks                        |     {scorecard.Rollbacks}    |    0     |     {scorecard.RollbackScore}     |");
            Console.WriteLine($"| Service downtime                 | {scorecard.Downtime} | 00:00:00 |     {scorecard.DowntimeScore}     |");
            Console.WriteLine($"| Failed to rollout                |   {scorecard.Failure.ToString().ToUpperInvariant()}  |   FALSE  |     {(scorecard.Failure ? StandardConfig.DefaultConfig.RolloutWeightConfig.FailurePoints : 0)}     |");
            Console.WriteLine($"| Total                            |          |          |   **{scorecard.TotalScore}**   |");

            if (_rolloutScorer.Upload)
            {
                Console.WriteLine("Directly uploading results.");
                await RolloutUploader.UploadResultsAsync(new List <Scorecard> {
                    scorecard
                }, Utilities.GetGithubClient(githubPat.Value), storageAccountConnectionString.Value, _rolloutScorer.GithubConfig);
            }

            if (_rolloutScorer.SkipOutput)
            {
                Console.WriteLine("Skipping output step.");
            }
            else
            {
                if (await scorecard.Output(_rolloutScorer.OutputFile) != 0)
                {
                    return(1);
                }
                Console.WriteLine($"Wrote output to file {_rolloutScorer.OutputFile}");
            }

            return(0);
        }