-
Notifications
You must be signed in to change notification settings - Fork 26.5k
/
release.js
164 lines (122 loc) · 3.61 KB
/
release.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// section -> label
const sectionLabelMap = {
'Core Changes': 'type: next',
'Documentation Changes': 'documentation',
'Example Changes': 'examples',
}
const fallbackSection = 'Misc Changes'
// --------------------------------------------------
const prNumberRegex = /\(#([-0-9]+)\)$/
const getCommitPullRequest = async (commit, github) => {
const match = prNumberRegex.exec(commit.title)
if (!match) {
return null
}
const number = parseInt(match[1], 10)
if (!number) {
return null
}
const { data } = await github.connection.pullRequests.get({
owner: github.repoDetails.user,
repo: github.repoDetails.repo,
number,
})
return data
}
const getSectionForPullRequest = (pullRequest) => {
const { labels } = pullRequest
// sections defined first will take priority
for (const [section, label] of Object.entries(sectionLabelMap)) {
if (labels.some((prLabel) => prLabel.name === label)) {
return section
}
}
return null
}
const groupByLabels = async (commits, github) => {
// Initialize the sections object with empty arrays
const sections = Object.keys(sectionLabelMap).reduce((sections, section) => {
sections[section] = []
return sections
}, {})
sections.__fallback = []
for (const commit of commits) {
const pullRequest = await getCommitPullRequest(commit, github)
if (pullRequest) {
const section = getSectionForPullRequest(pullRequest)
if (section) {
// Add the change to the respective section
sections[section].push({
title: pullRequest.title,
number: pullRequest.number,
})
continue
}
// No section found, add it to the fallback section
sections.__fallback.push({
title: pullRequest.title,
number: pullRequest.number,
})
continue
}
// No Pull Request found, add it to the fallback section but without the number
sections.__fallback.push({
title: commit.title,
})
}
return sections
}
function cleanupPRTitle(title) {
if (title.startsWith('[Docs] ')) {
return title.replace('[Docs] ', '')
}
return title
}
const buildChangelog = (sections, authors) => {
let text = ''
for (const section in sections) {
const changes = sections[section]
// No changes in this section? Don't render it
if (changes.length === 0) {
continue
}
const title = section === '__fallback' ? fallbackSection : section
text += `### ${title}\n\n`
for (const change of changes) {
const numberText = change.number != null ? `: #${change.number}` : ''
text += `- ${cleanupPRTitle(change.title)}${numberText}\n`
}
text += '\n'
}
if (authors.size > 0) {
text += '### Credits \n\n'
text += 'Huge thanks to '
let index = 1
authors.forEach((author) => {
// GitHub links usernames if prefixed with @
text += `@${author}`
const penultimate = index === authors.size - 1
const notLast = index !== authors.size
if (penultimate) {
// Oxford comma is applied when list is bigger than 2 names
if (authors.size > 2) {
text += ','
}
text += ' and '
} else if (notLast) {
text += ', '
}
index += 1
})
text += ' for helping!'
text += '\n'
}
return text
}
module.exports = async (markdown, metadata) => {
const { commits, authors, githubConnection, repoDetails } = metadata
const github = { connection: githubConnection, repoDetails }
const sections = await groupByLabels(commits.all, github)
const changelog = buildChangelog(sections, authors)
return changelog
}