renovate/lib/manager/terraform/modules.ts
Sergei Zharinov 8a7abfdf01
refactor(github): Convert datasources to class form (#14124)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
2022-02-13 03:24:40 +00:00

85 lines
3.5 KiB
TypeScript

import { BitBucketTagsDatasource } from '../../datasource/bitbucket-tags';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { TerraformModuleDatasource } from '../../datasource/terraform-module';
import { logger } from '../../logger';
import { regEx } from '../../util/regex';
import type { PackageDependency } from '../types';
import { TerraformDependencyTypes } from './common';
import { extractTerraformProvider } from './providers';
import type { ExtractionResult } from './types';
export const githubRefMatchRegex = regEx(
/github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?ref=(?<tag>.*)$/i
);
export const bitbucketRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:http|https|ssh)?(?::\/\/)?(?:.*@)?(?<path>bitbucket\.org\/(?<workspace>.*)\/(?<project>.*).git\/?(?<subfolder>.*)))\?ref=(?<tag>.*)$/
);
export const gitTagsRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?ref=(?<tag>.*)$/
);
const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);
export function extractTerraformModule(
startingLine: number,
lines: string[],
moduleName: string
): ExtractionResult {
const result = extractTerraformProvider(startingLine, lines, moduleName);
result.dependencies.forEach((dep) => {
dep.managerData.terraformDependencyType = TerraformDependencyTypes.module;
});
return result;
}
export function analyseTerraformModule(dep: PackageDependency): void {
const githubRefMatch = githubRefMatchRegex.exec(dep.managerData.source);
const bitbucketRefMatch = bitbucketRefMatchRegex.exec(dep.managerData.source);
const gitTagsRefMatch = gitTagsRefMatchRegex.exec(dep.managerData.source);
if (githubRefMatch) {
dep.lookupName = githubRefMatch.groups.project.replace(regEx(/\.git$/), '');
dep.depType = 'module';
dep.depName = 'github.com/' + dep.lookupName;
dep.currentValue = githubRefMatch.groups.tag;
dep.datasource = GithubTagsDatasource.id;
} else if (bitbucketRefMatch) {
dep.depType = 'module';
dep.depName =
bitbucketRefMatch.groups.workspace +
'/' +
bitbucketRefMatch.groups.project;
dep.lookupName = dep.depName;
dep.currentValue = bitbucketRefMatch.groups.tag;
dep.datasource = BitBucketTagsDatasource.id;
} else if (gitTagsRefMatch) {
dep.depType = 'module';
if (gitTagsRefMatch.groups.path.includes('//')) {
logger.debug('Terraform module contains subdirectory');
dep.depName = gitTagsRefMatch.groups.path.split('//')[0];
const tempLookupName = gitTagsRefMatch.groups.url.split('//');
dep.lookupName = tempLookupName[0] + '//' + tempLookupName[1];
} else {
dep.depName = gitTagsRefMatch.groups.path.replace('.git', '');
dep.lookupName = gitTagsRefMatch.groups.url;
}
dep.currentValue = gitTagsRefMatch.groups.tag;
dep.datasource = GitTagsDatasource.id;
} else if (dep.managerData.source) {
const moduleParts = dep.managerData.source.split('//')[0].split('/');
if (moduleParts[0] === '..') {
dep.skipReason = 'local';
} else if (moduleParts.length >= 3) {
const hostnameMatch = hostnameMatchRegex.exec(dep.managerData.source);
if (hostnameMatch) {
dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`];
}
dep.depType = 'module';
dep.depName = moduleParts.join('/');
dep.datasource = TerraformModuleDatasource.id;
}
} else {
logger.debug({ dep }, 'terraform dep has no source');
dep.skipReason = 'no-source';
}
}