diff --git a/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts b/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts index c1efc87dd4718..53909a4192b85 100644 --- a/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts +++ b/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts @@ -53,7 +53,7 @@ export interface GitConfiguration { * * Git tags is the only supported event type. * - * The length must be between 1 and 3. + * The length must be less than or equal to 3. * * @default - no filter. */ @@ -111,8 +111,8 @@ export class Trigger { } const pushFilter = this.props.gitConfiguration.pushFilter; - if (pushFilter !== undefined && (pushFilter.length < 1 || pushFilter.length > 3)) { - throw new Error(`length of pushFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' must be between 1 and 3, got ${pushFilter.length}`); + if (pushFilter !== undefined && pushFilter.length > 3) { + throw new Error(`length of pushFilter for sourceAction with name '${sourceAction.actionProperties.actionName}' must be less than or equal to 3, got ${pushFilter.length}`); } pushFilter?.forEach(filter => { @@ -139,7 +139,7 @@ export class Trigger { const push: CfnPipeline.GitPushFilterProperty[] | undefined = pushFilter?.map(filter => { const tags: CfnPipeline.GitTagFilterCriteriaProperty | undefined = { - // set to undefined if empty array + // set to undefined if empty array because CloudFormation does not accept empty array excludes: filter.tagsExcludes?.length ? filter.tagsExcludes : undefined, includes: filter.tagsIncludes?.length ? filter.tagsIncludes : undefined, }; @@ -147,7 +147,8 @@ export class Trigger { }); gitConfiguration = { - push, + // set to undefined if empty array because CloudFormation does not accept empty array + push: push?.length ? push : undefined, sourceActionName: sourceAction.actionProperties.actionName, }; } diff --git a/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts b/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts index dbd160f93d801..6c9048ddede4d 100644 --- a/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts +++ b/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts @@ -268,19 +268,30 @@ describe('triggers', () => { }).toThrow(/maximum length of tagsIncludes for sourceAction with name 'CodeStarConnectionsSourceAction' is 8, got 9/); }); - test('throw if length of pushFilter is less than 1', () => { - expect(() => { - new codepipeline.Pipeline(stack, 'Pipeline', { - pipelineType: codepipeline.PipelineType.V2, - triggers: [{ - providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION, - gitConfiguration: { - sourceAction, - pushFilter: [], - }, - }], - }); - }).toThrow(/length of pushFilter for sourceAction with name 'CodeStarConnectionsSourceAction' must be between 1 and 3, got 0/); + test('empty pushFilter for trigger is set to undefined', () => { + const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', { + pipelineType: codepipeline.PipelineType.V2, + triggers: [{ + providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION, + gitConfiguration: { + sourceAction, + pushFilter: [], + }, + }], + }); + + testPipelineSetup(pipeline, [sourceAction], [buildAction]); + + Template.fromStack(stack).hasResourceProperties('AWS::CodePipeline::Pipeline', { + PipelineType: 'V2', + Triggers: [{ + GitConfiguration: { + SourceActionName: 'CodeStarConnectionsSourceAction', + Push: Match.absent(), + }, + ProviderType: 'CodeStarSourceConnection', + }], + }); }); test('throw if length of pushFilter is greater than 3', () => { @@ -312,7 +323,7 @@ describe('triggers', () => { }, }], }); - }).toThrow(/length of pushFilter for sourceAction with name 'CodeStarConnectionsSourceAction' must be between 1 and 3, got 4/);; + }).toThrow(/length of pushFilter for sourceAction with name 'CodeStarConnectionsSourceAction' must be less than or equal to 3, got 4/);; }); test('throw if provider of sourceAction is not \'CodeStarSourceConnection\'', () => {