From 4ba68a52f069f83671354a981cd1a797ceb7dc30 Mon Sep 17 00:00:00 2001 From: Anshika Mehndiratta Date: Tue, 24 Oct 2023 21:00:03 +0000 Subject: [PATCH] fix(cdk): fix a bug with CodePipelineSource to create nested repositories --- .../lib/codepipeline/codepipeline-source.ts | 8 ++++---- .../codepipeline/codepipeline-sources.test.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/pipelines/lib/codepipeline/codepipeline-source.ts b/packages/aws-cdk-lib/pipelines/lib/codepipeline/codepipeline-source.ts index 4b2459868cd52..31826570a8bd2 100644 --- a/packages/aws-cdk-lib/pipelines/lib/codepipeline/codepipeline-source.ts +++ b/packages/aws-cdk-lib/pipelines/lib/codepipeline/codepipeline-source.ts @@ -256,11 +256,11 @@ class GitHubSource extends CodePipelineSource { super(repoString); const parts = repoString.split('/'); - if (Token.isUnresolved(repoString) || parts.length !== 2) { + if (Token.isUnresolved(repoString) || parts.length < 2) { throw new Error(`GitHub repository name should be a resolved string like '/', got '${repoString}'`); } this.owner = parts[0]; - this.repo = parts[1]; + this.repo = parts.slice(1).join('/'); this.authentication = props.authentication ?? SecretValue.secretsManager('github-token'); this.configurePrimaryOutput(new FileSet('Source', this)); } @@ -425,11 +425,11 @@ class CodeStarConnectionSource extends CodePipelineSource { super(`${repoString}-${branch}`); const parts = repoString.split('/'); - if (Token.isUnresolved(repoString) || parts.length !== 2) { + if (Token.isUnresolved(repoString) || parts.length < 2) { throw new Error(`CodeStar repository name should be a resolved string like '/', got '${repoString}'`); } this.owner = parts[0]; - this.repo = parts[1]; + this.repo = parts.slice(1).join('/'); this.configurePrimaryOutput(new FileSet('Source', this)); } diff --git a/packages/aws-cdk-lib/pipelines/test/codepipeline/codepipeline-sources.test.ts b/packages/aws-cdk-lib/pipelines/test/codepipeline/codepipeline-sources.test.ts index d721b23a1c026..d172fb2a340a0 100644 --- a/packages/aws-cdk-lib/pipelines/test/codepipeline/codepipeline-sources.test.ts +++ b/packages/aws-cdk-lib/pipelines/test/codepipeline/codepipeline-sources.test.ts @@ -193,6 +193,25 @@ test('Dashes in repo names are removed from artifact names', () => { }); }); +test('Nested repo names are allowed', () => { + new ModernTestGitHubNpmPipeline(pipelineStack, 'Pipeline', { + input: cdkp.CodePipelineSource.gitHub('owner/group1/group2/groupN/repo', 'main'), + }); + + Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', { + Stages: Match.arrayWith([{ + Name: 'Source', + Actions: [ + Match.objectLike({ + OutputArtifacts: [ + { Name: 'owner_group1_group2_groupN_repo_Source' }, + ], + }), + ], + }]), + }); +}); + test('artifact names are never longer than 128 characters', () => { new ModernTestGitHubNpmPipeline(pipelineStack, 'Pipeline', { input: cdkp.CodePipelineSource.gitHub('owner/' + 'my-repo'.repeat(100), 'main'),