2020-03-01 07:01:12 +00:00
|
|
|
import * as datasourceGithubTags from '../../datasource/github-tags';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { logger } from '../../logger';
|
2020-03-09 04:34:16 +00:00
|
|
|
import { SkipReason } from '../../types';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { isVersion } from '../../versioning/semver';
|
|
|
|
import { PackageDependency, PackageFile } from '../common';
|
2018-06-01 15:12:08 +00:00
|
|
|
|
2019-11-23 20:44:55 +00:00
|
|
|
export function extractPackageFile(content: string): PackageFile | null {
|
2019-07-25 06:17:19 +00:00
|
|
|
const deps: PackageDependency[] = [];
|
2018-04-27 17:54:44 +00:00
|
|
|
try {
|
|
|
|
const lines = content.split('\n');
|
2019-07-29 10:23:03 +00:00
|
|
|
let isPluginsSection = false;
|
|
|
|
let pluginsIndent = '';
|
|
|
|
for (let lineNumber = 1; lineNumber <= lines.length; lineNumber += 1) {
|
|
|
|
const lineIdx = lineNumber - 1;
|
|
|
|
const line = lines[lineIdx];
|
2020-02-05 18:17:20 +00:00
|
|
|
const pluginsSection = /^(?<pluginsIndent>\s*)(-?\s*)plugins:/.exec(line);
|
2019-07-29 10:23:03 +00:00
|
|
|
if (pluginsSection) {
|
2018-04-27 17:54:44 +00:00
|
|
|
logger.trace(`Matched plugins on line ${lineNumber}`);
|
2019-07-29 10:23:03 +00:00
|
|
|
isPluginsSection = true;
|
|
|
|
pluginsIndent = pluginsSection.groups.pluginsIndent;
|
|
|
|
} else if (isPluginsSection) {
|
|
|
|
logger.debug(`serviceImageLine: "${line}"`);
|
2020-02-05 18:17:20 +00:00
|
|
|
const { currentIndent } = /^(?<currentIndent>\s*)/.exec(line).groups;
|
|
|
|
const depLineMatch = /^\s+(?:-\s+)?(?<depName>[^#]+)#(?<currentValue>[^:]+)/.exec(
|
|
|
|
line
|
2019-07-29 10:23:03 +00:00
|
|
|
);
|
|
|
|
if (currentIndent.length <= pluginsIndent.length) {
|
|
|
|
isPluginsSection = false;
|
|
|
|
pluginsIndent = '';
|
|
|
|
} else if (depLineMatch) {
|
|
|
|
const { depName, currentValue } = depLineMatch.groups;
|
2018-04-27 17:54:44 +00:00
|
|
|
logger.trace('depLineMatch');
|
2020-03-09 04:34:16 +00:00
|
|
|
let skipReason: SkipReason;
|
2019-07-25 06:17:19 +00:00
|
|
|
let repo: string;
|
2018-06-01 15:12:08 +00:00
|
|
|
if (depName.startsWith('https://') || depName.startsWith('git@')) {
|
2018-09-20 10:13:18 +00:00
|
|
|
logger.debug({ dependency: depName }, 'Skipping git plugin');
|
2020-03-09 04:34:16 +00:00
|
|
|
skipReason = SkipReason.GitPlugin;
|
2018-06-04 04:03:21 +00:00
|
|
|
} else if (!isVersion(currentValue)) {
|
2018-06-01 15:12:08 +00:00
|
|
|
logger.debug(
|
2018-06-04 03:48:20 +00:00
|
|
|
{ currentValue },
|
2018-06-01 15:12:08 +00:00
|
|
|
'Skipping non-pinned current version'
|
|
|
|
);
|
2020-03-09 04:34:16 +00:00
|
|
|
skipReason = SkipReason.InvalidVersion;
|
2018-06-01 15:12:08 +00:00
|
|
|
} else {
|
|
|
|
const splitName = depName.split('/');
|
|
|
|
if (splitName.length === 1) {
|
|
|
|
repo = `buildkite-plugins/${depName}-buildkite-plugin`;
|
|
|
|
} else if (splitName.length === 2) {
|
|
|
|
repo = `${depName}-buildkite-plugin`;
|
|
|
|
} else {
|
|
|
|
logger.warn(
|
2018-09-20 10:13:18 +00:00
|
|
|
{ dependency: depName },
|
2018-06-01 15:12:08 +00:00
|
|
|
'Something is wrong with buildkite plugin name'
|
|
|
|
);
|
2021-02-18 07:09:04 +00:00
|
|
|
skipReason = SkipReason.InvalidDependencySpecification;
|
2018-06-01 15:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-25 06:17:19 +00:00
|
|
|
const dep: PackageDependency = {
|
2018-04-27 17:54:44 +00:00
|
|
|
depName,
|
2018-06-04 03:48:20 +00:00
|
|
|
currentValue,
|
2018-06-01 15:12:08 +00:00
|
|
|
skipReason,
|
2019-02-04 08:41:22 +00:00
|
|
|
};
|
|
|
|
if (repo) {
|
2020-03-01 07:01:12 +00:00
|
|
|
dep.datasource = datasourceGithubTags.id;
|
2019-02-04 08:41:22 +00:00
|
|
|
dep.lookupName = repo;
|
|
|
|
}
|
|
|
|
deps.push(dep);
|
2018-04-27 17:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
2019-07-02 05:08:48 +00:00
|
|
|
logger.warn({ err }, 'Error extracting buildkite plugins');
|
2018-04-27 17:54:44 +00:00
|
|
|
}
|
2018-05-03 16:09:18 +00:00
|
|
|
if (!deps.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return { deps };
|
2018-04-27 17:54:44 +00:00
|
|
|
}
|