Skip to content

Commit

Permalink
fix(codepipeline): allow multiple CodeCommit source actions using events
Browse files Browse the repository at this point in the history
There are use-cases when you want to add the same CodeCommit
repository to a CodePipeline multiple times, with different branches.
This wouldn't work when using CloudWatch Events to trigger the pipeline,
as the ID of the generated Event only used the pipeline ID for uniqueness.
Change it to also use the branch name when generating the Event ID
(which cannot be empty, as it turns out, so validate that as well).

Fixes aws#7802
  • Loading branch information
skinny85 committed Jun 2, 2020
1 parent 33212d2 commit ec644ec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export class CodeCommitSourceAction extends Action {
private readonly props: CodeCommitSourceActionProps;

constructor(props: CodeCommitSourceActionProps) {
const branch = props.branch || 'master';
const branch = props.branch ?? 'master';
if (!branch) {
throw new Error("'branch' parameter cannot be an empty string");
}

super({
...props,
Expand Down Expand Up @@ -119,7 +122,8 @@ export class CodeCommitSourceAction extends Action {
const createEvent = this.props.trigger === undefined ||
this.props.trigger === CodeCommitTrigger.EVENTS;
if (createEvent) {
this.props.repository.onCommit(stage.pipeline.node.uniqueId + 'EventRule', {
const branchIdDisambiguator = this.branch === 'master' ? '' : `-${this.branch}-`;
this.props.repository.onCommit(`${stage.pipeline.node.uniqueId}${branchIdDisambiguator}EventRule`, {
target: new targets.CodePipeline(stage.pipeline),
branches: [this.branch],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,66 @@ export = {
test.done();
},

'cannot be created with an empty branch'(test: Test) {
const stack = new Stack();
const repo = new codecommit.Repository(stack, 'MyRepo', {
repositoryName: 'my-repo',
});

test.throws(() => {
new cpactions.CodeCommitSourceAction({
actionName: 'Source2',
repository: repo,
output: new codepipeline.Artifact(),
branch: '',
});
}, /'branch' parameter cannot be an empty string/);

test.done();
},

'allows using the same repository multiple times with different branches when trigger=EVENTS'(test: Test) {
const stack = new Stack();

const repo = new codecommit.Repository(stack, 'MyRepo', {
repositoryName: 'my-repo',
});
const sourceOutput1 = new codepipeline.Artifact();
const sourceOutput2 = new codepipeline.Artifact();
new codepipeline.Pipeline(stack, 'MyPipeline', {
stages: [
{
stageName: 'Source',
actions: [
new cpactions.CodeCommitSourceAction({
actionName: 'Source1',
repository: repo,
output: sourceOutput1,
}),
new cpactions.CodeCommitSourceAction({
actionName: 'Source2',
repository: repo,
output: sourceOutput2,
branch: 'develop',
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'Build',
project: new codebuild.PipelineProject(stack, 'MyProject'),
input: sourceOutput1,
}),
],
},
],
});

test.done();
},

'exposes variables for other actions to consume'(test: Test) {
const stack = new Stack();

Expand Down

0 comments on commit ec644ec

Please sign in to comment.