/
updates-table.ts
76 lines (72 loc) · 2.12 KB
/
updates-table.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { logger } from '../../../logger';
import * as template from '../../../util/template';
import { BranchConfig } from '../../common';
type TableDefinition = {
header: string;
value: string;
};
function getTableDefinition(config: BranchConfig): TableDefinition[] {
const res = [];
for (const header of config.prBodyColumns) {
const value = config.prBodyDefinitions[header];
res.push({ header, value });
}
return res;
}
function getNonEmptyColumns(
definitions: TableDefinition[],
rows: Record<string, string>[]
): string[] {
const res: string[] = [];
for (const column of definitions) {
const { header } = column;
for (const row of rows) {
if (row[header] && row[header].length) {
if (!res.includes(header)) {
res.push(header);
}
}
}
}
return res;
}
export function getPrUpdatesTable(config: BranchConfig): string {
const tableDefinitions = getTableDefinition(config);
const tableValues = config.upgrades.map((upgrade) => {
const res: Record<string, string> = {};
for (const column of tableDefinitions) {
const { header, value } = column;
try {
// istanbul ignore else
if (value) {
res[header] = template.compile(value, upgrade).replace(/^``$/, '');
} else {
res[header] = '';
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ header, value, err }, 'Handlebars compilation error');
}
}
return res;
});
const tableColumns = getNonEmptyColumns(tableDefinitions, tableValues);
let res = '\n\nThis PR contains the following updates:\n\n';
res += '| ' + tableColumns.join(' | ') + ' |\n';
res += '|' + tableColumns.map(() => '---|').join('') + '\n';
const rows = [];
for (const row of tableValues) {
let val = '|';
for (const column of tableColumns) {
const content = row[column]
? row[column].replace(/^@/, '@​').replace(/\|/g, '\\|')
: '';
val += ` ${content} |`;
}
val += '\n';
rows.push(val);
}
const uniqueRows = [...new Set(rows)];
res += uniqueRows.join('');
res += '\n\n';
return res;
}