From 03ac68e912f3646766f24cb5593ad318c5335b92 Mon Sep 17 00:00:00 2001 From: Sebastian Poxhofer Date: Tue, 14 Sep 2021 16:18:36 +0200 Subject: [PATCH] fix(manager/gitlabci): name is not directly followed by image tag (#11647) Co-authored-by: Michael Kriese --- .../gitlabci/__fixtures__/gitlab-ci.yaml | 11 ++++ .../__snapshots__/extract.spec.ts.snap | 9 ++++ lib/manager/gitlabci/extract.spec.ts | 2 +- lib/manager/gitlabci/extract.ts | 51 ++++++++++--------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml b/lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml index e385b37942b5bf..83fe9889ed8b34 100644 --- a/lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml +++ b/lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml @@ -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 diff --git a/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap b/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap index bb3fedc1113f03..ebfa7c1717853f 100644 --- a/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap +++ b/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap @@ -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", }, diff --git a/lib/manager/gitlabci/extract.spec.ts b/lib/manager/gitlabci/extract.spec.ts index ed316fab868992..cc911594aeee12 100644 --- a/lib/manager/gitlabci/extract.spec.ts +++ b/lib/manager/gitlabci/extract.spec.ts @@ -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); }); diff --git a/lib/manager/gitlabci/extract.ts b/lib/manager/gitlabci/extract.ts index f971fd304317f3..3ec54aff72046f 100644 --- a/lib/manager/gitlabci/extract.ts +++ b/lib/manager/gitlabci/extract.ts @@ -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 = /^(?\s*)/; +const imageRe = + /^(?\s*)image:(?:\s+['"]?(?[^\s'"]+)['"]?)?\s*$/; +const nameRe = /^\s*name:\s+['"]?(?[^\s'"]+)['"]?\s*$/; +const serviceRe = /^\s*-\s*(?:name:\s+)?['"]?(?[^\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; } @@ -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); } @@ -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); }