Skip to content
Snippets Groups Projects
Unverified Commit 8d166a3f authored by Sebastian Poxhofer's avatar Sebastian Poxhofer Committed by GitHub
Browse files

refactor(manager/circleci): replace regex system with YAML parsing (#30142)

parent 7c7063d2
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ orbs:
no-version: abc/def
# Comments help me understand my work.
volatile: zzz/zzz@volatile # Comments help me understand my work.
volatile: "zzz/zzz@volatile" # Comments help me understand my work.
test_plan: &test_plan
steps:
......
aliases:
aliases:
- &nodejs
image: cimg/node:14.8.0
version: 2
jobs:
checkout:
<<: *defaults
checkout:
docker:
- *nodejs
steps:
- run: yarn build:runtime
release_docker:
<<: *defaults
machine:
image: ubuntu-1604:201903-01
docker_layer_caching: true
......
......@@ -10,7 +10,6 @@ exports[`modules/manager/circleci/extract extractPackageFile() extracts multiple
"depName": "node",
"depType": "docker",
"replaceString": "node",
"versioning": "docker",
},
{
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
......@@ -20,7 +19,6 @@ exports[`modules/manager/circleci/extract extractPackageFile() extracts multiple
"depName": "node",
"depType": "docker",
"replaceString": "node:4",
"versioning": "docker",
},
{
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
......@@ -30,7 +28,6 @@ exports[`modules/manager/circleci/extract extractPackageFile() extracts multiple
"depName": "node",
"depType": "docker",
"replaceString": "node:6",
"versioning": "docker",
},
{
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
......@@ -40,7 +37,6 @@ exports[`modules/manager/circleci/extract extractPackageFile() extracts multiple
"depName": "node",
"depType": "docker",
"replaceString": "node:8.9.0",
"versioning": "docker",
},
]
`;
......@@ -51,6 +51,7 @@ describe('modules/manager/circleci/extract', () => {
const res = extractPackageFile(file3);
expect(res?.deps).toMatchObject([
{ currentValue: '14.8.0', depName: 'cimg/node' },
{ currentValue: '14.8.0', depName: 'cimg/node' },
]);
});
......
import { logger } from '../../../logger';
import { newlineRegex, regEx } from '../../../util/regex';
import { coerceArray } from '../../../util/array';
import { parseSingleYaml } from '../../../util/yaml';
import { OrbDatasource } from '../../datasource/orb';
import * as npmVersioning from '../../versioning/npm';
import { getDep } from '../dockerfile/extract';
import type { PackageDependency, PackageFileContent } from '../types';
import { CircleCiFile } from './schema';
export function extractPackageFile(
content: string,
......@@ -11,70 +13,38 @@ export function extractPackageFile(
): PackageFileContent | null {
const deps: PackageDependency[] = [];
try {
const lines = content.split(newlineRegex);
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
const line = lines[lineNumber];
const orbs = regEx(/^\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 = regEx(/^\s*(#|$)/).exec(orbLine);
if (yamlNoop) {
logger.debug('orbNoop');
foundOrbOrNoop = true;
lineNumber += 1;
continue;
}
const orbMatch = regEx(/^\s+([^:]+):\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: OrbDatasource.id,
packageName: orbName,
commitMessageTopic: '{{{depName}}} orb',
versioning: npmVersioning.id,
};
deps.push(dep);
}
} while (foundOrbOrNoop);
}
const match = regEx(/^\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-') &&
!dep.depName?.startsWith('android-') &&
dep.depName !== 'android'
) {
deps.push(dep);
}
const parsed = parseSingleYaml(content, {
customSchema: CircleCiFile,
});
for (const [key, orb] of Object.entries(parsed.orbs ?? {})) {
const [packageName, currentValue] = orb.split('@');
deps.push({
depName: key,
packageName,
depType: 'orb',
currentValue,
versioning: npmVersioning.id,
datasource: OrbDatasource.id,
});
}
for (const job of Object.values(parsed.jobs)) {
for (const dockerElement of coerceArray(job.docker)) {
deps.push({
...getDep(dockerElement.image),
depType: 'docker',
});
}
}
for (const alias of coerceArray(parsed.aliases)) {
deps.push({
...getDep(alias.image),
depType: 'docker',
});
}
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, packageFile }, 'Error extracting circleci images');
}
......
import { z } from 'zod';
export const CircleCiDocker = z.object({
image: z.string(),
});
export const CircleCiJob = z.object({
docker: z.array(CircleCiDocker).optional(),
});
export const CircleCiFile = z.object({
aliases: z.array(CircleCiDocker).optional(),
jobs: z.record(z.string(), CircleCiJob),
orbs: z.record(z.string()).optional(),
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment