2019-07-25 06:17:19 +00:00
|
|
|
import { logger } from '../../logger';
|
|
|
|
import { isValid, isVersion } from '../../versioning/hashicorp';
|
|
|
|
import { PackageDependency, PackageFile } from '../common';
|
2018-10-06 11:39:39 +00:00
|
|
|
|
2019-07-25 06:17:19 +00:00
|
|
|
export function extractPackageFile(content: string): PackageFile {
|
2018-11-04 17:51:23 +00:00
|
|
|
logger.trace({ content }, 'terraform.extractPackageFile()');
|
2018-10-06 11:39:39 +00:00
|
|
|
if (!content.includes('module "')) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-25 06:17:19 +00:00
|
|
|
const deps: PackageDependency[] = [];
|
2018-10-06 11:39:39 +00:00
|
|
|
try {
|
|
|
|
const lines = content.split('\n');
|
|
|
|
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
|
|
|
|
let line = lines[lineNumber];
|
|
|
|
const module = line.match(/^module\s+"([^"]+)"\s+{\s*$/);
|
|
|
|
if (module) {
|
|
|
|
logger.trace(`Matched module on line ${lineNumber}`);
|
2019-07-25 06:17:19 +00:00
|
|
|
const dep: PackageDependency = {
|
2018-10-06 11:39:39 +00:00
|
|
|
moduleName: module[1],
|
2019-07-22 07:43:25 +00:00
|
|
|
managerData: {},
|
2018-10-06 11:39:39 +00:00
|
|
|
};
|
|
|
|
do {
|
|
|
|
lineNumber += 1;
|
|
|
|
line = lines[lineNumber];
|
|
|
|
const kvMatch = line.match(/^\s*([^\s]+)\s+=\s+"([^"]+)"\s*$/);
|
|
|
|
if (kvMatch) {
|
|
|
|
const [, key, value] = kvMatch;
|
|
|
|
if (key === 'version') {
|
|
|
|
dep.currentValue = value;
|
|
|
|
dep.versionLine = lineNumber;
|
|
|
|
} else if (key === 'source') {
|
|
|
|
dep.source = value;
|
|
|
|
dep.sourceLine = lineNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (line.trim() !== '}');
|
|
|
|
deps.push(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
2019-07-02 05:08:48 +00:00
|
|
|
logger.warn({ err }, 'Error extracting buildkite plugins');
|
2018-10-06 11:39:39 +00:00
|
|
|
}
|
|
|
|
deps.forEach(dep => {
|
|
|
|
const githubRefMatch =
|
|
|
|
dep.source &&
|
|
|
|
dep.source.match(/github.com(\/|:)([^/]+\/[a-z0-9-]+).*\?ref=(.*)$/);
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
if (githubRefMatch) {
|
|
|
|
dep.depType = 'github';
|
|
|
|
dep.depName = 'github.com/' + githubRefMatch[2];
|
|
|
|
dep.depNameShort = githubRefMatch[2];
|
|
|
|
dep.currentValue = githubRefMatch[3];
|
2019-02-04 08:41:22 +00:00
|
|
|
dep.datasource = 'github';
|
|
|
|
dep.lookupName = githubRefMatch[2];
|
2019-07-22 07:43:25 +00:00
|
|
|
dep.managerData.lineNumber = dep.sourceLine;
|
2018-12-13 04:29:11 +00:00
|
|
|
if (!isVersion(dep.currentValue)) {
|
2018-10-06 11:39:39 +00:00
|
|
|
dep.skipReason = 'unsupported-version';
|
|
|
|
}
|
2018-10-07 05:26:42 +00:00
|
|
|
} else if (dep.source) {
|
2018-10-06 11:39:39 +00:00
|
|
|
const moduleParts = dep.source.split('//')[0].split('/');
|
2018-10-06 12:45:32 +00:00
|
|
|
if (moduleParts[0] === '..') {
|
|
|
|
dep.skipReason = 'local';
|
2019-01-30 08:55:57 +00:00
|
|
|
} else if (moduleParts.length >= 3) {
|
2018-10-06 11:39:39 +00:00
|
|
|
dep.depType = 'terraform';
|
|
|
|
dep.depName = moduleParts.join('/');
|
|
|
|
dep.depNameShort = dep.depName;
|
2019-07-22 07:43:25 +00:00
|
|
|
dep.managerData.lineNumber = dep.versionLine;
|
2019-02-04 08:41:22 +00:00
|
|
|
dep.datasource = 'terraform';
|
2018-10-06 11:39:39 +00:00
|
|
|
}
|
2019-07-22 07:43:25 +00:00
|
|
|
if (dep.managerData.lineNumber) {
|
2018-12-13 04:29:11 +00:00
|
|
|
if (!isValid(dep.currentValue)) {
|
2018-10-06 11:39:39 +00:00
|
|
|
dep.skipReason = 'unsupported-version';
|
|
|
|
}
|
2018-10-06 12:45:32 +00:00
|
|
|
} else if (!dep.skipReason) {
|
2018-10-06 11:39:39 +00:00
|
|
|
dep.skipReason = 'no-version';
|
|
|
|
}
|
2018-10-07 05:26:42 +00:00
|
|
|
} else {
|
|
|
|
logger.info({ dep }, 'terraform dep has no source');
|
|
|
|
dep.skipReason = 'no-source';
|
2018-10-06 11:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete dep.sourceLine;
|
|
|
|
delete dep.versionLine;
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
});
|
2018-10-06 17:33:42 +00:00
|
|
|
if (deps.some(dep => dep.skipReason !== 'local')) {
|
|
|
|
return { deps };
|
|
|
|
}
|
|
|
|
return null;
|
2018-10-06 11:39:39 +00:00
|
|
|
}
|