"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const codepipeline = require("@aws-cdk/aws-codepipeline");
const cdk = require("@aws-cdk/cdk");
/**
 * CodePipeline build Action that uses AWS CodeBuild.
 */
class PipelineBuildAction extends codepipeline.BuildAction {
    constructor(parent, name, props) {
        // This happened when ProjectName was accidentally set to the project's ARN:
        // https://qiita.com/ikeisuke/items/2fbc0b80b9bbd981b41f
        super(parent, name, {
            provider: 'CodeBuild',
            inputArtifact: props.inputArtifact,
            artifactName: props.artifactName,
            configuration: {
                ProjectName: props.project.projectName
            }
        });
        const actions = [
            'codebuild:BatchGetBuilds',
            'codebuild:StartBuild',
            'codebuild:StopBuild',
        ];
        parent.pipeline.addToRolePolicy(new cdk.PolicyStatement()
            .addResource(props.project.projectArn)
            .addActions(...actions));
        // allow codebuild to read and write artifacts to the pipline's artifact bucket.
        parent.pipeline.artifactBucket.grantReadWrite(props.project.role);
        // policy must be added as a dependency to the pipeline!!
        // TODO: grants - build.addResourcePermission() and also make sure permission
        // includes the pipeline role AWS principal.
    }
}
exports.PipelineBuildAction = PipelineBuildAction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGlwZWxpbmUtYWN0aW9ucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInBpcGVsaW5lLWFjdGlvbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFDQSwwREFBMkQ7QUFDM0Qsb0NBQXFDO0FBc0JyQzs7R0FFRztBQUNILE1BQWEsbUJBQW9CLFNBQVEsWUFBWSxDQUFDLFdBQVc7SUFDN0QsWUFBWSxNQUEwQixFQUFFLElBQVksRUFBRSxLQUErQjtRQUNqRiw0RUFBNEU7UUFDNUUsd0RBQXdEO1FBRXhELEtBQUssQ0FBQyxNQUFNLEVBQUUsSUFBSSxFQUFFO1lBQ2hCLFFBQVEsRUFBRSxXQUFXO1lBQ3JCLGFBQWEsRUFBRSxLQUFLLENBQ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