generated from actions/typescript-action
/
getPrCommentBody.ts
111 lines (92 loc) · 2.93 KB
/
getPrCommentBody.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {PackageDependency} from 'renovate/dist/manager/types'
import {ChangeLogResult} from 'renovate/dist/workers/pr/changelog'
import {sanitizeMarkdown} from 'renovate/dist/util/markdown'
import {UpdatedDependencyWithChangelog} from './types'
export const commentTitle = '# Dependency updates summary'
const footer =
'\n---\n\nThis comment content is generated by [Renovate Bot](https://github.com/renovatebot/renovate)'
export function getPrCommentBody(
dependencies: UpdatedDependencyWithChangelog[]
): string {
const content = dependencies.map(getDependencyChangeContent)
return `${commentTitle}
This PR contains the following updates:
<table>
${content.map(x => x.tableRow).join('\n\n')}
</table>
---
### Release notes
${content.map(x => x.changelog).join('\n\n')}
${footer}`
}
function getDependencyChangeContent({
dependency,
update,
changelog
}: UpdatedDependencyWithChangelog): {tableRow: string; changelog: string} {
const dependencyLink = getDependencyNameLinked(dependency)
const type = dependency.prettyDepType ?? dependency.depType
const from = update.displayFrom ?? update.currentVersion
const to = update.displayTo ?? update.newVersion
const change = `<code>${from}</code> → <code>${to}</code>`
return {
tableRow: `<tr>
<td>${dependencyLink}</td>
<td>${type}</td>
<td>${change}</td>
</tr>
`,
changelog: `<details><summary>${dependency.depName}</summary>
${getReleaseNotes(dependencyLink, changelog)}</details>`
}
}
function getReleaseNotes(
dependencyLink: string,
changelog: ChangeLogResult | null
): string {
const releases =
changelog?.versions?.map(x => {
const versionWithPrefix = x.version.startsWith('v')
? x.version
: `v${x.version}`
const header = x.releaseNotes
? `### [\`${versionWithPrefix}\`](${x.releaseNotes.url})`
: `### \`${versionWithPrefix}\``
return `${header}
${x.compare.url ? `[Compare Source](${x.compare.url})` : ''}
${x.releaseNotes?.body ?? ''}`
}) ?? []
if (releases.length === 0) {
return `<blockquote></p><p>No changelog found, please review changelog from official resources of ${dependencyLink}</blockquote>`
}
return sanitizeMarkdown(`
<blockquote>
<p></p>
${releases.join('\n\n')}
</blockquote>`)
}
function getDependencyNameLinked({
depName,
homepage,
sourceUrl,
dependencyUrl,
changelogUrl
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
PackageDependency & Record<string, any>): string {
let depNameLinked = depName || ''
const primaryLink = homepage || sourceUrl || dependencyUrl
if (primaryLink) {
depNameLinked = `<a href="${primaryLink}">${depNameLinked}</a>`
}
const otherLinks = []
if (homepage && sourceUrl) {
otherLinks.push(`<a href="${sourceUrl}">source</a>`)
}
if (changelogUrl) {
otherLinks.push(`<a href="${changelogUrl}">changelog</a>`)
}
if (otherLinks.length) {
depNameLinked += ` (${otherLinks.join(', ')})`
}
return depNameLinked
}