Skip to content

Commit

Permalink
fix(workers/pr): improve deduplication in updates-table (#26771)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
RahulGautamSingh and viceice authored Jan 30, 2024
1 parent ed64def commit 88000a4
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 7 deletions.
129 changes: 128 additions & 1 deletion lib/workers/repository/update/pr/body/updates-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,42 @@ describe('workers/repository/update/pr/body/updates-table', () => {
displayFrom: '^6.2.3',
displayTo: '6.2.3',
});

// TODO #22198 allow or filter undefined
const upgrade3 = undefined as never;

// duplicate of upgrade2
const upgrade4 = partial<BranchUpgradeConfig>({
manager: 'some-manager',
branchName: 'some-branch',
prBodyDefinitions: {
Package: '{{{depNameLinked}}}',
Type: '{{{depType}}}',
Update: '{{{updateType}}}',
'Current value': '{{{currentValue}}}',
'New value': '{{{newValue}}}',
Change: '`{{{displayFrom}}}` -> `{{{displayTo}}}`',
Pending: '{{{displayPending}}}',
References: '{{{references}}}',
'Package file': '{{{packageFile}}}',
},
updateType: 'pin',
depNameLinked:
'[mocha](https://mochajs.org/) ([source](https://github.com/mochajs/mocha))',
depType: 'devDependencies',
depName: 'mocha',
currentValue: '^6.2.3',
newValue: '6.2.3',
currentVersion: '6.2.3',
newVersion: '6.2.3',
displayFrom: '^6.2.3',
displayTo: '6.2.3',
});
const configObj: BranchConfig = {
manager: 'some-manager',
branchName: 'some-branch',
baseBranch: 'base',
upgrades: [upgrade0, upgrade1, upgrade2, upgrade3],
upgrades: [upgrade0, upgrade1, upgrade2, upgrade3, upgrade4],
prBodyColumns: ['Package', 'Type', 'Update', 'Change', 'Pending'],
prBodyDefinitions: {
Package: '{{{depNameLinked}}}',
Expand Down Expand Up @@ -122,4 +151,102 @@ describe('workers/repository/update/pr/body/updates-table', () => {
'\n',
);
});

it('selects the best upgrade incase of duplicate table rows', () => {
const upgrade1 = partial<BranchUpgradeConfig>({
manager: 'some-manager',
branchName: 'some-branch',
prBodyDefinitions: {
Package: '{{{depNameLinked}}}',
Type: '{{{depType}}}',
Update: '{{{updateType}}}',
'Current value': '{{{currentValue}}}',
'New value': '{{{newValue}}}',
Change: '`{{{displayFrom}}}` -> `{{{displayTo}}}`',
},
updateType: 'pin',
depNameLinked:
'[mocha](https://mochajs.org/) ([source](https://github.com/mochajs/mocha))',
depType: 'devDependencies',
depName: 'mocha',
currentValue: '^6.2.3',
newValue: '6.2.3',
displayFrom: '^6.2.3',
displayTo: '6.2.3',
});

// duplicate of upgrade1
const upgrade2 = partial<BranchUpgradeConfig>({
manager: 'some-manager',
branchName: 'some-branch',
prBodyDefinitions: {
Package: '{{{depNameLinked}}}',
Type: '{{{depType}}}',
Update: '{{{updateType}}}',
'Current value': '{{{currentValue}}}',
'New value': '{{{newValue}}}',
Change:
"[{{#if displayFrom}}`{{{displayFrom}}}` -> {{else}}{{#if currentValue}}`{{{currentValue}}}` -> {{/if}}{{/if}}{{#if displayTo}}`{{{displayTo}}}`{{else}}`{{{newValue}}}`{{/if}}]({{#if depName}}https://renovatebot.com/diffs/npm/{{replace '/' '%2f' depName}}/{{{currentVersion}}}/{{{newVersion}}}{{/if}})",
},
updateType: 'pin',
depNameLinked:
'[mocha](https://mochajs.org/) ([source](https://github.com/mochajs/mocha))',
depType: 'devDependencies',
depName: 'mocha',
currentValue: '^6.2.3',
newValue: '6.2.3',
currentVersion: '6.2.3',
newVersion: '6.2.3',
displayFrom: '^6.2.3',
displayTo: '6.2.3',
});

// duplicate of upgrade1
const upgrade3 = partial<BranchUpgradeConfig>({
manager: 'some-manager',
branchName: 'some-branch',
updateType: 'pin',
prBodyDefinitions: {
Pending: '{{{displayPending}}}',
},
depNameLinked:
'[mocha](https://mochajs.org/) ([source](https://github.com/mochajs/mocha))',
depType: 'devDependencies',
depName: 'mocha',
currentValue: '^6.2.3',
newValue: '6.2.3',
displayFrom: '^6.2.3',
displayTo: '6.2.3',
displayPending: 'some-string',
});

const configObj: BranchConfig = {
manager: 'some-manager',
branchName: 'some-branch',
baseBranch: 'base',
upgrades: [upgrade1, upgrade2, upgrade3],
prBodyColumns: ['Package', 'Type', 'Update', 'Change', 'Pending'],
prBodyDefinitions: {
Package: '{{{depNameLinked}}}',
Type: '{{{depType}}}',
Update: '{{{updateType}}}',
'Current value': '{{{currentValue}}}',
'New value': '{{{newValue}}}',
Change: 'All locks refreshed',
Pending: '{{{displayPending}}}',
},
};
const result = getPrUpdatesTable(configObj);
expect(result).toMatch(
'\n' +
'\n' +
'This PR contains the following updates:\n' +
'\n' +
'| Package | Type | Update | Change |\n' +
'|---|---|---|---|\n' +
'| [mocha](https://mochajs.org/) ([source](https://github.com/mochajs/mocha)) | devDependencies | pin | [`^6.2.3` -> `6.2.3`](https://renovatebot.com/diffs/npm/mocha/6.2.3/6.2.3) |\n' +
'\n' +
'\n',
);
});
});
62 changes: 56 additions & 6 deletions lib/workers/repository/update/pr/body/updates-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ export function getPrUpdatesTable(config: BranchConfig): string {
logger.warn('getPrUpdatesTable - prBodyColumns is undefined');
return '';
}
const tableValues = config.upgrades
.filter((upgrade) => upgrade !== undefined)
.map((upgrade) => {

const tableKeyValuePairs: Record<string, Record<string, string>> = {};
for (const upgrade of config.upgrades) {
if (upgrade) {
// Create a key based on the properties which are significant in the updates table
const key = `${upgrade.depName ?? ''}_${upgrade.depType ?? ''}_${
upgrade.newValue ?? upgrade.newVersion ?? ''
}_${upgrade.currentValue ?? upgrade.currentVersion ?? ''}_${
upgrade.updateType
}`;

const res: Record<string, string> = {};
const rowDefinition = getRowDefinition(config.prBodyColumns!, upgrade);
const rowDefinition = getRowDefinition(config.prBodyColumns, upgrade);
for (const column of rowDefinition) {
const { header, value } = column;
try {
Expand All @@ -64,8 +72,22 @@ export function getPrUpdatesTable(config: BranchConfig): string {
logger.warn({ header, value, err }, 'Handlebars compilation error');
}
}
return res;
});

if (tableKeyValuePairs[key]) {
// compare the duplicate upgrades as per their table values
// and select one with better values
tableKeyValuePairs[key] = compareTableValues(
tableKeyValuePairs[key],
res,
config.prBodyColumns,
);
} else {
tableKeyValuePairs[key] = res;
}
}
}

const tableValues = Object.values(tableKeyValuePairs);
const tableColumns = getNonEmptyColumns(config.prBodyColumns, tableValues);
let res = '\n\nThis PR contains the following updates:\n\n';
res += '| ' + tableColumns.join(' | ') + ' |\n';
Expand All @@ -89,3 +111,31 @@ export function getPrUpdatesTable(config: BranchConfig): string {
res += '\n\n';
return res;
}

// return the row with better table values
function compareTableValues(
a: Record<string, string>,
b: Record<string, string>,
prBodyColumns: string[],
): Record<string, string> {
let score = 0;

for (const header of prBodyColumns) {
if (!b[header] && !a[header]) {
continue;
}
if (!b[header]) {
score--;
continue;
}
if (!a[header]) {
score++;
continue;
}

if (a[header] !== b[header]) {
a[header].length < b[header].length ? score++ : score--;
}
}
return score > 0 ? b : a;
}

0 comments on commit 88000a4

Please sign in to comment.