/
extract.ts
142 lines (125 loc) · 3.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import is from '@sindresorhus/is';
import { loadAll } from 'js-yaml';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { HelmDatasource } from '../../datasource/helm';
import { checkIfStringIsPath } from '../terraform/util';
import type { PackageDependency, PackageFileContent } from '../types';
import type { FleetFile, FleetHelmBlock, GitRepo } from './types';
function extractGitRepo(doc: GitRepo): PackageDependency {
const dep: PackageDependency = {
depType: 'git_repo',
datasource: GitTagsDatasource.id,
};
const repo = doc.spec?.repo;
if (!repo) {
return {
...dep,
skipReason: 'missing-depname',
};
}
dep.sourceUrl = repo;
dep.depName = repo;
const currentValue = doc.spec.revision;
if (!currentValue) {
return {
...dep,
skipReason: 'unspecified-version',
};
}
return {
...dep,
currentValue,
};
}
function extractFleetHelmBlock(doc: FleetHelmBlock): PackageDependency {
const dep: PackageDependency = {
depType: 'fleet',
datasource: HelmDatasource.id,
};
if (!doc.chart) {
return {
...dep,
skipReason: 'missing-depname',
};
}
dep.depName = doc.chart;
dep.packageName = doc.chart;
if (!doc.repo) {
if (checkIfStringIsPath(doc.chart)) {
return {
...dep,
skipReason: 'local-chart',
};
}
return {
...dep,
skipReason: 'no-repository',
};
}
dep.registryUrls = [doc.repo];
const currentValue = doc.version;
if (!doc.version) {
return {
...dep,
skipReason: 'unspecified-version',
};
}
return {
...dep,
currentValue,
};
}
function extractFleetFile(doc: FleetFile): PackageDependency[] {
const result: PackageDependency[] = [];
result.push(extractFleetHelmBlock(doc.helm));
if (!is.undefined(doc.targetCustomizations)) {
// remove version from helm block to allow usage of variables defined in the global block, but do not create PRs
// if there is no version defined in the customization.
const helmBlockContext: FleetHelmBlock = { ...doc.helm };
delete helmBlockContext.version;
for (const custom of doc.targetCustomizations) {
const dep = extractFleetHelmBlock({
// merge base config with customization
...helmBlockContext,
...custom.helm,
});
result.push({
// overwrite name with customization name to allow splitting of PRs
...dep,
depName: custom.name,
});
}
}
return result;
}
export function extractPackageFile(
content: string,
packageFile: string
): PackageFileContent | null {
if (!content) {
return null;
}
const deps: PackageDependency[] = [];
try {
if (regEx('fleet.ya?ml').test(packageFile)) {
// TODO: fix me (#9610)
const docs = loadAll(content, null, { json: true }) as FleetFile[];
const fleetDeps = docs
.filter((doc) => is.truthy(doc?.helm))
.flatMap((doc) => extractFleetFile(doc));
deps.push(...fleetDeps);
} else {
// TODO: fix me (#9610)
const docs = loadAll(content, null, { json: true }) as GitRepo[];
const gitRepoDeps = docs
.filter((doc) => doc.kind === 'GitRepo') // ensure only GitRepo manifests are processed
.flatMap((doc) => extractGitRepo(doc));
deps.push(...gitRepoDeps);
}
} catch (err) {
logger.warn({ error: err, packageFile }, 'Failed to parse fleet YAML');
}
return deps.length ? { deps } : null;
}