"use strict";
const assert_1 = require("@aws-cdk/assert");
const codebuild = require("@aws-cdk/aws-codebuild");
const codecommit = require("@aws-cdk/aws-codecommit");
const codecommitPipeline = require("@aws-cdk/aws-codecommit-codepipeline");
const codepipeline = require("@aws-cdk/aws-codepipeline");
const s3 = require("@aws-cdk/aws-s3");
const sns = require("@aws-cdk/aws-sns");
const cdk = require("@aws-cdk/cdk");
const codebuildPipeline = require("../lib");
module.exports = {
    'basic pipeline'(test) {
        const stack = new cdk.Stack();
        const repo = new codecommit.Repository(stack, 'MyRepo', { repositoryName: 'my-repo' });
        const pipeline = new codepipeline.Pipeline(stack, 'Pipeline');
        const sourceStage = new codepipeline.Stage(pipeline, 'source');
        const source = new codecommitPipeline.PipelineSource(sourceStage, 'source', {
            artifactName: 'SourceArtifact',
            repository: repo,
        });
        const buildStage = new codepipeline.Stage(pipeline, 'build');
        const project = new codebuild.Project(stack, 'MyBuildProject', {
            source: new codebuild.CodePipelineSource()
        });
        new codebuildPipeline.PipelineBuildAction(buildStage, 'build', {
            project,
            inputArtifact: source.artifact
        });
        test.notDeepEqual(stack.toCloudFormation(), {});
        test.deepEqual([], pipeline.validate());
        test.done();
    },
    'github action uses ThirdParty owner'(test) {
        const stack = new cdk.Stack();
        const secret = new cdk.SecretParameter(stack, 'GitHubToken', { ssmParameter: 'my-token' });
        const p = new codepipeline.Pipeline(stack, 'P');
        new codepipeline.GitHubSource(new codepipeline.Stage(p, 'Source'), 'GH', {
            artifactName: 'A',
            branch: 'branch',
            oauthToken: secret,
            owner: 'foo',
            repo: 'bar'
        });
        new codepipeline.ApprovalAction(new codepipeline.Stage(p, 'Two'), 'Boo');
        assert_1.expect(stack).to(assert_1.haveResource('AWS::CodePipeline::Pipeline', {
            "ArtifactStore": {
                "Location": {
                    "Ref": "PArtifactsBucket5E711C12"
                },
                "Type": "S3"
            },
            "RoleArn": {
                "Fn::GetAtt": [
                    "PRole07BDC907",
                    "Arn"
                ]
            },
            "Stages": [
                {
                    "Actions": [
                        {
                            "ActionTypeId": {
                                "Category": "Source",
                                "Owner": "ThirdParty",
                                "Provider": "GitHub",
                                "Version": "1"
                            },
                            "Configuration": {
                                "Owner": "foo",
                                "Repo": "bar",
                                "Branch": "branch",
                                "OAuthToken": {
                                    "Ref": "GitHubTokenParameterBB166B9D"
                                },
                                "PollForSourceChanges": true
                            },
                            "InputArtifacts": [],
                            "Name": "GH",
                            "OutputArtifacts": [
                                {
                                    "Name": "A"
                                }
                            ],
                            "RunOrder": 1
                        }
                    ],
                    "Name": "Source"
                },
                {
                    "Actions": [
                        {
                            "ActionTypeId": {
                                "Category": "Approval",
                                "Owner": "AWS",
                                "Provider": "Manual",
                                "Version": "1"
                            },
                            "InputArtifacts": [],
                            "Name": "Boo",
                            "OutputArtifacts": [],
                            "RunOrder": 1
                        }
                    ],
                    "Name": "Two"
                }
            ]
        }));
        test.deepEqual([], p.validate());
        test.done();
    },
    'onStateChange'(test) {
        const stack = new cdk.Stack();
        const topic = new sns.Topic(stack, 'Topic');
        const pipeline = new codepipeline.Pipeline(stack, 'PL');
        new codepipeline.AmazonS3Source(new codepipeline.Stage(pipeline, 'S1'), 'A1', {
            artifactName: 'Artifact',
            bucket: new s3.Bucket(stack, 'Bucket'),
            bucketKey: 'Key'
        });
        new codepipeline.ApprovalAction(new codepipeline.Stage(pipeline, 'S2'), 'A2');
        pipeline.onStateChange('OnStateChange', topic, {
            description: 'desc',
            scheduleExpression: 'now',
            eventPattern: {
                detail: {
                    state: ['FAILED']
                }
            }
        });
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "Description": "desc",
            "EventPattern": {
                "detail": {
                    "state": [
                        "FAILED"
                    ]
                },
                "detail-type": [
                    "CodePipeline Pipeline Execution State Change"
                ],
                "source": [
                    "aws.codepipeline"
                ],
                "resources": [
                    {
                        "Fn::Join": [
                            "",
                            [
                                "arn",
                                ":",
                                {
                                    "Ref": "AWS::Partition"
                                },
                                ":",
                                "codepipeline",
                                ":",
                                {
                                    "Ref": "AWS::Region"
                                },
                                ":",
                                {
                                    "Ref": "AWS::AccountId"
                                },
                                ":",
                                {
                                    "Ref": "PLD5425AEA"
                                }
                            ]
                        ]
                    }
                ]
            },
            "ScheduleExpression": "now",
            "State": "ENABLED",
            "Targets": [
                {
                    "Arn": {
                        "Ref": "TopicBFC7AF6E"
                    },
                    "Id": "Topic"
                }
            ]
        }));
        test.deepEqual([], pipeline.validate());
        test.done();
    },
    'PipelineProject': {
        'with a custom Project Name': {
            'sets the source and artifacts to CodePipeline'(test) {
                const stack = new cdk.Stack();
                new codebuildPipeline.PipelineProject(stack, 'MyProject', {
                    projectName: 'MyProject',
                });
                assert_1.expect(stack).to(assert_1.haveResource('AWS::CodeBuild::Project', {
                    "Name": "MyProject",
                    "Source": {
                        "Type": "CODEPIPELINE"
                    },
                    "Artifacts": {
                        "Type": "CODEPIPELINE"
                    },
                    "ServiceRole": {
                        "Fn::GetAtt": [
                            "MyProjectRole9BBE5233",
                            "Arn"
                        ]
                    },
                    "Environment": {
                        "Type": "LINUX_CONTAINER",
                        "PrivilegedMode": false,
                        "Image": "aws/codebuild/ubuntu-base:14.04",
                        "ComputeType": "BUILD_GENERAL1_SMALL"
                    }
                }));
                test.done();
            }
        }
    },
};
//# sourceMappingURL=data:application/json;base64, 