Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/workers/repository/update/branch/auto-replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,76 @@ describe('workers/repository/update/branch/auto-replace', () => {
);
});

it('updates with helm value image/repository wrong version', async () => {
const yml = codeBlock`
parser:
test: 3.14.3
image:
repository: docker.io/securecodebox/parser-nmap
tag: 2.14.3
`;
upgrade.manager = 'helm-values';
upgrade.depName = 'docker.io/securecodebox/parser-nmap';
upgrade.replaceString = '3.14.3';
upgrade.currentValue = '3.14.3';
upgrade.depIndex = 0;
upgrade.updateType = 'replacement';
upgrade.newName = 'iteratec/juice-balancer';
upgrade.newValue = 'v5.1.0';
upgrade.packageFile = 'values.yml';
await expect(
doAutoReplace(upgrade, yml, reuseExistingBranch),
).rejects.toThrowError(WORKER_FILE_UPDATE_FAILED);
});

it('updates with helm value image/repository prefix replacement', async () => {
const yml = codeBlock`
parser:
image:
repository: example.org/securecodebox/parser-nmap
tag: 3.14.3
`;
upgrade.manager = 'helm-values';
upgrade.depName = 'example.org/securecodebox/parser-nmap';
upgrade.replaceString = '3.14.3';
upgrade.currentValue = '3.14.3';
upgrade.depIndex = 0;
upgrade.updateType = 'replacement';
upgrade.newName = 'docker.example.org/securecodebox/parser-nmap';
upgrade.newValue = 'v5.1.0';
upgrade.packageFile = 'values.yml';
const res = await doAutoReplace(upgrade, yml, reuseExistingBranch);
expect(res).toBe(
yml
.replace(upgrade.depName, upgrade.newName)
.replace(upgrade.currentValue, upgrade.newValue),
);
});

it('updates with helm value image/repository version prefix replacement', async () => {
const yml = codeBlock`
parser:
image:
tag: 3.14.3
repository: docker.io/securecodebox/parser-nmap
`;
upgrade.manager = 'helm-values';
upgrade.depName = 'docker.io/securecodebox/parser-nmap';
upgrade.replaceString = '3.14.3';
upgrade.currentValue = '3.14.3';
upgrade.depIndex = 0;
upgrade.updateType = 'replacement';
upgrade.newName = 'iteratec/juice-balancer';
upgrade.newValue = 'v3.14.3';
upgrade.packageFile = 'values.yml';
const res = await doAutoReplace(upgrade, yml, reuseExistingBranch);
expect(res).toBe(
yml
.replace(upgrade.depName, upgrade.newName)
.replace(upgrade.currentValue, upgrade.newValue),
);
});

it('updates with jenkins plugin replacement', async () => {
const txt = 'script-security:1175\n';
upgrade.manager = 'jenkins';
Expand Down
51 changes: 46 additions & 5 deletions lib/workers/repository/update/branch/auto-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ function getDepsSignature(deps: PackageDependency[]): string {
.join(',');
}

function firstIndexOf(
existingContent: string,
depName: string,
currentValue: string,
position = 0,
): number {
const depIndex = existingContent.indexOf(depName, position);
const valIndex = existingContent.indexOf(currentValue, position);
const index = depIndex < valIndex ? depIndex : valIndex;
if (index < 0) {
return position === 0 ? -1 : existingContent.length;
}
return index;
}

export async function checkBranchDepsMatchBaseDeps(
upgrade: BranchUpgradeConfig,
branchContent: string,
Expand Down Expand Up @@ -218,9 +233,7 @@ export async function doAutoReplace(
logger.trace({ depName, replaceString }, 'autoReplace replaceString');
let searchIndex: number;
if (replaceWithoutReplaceString) {
const depIndex = existingContent.indexOf(depName!);
const valIndex = existingContent.indexOf(currentValue!);
searchIndex = depIndex < valIndex ? depIndex : valIndex;
searchIndex = firstIndexOf(existingContent, depName!, currentValue!);
} else {
searchIndex = existingContent.indexOf(replaceString!);
}
Expand Down Expand Up @@ -317,8 +330,13 @@ export async function doAutoReplace(
`Found depName at index ${searchIndex}`,
);
if (nameReplaced) {
startIndex += 1;
searchIndex = startIndex;
startIndex = firstIndexOf(
existingContent,
depName!,
currentValue!,
startIndex + 1,
);
searchIndex = startIndex - 1;
await writeLocalFile(upgrade.packageFile!, existingContent);
newContent = existingContent;
nameReplaced = false;
Expand All @@ -329,6 +347,7 @@ export async function doAutoReplace(
newContent = replaceAt(newContent, searchIndex, depName!, newName);
await writeLocalFile(upgrade.packageFile!, newContent);
nameReplaced = true;
searchIndex += newName.length - 1;
} else if (
newValue &&
matchAt(newContent, searchIndex, currentValue!)
Expand All @@ -337,6 +356,20 @@ export async function doAutoReplace(
{ packageFile, currentValue },
`Found currentValue at index ${searchIndex}`,
);
if (valueReplaced) {
startIndex = firstIndexOf(
existingContent,
depName!,
currentValue!,
startIndex + 1,
);
searchIndex = startIndex - 1;
await writeLocalFile(upgrade.packageFile!, existingContent);
newContent = existingContent;
nameReplaced = false;
valueReplaced = false;
continue;
}
// Now test if the result matches
newContent = replaceAt(
newContent,
Expand All @@ -346,11 +379,19 @@ export async function doAutoReplace(
);
await writeLocalFile(upgrade.packageFile!, newContent);
valueReplaced = true;
searchIndex += newValue.length - 1;
}
if (nameReplaced && valueReplaced) {
if (await confirmIfDepUpdated(upgrade, newContent)) {
return newContent;
}
startIndex = firstIndexOf(
existingContent,
depName!,
currentValue!,
startIndex + 1,
);
searchIndex = startIndex - 1;
await writeLocalFile(upgrade.packageFile!, existingContent);
newContent = existingContent;
nameReplaced = false;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"typescript": "5.8.2",
"typescript-eslint": "8.28.0",
"unified": "9.2.2",
"vite": "6.2.5",
"vite": "6.2.6",
"vite-tsconfig-paths": "5.1.4",
"vitest": "3.1.1",
"vitest-mock-extended": "3.0.1"
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading