Skip to content

Commit

Permalink
validate triggers with duplicate source action
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Feb 10, 2024
1 parent d5f5bc2 commit 1684bfd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,13 @@ export class Pipeline extends PipelineBase {
*/
public addTrigger(props: TriggerProps): Trigger {
const trigger = new Trigger(props);
const actionName = props.gitConfiguration?.sourceAction.actionProperties.actionName;

// check for duplicate source actions for triggers
if (actionName !== undefined && this.triggers.find(t => t.sourceAction?.actionProperties.actionName === actionName)) {
throw new Error(`Trigger with duplicate source action '${actionName}' added to the Pipeline`);
}

this.triggers.push(trigger);
return trigger;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ export interface TriggerProps {
* Trigger.
*/
export class Trigger {
public readonly sourceAction: IAction | undefined;

constructor(private readonly props: TriggerProps) {
this.sourceAction = props.gitConfiguration?.sourceAction;
this.validate();
}

Expand Down
24 changes: 24 additions & 0 deletions packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,30 @@ describe('triggers', () => {
}).toThrow(/provider for actionProperties in sourceAction with name 'FakeSource' must be 'CodeStarSourceConnection', got 'Fake'/);
});

test('throw if source action with duplicate action name added to the Pipeline', () => {
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', {
pipelineType: codepipeline.PipelineType.V2,
triggers: [{
providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION,
gitConfiguration: {
sourceAction,
pushFilter: [{
excludedTags: ['exclude1', 'exclude2'],
includedTags: ['include1', 'include2'],
}],
},
}],
});
expect(() => {
pipeline.addTrigger({
providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION,
gitConfiguration: {
sourceAction,
},
});
}).toThrow(/Trigger with duplicate source action 'CodeStarConnectionsSourceAction' added to the Pipeline/);
});

test('throw if triggers are specified when pipelineType is not set to V2', () => {
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', {
pipelineType: codepipeline.PipelineType.V1,
Expand Down

0 comments on commit 1684bfd

Please sign in to comment.