Skip to content

Commit

Permalink
feat: support depth URL argument in Terraform modules (#21287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moglum committed Apr 12, 2023
1 parent 2de1b29 commit d7b7f04
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
77 changes: 77 additions & 0 deletions lib/modules/manager/terraform/extractors/others/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
const groups = githubRefMatchRegex.exec(
'github.com/hashicorp/example?ref=v1.0.0'
)?.groups;
const depth = githubRefMatchRegex.exec(
'github.com/hashicorp/example?depth=1&ref=v1.0.0'
)?.groups;
const depth2 = githubRefMatchRegex.exec(
'github.com/hashicorp/example?ref=v1.0.0&depth=1'
)?.groups;
expect(groups).toEqual({
project: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(depth).toEqual({
project: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(depth2).toEqual({
project: 'hashicorp/example',
tag: 'v1.0.0',
});
});

it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
Expand All @@ -47,6 +61,15 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
const ssh = gitTagsRefMatchRegex.exec(
'ssh://github.com/hashicorp/example?ref=v1.0.0'
)?.groups;
const depth = gitTagsRefMatchRegex.exec(
'ssh://github.com/hashicorp/example?depth=1&ref=v1.0.0'
)?.groups;
const depth2 = gitTagsRefMatchRegex.exec(
'ssh://github.com/hashicorp/example?ref=v1.0.0&depth=1'
)?.groups;
const folder = gitTagsRefMatchRegex.exec(
'git::ssh://git@git.example.com/modules/foo-module.git//bar?depth=1&ref=v1.0.0'
)?.groups;

expect(http).toMatchObject({
project: 'hashicorp/example',
Expand All @@ -60,6 +83,18 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
project: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(depth).toMatchObject({
project: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(depth2).toMatchObject({
project: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(folder).toMatchObject({
project: '/bar',
tag: 'v1.0.0',
});
});

it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
Expand Down Expand Up @@ -113,6 +148,12 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
const subfolderWithDoubleSlash = bitbucketRefMatchRegex.exec(
'bitbucket.org/hashicorp/example.git//terraform?ref=v1.0.0'
)?.groups;
const depth = bitbucketRefMatchRegex.exec(
'git::https://git@bitbucket.org/hashicorp/example.git?depth=1&ref=v1.0.0'
)?.groups;
const depth2 = bitbucketRefMatchRegex.exec(
'git::https://git@bitbucket.org/hashicorp/example.git?ref=v1.0.0&depth=1'
)?.groups;

expect(ssh).toMatchObject({
workspace: 'hashicorp',
Expand All @@ -139,6 +180,16 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
project: 'example',
tag: 'v1.0.0',
});
expect(depth).toMatchObject({
workspace: 'hashicorp',
project: 'example',
tag: 'v1.0.0',
});
expect(depth2).toMatchObject({
workspace: 'hashicorp',
project: 'example',
tag: 'v1.0.0',
});
});

it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
Expand Down Expand Up @@ -200,6 +251,32 @@ describe('modules/manager/terraform/extractors/others/modules', () => {
});
});

it('should split organization, project, repository and tag from source url with depth argument', () => {
const depth = azureDevOpsSshRefMatchRegex.exec(
'git::git@ssh.dev.azure.com:v3/MyOrg/MyProject/MyRepository//some-module/path?depth=1&ref=1.0.0'
)?.groups;
const depth2 = azureDevOpsSshRefMatchRegex.exec(
'git::git@ssh.dev.azure.com:v3/MyOrg/MyProject/MyRepository//some-module/path?ref=1.0.0&depth=1'
)?.groups;

expect(depth).toEqual({
modulepath: '//some-module/path',
organization: 'MyOrg',
project: 'MyProject',
repository: 'MyRepository',
tag: '1.0.0',
url: 'git@ssh.dev.azure.com:v3/MyOrg/MyProject/MyRepository',
});
expect(depth2).toEqual({
modulepath: '//some-module/path',
organization: 'MyOrg',
project: 'MyProject',
repository: 'MyRepository',
tag: '1.0.0',
url: 'git@ssh.dev.azure.com:v3/MyOrg/MyProject/MyRepository',
});
});

it('should parse alpha-numeric characters as well as dots, underscores, and dashes in repo names', () => {
const dots = azureDevOpsSshRefMatchRegex.exec(
'git::git@ssh.dev.azure.com:v3/MyOrg/MyProject/MyRepository//some-module/path?ref=v1.0.0'
Expand Down
8 changes: 4 additions & 4 deletions lib/modules/manager/terraform/extractors/others/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import { DependencyExtractor } from '../../base';
import type { TerraformDefinitionFile } from '../../hcl/types';

export const githubRefMatchRegex = regEx(
/github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?ref=(?<tag>.*)$/i
/github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/i
);
export const bitbucketRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:http|https|ssh)?(?::\/\/)?(?:.*@)?(?<path>bitbucket\.org\/(?<workspace>.*)\/(?<project>.*).git\/?(?<subfolder>.*)))\?ref=(?<tag>.*)$/
/(?:git::)?(?<url>(?:http|https|ssh)?(?::\/\/)?(?:.*@)?(?<path>bitbucket\.org\/(?<workspace>.*)\/(?<project>.*).git\/?(?<subfolder>.*)))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/
);
export const gitTagsRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:(?:http|https|ssh):\/\/)?(?:.*@)?(?<path>.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/
/(?:git::)?(?<url>(?:(?:http|https|ssh):\/\/)?(?:.*@)?(?<path>.*\/(?<project>.*\/.*)))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/
);
export const azureDevOpsSshRefMatchRegex = regEx(
/(?:git::)?(?<url>git@ssh\.dev\.azure\.com:v3\/(?<organization>[^/]*)\/(?<project>[^/]*)\/(?<repository>[^/]*))(?<modulepath>.*)?\?ref=(?<tag>.*)$/
/(?: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]+)/);

Expand Down

0 comments on commit d7b7f04

Please sign in to comment.