Exemplo n.º 1
0
            public async Task NotifyPending_ShouldSendARequest_WithPendingDescription()
            {
                var logger     = Substitute.For <ILogger <GithubStatusNotifier> >();
                var httpClient = Substitute.For <GithubHttpClient>(options);
                var fetcher    = new GithubStatusNotifier(httpClient, options, logger);

                httpClient.SendAsync(Any <HttpRequestMessage>()).Returns(new HttpResponseMessage {
                    StatusCode = OK
                });
                await fetcher.NotifyPending(repoName, sha);

                await httpClient.Received().SendAsync(Is <HttpRequestMessage>(req =>
                                                                              req.Json <CreateStatusRequest>().Description == expectedPendingDescription
                                                                              ));
            }
Exemplo n.º 2
0
            public async Task NotifyPending_ShouldSendARequest_ToTheCorrectUrl()
            {
                var logger     = Substitute.For <ILogger <GithubStatusNotifier> >();
                var httpClient = Substitute.For <GithubHttpClient>(options);
                var fetcher    = new GithubStatusNotifier(httpClient, options, logger);

                httpClient.SendAsync(Any <HttpRequestMessage>()).Returns(new HttpResponseMessage {
                    StatusCode = OK
                });
                await fetcher.NotifyPending(repoName, sha);

                await httpClient.Received().SendAsync(Is <HttpRequestMessage>(req =>
                                                                              req.RequestUri == new Uri(expectedUrl)
                                                                              ));
            }
Exemplo n.º 3
0
            public async Task NotifyPending_ShouldSendAPostRequest()
            {
                var logger     = Substitute.For <ILogger <GithubStatusNotifier> >();
                var httpClient = Substitute.For <GithubHttpClient>(options);
                var fetcher    = new GithubStatusNotifier(httpClient, options, logger);

                httpClient.SendAsync(Any <HttpRequestMessage>()).Returns(new HttpResponseMessage {
                    StatusCode = OK
                });
                await fetcher.NotifyPending(repoName, sha);

                await httpClient.Received().SendAsync(Is <HttpRequestMessage>(req =>
                                                                              req.Method == HttpMethod.Post
                                                                              ));
            }
Exemplo n.º 4
0
        public virtual async Task Deploy(PushEvent payload)
        {
            var stackName       = $"{payload.Repository.Name}-{config.StackSuffix}";
            var contentsUrl     = payload.Repository.ContentsUrl;
            var templateContent = await fileFetcher.Fetch(contentsUrl, config.TemplateFilename, payload.Ref);

            var pipelineDefinition = await fileFetcher.Fetch(contentsUrl, config.PipelineDefinitionFilename, payload.Ref);

            var roleArn = config.RoleArn;

            if (templateContent == null)
            {
                logger.LogInformation($"Couldn't find template for {payload.Repository.Name}");
                return;
            }

            var parameters = new List <Parameter> {
                new Parameter {
                    ParameterKey = "GithubToken", ParameterValue = config.GithubToken
                },
                new Parameter {
                    ParameterKey = "GithubOwner", ParameterValue = config.GithubOwner
                },
                new Parameter {
                    ParameterKey = "GithubRepo", ParameterValue = payload.Repository.Name
                },
                new Parameter {
                    ParameterKey = "GithubBranch", ParameterValue = payload.Repository.DefaultBranch
                }
            };

            if (pipelineDefinition != null)
            {
                var key = await UploadDefinition(payload.Repository.Name, pipelineDefinition);

                parameters.Add(new Parameter
                {
                    ParameterKey   = "PipelineDefinitionBucket",
                    ParameterValue = config.ArtifactStore
                });

                parameters.Add(new Parameter
                {
                    ParameterKey   = "PipelineDefinitionKey",
                    ParameterValue = key
                });
            }

            try
            {
                await statusNotifier.NotifyPending(payload.Repository.Name, payload.HeadCommit.Id);

                await stackDeployer.Deploy(new DeployStackContext
                {
                    StackName          = stackName,
                    Template           = templateContent,
                    NotificationArn    = config.StatusNotificationTopicArn,
                    ClientRequestToken = payload.HeadCommit.Id,
                    PassRoleArn        = roleArn,
                    Parameters         = parameters
                });
            }
            catch (NoUpdatesException)
            {
                await statusNotifier.NotifySuccess(payload.Repository.Name, payload.HeadCommit.Id);
            }
            catch (Exception e)
            {
                logger.LogError($"Failed to create/update stack: {e.Message} {e.StackTrace}");

                await statusNotifier.NotifyFailure(payload.Repository.Name, payload.HeadCommit.Id);
            }
        }
Exemplo n.º 5
0
        public async Task <Response> Handle(SQSEvent sqsEvent, CancellationToken cancellationToken = default)
        {
            var request         = requestFactory.CreateFromSqsEvent(sqsEvent);
            var owner           = request.CommitInfo.GithubOwner;
            var repository      = request.CommitInfo.GithubRepository;
            var sha             = request.CommitInfo.GithubRef;
            var stackName       = request.StackName;
            var environmentName = request.EnvironmentName;

            try
            {
                var notificationArn = Environment.GetEnvironmentVariable(notificationArnKey);
                var template        = await s3Util.GetZipEntryInObject(request.ZipLocation, request.TemplateFileName);

                var stackConfig = await GetConfig(request);

                var token = await tokenGenerator.Generate(sqsEvent, request);

                await statusNotifier.NotifyPending(owner, repository, sha, stackName, environmentName);

                await stackDeployer.Deploy(new DeployStackContext
                {
                    StackName          = request.StackName,
                    Template           = template,
                    RoleArn            = request.RoleArn,
                    NotificationArn    = config.NotificationArn,
                    Parameters         = MergeParameters(stackConfig?.Parameters, request.ParameterOverrides),
                    Tags               = stackConfig?.Tags,
                    StackPolicyBody    = stackConfig?.StackPolicy?.Value,
                    ClientRequestToken = token,
                    Capabilities       = request.Capabilities,
                });
            }
            catch (NoUpdatesException)
            {
                var outputs = await GetStackOutputs(request.StackName, request.RoleArn);

                var response = await stepFunctionsClient.SendTaskSuccessAsync(new SendTaskSuccessRequest
                {
                    TaskToken = request.Token,
                    Output    = JsonSerializer.Serialize(outputs)
                });

                await statusNotifier.NotifySuccess(owner, repository, sha, stackName, environmentName);

                return(new Response
                {
                    Success = true
                });
            }
            catch (Exception e)
            {
                var response = await stepFunctionsClient.SendTaskFailureAsync(new SendTaskFailureRequest
                {
                    TaskToken = request.Token,
                    Cause     = e.Message
                });

                await statusNotifier.NotifyFailure(owner, repository, sha, stackName, environmentName);

                return(new Response
                {
                    Success = true
                });
            }

            throw new Exception();
        }