Skip to content

Commit

Permalink
fix(codebuild): take the account & region of an imported Project from…
Browse files Browse the repository at this point in the history
… its ARN (#13708)

This is needed to correctly use CodeBuild in CodePipeline
(which needs to know whether the Project is from a different account/region).

Fixes #13694

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
skinny85 authored Mar 30, 2021
1 parent 294f546 commit fb65123
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,7 @@ export = {

test.done();
},

'can override build timeout'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -1407,4 +1408,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();
},
},
};

0 comments on commit fb65123

Please sign in to comment.