"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cdk = require("@aws-cdk/cdk");
/**
 * Source Provider definition for a CodeBuild project
 * TODO: Abstract class should be an interface
 */
class BuildSource {
    /**
     * Called by the project when the source is added so that the source can perform
     * binding operations on the source. For example, it can grant permissions to the
     * code build project to read from the S3 bucket.
     */
    bind(_project) {
        return;
    }
}
exports.BuildSource = BuildSource;
/**
 * CodeCommit Source definition for a CodeBuild project
 */
class CodeCommitSource extends BuildSource {
    constructor(repo) {
        super();
        this.repo = repo;
    }
    bind(project) {
        // https://docs.aws.amazon.com/codebuild/latest/userguide/setting-up.html
        project.addToRolePolicy(new cdk.PolicyStatement()
            .addAction('codecommit:GitPull')
            .addResource(this.repo.repositoryArn));
    }
    toSourceJSON() {
        return {
            type: SourceType.CodeCommit,
            location: this.repo.repositoryCloneUrlHttp
        };
    }
}
exports.CodeCommitSource = CodeCommitSource;
/**
 * CodePipeline Source definition for a CodeBuild project
 */
class CodePipelineSource extends BuildSource {
    toSourceJSON() {
        return {
            type: SourceType.CodePipeline
        };
    }
    bind(_project) {
        // TODO: permissions on the pipeline bucket?
    }
}
exports.CodePipelineSource = CodePipelineSource;
/**
 * GitHub Source definition for a CodeBuild project
 */
class GitHubSource extends BuildSource {
    constructor(httpscloneUrl, oauthToken) {
        super();
        this.httpscloneUrl = httpscloneUrl;
        this.oauthToken = oauthToken;
        this.httpscloneUrl = httpscloneUrl;
        this.oauthToken = oauthToken;
    }
    toSourceJSON() {
        return {
            type: SourceType.GitHub,
            auth: this.oauthToken != null ? { type: 'OAUTH', resource: this.oauthToken } : undefined,
            location: this.httpscloneUrl
        };
    }
}
exports.GitHubSource = GitHubSource;
/**
 * GitHub Enterprice Source definition for a CodeBuild project
 */
class GitHubEnterpriseSource extends BuildSource {
    constructor(cloneUrl) {
        super();
        this.cloneUrl = cloneUrl;
        this.cloneUrl = cloneUrl;
    }
    toSourceJSON() {
        return {
            type: SourceType.GitHubEnterPrise,
            location: this.cloneUrl,
        };
    }
}
exports.GitHubEnterpriseSource = GitHubEnterpriseSource;
/**
 * BitBucket Source definition for a CodeBuild project
 */
class BitBucketSource extends BuildSource {
    constructor(httpsCloneUrl) {
        super();
        this.httpsCloneUrl = httpsCloneUrl;
        this.httpsCloneUrl = httpsCloneUrl;
    }
    toSourceJSON() {
        return {
            type: SourceType.BitBucket,
            location: this.httpsCloneUrl
        };
    }
}
exports.BitBucketSource = BitBucketSource;
/**
 * S3 bucket definition for a CodeBuild project.
 */
class S3BucketSource extends BuildSource {
    constructor(bucket, path) {
        super();
        this.bucket = bucket;
        this.path = path;
    }
    toSourceJSON() {
        return {
            type: SourceType.S3,
            location: new cdk.FnConcat(this.bucket.bucketName, '/', this.path)
        };
    }
    bind(project) {
        this.bucket.grantRead(project.role);
    }
}
exports.S3BucketSource = S3BucketSource;
/**
 * Source types for CodeBuild Project
 */
var SourceType;
(function (SourceType) {
    SourceType["CodeCommit"] = "CODECOMMIT";
    SourceType["CodePipeline"] = "CODEPIPELINE";
    SourceType["GitHub"] = "GITHUB";
    SourceType["GitHubEnterPrise"] = "GITHUB_ENTERPRISE";
    SourceType["BitBucket"] = "BITBUCKET";
    SourceType["S3"] = "S3";
})(SourceType = exports.SourceType || (exports.SourceType = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic291cmNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsic291cmNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBRUEsb0NBQXFDO0FBSXJDOzs7R0FHRztBQUNILE1BQXNCLFdBQVc7SUFDN0I7Ozs7T0FJRztJQUNJLElBQUksQ0FBQyxRQUFpQjtRQUN6QixPQUFPO0lBQ1gsQ0FBQztDQUdKO0FBWEQsa0NBV0M7QUFFRDs7R0FFRztBQUNILE1BQWEsZ0JBQWlCLFNBQVEsV0FBVztJQUM3QyxZQUE2QixJQUEyQjtRQUNwRCxLQUFLLEVBQUUsQ0FB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