From 4a6b26e95eee8e204725bda594af02a1821fda2b Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Sun, 10 Dec 2023 02:36:29 +0100 Subject: [PATCH 01/12] feat: add support for tfr prefix --- lib/modules/manager/terragrunt/modules.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 56722fe7a12323..d667fb49a2b334 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -62,7 +62,16 @@ export function analyseTerragruntModule( const moduleParts = source.split('//')[0].split('/'); if (moduleParts[0] === '..') { dep.skipReason = 'local'; - } else if (moduleParts.length >= 3) { + } else if (source.startsWith('tfr:///')) { + const hostnameMatch = hostnameMatchRegex.exec('registry.terraform.io'); + if (hostnameMatch?.groups) { + dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`]; + } + dep.depType = 'terragrunt'; + dep.depName = source.split('//')[1].replace('/', ''); + dep.datasource = TerraformModuleDatasource.id; + } + } else if (moduleParts.length >= 3) { const hostnameMatch = hostnameMatchRegex.exec(source); if (hostnameMatch?.groups) { dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`]; From 7f91fcde29a0c3a154e89f76c0accdfcb1f39f16 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 11:03:33 +0100 Subject: [PATCH 02/12] fix: Hardcode registry url --- lib/modules/manager/terragrunt/modules.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index d667fb49a2b334..2c816780976f9f 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -63,10 +63,7 @@ export function analyseTerragruntModule( if (moduleParts[0] === '..') { dep.skipReason = 'local'; } else if (source.startsWith('tfr:///')) { - const hostnameMatch = hostnameMatchRegex.exec('registry.terraform.io'); - if (hostnameMatch?.groups) { - dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`]; - } + dep.registryUrls = ['registry.terraform.io'] dep.depType = 'terragrunt'; dep.depName = source.split('//')[1].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; From 0d6caf2a25930db16708d5c84a0692a502b0ac1b Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 11:09:15 +0100 Subject: [PATCH 03/12] fix: fix indentation --- lib/modules/manager/terragrunt/modules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 2c816780976f9f..d6bfd2da02cd02 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -68,7 +68,7 @@ export function analyseTerragruntModule( dep.depName = source.split('//')[1].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; } - } else if (moduleParts.length >= 3) { + } else if (moduleParts.length >= 3) { const hostnameMatch = hostnameMatchRegex.exec(source); if (hostnameMatch?.groups) { dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`]; From a1929110880a463d071f62bf2d54e74cb19e6cbe Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 11:16:55 +0100 Subject: [PATCH 04/12] fix: Fix typo --- lib/modules/manager/terragrunt/modules.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index d6bfd2da02cd02..11ab9c8d0a9aea 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -67,7 +67,6 @@ export function analyseTerragruntModule( dep.depType = 'terragrunt'; dep.depName = source.split('//')[1].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; - } } else if (moduleParts.length >= 3) { const hostnameMatch = hostnameMatchRegex.exec(source); if (hostnameMatch?.groups) { From cebb84308260acfb6535d9cb878b640cc41410f9 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 16:04:12 +0100 Subject: [PATCH 05/12] test: added tests and fixed dependency extraction --- .../manager/terragrunt/__fixtures__/1.hcl | 39 +++++++++++++++++++ .../manager/terragrunt/extract.spec.ts | 29 ++++++++++++++ lib/modules/manager/terragrunt/modules.ts | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 lib/modules/manager/terragrunt/__fixtures__/1.hcl diff --git a/lib/modules/manager/terragrunt/__fixtures__/1.hcl b/lib/modules/manager/terragrunt/__fixtures__/1.hcl new file mode 100644 index 00000000000000..bd19cac6786432 --- /dev/null +++ b/lib/modules/manager/terragrunt/__fixtures__/1.hcl @@ -0,0 +1,39 @@ +#real +terraform { + extra_arguments "common_vars" { + commands = ["plan", "apply"] + + arguments = [ + "-var-file=../../common.tfvars", + "-var-file=../region.tfvars" + ] + } + + before_hook "before_hook" { + commands = ["apply", "plan"] + execute = ["echo", "Running Terraform"] + } + + source = "tfr:///myuser/myrepo//folder/modules/moduleone?ref=v0.0.9" + + after_hook "after_hook" { + commands = ["apply", "plan"] + execute = ["echo", "Finished running Terraform"] + run_on_error = true + } +} + +#submodule +terraform { + source = "tfr:///terraform-google-modules/kubernetes-engine/google//modules/private-cluster?version=1.2.3" +} + +#bar +terraform { + source = "tfr:///terraform-aws-modules/vpc/aws?version=3.3.0" +} + +#missing third backslash +terraform { + source = "tfr://terraform-aws-modules/vpc/aws?version=3.3.0" +} diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index 5b7af54845281c..c6cbfa039012ea 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -7,6 +7,35 @@ describe('modules/manager/terragrunt/extract', () => { expect(extractPackageFile('nothing here')).toBeNull(); }); + it('extracts terragrunt sources', () => { + const res = extractPackageFile(Fixtures.get('1.hcl')); + expect(res).toEqual({ + deps: [ + { + datasource: 'terraform-module', + depName: 'myuser/myrepo', + depType: 'terragrunt', + registryUrls: ['registry.terraform.io'], + }, + { + datasource: 'terraform-module', + depName: 'terraform-google-modules/kubernetes-engine/google', + depType: 'terragrunt', + registryUrls: ['registry.terraform.io'], + }, + { + datasource: 'terraform-module', + depName: 'terraform-aws-modules/vpc/aws', + depType: 'terragrunt', + registryUrls: ['registry.terraform.io'], + }, + {} + ], + }); + expect(res?.deps).toHaveLength(4); + expect(res?.deps.filter((dep) => dep.skipReason)).toHaveLength(0); + }); + it('extracts terragrunt sources', () => { const res = extractPackageFile(Fixtures.get('2.hcl')); expect(res).toEqual({ diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 11ab9c8d0a9aea..0481908615ca3f 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -65,7 +65,7 @@ export function analyseTerragruntModule( } else if (source.startsWith('tfr:///')) { dep.registryUrls = ['registry.terraform.io'] dep.depType = 'terragrunt'; - dep.depName = source.split('//')[1].replace('/', ''); + dep.depName = source.split('//')[1].split('?')[0].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; } else if (moduleParts.length >= 3) { const hostnameMatch = hostnameMatchRegex.exec(source); From b07d3d9cba39e995b30de12d41f30195f247fe09 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 16:06:21 +0100 Subject: [PATCH 06/12] fix: Update comment for tests --- lib/modules/manager/terragrunt/extract.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index c6cbfa039012ea..bbe3cdb539151c 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -7,7 +7,7 @@ describe('modules/manager/terragrunt/extract', () => { expect(extractPackageFile('nothing here')).toBeNull(); }); - it('extracts terragrunt sources', () => { + it('extracts terragrunt sources using tfr protocol', () => { const res = extractPackageFile(Fixtures.get('1.hcl')); expect(res).toEqual({ deps: [ From b3ec7665e75d818010ff91fa1fd3a04de0bde21a Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Mon, 11 Dec 2023 17:02:14 +0100 Subject: [PATCH 07/12] fix: Fix syntax --- lib/modules/manager/terragrunt/extract.spec.ts | 2 +- lib/modules/manager/terragrunt/modules.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index bbe3cdb539151c..a9c7bd1d52edcb 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -29,7 +29,7 @@ describe('modules/manager/terragrunt/extract', () => { depType: 'terragrunt', registryUrls: ['registry.terraform.io'], }, - {} + {}, ], }); expect(res?.deps).toHaveLength(4); diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 0481908615ca3f..4c08f924fb922b 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -63,7 +63,7 @@ export function analyseTerragruntModule( if (moduleParts[0] === '..') { dep.skipReason = 'local'; } else if (source.startsWith('tfr:///')) { - dep.registryUrls = ['registry.terraform.io'] + dep.registryUrls = ['registry.terraform.io']; dep.depType = 'terragrunt'; dep.depName = source.split('//')[1].split('?')[0].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; From 128b19566a5cc32fdca3ce1fef69784b035701fe Mon Sep 17 00:00:00 2001 From: ps-occrp <138824595+ps-occrp@users.noreply.github.com> Date: Tue, 12 Dec 2023 17:45:15 +0100 Subject: [PATCH 08/12] Fix: remove default registry url --- lib/modules/manager/terragrunt/modules.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 4c08f924fb922b..efc2da397ed038 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -63,7 +63,6 @@ export function analyseTerragruntModule( if (moduleParts[0] === '..') { dep.skipReason = 'local'; } else if (source.startsWith('tfr:///')) { - dep.registryUrls = ['registry.terraform.io']; dep.depType = 'terragrunt'; dep.depName = source.split('//')[1].split('?')[0].replace('/', ''); dep.datasource = TerraformModuleDatasource.id; From 7cdbb56d290763b77ebe1c7d3dd9378e5b5e69c0 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Tue, 12 Dec 2023 18:25:52 +0100 Subject: [PATCH 09/12] fix: Fix tests --- lib/modules/manager/terragrunt/extract.spec.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index a9c7bd1d52edcb..ed5432e837c350 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -15,19 +15,16 @@ describe('modules/manager/terragrunt/extract', () => { datasource: 'terraform-module', depName: 'myuser/myrepo', depType: 'terragrunt', - registryUrls: ['registry.terraform.io'], }, { datasource: 'terraform-module', depName: 'terraform-google-modules/kubernetes-engine/google', depType: 'terragrunt', - registryUrls: ['registry.terraform.io'], }, { datasource: 'terraform-module', depName: 'terraform-aws-modules/vpc/aws', depType: 'terragrunt', - registryUrls: ['registry.terraform.io'], }, {}, ], From ab0aa050ef9b56a566bec13fa9ea3b9753f991fc Mon Sep 17 00:00:00 2001 From: ps-occrp <138824595+ps-occrp@users.noreply.github.com> Date: Tue, 12 Dec 2023 20:19:54 +0100 Subject: [PATCH 10/12] Update lib/modules/manager/terragrunt/extract.spec.ts Co-authored-by: Michael Kriese --- lib/modules/manager/terragrunt/extract.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index ed5432e837c350..0a3dffb6969287 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -29,8 +29,6 @@ describe('modules/manager/terragrunt/extract', () => { {}, ], }); - expect(res?.deps).toHaveLength(4); - expect(res?.deps.filter((dep) => dep.skipReason)).toHaveLength(0); }); it('extracts terragrunt sources', () => { From d5d7089fa6167d9efa57d56a5ec3f030a9d9a762 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Tue, 12 Dec 2023 22:55:15 +0100 Subject: [PATCH 11/12] feat: refactor using regex --- .../manager/terragrunt/__fixtures__/1.hcl | 2 +- lib/modules/manager/terragrunt/extract.spec.ts | 5 ++++- lib/modules/manager/terragrunt/modules.ts | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/modules/manager/terragrunt/__fixtures__/1.hcl b/lib/modules/manager/terragrunt/__fixtures__/1.hcl index bd19cac6786432..61164ff13f4e93 100644 --- a/lib/modules/manager/terragrunt/__fixtures__/1.hcl +++ b/lib/modules/manager/terragrunt/__fixtures__/1.hcl @@ -14,7 +14,7 @@ terraform { execute = ["echo", "Running Terraform"] } - source = "tfr:///myuser/myrepo//folder/modules/moduleone?ref=v0.0.9" + source = "tfr:///myuser/myrepo/cloud//folder/modules/moduleone?ref=v0.0.9" after_hook "after_hook" { commands = ["apply", "plan"] diff --git a/lib/modules/manager/terragrunt/extract.spec.ts b/lib/modules/manager/terragrunt/extract.spec.ts index 0a3dffb6969287..7dfae48e2f8f59 100644 --- a/lib/modules/manager/terragrunt/extract.spec.ts +++ b/lib/modules/manager/terragrunt/extract.spec.ts @@ -12,16 +12,19 @@ describe('modules/manager/terragrunt/extract', () => { expect(res).toEqual({ deps: [ { + currentValue: 'v0.0.9', datasource: 'terraform-module', - depName: 'myuser/myrepo', + depName: 'myuser/myrepo/cloud', depType: 'terragrunt', }, { + currentValue: '1.2.3', datasource: 'terraform-module', depName: 'terraform-google-modules/kubernetes-engine/google', depType: 'terragrunt', }, { + currentValue: '3.3.0', datasource: 'terraform-module', depName: 'terraform-aws-modules/vpc/aws', depType: 'terragrunt', diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index efc2da397ed038..5451df00d153d7 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -13,6 +13,9 @@ export const githubRefMatchRegex = regEx( export const gitTagsRefMatchRegex = regEx( /(?:git::)?(?(?:http|https|ssh):\/\/(?:.*@)?(?.*.*\/(?.*\/.*)))\?(depth=\d+&)?ref=(?.*?)(&depth=\d+)?$/, ); +export const tfrVersionMatchRegex = regEx( + /tfr:\/\/(?.*?)\/(?[^\/]+?)\/(?[^\/]+?)\/(?[^\/?]+).*\?(?:ref|version)=(?.*?)$/, +); const hostnameMatchRegex = regEx(/^(?([\w|\d]+\.)+[\w|\d]+)/); export function extractTerragruntModule( @@ -35,6 +38,7 @@ export function analyseTerragruntModule( const source = dep.managerData!.source; const githubRefMatch = githubRefMatchRegex.exec(source ?? ''); const gitTagsRefMatch = gitTagsRefMatchRegex.exec(source ?? ''); + const tfrVersionMatch = tfrVersionMatchRegex.exec(source ?? ''); if (githubRefMatch?.groups) { dep.depType = 'github'; @@ -58,14 +62,20 @@ export function analyseTerragruntModule( } dep.currentValue = gitTagsRefMatch.groups.tag; dep.datasource = GitTagsDatasource.id; + } else if (tfrVersionMatch?.groups) { + dep.depType = 'terragrunt'; + dep.depName = + tfrVersionMatch.groups.org + + '/' + + tfrVersionMatch.groups.name + + '/' + + tfrVersionMatch.groups.cloud; + dep.currentValue = tfrVersionMatch.groups.currentValue; + dep.datasource = TerraformModuleDatasource.id; } else if (source) { const moduleParts = source.split('//')[0].split('/'); if (moduleParts[0] === '..') { dep.skipReason = 'local'; - } else if (source.startsWith('tfr:///')) { - dep.depType = 'terragrunt'; - dep.depName = source.split('//')[1].split('?')[0].replace('/', ''); - dep.datasource = TerraformModuleDatasource.id; } else if (moduleParts.length >= 3) { const hostnameMatch = hostnameMatchRegex.exec(source); if (hostnameMatch?.groups) { From 33e054f0ef869e4e2ad910e31f6bfde0485c9c19 Mon Sep 17 00:00:00 2001 From: Pujan Shah Date: Tue, 12 Dec 2023 22:57:00 +0100 Subject: [PATCH 12/12] fix: fix linting issue --- lib/modules/manager/terragrunt/modules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts index 5451df00d153d7..2e22f7d3c1bbc8 100644 --- a/lib/modules/manager/terragrunt/modules.ts +++ b/lib/modules/manager/terragrunt/modules.ts @@ -14,7 +14,7 @@ export const gitTagsRefMatchRegex = regEx( /(?:git::)?(?(?:http|https|ssh):\/\/(?:.*@)?(?.*.*\/(?.*\/.*)))\?(depth=\d+&)?ref=(?.*?)(&depth=\d+)?$/, ); export const tfrVersionMatchRegex = regEx( - /tfr:\/\/(?.*?)\/(?[^\/]+?)\/(?[^\/]+?)\/(?[^\/?]+).*\?(?:ref|version)=(?.*?)$/, + /tfr:\/\/(?.*?)\/(?[^/]+?)\/(?[^/]+?)\/(?[^/?]+).*\?(?:ref|version)=(?.*?)$/, ); const hostnameMatchRegex = regEx(/^(?([\w|\d]+\.)+[\w|\d]+)/);