-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
modules.ts
129 lines (124 loc) · 4.79 KB
/
modules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { logger } from '../../../logger';
import { detectPlatform } from '../../../util/common';
import { regEx } from '../../../util/regex';
import { BitbucketTagsDatasource } from '../../datasource/bitbucket-tags';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GiteaTagsDatasource } from '../../datasource/gitea-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
import { TerraformModuleDatasource } from '../../datasource/terraform-module';
import type { PackageDependency } from '../types';
import { extractTerragruntProvider } from './providers';
import type { ExtractionResult, TerraformManagerData } from './types';
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):\/\/(?:.*@)?(?<host>[^/]*)\/(?<path>.*))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
);
export const tfrVersionMatchRegex = regEx(
/tfr:\/\/(?<registry>.*?)\/(?<org>[^/]+?)\/(?<name>[^/]+?)\/(?<cloud>[^/?]+).*\?(?:ref|version)=(?<currentValue>.*?)$/,
);
const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);
export function extractTerragruntModule(
startingLine: number,
lines: string[],
): ExtractionResult {
const moduleName = 'terragrunt';
const result = extractTerragruntProvider(startingLine, lines, moduleName);
result.dependencies.forEach((dep) => {
// TODO #22198
dep.managerData!.terragruntDependencyType = 'terraform';
});
return result;
}
function detectGitTagDatasource(registryUrl: string): string {
const platform = detectPlatform(registryUrl);
switch (platform) {
case 'gitlab':
return GitlabTagsDatasource.id;
case 'bitbucket':
return BitbucketTagsDatasource.id;
case 'gitea':
return GiteaTagsDatasource.id;
default:
return GitTagsDatasource.id;
}
}
export function analyseTerragruntModule(
dep: PackageDependency<TerraformManagerData>,
): void {
// TODO #22198
const source = dep.managerData!.source;
const githubRefMatch = githubRefMatchRegex.exec(source ?? '');
const gitTagsRefMatch = gitTagsRefMatchRegex.exec(source ?? '');
const tfrVersionMatch = tfrVersionMatchRegex.exec(source ?? '');
if (githubRefMatch?.groups) {
dep.depType = 'github';
dep.packageName = githubRefMatch.groups.project.replace(
regEx(/\.git$/),
'',
);
dep.depName = 'github.com/' + dep.packageName;
dep.currentValue = githubRefMatch.groups.tag;
dep.datasource = GithubTagsDatasource.id;
} else if (gitTagsRefMatch?.groups) {
const { url, tag } = gitTagsRefMatch.groups;
const { hostname, host, origin, pathname, protocol } = new URL(url);
const containsSubDirectory = pathname.includes('//');
if (containsSubDirectory) {
logger.debug('Terragrunt module contains subdirectory');
}
dep.depType = 'gitTags';
// We don't want to have leading slash, .git or subdirectory in the repository path
const repositoryPath = pathname
.replace(regEx(/^\//), '')
.split('//')[0]
.replace(regEx('.git$'), '');
dep.depName = `${hostname}/${repositoryPath}`;
dep.currentValue = tag;
dep.datasource = detectGitTagDatasource(url);
if (dep.datasource === GitTagsDatasource.id) {
if (containsSubDirectory) {
dep.packageName = `${origin}${pathname.split('//')[0]}`;
} else {
dep.packageName = url;
}
} else {
// The packageName should only contain the path to the repository
dep.packageName = repositoryPath;
dep.registryUrls = [
protocol === 'https:' ? `https://${host}` : `https://${hostname}`,
];
}
} else if (tfrVersionMatch?.groups) {
dep.depType = 'terragrunt';
dep.depName =
tfrVersionMatch.groups.org +
'/' +
tfrVersionMatch.groups.name +
'/' +
tfrVersionMatch.groups.cloud;
dep.currentValue = tfrVersionMatch.groups.currentValue;
dep.datasource = TerraformModuleDatasource.id;
if (tfrVersionMatch.groups.registry) {
dep.registryUrls = [`https://${tfrVersionMatch.groups.registry}`];
}
} else if (source) {
const moduleParts = source.split('//')[0].split('/');
if (moduleParts[0] === '..') {
dep.skipReason = 'local';
} else if (moduleParts.length >= 3) {
const hostnameMatch = hostnameMatchRegex.exec(source);
if (hostnameMatch?.groups) {
dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`];
}
dep.depType = 'terragrunt';
dep.depName = moduleParts.join('/');
dep.datasource = TerraformModuleDatasource.id;
}
} else {
logger.debug({ dep }, 'terragrunt dep has no source');
dep.skipReason = 'no-source';
}
}