From dd1b316687974fc827f6a0afbd1a499a15be16e9 Mon Sep 17 00:00:00 2001
From: Jellyfrog <Jellyfrog@users.noreply.github.com>
Date: Mon, 18 Nov 2024 12:17:35 +0100
Subject: [PATCH] fix(terraform): correct hostname regex and add tests (#32565)

---
 .../extractors/others/modules.spec.ts         | 19 +++++++++++++++++++
 .../terraform/extractors/others/modules.ts    |  4 +++-
 lib/modules/manager/terragrunt/modules.ts     |  4 +++-
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/lib/modules/manager/terraform/extractors/others/modules.spec.ts b/lib/modules/manager/terraform/extractors/others/modules.spec.ts
index 845e7b7c46..7062f4d39e 100644
--- a/lib/modules/manager/terraform/extractors/others/modules.spec.ts
+++ b/lib/modules/manager/terraform/extractors/others/modules.spec.ts
@@ -4,6 +4,7 @@ import {
   bitbucketRefMatchRegex,
   gitTagsRefMatchRegex,
   githubRefMatchRegex,
+  hostnameMatchRegex,
 } from './modules';
 
 describe('modules/manager/terraform/extractors/others/modules', () => {
@@ -300,4 +301,22 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
       });
     });
   });
+
+  describe('hostnameMatchRegex', () => {
+    it('should extact hostname from source url', () => {
+      const host1 = hostnameMatchRegex.exec(
+        'git-lab.git-server.com/my/terraform/module',
+      )?.groups;
+      const host2 = hostnameMatchRegex.exec(
+        'example.com/my/terraform/module',
+      )?.groups;
+
+      expect(host1).toEqual({
+        hostname: 'git-lab.git-server.com',
+      });
+      expect(host2).toEqual({
+        hostname: 'example.com',
+      });
+    });
+  });
 });
diff --git a/lib/modules/manager/terraform/extractors/others/modules.ts b/lib/modules/manager/terraform/extractors/others/modules.ts
index 5ba4f31c3c..21cbddca1f 100644
--- a/lib/modules/manager/terraform/extractors/others/modules.ts
+++ b/lib/modules/manager/terraform/extractors/others/modules.ts
@@ -21,7 +21,9 @@ export const gitTagsRefMatchRegex = regEx(
 export const azureDevOpsSshRefMatchRegex = regEx(
   /(?:git::)?(?<url>git@ssh\.dev\.azure\.com:v3\/(?<organization>[^/]*)\/(?<project>[^/]*)\/(?<repository>[^/]*))(?<modulepath>.*)?\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
 );
-const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);
+export const hostnameMatchRegex = regEx(
+  /^(?<hostname>[a-zA-Z\d]([a-zA-Z\d-]*\.)+[a-zA-Z\d]+)/,
+);
 
 export class ModuleExtractor extends DependencyExtractor {
   getCheckList(): string[] {
diff --git a/lib/modules/manager/terragrunt/modules.ts b/lib/modules/manager/terragrunt/modules.ts
index 9d71e1e8aa..0079b18581 100644
--- a/lib/modules/manager/terragrunt/modules.ts
+++ b/lib/modules/manager/terragrunt/modules.ts
@@ -20,7 +20,9 @@ export const gitTagsRefMatchRegex = regEx(
 export const tfrVersionMatchRegex = regEx(
   /tfr:\/\/(?<registry>.*?)\/(?<org>[^/]+?)\/(?<name>[^/]+?)\/(?<cloud>[^/?]+).*\?(?:ref|version)=(?<currentValue>.*?)$/,
 );
-const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);
+const hostnameMatchRegex = regEx(
+  /^(?<hostname>[a-zA-Z\d]([a-zA-Z\d-]*\.)+[a-zA-Z\d]+)/,
+);
 
 export function extractTerragruntModule(
   startingLine: number,
-- 
GitLab