/
extract.ts
61 lines (59 loc) · 1.75 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
import is from '@sindresorhus/is';
import yaml from 'js-yaml';
import * as datasourceGitlabTags from '../../datasource/gitlab-tags';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { replaceReferenceTags } from '../gitlabci/utils';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
function extractDepFromIncludeFile(includeObj: {
file: any;
project: string;
ref: string;
}): PackageDependency {
const dep: PackageDependency = {
datasource: datasourceGitlabTags.id,
depName: includeObj.project,
depType: 'repository',
};
if (!includeObj.ref) {
dep.skipReason = SkipReason.UnknownVersion;
return dep;
}
dep.currentValue = includeObj.ref;
return dep;
}
export function extractPackageFile(
content: string,
_packageFile: string,
config: ExtractConfig
): PackageFile | null {
const deps: PackageDependency[] = [];
try {
// TODO: fix me (#9610)
const doc = yaml.safeLoad(replaceReferenceTags(content), {
json: true,
}) as any;
if (doc?.include && is.array(doc.include)) {
for (const includeObj of doc.include) {
if (includeObj.file && includeObj.project) {
const dep = extractDepFromIncludeFile(includeObj);
if (config.endpoint) {
dep.registryUrls = [config.endpoint.replace(/\/api\/v4\/?/, '')];
}
deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
logger.debug({ err });
logger.debug('YAML exception extracting GitLab CI includes');
} else {
logger.warn({ err }, 'Error extracting GitLab CI includes');
}
}
if (!deps.length) {
return null;
}
return { deps };
}