-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathextract.ts
78 lines (77 loc) · 2.58 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
import * as datasourceOrb from '../../datasource/orb';
import { logger } from '../../logger';
import * as npmVersioning from '../../versioning/npm';
import { getDep } from '../dockerfile/extract';
import type { PackageDependency, PackageFile } from '../types';
export function extractPackageFile(content: string): PackageFile | null {
const deps: PackageDependency[] = [];
try {
const lines = content.split('\n');
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
const line = lines[lineNumber];
const orbs = /^\s*orbs:\s*$/.exec(line);
if (orbs) {
logger.trace(`Matched orbs on line ${lineNumber}`);
let foundOrbOrNoop: boolean;
do {
foundOrbOrNoop = false;
const orbLine = lines[lineNumber + 1];
logger.trace(`orbLine: "${orbLine}"`);
const yamlNoop = /^\s*(#|$)/.exec(orbLine);
if (yamlNoop) {
logger.debug('orbNoop');
foundOrbOrNoop = true;
lineNumber += 1;
continue; // eslint-disable-line no-continue
}
const orbMatch = /^\s+([^:]+):\s(.+)$/.exec(orbLine);
if (orbMatch) {
logger.trace('orbMatch');
foundOrbOrNoop = true;
lineNumber += 1;
const depName = orbMatch[1];
const [orbName, currentValue] = orbMatch[2].split('@');
const dep: PackageDependency = {
depType: 'orb',
depName,
currentValue,
datasource: datasourceOrb.id,
lookupName: orbName,
commitMessageTopic: '{{{depName}}} orb',
versioning: npmVersioning.id,
rangeStrategy: 'pin',
};
deps.push(dep);
}
} while (foundOrbOrNoop);
}
const match = /^\s*-? image:\s*'?"?([^\s'"]+)'?"?\s*$/.exec(line);
if (match) {
const currentFrom = match[1];
const dep = getDep(currentFrom);
logger.debug(
{
depName: dep.depName,
currentValue: dep.currentValue,
currentDigest: dep.currentDigest,
},
'CircleCI docker image'
);
dep.depType = 'docker';
dep.versioning = 'docker';
if (
!dep.depName?.startsWith('ubuntu-') &&
!dep.depName?.startsWith('windows-server-')
) {
deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error extracting circleci images');
}
if (!deps.length) {
return null;
}
return { deps };
}