feat(droneci): Preserve multiline image with digest format (#15673)

This commit is contained in:
George Georgiev 2022-05-23 22:29:30 -07:00 committed by GitHub
parent 2de0acd6d3
commit a5b26f6a97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 23 deletions

View file

@ -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",

View file

@ -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(
/^(?<leading>\s* image:\s*)(?<replaceString>['"](?<currentFrom>[^\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(
/^(?<replaceString>\s*(?<currentFrom>[^\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(
/^(?<replaceString>\s*(?<currentFrom>[^\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*'?"?(?<currentFrom>[^\s'"]+)'?"?\s*$/
).exec(line);
if (match?.groups) {
const dep = getDep(match.groups.currentFrom);
dep.depType = 'docker';
deps.push(dep);
}