forked from renovatebot/renovate
/
extract.ts
115 lines (101 loc) · 3.05 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import { logger } from '../../../logger';
import { isSkipComment } from '../../../util/ignore';
import { newlineRegex, regEx } from '../../../util/regex';
import { JenkinsPluginsDatasource } from '../../datasource/jenkins-plugins';
import * as mavenVersioning from '../../versioning/maven';
import type { PackageDependency, PackageFileContent } from '../types';
import type { JenkinsPlugin, JenkinsPlugins } from './types';
const YamlExtension = regEx(/\.ya?ml$/);
function getDependency(plugin: JenkinsPlugin): PackageDependency {
const dep: PackageDependency = {
datasource: JenkinsPluginsDatasource.id,
versioning: mavenVersioning.id,
depName: plugin.artifactId,
};
if (plugin.source?.version) {
dep.currentValue = plugin.source.version.toString();
if (!is.string(plugin.source.version)) {
dep.skipReason = 'invalid-version';
logger.warn(
{ dep },
'Jenkins plugin dependency version is not a string and will be ignored'
);
}
} else {
dep.skipReason = 'unspecified-version';
}
if (
plugin.source?.version === 'latest' ||
plugin.source?.version === 'experimental' ||
plugin.groupId
) {
dep.skipReason = 'unsupported-version';
}
if (plugin.source?.url) {
dep.skipReason = 'internal-package';
}
if (!dep.skipReason && plugin.renovate?.ignore) {
dep.skipReason = 'ignored';
}
logger.debug({ dep }, 'Jenkins plugin dependency');
return dep;
}
function extractYaml(content: string): PackageDependency[] {
const deps: PackageDependency[] = [];
try {
const doc = load(content, { json: true }) as JenkinsPlugins;
if (is.nonEmptyArray(doc?.plugins)) {
for (const plugin of doc.plugins) {
if (plugin.artifactId) {
const dep = getDependency(plugin);
deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error parsing Jenkins plugins');
}
return deps;
}
function extractText(content: string): PackageDependency[] {
const deps: PackageDependency[] = [];
const regex = regEx(
/^\s*(?<depName>[\d\w-]+):(?<currentValue>[^#\s]+)[#\s]*(?<comment>.*)$/
);
for (const line of content.split(newlineRegex)) {
const match = regex.exec(line);
if (match?.groups) {
const { depName, currentValue, comment } = match.groups;
const plugin: JenkinsPlugin = {
artifactId: depName,
source: {
version: currentValue,
},
renovate: {
ignore: isSkipComment(comment),
},
};
const dep = getDependency(plugin);
deps.push(dep);
}
}
return deps;
}
export function extractPackageFile(
content: string,
fileName: string
): PackageFileContent | null {
logger.trace('jenkins.extractPackageFile()');
const deps: PackageDependency[] = [];
if (YamlExtension.test(fileName)) {
deps.push(...extractYaml(content));
} else {
deps.push(...extractText(content));
}
if (deps.length === 0) {
return null;
}
return { deps };
}