Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/gitlabci): name is not directly followed by image tag #11647

Merged
merged 9 commits into from Sep 14, 2021
11 changes: 11 additions & 0 deletions lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml
Expand Up @@ -106,3 +106,14 @@ image-name-test:
entrypoint: [""]
script:
- image-name-test Dockerfile

image-name-with-entrypoint-as-list-test:
stage: build
<<: *executor-docker
image:
entrypoint:
- ""
# test comment
name: image-name-test:1.15
script:
- image-name-test Dockerfile
9 changes: 9 additions & 0 deletions lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap
Expand Up @@ -67,6 +67,15 @@ Array [
"depType": "image-name",
"replaceString": "image-name-test:1.15",
},
Object {
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
"currentDigest": undefined,
"currentValue": "1.15",
"datasource": "docker",
"depName": "image-name-test",
"depType": "image-name",
"replaceString": "image-name-test:1.15",
},
],
"packageFile": "lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml",
},
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/gitlabci/extract.spec.ts
Expand Up @@ -64,7 +64,7 @@ describe('manager/gitlabci/extract', () => {
deps.push(d);
});
});
expect(deps).toHaveLength(7);
expect(deps).toHaveLength(8);

expect(deps.some((dep) => dep.currentValue.includes("'"))).toBe(false);
});
Expand Down
49 changes: 26 additions & 23 deletions lib/manager/gitlabci/extract.ts
Expand Up @@ -7,12 +7,17 @@ import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import type { GitlabPipeline } from './types';
import { replaceReferenceTags } from './utils';

const commentsRe = /^\s*#/;
const whitespaceRe = /^(?<whitespace>\s*)/;
const imageRe = /^(?<whitespace>\s*)image:\s*'?"?(?<image>[^\s'"]+|)'?"?\s*$/;
viceice marked this conversation as resolved.
Show resolved Hide resolved
const nameRe = /^\s*name:\s+'?"?(?<depName>[^\s'"]+|)'?"?\s*$/;
const serviceRe = /^\s*-\s*(?:name:\s+)?'?"?(?<depName>[^\s'"]+)'?"?\s*$/;

function skipCommentLines(
lines: string[],
lineNumber: number
): { lineNumber: number; line: string } {
let ln = lineNumber;
const commentsRe = /^\s*#/;
while (ln < lines.length - 1 && commentsRe.test(lines[ln])) {
ln += 1;
}
Expand All @@ -25,29 +30,31 @@ export function extractPackageFile(content: string): PackageFile | null {
const lines = content.split('\n');
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
const line = lines[lineNumber];
const imageMatch = /^\s*image:\s*'?"?([^\s'"]+|)'?"?\s*$/.exec(line);
const imageMatch = imageRe.exec(line);
if (imageMatch) {
switch (imageMatch[1]) {
switch (imageMatch.groups.image) {
case '': {
const imageNameLine = skipCommentLines(lines, lineNumber + 1);
const imageNameMatch = /^\s*name:\s*'?"?([^\s'"]+|)'?"?\s*$/.exec(
imageNameLine.line
let blockLine;
do {
lineNumber += 1;
blockLine = lines[lineNumber];
const imageNameMatch = nameRe.exec(blockLine);
if (imageNameMatch) {
logger.trace(`Matched image name on line ${lineNumber}`);
const dep = getDep(imageNameMatch.groups.depName);
dep.depType = 'image-name';
deps.push(dep);
break;
}
} while (
whitespaceRe.exec(blockLine)?.groups.whitespace.length >
imageMatch.groups.whitespace.length
);

if (imageNameMatch) {
lineNumber = imageNameLine.lineNumber;
logger.trace(`Matched image name on line ${lineNumber}`);
const currentFrom = imageNameMatch[1];
const dep = getDep(currentFrom);
dep.depType = 'image-name';
deps.push(dep);
}
break;
}
default: {
logger.trace(`Matched image on line ${lineNumber}`);
const currentFrom = imageMatch[1];
const dep = getDep(currentFrom);
const dep = getDep(imageMatch.groups.image);
dep.depType = 'image';
deps.push(dep);
}
Expand All @@ -61,16 +68,12 @@ export function extractPackageFile(content: string): PackageFile | null {
foundImage = false;
const serviceImageLine = skipCommentLines(lines, lineNumber + 1);
logger.trace(`serviceImageLine: "${serviceImageLine.line}"`);
const serviceImageMatch =
/^\s*-\s*(?:name:\s*)?'?"?([^\s'"]+)'?"?\s*$/.exec(
serviceImageLine.line
);
const serviceImageMatch = serviceRe.exec(serviceImageLine.line);
if (serviceImageMatch) {
logger.trace('serviceImageMatch');
foundImage = true;
const currentFrom = serviceImageMatch[1];
lineNumber = serviceImageLine.lineNumber;
const dep = getDep(currentFrom);
const dep = getDep(serviceImageMatch.groups.depName);
dep.depType = 'service-image';
deps.push(dep);
}
Expand Down