From a53c609294bd9c2367bd3b2595db51566fa89e52 Mon Sep 17 00:00:00 2001 From: Jonas Michaelis <jonas.michaelis@outlook.com> Date: Sat, 29 Jan 2022 10:02:16 +0100 Subject: [PATCH] fix(gitlabci): multiple named services (#13867) * fix(gitlabci): multiple named services * improve regex to allow different ordering * regex allow only one dash Co-authored-by: Rhys Arkins <rhys@arkins.net> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- .../gitlabci/__fixtures__/gitlab-ci.6.yaml | 11 ++++++ .../__snapshots__/extract.spec.ts.snap | 37 +++++++++++++++++++ lib/manager/gitlabci/extract.spec.ts | 9 +++++ lib/manager/gitlabci/extract.ts | 15 ++++++-- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml diff --git a/lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml b/lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml new file mode 100644 index 0000000000..e3dc68bfe8 --- /dev/null +++ b/lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml @@ -0,0 +1,11 @@ +image: + # comment + name: renovate/renovate:19.70.8-slim + +services: + # comment + - name: other/image1:1.0.0 + alias: imagealias1 + # another comment + - alias: imagealias2 + name: other/image2:1.0.0 diff --git a/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap b/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap index ebfa7c1717..138f50d667 100644 --- a/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap +++ b/lib/manager/gitlabci/__snapshots__/extract.spec.ts.snap @@ -184,6 +184,43 @@ Array [ ] `; +exports[`manager/gitlabci/extract extractAllPackageFiles() extracts multiple named services 1`] = ` +Array [ + Object { + "deps": Array [ + Object { + "autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}", + "currentDigest": undefined, + "currentValue": "19.70.8-slim", + "datasource": "docker", + "depName": "renovate/renovate", + "depType": "image-name", + "replaceString": "renovate/renovate:19.70.8-slim", + }, + Object { + "autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}", + "currentDigest": undefined, + "currentValue": "1.0.0", + "datasource": "docker", + "depName": "other/image1", + "depType": "service-image", + "replaceString": "other/image1:1.0.0", + }, + Object { + "autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}", + "currentDigest": undefined, + "currentValue": "1.0.0", + "datasource": "docker", + "depName": "other/image2", + "depType": "service-image", + "replaceString": "other/image2:1.0.0", + }, + ], + "packageFile": "lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml", + }, +] +`; + exports[`manager/gitlabci/extract extractAllPackageFiles() extracts named services 1`] = ` Array [ Object { diff --git a/lib/manager/gitlabci/extract.spec.ts b/lib/manager/gitlabci/extract.spec.ts index ca18521d5c..89f161453e 100644 --- a/lib/manager/gitlabci/extract.spec.ts +++ b/lib/manager/gitlabci/extract.spec.ts @@ -51,6 +51,15 @@ describe('manager/gitlabci/extract', () => { expect(res[0].deps).toHaveLength(3); }); + it('extracts multiple named services', async () => { + const res = await extractAllPackageFiles(config, [ + 'lib/manager/gitlabci/__fixtures__/gitlab-ci.6.yaml', + ]); + expect(res).toMatchSnapshot(); + expect(res).toHaveLength(1); + expect(res[0].deps).toHaveLength(3); + }); + it('extracts multiple image lines', async () => { const res = await extractAllPackageFiles(config, [ 'lib/manager/gitlabci/__fixtures__/gitlab-ci.yaml', diff --git a/lib/manager/gitlabci/extract.ts b/lib/manager/gitlabci/extract.ts index 7fe6b85617..af6b4e2026 100644 --- a/lib/manager/gitlabci/extract.ts +++ b/lib/manager/gitlabci/extract.ts @@ -9,20 +9,24 @@ import type { GitlabPipeline } from './types'; import { replaceReferenceTags } from './utils'; const commentsRe = regEx(/^\s*#/); +const aliasesRe = regEx(`^\\s*-?\\s*alias:`); const whitespaceRe = regEx(`^(?<whitespace>\\s*)`); const imageRe = regEx( `^(?<whitespace>\\s*)image:(?:\\s+['"]?(?<image>[^\\s'"]+)['"]?)?\\s*$` ); const nameRe = regEx(`^\\s*name:\\s+['"]?(?<depName>[^\\s'"]+)['"]?\\s*$`); const serviceRe = regEx( - `^\\s*-\\s*(?:name:\\s+)?['"]?(?<depName>[^\\s'"]+)['"]?\\s*$` + `^\\s*-?\\s*(?:name:\\s+)?['"]?(?<depName>[^\\s'"]+)['"]?\\s*$` ); -function skipCommentLines( +function skipCommentAndAliasLines( lines: string[], lineNumber: number ): { lineNumber: number; line: string } { let ln = lineNumber; - while (ln < lines.length - 1 && commentsRe.test(lines[ln])) { + while ( + ln < lines.length - 1 && + (commentsRe.test(lines[ln]) || aliasesRe.test(lines[ln])) + ) { ln += 1; } return { line: lines[ln], lineNumber: ln }; @@ -71,7 +75,10 @@ export function extractPackageFile(content: string): PackageFile | null { let foundImage: boolean; do { foundImage = false; - const serviceImageLine = skipCommentLines(lines, lineNumber + 1); + const serviceImageLine = skipCommentAndAliasLines( + lines, + lineNumber + 1 + ); logger.trace(`serviceImageLine: "${serviceImageLine.line}"`); const serviceImageMatch = serviceRe.exec(serviceImageLine.line); if (serviceImageMatch) { -- GitLab