renovate/lib/modules/manager/terraform/required-version.ts

54 lines
2.1 KiB
TypeScript
Raw Normal View History

import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { GithubReleasesDatasource } from '../../datasource/github-releases';
2021-03-02 20:44:55 +00:00
import type { PackageDependency } from '../types';
2021-05-11 17:08:02 +00:00
import { TerraformDependencyTypes } from './common';
import type { ExtractionResult, TerraformManagerData } from './types';
2021-05-11 17:08:02 +00:00
import { keyValueExtractionRegex } from './util';
export function extractTerraformRequiredVersion(
startingLine: number,
lines: string[]
): ExtractionResult | null {
const deps: PackageDependency<TerraformManagerData>[] = [];
let lineNumber = startingLine;
let braceCounter = 0;
do {
// istanbul ignore if
if (lineNumber > lines.length - 1) {
logger.debug(`Malformed Terraform file detected.`);
}
const line = lines[lineNumber];
// `{` will be counted wit +1 and `}` with -1. Therefore if we reach braceCounter == 0. We have found the end of the terraform block
const openBrackets = (line.match(regEx(/\{/g)) ?? []).length;
const closedBrackets = (line.match(regEx(/\}/g)) ?? []).length;
braceCounter = braceCounter + openBrackets - closedBrackets;
const kvMatch = keyValueExtractionRegex.exec(line);
if (kvMatch?.groups && kvMatch.groups.key === 'required_version') {
const dep: PackageDependency<TerraformManagerData> = {
currentValue: kvMatch.groups.value,
lineNumber,
managerData: {
terraformDependencyType: TerraformDependencyTypes.terraform_version,
},
};
deps.push(dep);
// returning starting line as required_providers are also in the terraform block
// if we would return the position of the required_version line we would potentially skip the providers
return { lineNumber: startingLine, dependencies: deps };
}
lineNumber += 1;
} while (braceCounter !== 0);
return null;
}
export function analyseTerraformVersion(dep: PackageDependency): void {
dep.depType = 'required_version';
dep.datasource = GithubReleasesDatasource.id;
dep.depName = 'hashicorp/terraform';
dep.extractVersion = 'v(?<version>.*)$';
}