forked from requarks/changelog-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
432 lines (388 loc) · 14.9 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
const github = require('@actions/github')
const core = require('@actions/core')
const _ = require('lodash')
const cc = require('@conventional-commits/parser')
const fs = require('fs').promises
const { setTimeout } = require('timers/promises')
const pluralize = require('pluralize')
const humanizeString = require('humanize-string').default
const types = [
{ types: ['feat', 'feature'], header: 'New Features', icon: ':sparkles:' },
{ types: ['fix', 'bugfix'], header: 'Bug Fixes', icon: ':bug:', relIssuePrefix: 'fixes' },
{ types: ['perf'], header: 'Performance Improvements', icon: ':zap:' },
{ types: ['refactor'], header: 'Refactors', icon: ':recycle:' },
{ types: ['test', 'tests'], header: 'Tests', icon: ':white_check_mark:' },
{ types: ['build', 'ci'], header: 'Build System', icon: ':construction_worker:' },
{ types: ['doc', 'docs'], header: 'Documentation Changes', icon: ':memo:' },
{ types: ['style'], header: 'Code Style Changes', icon: ':art:' },
{ types: ['chore'], header: 'Chores', icon: ':wrench:' },
{ types: ['oafa'], header: 'Once And For All', icon: ':one-and-for-all:' },
{ types: ['other'], header: 'Other Changes', icon: ':flying_saucer:' }
]
const rePrId = /#([0-9]+)/g
const rePrEnding = /\(#([0-9]+)\)$/
function formatScopeForSlack(scope) {
if (!scope) return;
let formattedScope = humanizeString(scope).split(' ').map(w => `${w[0].toUpperCase()}${w.substring(1)}`).join(' ');
return `*${formattedScope}*`;
}
function buildSubject ({ writeToFile, subject, author, authorUrl, commitScope, commitUrl, commitShaSubstr, owner, repo, formatForSlack }) {
const hasPR = rePrEnding.test(subject)
const prs = []
let output = subject
let outputForSlack = subject
if (writeToFile) {
if (hasPR) {
const prMatch = subject.match(rePrEnding)
const msgOnly = subject.slice(0, prMatch[0].length * -1)
output = msgOnly.replace(rePrId, (m, prId) => {
prs.push(prId)
return `[#${prId}](https://github.com/${owner}/${repo}/pull/${prId})`
})
output += `*(PR [#${prMatch[1]}](https://github.com/${owner}/${repo}/pull/${prMatch[1]}) by [@${author}](${authorUrl}))*`
} else {
output = subject.replace(rePrId, (m, prId) => {
return `[#${prId}](https://github.com/${owner}/${repo}/pull/${prId})`
})
output += ` *(commit by [@${author}](${authorUrl}))*`
}
} else {
if (hasPR) {
if (formatForSlack) {
outputForSlack = subject.replace(rePrEnding, (m, prId) => {
prs.push(prId)
return `(<${commitUrl}|${commitShaSubstr}>, <${authorUrl}|@${author}>)`;
});
outputForSlack = `${commitScope ? `${formatScopeForSlack(commitScope)}: ` : ''}${outputForSlack}`;
} else {
output = subject.replace(rePrEnding, (m, prId) => {
prs.push(prId)
return `*(PR #${prId} by @${author})*`
})
}
} else {
if (formatForSlack) {
outputForSlack = `${subject} (commit by <${authorUrl}|@${author}>)`
} else {
output = `${subject} *(commit by @${author})*`
}
}
}
return {
output,
outputForSlack,
prs,
}
}
async function main () {
const token = core.getInput('token')
const tag = core.getInput('tag')
const fromTag = core.getInput('fromTag')
const toTag = core.getInput('toTag')
const excludeTypes = (core.getInput('excludeTypes') || '').split(',').map(t => t.trim())
const writeToFile = core.getBooleanInput('writeToFile')
const includeRefIssues = core.getBooleanInput('includeRefIssues')
const includeBreaking = core.getBooleanInput('includeBreaking')
const useGitmojis = core.getBooleanInput('useGitmojis')
const includeInvalidCommits = core.getBooleanInput('includeInvalidCommits')
const formatForSlack = core.getBooleanInput('formatForSlack')
const gh = github.getOctokit(token)
const owner = github.context.repo.owner
const repo = github.context.repo.repo
const currentISODate = (new Date()).toISOString().substring(0, 10)
let latestTag = null
let previousTag = null
if (tag && (fromTag || toTag)) {
return core.setFailed(`Must provide EITHER input tag OR (fromTag and toTag), not both!`)
} else if (tag) {
// GET LATEST + PREVIOUS TAGS
core.info(`Using input tag: ${tag}`)
const tagsRaw = await gh.graphql(`
query lastTags ($owner: String!, $repo: String!) {
repository (owner: $owner, name: $repo) {
refs(first: 2, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
name
target {
oid
}
}
}
}
}
`, {
owner,
repo
})
latestTag = _.get(tagsRaw, 'repository.refs.nodes[0]')
previousTag = _.get(tagsRaw, 'repository.refs.nodes[1]')
if (!latestTag) {
return core.setFailed('Couldn\'t find the latest tag. Make sure you have an existing tag already before creating a new one.')
}
if (!previousTag) {
return core.setFailed('Couldn\'t find a previous tag. Make sure you have at least 2 tags already (current tag + previous initial tag).')
}
if (latestTag.name !== tag) {
return core.setFailed(`Provided tag doesn\'t match latest tag ${tag}.`)
}
core.info(`Using latest tag: ${latestTag.name}`)
core.info(`Using previous tag: ${previousTag.name}`)
} else if (fromTag && toTag) {
// GET FROM + TO TAGS FROM INPUTS
latestTag = { name: fromTag }
previousTag = { name: toTag }
core.info(`Using tag range: ${fromTag} to ${toTag}`)
} else {
return core.setFailed(`Must provide either input tag OR (fromTag and toTag). None were provided!`)
}
// GET COMMITS
let curPage = 0
let totalCommits = 0
let hasMoreCommits = false
const commits = []
do {
hasMoreCommits = false
curPage++
const commitsRaw = await gh.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${previousTag.name}...${latestTag.name}`,
page: curPage,
per_page: 100
})
totalCommits = _.get(commitsRaw, 'data.total_commits', 0)
const rangeCommits = _.get(commitsRaw, 'data.commits', [])
commits.push(...rangeCommits)
if ((curPage - 1) * 100 + rangeCommits.length < totalCommits) {
hasMoreCommits = true
}
} while (hasMoreCommits)
if (!commits || commits.length < 1) {
return core.setFailed('Couldn\'t find any commits between latest and previous tags.')
}
// PARSE COMMITS
const commitsParsed = []
const breakingChanges = []
for (const commit of commits) {
try {
const cAst = cc.toConventionalChangelogFormat(cc.parser(commit.commit.message))
commitsParsed.push({
...cAst,
sha: commit.sha,
url: commit.html_url,
author: commit.author.login,
authorUrl: commit.author.html_url
})
for (const note of cAst.notes) {
if (note.title === 'BREAKING CHANGE' && includeBreaking) {
breakingChanges.push({
sha: commit.sha,
url: commit.html_url,
subject: cAst.subject,
author: commit.author.login,
authorUrl: commit.author.html_url,
text: note.text
})
}
}
core.info(`[OK] Commit ${commit.sha} of type ${cAst.type} - ${cAst.subject}`)
} catch (err) {
if (includeInvalidCommits) {
commitsParsed.push({
type: 'other',
subject: commit.commit.message,
sha: commit.sha,
url: commit.html_url,
author: commit.author.login,
authorUrl: commit.author.html_url
})
core.info(`[OK] Commit ${commit.sha} with invalid type, falling back to other - ${commit.commit.message}`)
} else {
core.info(`[INVALID] Skipping commit ${commit.sha} as it doesn't follow conventional commit format.`)
}
}
}
if (commitsParsed.length < 1) {
return core.setFailed('No valid commits parsed since previous tag.')
}
// BUILD CHANGELOG
const changesFile = []
const changesVar = []
const changesForSlack = []
let idx = 0
if (breakingChanges.length > 0) {
changesFile.push(useGitmojis ? '### :boom: BREAKING CHANGES' : '### BREAKING CHANGES')
changesVar.push(useGitmojis ? '### :boom: BREAKING CHANGES' : '### BREAKING CHANGES')
if (formatForSlack) {
changesForSlack.push(useGitmojis ? ':boom: *BREAKING CHANGES*' : '*BREAKING CHANGES*')
}
for (const breakChange of breakingChanges) {
const body = breakChange.text.split('\n').map(ln => ` ${ln}`).join(' \n')
const subjectFile = buildSubject({
writeToFile: true,
subject: breakChange.subject,
author: breakChange.author,
authorUrl: breakChange.authorUrl,
owner,
repo
})
const subjectVar = buildSubject({
writeToFile: false,
subject: breakChange.subject,
author: breakChange.author,
authorUrl: breakChange.authorUrl,
formatForSlack,
owner,
repo
})
changesFile.push(`- due to [\`${breakChange.sha.substring(0, 7)}\`](${breakChange.url}) - ${subjectFile.output}:\n\n${body}\n`)
changesVar.push(`- due to [\`${breakChange.sha.substring(0, 7)}\`](${breakChange.url}) - ${subjectVar.output}:\n\n${body}\n`)
if (formatForSlack) {
changesForSlack.push(`• due to <${breakChange.url}|\`${breakChange.sha.substring(0, 7)}\`> ${subjectVar.outputForSlack}:\n\n${body}\n`)
}
}
idx++
}
for (const type of types) {
if (_.intersection(type.types, excludeTypes).length > 0) {
continue
}
const matchingCommits = commitsParsed.filter(c => type.types.includes(c.type))
if (matchingCommits.length < 1) {
continue
}
if (idx > 0) {
changesFile.push('')
changesVar.push('')
if (formatForSlack) {
changesForSlack.push('')
}
}
changesFile.push(useGitmojis ? `### ${type.icon} ${type.header}` : `### ${type.header}`)
changesVar.push(useGitmojis ? `### ${type.icon} ${type.header}` : `### ${type.header}`)
if (formatForSlack) {
changesForSlack.push(useGitmojis ? `${type.icon} *${type.header}*` : `*${type.header}*`)
}
const relIssuePrefix = type.relIssuePrefix || 'addresses'
for (const commit of matchingCommits) {
const scope = commit.scope ? `**${commit.scope}**: ` : ''
const subjectFile = buildSubject({
writeToFile: true,
subject: commit.subject,
author: commit.author,
authorUrl: commit.authorUrl,
owner,
repo
})
const subjectVar = buildSubject({
writeToFile: false,
subject: commit.subject,
author: commit.author,
authorUrl: commit.authorUrl,
commitScope: commit.scope,
commitUrl: commit.url,
commitShaSubstr: commit.sha.substring(0, 7),
formatForSlack,
owner,
repo
})
changesFile.push(`- [\`${commit.sha.substring(0, 7)}\`](${commit.url}) - ${scope}${subjectFile.output}`)
changesVar.push(`- [\`${commit.sha.substring(0, 7)}\`](${commit.url}) - ${scope}${subjectVar.output}`)
if (formatForSlack) {
changesForSlack.push(`• ${subjectVar.outputForSlack}`)
}
if (includeRefIssues && subjectVar.prs.length > 0) {
for (const prId of subjectVar.prs) {
core.info(`Querying related issues for PR ${prId}...`)
await setTimeout(500) // Make sure we don't go over GitHub API rate limits
const issuesRaw = await gh.graphql(`
query relIssues ($owner: String!, $repo: String!, $prId: Int!) {
repository (owner: $owner, name: $repo) {
pullRequest(number: $prId) {
closingIssuesReferences(first: 50) {
nodes {
number
url
author {
login
url
}
}
}
}
}
}
`, {
owner,
repo,
prId: parseInt(prId)
})
const relIssues = _.get(issuesRaw, 'repository.pullRequest.closingIssuesReferences.nodes')
if (formatForSlack) {
// Group the issues by author
const relIssuesByAuthor = _.groupBy(relIssues, 'author.login')
for (const author in relIssuesByAuthor) {
const relIssues = relIssuesByAuthor[author]
relIssues.sort((a, b) => a.number - b.number)
const relIssueStrs = relIssues.map(relIssue => `<${relIssue.url}|#${relIssue.number}>`);
const authorUrl = relIssues[0].author.url
changesForSlack.push(` • ${relIssuePrefix} ${pluralize('issue', relIssues.length)} ${relIssueStrs.join(', ')} opened by <${authorUrl}|@${author}>`)
}
}
for (const relIssue of relIssues) {
changesFile.push(` - :arrow_lower_right: *${relIssuePrefix} issue [#${relIssue.number}](${relIssue.url}) opened by [@${relIssue.author.login}](${relIssue.author.url})*`)
changesVar.push(` - :arrow_lower_right: *${relIssuePrefix} issue #${relIssue.number} opened by @${relIssue.author.login}*`)
}
}
}
}
idx++
}
if (changesFile.length > 0) {
changesFile.push('')
changesVar.push('')
if (formatForSlack) {
changesForSlack.push('')
}
} else {
return core.warning('Nothing to add to changelog because of excluded types.')
}
core.setOutput('changes', changesVar.join('\n'))
if (formatForSlack) {
core.setOutput('changesForSlack', changesForSlack.join('\n'))
}
if (!writeToFile) { return }
// PARSE EXISTING CHANGELOG
let chglog = ''
try {
chglog = await fs.readFile('CHANGELOG.md', 'utf8')
} catch (err) {
core.info('Couldn\'t find a CHANGELOG.md, creating a new one...')
chglog = `# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
`
}
// UPDATE CHANGELOG CONTENTS
const lines = chglog.replace(/\r/g, '').split('\n')
let firstVersionLine = _.findIndex(lines, l => l.startsWith('## '))
if (firstVersionLine >= 0 && lines[firstVersionLine].startsWith(`## [${latestTag.name}`)) {
return core.notice('This version already exists in the CHANGELOG! No change will be made to the CHANGELOG.')
}
if (firstVersionLine < 0) {
firstVersionLine = lines.length
}
let output = ''
if (firstVersionLine > 0) {
output += lines.slice(0, firstVersionLine).join('\n') + '\n'
}
output += `## [${latestTag.name}] - ${currentISODate}\n${changesFile.join('\n')}\n`
if (firstVersionLine < lines.length) {
output += '\n' + lines.slice(firstVersionLine).join('\n')
}
output += `\n[${latestTag.name}]: https://github.com/${owner}/${repo}/compare/${previousTag.name}...${latestTag.name}`
// WRITE CHANGELOG TO FILE
await fs.writeFile('CHANGELOG.md', output)
}
main()