From a5b26f6a972c91010ce0a27a1289c91a05e43888 Mon Sep 17 00:00:00 2001 From: George Georgiev Date: Mon, 23 May 2022 22:29:30 -0700 Subject: [PATCH] feat(droneci): Preserve multiline image with digest format (#15673) --- .../__snapshots__/extract.spec.ts.snap | 4 +- lib/modules/manager/droneci/extract.ts | 53 +++++++++++-------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/lib/modules/manager/droneci/__snapshots__/extract.spec.ts.snap b/lib/modules/manager/droneci/__snapshots__/extract.spec.ts.snap index 8e549914e3..8a53439885 100644 --- a/lib/modules/manager/droneci/__snapshots__/extract.spec.ts.snap +++ b/lib/modules/manager/droneci/__snapshots__/extract.spec.ts.snap @@ -40,7 +40,7 @@ Array [ "replaceString": "redis:alpine", }, Object { - "autoReplaceStringTemplate": "{{packageName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}", + "autoReplaceStringTemplate": "\\"{{packageName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}\\\\\n @{{newDigest}}{{/if}}\\"", "currentDigest": "sha256:36adc17e9cceab32179d3314da9cb9c737ffb11f0de4e688f407ad6d9ca32201", "currentValue": "10.0.0", "datasource": "docker", @@ -51,7 +51,7 @@ Array [ @sha256:36adc17e9cceab32179d3314da9cb9c737ffb11f0de4e688f407ad6d9ca32201\\\"", }, Object { - "autoReplaceStringTemplate": "{{packageName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}", + "autoReplaceStringTemplate": "\\"{{packageName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}\\\\\n @{{newDigest}}{{/if}}\\"", "currentDigest": "sha256:36adc17e9cceab32179d3314da9cb9c737ffb11f0de4e688f407ad6d9ca32201", "currentValue": "10.0.0", "datasource": "docker", diff --git a/lib/modules/manager/droneci/extract.ts b/lib/modules/manager/droneci/extract.ts index ff4bbfa905..c0fdcfa1ab 100644 --- a/lib/modules/manager/droneci/extract.ts +++ b/lib/modules/manager/droneci/extract.ts @@ -10,40 +10,51 @@ export function extractPackageFile(content: string): PackageFile | null { for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) { const line = lines[lineNumber]; - const first_line_match = regEx(/^\s* image:\s*(['"]([^\s'"]+)\\)$/).exec( - line - ); - if (first_line_match) { - let currentFrom = first_line_match[2]; - let replaceString = first_line_match[1]; + const firstLineMatch = regEx( + /^(?\s* image:\s*)(?['"](?[^\s'"]+)\\)$/ + ).exec(line); + + if (firstLineMatch?.groups) { + let currentFrom = firstLineMatch.groups.currentFrom; + let replaceString = firstLineMatch.groups.replaceString; for (let i = lineNumber + 1; i < lines.length; i += 1) { - const internal_line = lines[i]; - const middle_line_match = - regEx(/^(\s*([^\s'"]+)\\)$/).exec(internal_line); - if (middle_line_match) { - currentFrom += middle_line_match[2]; - replaceString += '\n' + middle_line_match[1]; + const internalLine = lines[i]; + const middleLineMatch = regEx( + /^(?\s*(?[^\s'"]+)\\)$/ + ).exec(internalLine); + if (middleLineMatch?.groups) { + currentFrom += middleLineMatch.groups.currentFrom; + replaceString += '\n' + middleLineMatch.groups.replaceString; } else { - const final_line_match = regEx(/^(\s*([^\s'"]+)['"])$/).exec( - internal_line - ); - if (final_line_match) { - currentFrom += final_line_match[2]; - replaceString += '\n' + final_line_match[1]; + const finalLineMatch = regEx( + /^(?\s*(?[^\s'"]+)['"])$/ + ).exec(internalLine); + if (finalLineMatch?.groups) { + currentFrom += finalLineMatch.groups.currentFrom; + replaceString += '\n' + finalLineMatch.groups.replaceString; const dep = getDep(currentFrom); dep.depType = 'docker'; dep.replaceString = replaceString; + if (dep.autoReplaceStringTemplate) { + const d = '@{{newDigest}}'; + const c = firstLineMatch.groups.leading.length + 1; + const nd = `\\\n${' '.repeat(c)}${d}`; + const replaced = dep.autoReplaceStringTemplate.replace(d, nd); + dep.autoReplaceStringTemplate = `"${replaced}"`; + } deps.push(dep); } break; } } } else { - const match = regEx(/^\s* image:\s*'?"?([^\s'"]+)'?"?\s*$/).exec(line); - if (match) { - const dep = getDep(match[1]); + const match = regEx( + /^\s* image:\s*'?"?(?[^\s'"]+)'?"?\s*$/ + ).exec(line); + if (match?.groups) { + const dep = getDep(match.groups.currentFrom); dep.depType = 'docker'; deps.push(dep); }