Skip to content

Commit

Permalink
fix(manager/gitlabci): name is not directly followed by image tag (#1…
Browse files Browse the repository at this point in the history
…1647)


Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
secustor and viceice committed Sep 14, 2021
1 parent e6a26a7 commit 03ac68e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
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
51 changes: 28 additions & 23 deletions lib/manager/gitlabci/extract.ts
Expand Up @@ -7,12 +7,18 @@ 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*$/;
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 +31,32 @@ 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 undefined:
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 +70,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

0 comments on commit 03ac68e

Please sign in to comment.