/
index.ts
86 lines (84 loc) · 2.96 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
import { platform } from '../../../platform';
import * as template from '../../../util/template';
import { get } from '../../../versioning';
import { BranchConfig } from '../../common';
import { getChangelogs } from './changelogs';
import { getPrConfigDescription } from './config-description';
import { getControls } from './controls';
import { getPrFooter } from './footer';
import { getPrHeader } from './header';
import { getPrExtraNotes, getPrNotes } from './notes';
import { getPrUpdatesTable } from './updates-table';
function massageUpdateMetadata(config: BranchConfig): void {
config.upgrades.forEach((upgrade) => {
/* eslint-disable no-param-reassign */
const { homepage, sourceUrl, sourceDirectory, changelogUrl } = upgrade;
let depNameLinked = upgrade.depName;
const primaryLink = homepage || sourceUrl;
if (primaryLink) {
depNameLinked = `[${depNameLinked}](${primaryLink})`;
}
const otherLinks = [];
if (homepage && sourceUrl) {
otherLinks.push(`[source](${sourceUrl})`);
}
if (changelogUrl) {
otherLinks.push(`[changelog](${changelogUrl})`);
}
if (otherLinks.length) {
depNameLinked += ` (${otherLinks.join(', ')})`;
}
upgrade.depNameLinked = depNameLinked;
const references: string[] = [];
if (homepage) {
references.push(`[homepage](${homepage})`);
}
if (sourceUrl) {
let fullUrl = sourceUrl;
if (sourceDirectory) {
fullUrl =
sourceUrl.replace(/\/?$/, '/') +
'tree/HEAD/' +
sourceDirectory.replace('^/?/', '');
}
references.push(`[source](${fullUrl})`);
}
if (changelogUrl) {
references.push(`[changelog](${changelogUrl})`);
}
upgrade.references = references.join(', ');
const { fromVersion, toVersion, updateType, versioning } = upgrade;
// istanbul ignore if
if (updateType === 'minor') {
try {
const version = get(versioning);
if (version.getMinor(fromVersion) === version.getMinor(toVersion)) {
upgrade.updateType = 'patch';
}
} catch (err) {
// do nothing
}
}
/* eslint-enable no-param-reassign */
});
}
export async function getPrBody(config: BranchConfig): Promise<string> {
massageUpdateMetadata(config);
const content = {
header: getPrHeader(config),
table: getPrUpdatesTable(config),
notes: getPrNotes(config) + getPrExtraNotes(config),
changelogs: getChangelogs(config),
configDescription: await getPrConfigDescription(config),
controls: getControls(),
footer: getPrFooter(config),
};
const defaultPrBodyTemplate =
'{{{header}}}{{{table}}}{{{notes}}}{{{changelogs}}}{{{configDescription}}}{{{controls}}}{{{footer}}}';
const prBodyTemplate = config.prBodyTemplate || defaultPrBodyTemplate;
let prBody = template.compile(prBodyTemplate, content, false);
prBody = prBody.trim();
prBody = prBody.replace(/\n\n\n+/g, '\n\n');
prBody = platform.getPrBody(prBody);
return prBody;
}