"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const codepipeline = require("@aws-cdk/aws-codepipeline-api");
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, {
            stage: props.stage,
            provider: 'CodeBuild',
            inputArtifact: props.inputArtifact,
            artifactName: props.artifactName,
            configuration: {
                ProjectName: props.project.projectName
            }
        });
        const actions = [
            'codebuild:BatchGetBuilds',
            'codebuild:StartBuild',
            'codebuild:StopBuild',
        ];
        props.stage.pipelineRole.addToPolicy(new cdk.PolicyStatement()
            .addResource(props.project.projectArn)
            .addActions(...actions));
        // allow codebuild to read and write artifacts to the pipline's artifact bucket.
        if (props.project.role) {
            props.stage.grantPipelineBucketReadWrite(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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGlwZWxpbmUtYWN0aW9ucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInBpcGVsaW5lLWFjdGlvbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSw4REFBK0Q7QUFDL0Qsb0NBQXFDO0FBdUJyQzs7R0FFRztBQUNILE1BQWEsbUJBQW9CLFNBQVEsWUFBWSxDQUFDLFdBQVc7SUFDN0QsWUFBWSxNQUFxQixFQUFFLElBQVksRUFBRSxLQUErQjtRQUM1RSw0RUFBNEU7UUFDNUUsd0RBQXdEO1FBRXhELEtBQUssQ0FBQyxNQUFNLEVBQUUsSUFBSSxFQUFFO1lBQ2hCLEtBQUssRUFBRSxLQUFLLENBQUMsS0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