Skip to content

Commit

Permalink
fix(codepipeline): allow multiple CodeCommit source actions using eve…
Browse files Browse the repository at this point in the history
…nts (#8018)

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 #7802

----

*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 committed Jun 2, 2020
1 parent 33212d2 commit 103c144
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
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
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 103c144

Please sign in to comment.