Skip to content

Commit

Permalink
fix(terragrunt): wrong packageName resolution for GitLab, Bitbucket a…
Browse files Browse the repository at this point in the history
…nd Gitea datasources (#28075)
  • Loading branch information
lstoeferle authored Apr 25, 2024
1 parent 5321e28 commit 8b3fb49
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
51 changes: 39 additions & 12 deletions lib/modules/manager/terragrunt/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ describe('modules/manager/terragrunt/extract', () => {
depType: 'github',
packageName: 'hashicorp/example',
},
{},
{
currentValue: 'next',
datasource: 'git-tags',
depName: '104.196.242.174/example',
depType: 'gitTags',
packageName: 'https://104.196.242.174/example',
},
{},
{
datasource: 'terraform-module',
Expand Down Expand Up @@ -205,21 +211,24 @@ describe('modules/manager/terragrunt/extract', () => {
datasource: 'bitbucket-tags',
depName: 'bitbucket.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://bitbucket.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://bitbucket.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitlab-tags',
depName: 'gitlab.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitlab.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitlab.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitea-tags',
depName: 'gitea.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitea.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitea.com'],
},
],
});
Expand Down Expand Up @@ -252,7 +261,13 @@ describe('modules/manager/terragrunt/extract', () => {
depType: 'github',
packageName: 'hashicorp/example',
},
{},
{
currentValue: 'next',
datasource: 'git-tags',
depName: '104.196.242.174/example',
depType: 'gitTags',
packageName: 'https://104.196.242.174/example',
},
{},
{
datasource: 'terraform-module',
Expand Down Expand Up @@ -391,21 +406,24 @@ describe('modules/manager/terragrunt/extract', () => {
datasource: 'bitbucket-tags',
depName: 'bitbucket.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://bitbucket.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://bitbucket.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitlab-tags',
depName: 'gitlab.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitlab.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitlab.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitea-tags',
depName: 'gitea.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitea.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitea.com'],
},
],
});
Expand Down Expand Up @@ -438,7 +456,13 @@ describe('modules/manager/terragrunt/extract', () => {
depType: 'github',
packageName: 'hashicorp/example',
},
{},
{
currentValue: 'next',
datasource: 'git-tags',
depName: '104.196.242.174/example',
depType: 'gitTags',
packageName: 'https://104.196.242.174/example',
},
{},
{
datasource: 'terraform-module',
Expand Down Expand Up @@ -577,21 +601,24 @@ describe('modules/manager/terragrunt/extract', () => {
datasource: 'bitbucket-tags',
depName: 'bitbucket.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://bitbucket.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://bitbucket.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitlab-tags',
depName: 'gitlab.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitlab.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitlab.com'],
},
{
currentValue: 'v1.0.0',
datasource: 'gitea-tags',
depName: 'gitea.com/hashicorp/example',
depType: 'gitTags',
packageName: 'https://gitea.com/hashicorp/example',
packageName: 'hashicorp/example',
registryUrls: ['https://gitea.com'],
},
],
});
Expand Down
17 changes: 10 additions & 7 deletions lib/modules/manager/terragrunt/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('modules/manager/terragrunt/modules', () => {
});

describe('gitTagsRefMatchRegex', () => {
it('should split project and tag from source', () => {
it('should split host, path and tag from source', () => {
const http = gitTagsRefMatchRegex.exec(
'http://github.com/hashicorp/example?ref=v1.0.0',
)?.groups;
Expand All @@ -36,15 +36,18 @@ describe('modules/manager/terragrunt/modules', () => {
)?.groups;

expect(http).toMatchObject({
project: 'hashicorp/example',
host: 'github.com',
path: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(https).toMatchObject({
project: 'hashicorp/example',
host: 'github.com',
path: 'hashicorp/example',
tag: 'v1.0.0',
});
expect(ssh).toMatchObject({
project: 'hashicorp/example',
host: 'github.com',
path: 'hashicorp/example',
tag: 'v1.0.0',
});
});
Expand All @@ -61,15 +64,15 @@ describe('modules/manager/terragrunt/modules', () => {
)?.groups;

expect(http).toMatchObject({
project: 'hashicorp/example.repo-123',
path: 'hashicorp/example.repo-123',
tag: 'v1.0.0',
});
expect(https).toMatchObject({
project: 'hashicorp/example.repo-123',
path: 'hashicorp/example.repo-123',
tag: 'v1.0.0',
});
expect(ssh).toMatchObject({
project: 'hashicorp/example.repo-123',
path: 'hashicorp/example.repo-123',
tag: 'v1.0.0',
});
});
Expand Down
30 changes: 20 additions & 10 deletions lib/modules/manager/terragrunt/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const githubRefMatchRegex = regEx(
/github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/i,
);
export const gitTagsRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
/(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<host>[^/]*)\/(?<path>.*))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
);
export const tfrVersionMatchRegex = regEx(
/tfr:\/\/(?<registry>.*?)\/(?<org>[^/]+?)\/(?<name>[^/]+?)\/(?<cloud>[^/?]+).*\?(?:ref|version)=(?<currentValue>.*?)$/,
Expand Down Expand Up @@ -68,18 +68,28 @@ export function analyseTerragruntModule(
dep.currentValue = githubRefMatch.groups.tag;
dep.datasource = GithubTagsDatasource.id;
} else if (gitTagsRefMatch?.groups) {
dep.depType = 'gitTags';
if (gitTagsRefMatch.groups.path.includes('//')) {
const { url, host, path, tag } = gitTagsRefMatch.groups;
const containsSubDirectory = path.includes('//');
if (containsSubDirectory) {
logger.debug('Terragrunt module contains subdirectory');
dep.depName = gitTagsRefMatch.groups.path.split('//')[0];
const tempLookupName = gitTagsRefMatch.groups.url.split('//');
dep.packageName = tempLookupName[0] + '//' + tempLookupName[1];
}
dep.depType = 'gitTags';
// We don't want to have .git or subdirectory in the depName
dep.depName = `${host}/${path.split('//')[0].replace('.git', '')}`;
dep.currentValue = tag;
dep.datasource = detectGitTagDatasource(url);
if (dep.datasource === GitTagsDatasource.id) {
if (containsSubDirectory) {
const [protocol, hostAndPath] = url.split('//');
dep.packageName = `${protocol}//${hostAndPath}`;
} else {
dep.packageName = url;
}
} else {
dep.depName = gitTagsRefMatch.groups.path.replace('.git', '');
dep.packageName = gitTagsRefMatch.groups.url;
// The packageName should only contain the path to the repository
dep.packageName = path.split('//')[0];
dep.registryUrls = [`https://${host}`];
}
dep.currentValue = gitTagsRefMatch.groups.tag;
dep.datasource = detectGitTagDatasource(gitTagsRefMatch.groups.url);
} else if (tfrVersionMatch?.groups) {
dep.depType = 'terragrunt';
dep.depName =
Expand Down

0 comments on commit 8b3fb49

Please sign in to comment.