Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support depth URL argument in Terraform modules #21287

Merged
merged 8 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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