From 136ce123ba1b0d5dc5429ae63f4dc4c771920377 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Fri, 19 Mar 2021 16:19:41 -0700 Subject: [PATCH] fix(codebuild): take the account & region of an imported Project from its ARN This is needed to correctly use CodeBuild in CodePipeline (which needs to know whether the Project is from a different account/region). Fixes #13694 --- packages/@aws-cdk/aws-codebuild/lib/project.ts | 9 +++++++-- .../@aws-cdk/aws-codebuild/test/test.project.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index 718f7263ada13..69baf80da688e 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -639,14 +639,19 @@ export interface BindToCodePipelineOptions { export class Project extends ProjectBase { public static fromProjectArn(scope: Construct, id: string, projectArn: string): IProject { + const parsedArn = Stack.of(scope).parseArn(projectArn); + class Import extends ProjectBase { public readonly grantPrincipal: iam.IPrincipal; public readonly projectArn = projectArn; - public readonly projectName = Stack.of(scope).parseArn(projectArn).resourceName!; + public readonly projectName = parsedArn.resourceName!; public readonly role?: iam.Role = undefined; constructor(s: Construct, i: string) { - super(s, i); + super(s, i, { + account: parsedArn.account, + region: parsedArn.region, + }); this.grantPrincipal = new iam.UnknownPrincipal({ resource: this }); } } diff --git a/packages/@aws-cdk/aws-codebuild/test/test.project.ts b/packages/@aws-cdk/aws-codebuild/test/test.project.ts index 159511589ead7..421b0efd95767 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.project.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.project.ts @@ -982,6 +982,7 @@ export = { test.done(); }, + 'can override build timeout'(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -1003,4 +1004,18 @@ export = { test.done(); }, }, + + 'can be imported': { + 'by ARN'(test: Test) { + const stack = new cdk.Stack(); + const project = codebuild.Project.fromProjectArn(stack, 'Project', + 'arn:aws:codebuild:us-west-2:123456789012:project/My-Project'); + + test.equal(project.projectName, 'My-Project'); + test.equal(project.env.account, '123456789012'); + test.equal(project.env.region, 'us-west-2'); + + test.done(); + }, + }, };