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 all commits
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,24 @@ export class CodeCommitSourceAction extends Action {
},
};
}

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`;
}
}
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,49 @@ export = {

test.done();
},

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

const sourceOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(stack, 'P', {
stages: [
{
stageName: 'Source',
actions: [
new cpactions.CodeCommitSourceAction({
actionName: 'CodeCommit',
repository: new codecommit.Repository(stack, 'R', {
repositoryName: 'repository',
}),
branch: Lazy.stringValue({ produce: () => 'my-branch' }),
output: sourceOutput,
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'Build',
project: new codebuild.PipelineProject(stack, 'CodeBuild'),
input: sourceOutput,
}),
],
},
],
});

expect(stack).to(haveResourceLike('AWS::Events::Rule', {
EventPattern: {
detail: {
referenceName: ['my-branch'],
},
},
}));

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

Expand Down