Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codepipeline-actions): use token as CodeCommitSourceAction branch #10463

Merged
merged 7 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as targets from '@aws-cdk/aws-events-targets';
import * as iam from '@aws-cdk/aws-iam';
import { Construct } from '@aws-cdk/core';
import { Construct, Token } from '@aws-cdk/core';
import { Action } from '../action';
import { sourceArtifactBounds } from '../common';

Expand Down Expand Up @@ -122,8 +122,8 @@ export class CodeCommitSourceAction extends Action {
const createEvent = this.props.trigger === undefined ||
this.props.trigger === CodeCommitTrigger.EVENTS;
if (createEvent) {
const branchIdDisambiguator = this.branch === 'master' ? '' : `-${this.branch}-`;
this.props.repository.onCommit(`${stage.pipeline.node.uniqueId}${branchIdDisambiguator}EventRule`, {
const eventId = this.generateEventId(stage);
this.props.repository.onCommit(eventId, {
target: new targets.CodePipeline(stage.pipeline),
branches: [this.branch],
});
Expand Down Expand Up @@ -153,4 +153,23 @@ export class CodeCommitSourceAction extends Action {
},
};
}

private generateEventId(stage: codepipeline.IStage): string {
let branchIdDisambiguator: string;
let baseId = stage.pipeline.node.uniqueId;
if (this.branch === 'master') {
branchIdDisambiguator = baseId;
} else if (Token.isUnresolved(this.branch)) {
let candidate = baseId;
let counter = 0;
while (this.props.repository.node.tryFindChild(candidate) !== undefined) {
counter += 1;
candidate = `${baseId}-Branch${counter}-`;
}
branchIdDisambiguator = candidate;
} else {
branchIdDisambiguator = `${baseId}-${this.branch}-`;
}
return `${branchIdDisambiguator}EventRule`;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a hard time keeping up with this code. For example, branchIdDisambiguator sounds to me like it should be just the branch-changing part of the ID, but somehow it's the entire thing.

What do you think of this instead?

Suggested change
private generateEventId(stage: codepipeline.IStage): string {
let branchIdDisambiguator: string;
let baseId = stage.pipeline.node.uniqueId;
if (this.branch === 'master') {
branchIdDisambiguator = baseId;
} else if (Token.isUnresolved(this.branch)) {
let candidate = baseId;
let counter = 0;
while (this.props.repository.node.tryFindChild(candidate) !== undefined) {
counter += 1;
candidate = `${baseId}-Branch${counter}-`;
}
branchIdDisambiguator = candidate;
} else {
branchIdDisambiguator = `${baseId}-${this.branch}-`;
}
return `${branchIdDisambiguator}EventRule`;
}
private generateEventId(stage: codepipeline.IStage): string {
const baseId = stage.pipeline.node.uniqueId;
if (Token.isUnresolved(this.branch)) {
let candidate = '';
let counter = 0;
do {
candidate = this.eventIdFromPrefix(`${baseId}-${counter}`);
counter += 1;
} while (this.props.repository.node.tryFindChild(candidate) !== undefined);
return candidate;
} else {
const branchIdDisambiguator = this.branch === 'master' ? '' : '-${this.branch}-';
return this.eventIdFromPrefix(`${baseId}${branchIdDisambiguator}`);
}
}
private eventIdFromPrefix(eventIdPrefix: string) {
return `${eventIdPrefix}EventRule`;
}

(Notice the different loop; the current one actually checks the wrong ID, without the EvenRule suffix!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks for spotting that! I think that your naming scheme with the counter needs a slight tweak though, so I will just update my branch with that.

Copy link
Contributor

@gotodeploy gotodeploy Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skinny85 This might have a bug:

# Use buckticks instead
'-${this.branch}-' => `-${this.branch}-`

P.S. I opened #10665

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { countResources, expect, haveResourceLike, not } from '@aws-cdk/assert';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import { Stack } from '@aws-cdk/core';
import { Stack, Lazy } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as cpactions from '../../lib';

Expand Down Expand Up @@ -224,6 +224,51 @@ export = {

test.done();
},

'allows using a Token for the branch name'(test: Test) {
const stack = new Stack();

const branch = Lazy.stringValue({ produce: () => 'branch' });

const p = new codepipeline.Pipeline(stack, 'P');

const r = new codecommit.Repository(stack, 'R', {
repositoryName: 'repository',
});

const artifact = new codepipeline.Artifact();
const ccSourceAction = new cpactions.CodeCommitSourceAction({
actionName: 'CodeCommit',
repository: r,
branch: branch,
output: artifact,
});

p.addStage({
stageName: 'Source',
actions: [ccSourceAction],
});
p.addStage({
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'Build',
project: new codebuild.PipelineProject(stack, 'CodeBuild'),
input: artifact,
}),
],
});
alanraison marked this conversation as resolved.
Show resolved Hide resolved

expect(stack).to(haveResourceLike('AWS::Events::Rule', {
EventPattern: {
detail: {
referenceName: [stack.resolve(branch)],
alanraison marked this conversation as resolved.
Show resolved Hide resolved
},
},
}));

test.done();
},
},
};

Expand Down