/
extract.ts
84 lines (79 loc) · 2.86 KB
/
extract.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
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { newlineRegex, regEx } from '../../../util/regex';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { isVersion } from '../../versioning/semver';
import type { PackageDependency, PackageFileContent } from '../types';
export function extractPackageFile(
content: string,
packageFile?: string
): PackageFileContent | null {
const deps: PackageDependency[] = [];
try {
const lines = content.split(newlineRegex);
for (const line of lines) {
// Search each line for plugin names
const depLineMatch = regEx(
/^\s*(?:-\s+(?:\?\s+)?)?(?<depName>[^#\s]+)#(?<currentValue>[^:]+)/
).exec(line);
if (depLineMatch?.groups) {
const { depName, currentValue } = depLineMatch.groups;
logger.trace('depLineMatch');
let skipReason: SkipReason | undefined;
let repo: string | undefined;
logger.trace(`Found BuildKite plugin ${depName}`);
// Plugins may simply be git repos. If so, we need to parse out the registry.
const gitPluginMatch = regEx(
/(ssh:\/\/git@|https:\/\/)(?<registry>[^/]+)\/(?<gitPluginName>.*)/
).exec(depName);
if (gitPluginMatch?.groups) {
logger.debug('Examining git plugin');
const { registry, gitPluginName } = gitPluginMatch.groups;
const gitDepName = gitPluginName.replace(regEx('\\.git$'), '');
const dep: PackageDependency = {
depName: gitDepName,
currentValue,
registryUrls: ['https://' + registry],
datasource: GithubTagsDatasource.id,
};
deps.push(dep);
continue;
} else if (isVersion(currentValue)) {
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(
{ dependency: depName },
'Something is wrong with BuildKite plugin name'
);
skipReason = 'invalid-dependency-specification';
}
} else {
logger.debug(
`Skipping non-pinned buildkite current version ${currentValue}`
);
skipReason = 'invalid-version';
}
const dep: PackageDependency = {
depName,
currentValue,
skipReason,
};
if (repo) {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = repo;
}
deps.push(dep);
}
}
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, packageFile }, 'Error extracting BuildKite plugins');
}
if (!deps.length) {
return null;
}
return { deps };
}