Exemplo n.º 1
0
        public static async Task <Credentials> GetCredentialsForAppAsync(BotOptions options)
        {
            var creds = new SigningCredentials(GetRsaSecurityKeyFromPemKey(options.AppKey), SecurityAlgorithms.RsaSha256);

            var jwtToken = new JwtSecurityToken(
                new JwtHeader(creds),
                new JwtPayload(
                    issuer: options.AppId,
                    issuedAt: DateTime.Now,
                    expires: DateTime.Now.Add(GitHubJwtTimeout),
                    audience: null,
                    claims: null,
                    notBefore: null));

            var jwtTokenString = new JwtSecurityTokenHandler().WriteToken(jwtToken);
            var initClient     = new GitHubClient(new ProductHeaderValue(AppName))
            {
                Credentials = new Credentials(jwtTokenString, AuthenticationType.Bearer),
            };

            var installationToken = await initClient.GitHubApps.CreateInstallationToken(options.InstallId);

            return(new Credentials(installationToken.Token, AuthenticationType.Bearer));
        }
Exemplo n.º 2
0
        private static async Task <int> Controller(BotOptions options)
        {
            // Validate arguments
            try
            {
                options.Validate();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);

                return(1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception: " + e.ToString());

                return(1);
            }

            // Substitute with ENV value if it exists

            if (!String.IsNullOrEmpty(options.ConnectionString) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.ConnectionString)))
            {
                options.ConnectionString = Environment.GetEnvironmentVariable(options.ConnectionString);
            }

            if (!String.IsNullOrEmpty(options.AccessToken) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.AccessToken)))
            {
                options.AccessToken = Environment.GetEnvironmentVariable(options.AccessToken);
            }

            if (!String.IsNullOrEmpty(options.AppKey) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.AppKey)))
            {
                options.AppKey = Environment.GetEnvironmentVariable(options.AppKey);
            }

            _options = options;

            // Load configuration files

            var sources   = new List <Source>();
            var templates = new Dictionary <string, string>();

            foreach (var configurationFilenameOrUrl in options.Config)
            {
                try
                {
                    var configuration = await LoadConfigurationAsync(configurationFilenameOrUrl);

                    sources.AddRange(configuration.Sources);

                    foreach (var template in configuration.Templates)
                    {
                        templates[template.Key] = template.Value;
                    }
                }
                catch (RegressionBotException e)
                {
                    Console.WriteLine(e.Message);
                    return(1);
                }
            }

            if (!sources.Any())
            {
                Console.WriteLine("No source could be found.");
                return(1);
            }

            // Creating GitHub credentials

            if (!options.Debug)
            {
                if (!String.IsNullOrEmpty(options.AccessToken))
                {
                    _credentials = CredentialsHelper.GetCredentialsForUser(options);
                }
                else
                {
                    _credentials = await CredentialsHelper.GetCredentialsForAppAsync(options);
                }
            }

            // Regressions

            Console.WriteLine("Looking for regressions...");

            foreach (var s in sources)
            {
                if (!String.IsNullOrEmpty(s.Name))
                {
                    Console.WriteLine($"Processing source '{s.Name}'");
                }

                var template = templates[s.Regressions.Template];

                var regressions = await FindRegression(s).ToListAsync();

                if (!regressions.Any())
                {
                    continue;
                }

                Console.WriteLine($"Found {regressions.Count()} regressions");

                Console.WriteLine("Updating existing issues...");

                var newRegressions = await UpdateIssues(regressions, s, template);

                if (newRegressions.Any())
                {
                    Console.WriteLine("Reporting new regressions...");

                    await CreateRegressionIssue(newRegressions, template);
                }
                else
                {
                    Console.WriteLine("No new regressions were found.");
                }
            }

            return(0);
        }
Exemplo n.º 3
0
 public static Credentials GetCredentialsForUser(BotOptions options)
 {
     return(new Credentials(options.AccessToken));
 }
Exemplo n.º 4
0
        private static async Task <int> Controller(BotOptions options)
        {
            // Validate arguments
            try
            {
                options.Validate();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);

                return(1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception: " + e.ToString());

                return(1);
            }

            // Substitute with ENV value if it exists

            if (!String.IsNullOrEmpty(options.ConnectionString) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.ConnectionString)))
            {
                options.ConnectionString = Environment.GetEnvironmentVariable(options.ConnectionString);
            }

            if (!String.IsNullOrEmpty(options.AccessToken) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.AccessToken)))
            {
                options.AccessToken = Environment.GetEnvironmentVariable(options.AccessToken);
            }

            if (!String.IsNullOrEmpty(options.AppKey) && !String.IsNullOrEmpty(Environment.GetEnvironmentVariable(options.AppKey)))
            {
                options.AppKey = Environment.GetEnvironmentVariable(options.AppKey);
            }

            _options = options;

            // Load configuration files

            var sources   = new List <Source>();
            var templates = new Dictionary <string, string>();

            foreach (var configurationFilenameOrUrl in options.Config)
            {
                try
                {
                    var configuration = await LoadConfigurationAsync(configurationFilenameOrUrl);

                    sources.AddRange(configuration.Sources);

                    foreach (var template in configuration.Templates)
                    {
                        templates[template.Key] = template.Value;
                    }
                }
                catch (RegressionBotException e)
                {
                    Console.WriteLine(e.Message);
                    return(1);
                }
            }

            if (!sources.Any())
            {
                Console.WriteLine("No source could be found.");
                return(1);
            }

            // Creating GitHub credentials

            if (!options.Debug)
            {
                if (!String.IsNullOrEmpty(options.AccessToken))
                {
                    _credentials = CredentialsHelper.GetCredentialsForUser(options);
                }
                else
                {
                    _credentials = await CredentialsHelper.GetCredentialsForAppAsync(options);
                }
            }

            // Regressions

            Console.WriteLine("Looking for regressions...");

            foreach (var s in sources)
            {
                if (!String.IsNullOrEmpty(s.Name))
                {
                    Console.WriteLine($"Processing source '{s.Name}'");
                }

                var template = templates[s.Regressions.Template];

                var regressions = await FindRegression(s).ToListAsync();

                if (!regressions.Any())
                {
                    continue;
                }

                Console.WriteLine($"Found {regressions.Count()} regressions");

                Console.WriteLine("Updating existing issues...");

                // The result of updating issues is the list of regressions that are not yet reported
                var newRegressions = await UpdateIssues(regressions, s, template);

                Console.WriteLine($"{newRegressions.Count()} of them were not reported");

                // Exclude all regressions that have recovered, and have not been reported
                newRegressions = newRegressions.Where(x => !x.HasRecovered);

                Console.WriteLine($"{newRegressions.Count()} of them have not recovered");

                if (newRegressions.Any())
                {
                    Console.WriteLine("Reporting new regressions...");

                    if (_options.Verbose)
                    {
                        Console.WriteLine(JsonConvert.SerializeObject(newRegressions, Formatting.None));
                    }

                    var skip     = 0;
                    var pageSize = 10;

                    while (true)
                    {
                        // Create issues with 10 regressions per issue max
                        var page = newRegressions.Skip(skip).Take(pageSize);

                        if (!page.Any())
                        {
                            break;
                        }

                        await CreateRegressionIssue(page, template);

                        skip += pageSize;
                    }
                }
                else
                {
                    Console.WriteLine("No new regressions were found.");
                }
            }

            return(0);
        }