From 108a00bad4707de24f077eec274fb2e4ee0d8fe5 Mon Sep 17 00:00:00 2001
From: Ross Rasmussen <raspy8766@gmail.com>
Date: Fri, 23 Apr 2021 10:32:45 -0700
Subject: [PATCH] fix(terraform): support github repo source url with
 underscores (#9661)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/manager/terraform/modules.spec.ts  | 64 ++++++++++++++++++++++++++
 lib/manager/terraform/modules.ts       |  4 +-
 lib/manager/terragrunt/modules.spec.ts | 64 ++++++++++++++++++++++++++
 lib/manager/terragrunt/modules.ts      |  4 +-
 4 files changed, 132 insertions(+), 4 deletions(-)
 create mode 100644 lib/manager/terraform/modules.spec.ts
 create mode 100644 lib/manager/terragrunt/modules.spec.ts

diff --git a/lib/manager/terraform/modules.spec.ts b/lib/manager/terraform/modules.spec.ts
new file mode 100644
index 0000000000..2dcf3a594f
--- /dev/null
+++ b/lib/manager/terraform/modules.spec.ts
@@ -0,0 +1,64 @@
+import { getName } from '../../../test/util';
+import { gitTagsRefMatchRegex, githubRefMatchRegex } from './modules';
+
+describe(getName(__filename), () => {
+  describe('githubRefMatchRegex', () => {
+    it('should split project and tag from source', () => {
+      const { project, tag } = githubRefMatchRegex.exec(
+        'github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      expect(project).toBe('hashicorp/example');
+      expect(tag).toBe('v1.0.0');
+    });
+
+    it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
+      const { project } = githubRefMatchRegex.exec(
+        'github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      expect(project).toBe('hashicorp/example.repo-123');
+    });
+  });
+  describe('gitTagsRefMatchRegex', () => {
+    it('should split project and tag from source', () => {
+      const http = gitTagsRefMatchRegex.exec(
+        'http://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      const https = gitTagsRefMatchRegex.exec(
+        'https://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      const ssh = gitTagsRefMatchRegex.exec(
+        'ssh://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+
+      expect(http.project).toBe('hashicorp/example');
+      expect(http.tag).toBe('v1.0.0');
+
+      expect(https.project).toBe('hashicorp/example');
+      expect(https.tag).toBe('v1.0.0');
+
+      expect(ssh.project).toBe('hashicorp/example');
+      expect(ssh.tag).toBe('v1.0.0');
+    });
+
+    it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
+      const http = gitTagsRefMatchRegex.exec(
+        'http://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      const https = gitTagsRefMatchRegex.exec(
+        'https://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      const ssh = gitTagsRefMatchRegex.exec(
+        'ssh://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+
+      expect(http.project).toBe('hashicorp/example.repo-123');
+      expect(http.tag).toBe('v1.0.0');
+
+      expect(https.project).toBe('hashicorp/example.repo-123');
+      expect(https.tag).toBe('v1.0.0');
+
+      expect(ssh.project).toBe('hashicorp/example.repo-123');
+      expect(ssh.tag).toBe('v1.0.0');
+    });
+  });
+});
diff --git a/lib/manager/terraform/modules.ts b/lib/manager/terraform/modules.ts
index 692c120328..d4d645565f 100644
--- a/lib/manager/terraform/modules.ts
+++ b/lib/manager/terraform/modules.ts
@@ -7,8 +7,8 @@ import type { PackageDependency } from '../types';
 import { extractTerraformProvider } from './providers';
 import { ExtractionResult, TerraformDependencyTypes } from './util';
 
-const githubRefMatchRegex = /github.com([/:])(?<project>[^/]+\/[a-z0-9-.]+).*\?ref=(?<tag>.*)$/;
-const gitTagsRefMatchRegex = /(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/;
+export const githubRefMatchRegex = /github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?ref=(?<tag>.*)$/i;
+export const gitTagsRefMatchRegex = /(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/;
 const hostnameMatchRegex = /^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/;
 
 export function extractTerraformModule(
diff --git a/lib/manager/terragrunt/modules.spec.ts b/lib/manager/terragrunt/modules.spec.ts
new file mode 100644
index 0000000000..2dcf3a594f
--- /dev/null
+++ b/lib/manager/terragrunt/modules.spec.ts
@@ -0,0 +1,64 @@
+import { getName } from '../../../test/util';
+import { gitTagsRefMatchRegex, githubRefMatchRegex } from './modules';
+
+describe(getName(__filename), () => {
+  describe('githubRefMatchRegex', () => {
+    it('should split project and tag from source', () => {
+      const { project, tag } = githubRefMatchRegex.exec(
+        'github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      expect(project).toBe('hashicorp/example');
+      expect(tag).toBe('v1.0.0');
+    });
+
+    it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
+      const { project } = githubRefMatchRegex.exec(
+        'github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      expect(project).toBe('hashicorp/example.repo-123');
+    });
+  });
+  describe('gitTagsRefMatchRegex', () => {
+    it('should split project and tag from source', () => {
+      const http = gitTagsRefMatchRegex.exec(
+        'http://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      const https = gitTagsRefMatchRegex.exec(
+        'https://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+      const ssh = gitTagsRefMatchRegex.exec(
+        'ssh://github.com/hashicorp/example?ref=v1.0.0'
+      ).groups;
+
+      expect(http.project).toBe('hashicorp/example');
+      expect(http.tag).toBe('v1.0.0');
+
+      expect(https.project).toBe('hashicorp/example');
+      expect(https.tag).toBe('v1.0.0');
+
+      expect(ssh.project).toBe('hashicorp/example');
+      expect(ssh.tag).toBe('v1.0.0');
+    });
+
+    it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
+      const http = gitTagsRefMatchRegex.exec(
+        'http://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      const https = gitTagsRefMatchRegex.exec(
+        'https://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+      const ssh = gitTagsRefMatchRegex.exec(
+        'ssh://github.com/hashicorp/example.repo-123?ref=v1.0.0'
+      ).groups;
+
+      expect(http.project).toBe('hashicorp/example.repo-123');
+      expect(http.tag).toBe('v1.0.0');
+
+      expect(https.project).toBe('hashicorp/example.repo-123');
+      expect(https.tag).toBe('v1.0.0');
+
+      expect(ssh.project).toBe('hashicorp/example.repo-123');
+      expect(ssh.tag).toBe('v1.0.0');
+    });
+  });
+});
diff --git a/lib/manager/terragrunt/modules.ts b/lib/manager/terragrunt/modules.ts
index 77ae489ab3..e11e12e20b 100644
--- a/lib/manager/terragrunt/modules.ts
+++ b/lib/manager/terragrunt/modules.ts
@@ -7,8 +7,8 @@ import type { PackageDependency } from '../types';
 import { extractTerragruntProvider } from './providers';
 import { ExtractionResult, TerragruntDependencyTypes } from './util';
 
-const githubRefMatchRegex = /github.com([/:])(?<project>[^/]+\/[a-z0-9-.]+).*\?ref=(?<tag>.*)$/;
-const gitTagsRefMatchRegex = /(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/;
+export const githubRefMatchRegex = /github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?ref=(?<tag>.*)$/i;
+export const gitTagsRefMatchRegex = /(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/;
 const hostnameMatchRegex = /^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/;
 
 export function extractTerragruntModule(
-- 
GitLab