Skip to content

Commit

Permalink
refactor: parentBranch -> reuseExistingBranch (#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 15, 2020
1 parent 0d1e273 commit 26fd21c
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 111 deletions.
2 changes: 1 addition & 1 deletion lib/manager/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,5 @@ export interface PostUpdateConfig extends ManagerConfig, Record<string, any> {
npmLock?: string;
yarnLock?: string;
branchName?: string;
parentBranch?: string;
reuseExistingBranch?: boolean;
}
10 changes: 5 additions & 5 deletions lib/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export async function getAdditionalFiles(
logger.debug('Getting updated lock files');
if (
config.updateType === 'lockFileMaintenance' &&
config.parentBranch &&
config.reuseExistingBranch &&
(await platform.branchExists(config.branchName))
) {
logger.debug('Skipping lockFileMaintenance update');
Expand Down Expand Up @@ -466,7 +466,7 @@ export async function getAdditionalFiles(
} else {
const existingContent = await platform.getFile(
lockFile,
config.parentBranch
config.reuseExistingBranch ? config.branchName : config.baseBranch
);
if (res.lockFile !== existingContent) {
logger.debug(`${lockFile} needs updating`);
Expand Down Expand Up @@ -531,7 +531,7 @@ export async function getAdditionalFiles(
} else {
const existingContent = await platform.getFile(
lockFileName,
config.parentBranch
config.reuseExistingBranch ? config.branchName : config.baseBranch
);
if (res.lockFile !== existingContent) {
logger.debug('yarn.lock needs updating');
Expand Down Expand Up @@ -628,7 +628,7 @@ export async function getAdditionalFiles(
} else {
const existingContent = await platform.getFile(
lockFile,
config.parentBranch
config.reuseExistingBranch ? config.branchName : config.baseBranch
);
if (res.lockFile !== existingContent) {
logger.debug('pnpm-lock.yaml needs updating');
Expand Down Expand Up @@ -724,7 +724,7 @@ export async function getAdditionalFiles(
logger.trace('Checking for ' + filename);
const existingContent = await platform.getFile(
filename,
config.parentBranch
config.reuseExistingBranch ? config.branchName : config.baseBranch
);
if (existingContent) {
logger.trace('Found lock file');
Expand Down
20 changes: 10 additions & 10 deletions lib/workers/branch/__snapshots__/get-updated.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles autoreplace base updated 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
Expand All @@ -17,7 +17,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles autoreplace branch needs update 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": false,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
Expand All @@ -31,7 +31,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles autoreplace branch no update 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [],
}
Expand All @@ -40,7 +40,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles content change 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": false,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
Expand All @@ -54,7 +54,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles empty 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [],
}
Expand All @@ -63,7 +63,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles git submodules 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
Expand All @@ -82,7 +82,7 @@ Object {
"stderr": "some error",
},
],
"parentBranch": "some-branch",
"reuseExistingBranch": true,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
Expand All @@ -96,7 +96,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles lock files 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": "some-branch",
"reuseExistingBranch": true,
"updatedArtifacts": Array [
Object {
"contents": "some contents",
Expand All @@ -115,7 +115,7 @@ Object {
exports[`workers/branch/get-updated getUpdatedPackageFiles() handles lockFileMaintenance 1`] = `
Object {
"artifactErrors": Array [],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [
Object {
"contents": "some contents",
Expand All @@ -134,7 +134,7 @@ Object {
"stderr": "some error",
},
],
"parentBranch": undefined,
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [],
}
Expand Down
34 changes: 21 additions & 13 deletions lib/workers/branch/auto-replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ jest.mock('../../util/fs');

describe('workers/branch/auto-replace', () => {
describe('doAutoReplace', () => {
let parentBranch: string;
let reuseExistingBranch: boolean;
let upgrade: BranchUpgradeConfig;
beforeEach(() => {
upgrade = {
...JSON.parse(JSON.stringify(defaultConfig)),
manager: 'html',
};
parentBranch = undefined;
reuseExistingBranch = false;
});
it('rebases if the deps list has changed', async () => {
upgrade.baseDeps = extractPackageFile(sampleHtml).deps;
parentBranch = 'some existing branch';
reuseExistingBranch = true;
const res = await doAutoReplace(
upgrade,
'existing content',
parentBranch
reuseExistingBranch
);
expect(res).toBeNull();
});
it('rebases if the deps to update has changed', async () => {
upgrade.baseDeps = extractPackageFile(sampleHtml).deps;
upgrade.baseDeps[0].currentValue = '1.0.0';
parentBranch = 'some existing branch';
const res = await doAutoReplace(upgrade, sampleHtml, parentBranch);
reuseExistingBranch = true;
const res = await doAutoReplace(upgrade, sampleHtml, reuseExistingBranch);
expect(res).toBeNull();
});
it('updates version only', async () => {
Expand All @@ -51,7 +51,7 @@ describe('workers/branch/auto-replace', () => {
upgrade.newValue = '7.1.1';
upgrade.newDigest = 'some-digest';
upgrade.depIndex = 0;
const res = await doAutoReplace(upgrade, src, parentBranch);
const res = await doAutoReplace(upgrade, src, reuseExistingBranch);
expect(res).toMatchSnapshot();
});
it('handles a double attempt', async () => {
Expand All @@ -64,7 +64,7 @@ describe('workers/branch/auto-replace', () => {
upgrade.currentValue = '7.1.0';
upgrade.newValue = '7.1.1';
upgrade.depIndex = 1;
const res = await doAutoReplace(upgrade, src, parentBranch);
const res = await doAutoReplace(upgrade, src, reuseExistingBranch);
expect(res).toMatchSnapshot();
});
it('handles already updated', async () => {
Expand All @@ -78,9 +78,13 @@ describe('workers/branch/auto-replace', () => {
upgrade.newValue = '7.1.1';
upgrade.depIndex = 0;
upgrade.replaceString = script;
parentBranch = 'something';
reuseExistingBranch = true;
const srcAlreadyUpdated = src.replace('7.1.0', '7.1.1');
const res = await doAutoReplace(upgrade, srcAlreadyUpdated, parentBranch);
const res = await doAutoReplace(
upgrade,
srcAlreadyUpdated,
reuseExistingBranch
);
expect(res).toMatchSnapshot();
});
it('returns existing content if replaceString mismatch', async () => {
Expand All @@ -94,7 +98,11 @@ describe('workers/branch/auto-replace', () => {
upgrade.newValue = '7.1.1';
upgrade.depIndex = 0;
upgrade.replaceString = script;
const res = await doAutoReplace(upgrade, 'wrong source', parentBranch);
const res = await doAutoReplace(
upgrade,
'wrong source',
reuseExistingBranch
);
expect(res).toEqual('wrong source');
});
it('updates version and integrity', async () => {
Expand All @@ -111,7 +119,7 @@ describe('workers/branch/auto-replace', () => {
upgrade.newDigest = 'sha256-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
upgrade.depIndex = 0;
upgrade.replaceString = script;
const res = await doAutoReplace(upgrade, src, parentBranch);
const res = await doAutoReplace(upgrade, src, reuseExistingBranch);
expect(res).toMatchSnapshot();
});
it('updates with autoReplaceNewString', async () => {
Expand All @@ -130,7 +138,7 @@ describe('workers/branch/auto-replace', () => {
'node:8.11.3-alpine@sha256:d743b4141b02fcfb8beb68f92b4cd164f60ee457bf2d053f36785bf86de16b0d';
upgrade.autoReplaceStringTemplate =
'{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}';
const res = await doAutoReplace(upgrade, dockerfile, parentBranch);
const res = await doAutoReplace(upgrade, dockerfile, reuseExistingBranch);
expect(res).toMatchSnapshot();
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/branch/auto-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function checkBranchDepsMatchBaseDeps(
export async function doAutoReplace(
upgrade: BranchUpgradeConfig,
existingContent: string,
parentBranch: string | null
reuseExistingBranch: boolean
): Promise<string | null> {
const {
packageFile,
Expand All @@ -114,7 +114,7 @@ export async function doAutoReplace(
newDigest,
autoReplaceStringTemplate,
} = upgrade;
if (parentBranch) {
if (reuseExistingBranch) {
if (!(await checkBranchDepsMatchBaseDeps(upgrade, existingContent))) {
logger.debug(
{ packageFile, depName },
Expand Down
12 changes: 5 additions & 7 deletions lib/workers/branch/get-updated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('workers/branch/get-updated', () => {
await expect(getUpdatedPackageFiles(config)).rejects.toThrow();
});
it('handles autoreplace branch needs update', async () => {
config.parentBranch = 'some branch';
config.reuseExistingBranch = true;
config.upgrades.push({ manager: 'html', branchName: undefined });
autoReplace.doAutoReplace.mockResolvedValueOnce(null);
autoReplace.doAutoReplace.mockResolvedValueOnce('updated-file');
Expand All @@ -58,14 +58,14 @@ describe('workers/branch/get-updated', () => {
expect(res).toMatchSnapshot();
});
it('handles null content', async () => {
config.parentBranch = 'some-branch';
config.reuseExistingBranch = true;
config.upgrades.push({
manager: 'npm',
} as never);
await expect(getUpdatedPackageFiles(config)).rejects.toThrow();
});
it('handles content change', async () => {
config.parentBranch = 'some-branch';
config.reuseExistingBranch = true;
config.upgrades.push({
manager: 'npm',
} as never);
Expand All @@ -74,7 +74,7 @@ describe('workers/branch/get-updated', () => {
expect(res).toMatchSnapshot();
});
it('handles lock files', async () => {
config.parentBranch = 'some-branch';
config.reuseExistingBranch = true;
config.upgrades.push({
manager: 'composer',
branchName: undefined,
Expand All @@ -92,7 +92,6 @@ describe('workers/branch/get-updated', () => {
expect(res).toMatchSnapshot();
});
it('handles lockFileMaintenance', async () => {
// config.parentBranch = 'some-branch';
config.upgrades.push({
manager: 'composer',
updateType: 'lockFileMaintenance',
Expand All @@ -109,7 +108,6 @@ describe('workers/branch/get-updated', () => {
expect(res).toMatchSnapshot();
});
it('handles lockFileMaintenance error', async () => {
// config.parentBranch = 'some-branch';
config.upgrades.push({
manager: 'composer',
updateType: 'lockFileMaintenance',
Expand All @@ -126,7 +124,7 @@ describe('workers/branch/get-updated', () => {
expect(res).toMatchSnapshot();
});
it('handles lock file errors', async () => {
config.parentBranch = 'some-branch';
config.reuseExistingBranch = true;
config.upgrades.push({
manager: 'composer',
branchName: undefined,
Expand Down

0 comments on commit 26fd21c

Please sign in to comment.