-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
400 lines (319 loc) · 12.6 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
'use strict'
const is = require('unist-util-is')
const u = require('unist-builder')
const semver = require('semver')
const isSorted = require('is-array-sorted')
const github = require('github-from-package')
const closest = require('read-closest-package')
const path = require('path')
const execFileSync = require('child_process').execFileSync
const Changelog = require('./lib/changelog')
const getCommits = require('./lib/git-log-between')
const getChanges = require('./lib/get-changes')
const plugin = require('./package.json').name
const REJECT_NAMES = new Set(['history', 'releases', 'changelog'])
const GROUP_TYPES = new Set(['Changed', 'Added', 'Deprecated', 'Removed', 'Fixed', 'Security'])
const UNCATEGORIZED = 'Uncategorized'
module.exports = function attacher (opts) {
opts = opts || {}
const fix = !!opts.fix
const submodules = !!opts.submodules
const add = opts.add
const parse = (str) => this.parse(str).children
return async function transform (root, file) {
if (file.basename && file.basename !== 'CHANGELOG.md') {
if (REJECT_NAMES.has(file.stem.toLowerCase())) {
warn('Filename must be CHANGELOG.md', root, 'filename')
}
return
}
if (!is(root, 'root') || !root.children) {
throw new Error('Expected a root node')
}
const cwd = path.resolve(opts.cwd || file.cwd)
const pkg = lazyPkg(cwd, opts.pkg)
const repository = repo(opts.repository || pkg().repository)
const tags = gitTags(cwd)
const currentVersion = opts.version || pkg().version || lastTagVersion(tags) || '0.0.0'
if (!repository) {
throw new Error('No repository url found in package.json or options')
} else if (semver.valid(currentVersion) !== currentVersion) {
throw new Error('No valid version found in package.json or options')
}
const githubUrl = github2(repository)
const changelog = Changelog(parse, root.children)
const versions = new Set()
if (fix) {
changelog.buildHeading()
} else if (!changelog.hasValidHeading()) {
warn('Changelog must start with a top-level "Changelog" heading', changelog.heading || root, 'title')
}
if (fix) {
if (add) {
addRelease(add)
}
changelog.children.sort(cmpRelease)
} else if (!isSorted(changelog.children, { comparator: cmpRelease })) {
warn('Releases must be sorted latest-first', root, 'latest-release-first')
// Sort anyway (doesn't affect original tree) so that we
// can correctly compute diff urls and commit ranges below.
changelog.children.sort(cmpRelease)
}
changelog.children.forEach(relateVersions)
await Promise.all(changelog.children.map(lintRelease))
// Lint or rebuild headings, with links and definitions
for (let i = 0; i < changelog.children.length; i++) {
const { version, previousVersion, date, linkType, heading } = changelog.children[i]
if (!version) continue
const identifier = version.toLowerCase()
const oldUrl = (changelog.definitions.get(identifier) || {}).url
const url = oldUrl || defaultReleaseUrl(githubUrl, tags, version, previousVersion)
const isFirstRelease = i === changelog.children.length - 1
if (version === 'unreleased' && !url) {
continue
}
if (fix) {
const label = identifier
const referenceType = version === 'unreleased' ? 'full' : 'shortcut'
heading.children = [u('linkReference', { identifier, label, referenceType }, [
u('text', version === 'unreleased' ? 'Unreleased' : version)
])]
if (version !== 'unreleased') {
heading.children.push(u('text', ` - ${date || 'YYYY-MM-DD'}`))
}
changelog.definitions.set(identifier, u('definition', { identifier, label, url, title: null }))
} else if (!isFirstRelease && version !== 'unreleased') {
if (!linkType) {
warn('Release version must have a link', heading, 'release-version-link')
} else if (linkType !== 'linkReference') {
warn('Use link reference in release heading', heading, 'release-version-link-reference')
}
}
}
if (fix) {
changelog.definitions = sortMap(changelog.definitions, cmpVersion)
} else if (!isMapSorted(changelog.definitions, cmpVersion)) {
warn('Definitions must be sorted latest-first', root, 'latest-definition-first')
}
if (fix) {
// Reconstruct tree
root.children = changelog.tree()
return root
}
function relateVersions (release, i, arr) {
release.previousVersion = arr[i + 1] ? arr[i + 1].version : null
}
function addRelease (add) {
if (Array.isArray(add)) {
add.forEach(addRelease)
return
} else if (typeof add !== 'string' || add === '') {
warn('Target must be a non-empty string', root, 'add-new-release')
return
}
let target = semver.valid(add)
if (!target) {
const lastRelease = changelog.children[0]
const from = (lastRelease && lastRelease.version) || currentVersion
if (from === 'unreleased') { // TODO
warn('Bumping Unreleased is not implemented yet', root, 'add-new-release')
return
} else if (!from) {
warn('No version found to start from', root, 'add-new-release')
return
}
target = semver.inc(from, add)
}
if (!target) {
warn(`Target (${add}) must be a version or release type ([pre]major, [pre]minor, [pre]patch or prerelease)`, root, 'add-new-release')
return
} else if (changelog.children.some(release => release.version === target)) {
warn(`Target version ${target} already exists`, root, 'add-new-release')
return
}
// Will be sorted and populated by other code
// TODO: take date from tag if it exists
changelog.createRelease(target, releaseDate(opts.Date || Date))
}
async function lintRelease (release) {
const { heading } = release
if (!is(heading, { depth: 2 })) {
warn('Release must start with second-level heading', heading, 'release-heading-depth')
return
} else if (!release.parseable) {
warn('Release heading must be "Unreleased" or have the format "<version> - <date>"', heading, 'release-heading')
return
}
if (release.version) {
if (versions.has(release.version)) {
warn('Release version must be unique', heading, 'unique-release')
}
if (!fix && release.version === 'unreleased' && release.title !== 'Unreleased') {
warn('Release heading must be "Unreleased"', heading, 'release-heading')
}
versions.add(release.version)
}
if (release.version !== 'unreleased') {
if (!release.version) {
warn('Release must have a version', heading, 'release-version')
} else if (semver.valid(release.version) !== release.version) {
warn('Release version must be semver-valid', heading, 'release-version')
}
if (!release.date) {
warn('Release must have date', heading, 'release-date')
} else if (!/^\d{4}-\d{2}-\d{2}$/.test(release.date)) {
warn('Release date must have format YYYY-MM-DD', heading, 'release-date')
}
}
if (release.isEmpty()) {
await lintEmptyRelease(release)
}
const hasUncategorizedChanges = release.children.some(function (group) {
return group.type() === UNCATEGORIZED && !group.isEmpty()
})
release.children.forEach(function (group) {
lintGroup(group, hasUncategorizedChanges)
})
}
async function lintEmptyRelease (release) {
const { heading, version, previousVersion } = release
if (fix && version && previousVersion) {
const gt = forgivingTag(previousVersion, tags)
const opts = { cwd, gt, limit: 100, submodules }
if (version === 'unreleased' || isNewVersion(version, previousVersion)) {
opts.lte = 'HEAD'
} else {
opts.lt = forgivingTag(version, tags)
}
let commits
try {
commits = await getCommits(opts)
} catch (err) {
const msg = `Failed to get commits for release (${version}): ${err.message}`
warn(msg, heading, 'no-empty-release')
return
}
const grouped = getChanges(commits)
// Add other types as a hint to categorize
const insertEmpty = grouped[UNCATEGORIZED].length > 0
for (const type in grouped) {
const changes = grouped[type]
if (!changes.length && (!insertEmpty || type === UNCATEGORIZED)) {
continue
}
const group = release.createGroup(type)
if (changes.length) {
group.createList(changes)
}
}
if (!release.isEmpty()) return
}
warn(`Release (${version || 'n/a'}) is empty`, heading, 'no-empty-release')
}
function lintGroup (group, hasUncategorizedChanges) {
if (!group.hasValidHeading()) {
warn('Group must start with a third-level, text-only heading', group.heading, 'group-heading')
return
}
const type = group.type()
const types = Array.from(GROUP_TYPES).join(', ')
if (!type) {
warn(`Group heading must be one of ${types}`, group.heading, 'group-heading-type')
} else if ((type === UNCATEGORIZED || !hasUncategorizedChanges) && group.isEmpty()) {
warn(`Remove empty group ${type}`, group.heading, 'no-empty-group')
} else if (!GROUP_TYPES.has(type)) {
if (type === UNCATEGORIZED) {
warn('Categorize the changes', group.heading, 'no-uncategorized-changes')
} else {
warn(`Group heading must be one of ${types}`, group.heading, 'group-heading-type')
}
}
}
function warn (msg, node, rule) {
file.message(msg, node, `${plugin}:${rule}`)
}
function lazyPkg (cwd, pkg) {
return function () {
pkg = pkg || closest.sync({ cwd }) || {}
return pkg
}
}
function isNewVersion (nextVersion, previousVersion) {
return previousVersion === currentVersion &&
cmpVersion(nextVersion, currentVersion) < 0
}
}
}
function cmpRelease (a, b) {
// Retain original sort order of invalid releases
if (!a.version || !b.version) return a.index - b.index
return cmpVersion(a.version, b.version)
}
function cmpVersion (a, b) {
if (a === b) return 0
if (a === 'unreleased') return -1
if (b === 'unreleased') return 1
let av = semver.valid(a)
let bv = semver.valid(b)
// Make -rc9 vs -rc10 sortable by converting to (proper) -rc.9 vs -rc.10
if (av) av = av.replace(/-rc(\d+)$/, (m, p1) => '-rc.' + p1)
if (bv) bv = bv.replace(/-rc(\d+)$/, (m, p1) => '-rc.' + p1)
return av && bv ? semver.compare(bv, av) : av ? -1 : bv ? 1 : a.localeCompare(b)
}
// TODO: https://github.com/vweevers/hallmark/issues/82
function defaultReleaseUrl (githubUrl, tags, version, prevVersion) {
if (!prevVersion) {
return version === 'unreleased' ? null : `${githubUrl}/releases/tag/${forgivingTag(`v${version}`, tags)}`
}
const left = forgivingTag(`v${prevVersion}`, tags)
const right = version === 'unreleased' ? 'HEAD' : forgivingTag(`v${version}`, tags)
return `${githubUrl}/compare/${left}...${right}`
}
// If a (historical) tag without "v" prefix exists, use that.
function forgivingTag (tag, tags) {
if (tag[0] !== 'v') tag = 'v' + tag
if (tags.indexOf(tag) >= 0) return tag
const unprefixed = tag.replace(/^v/, '')
if (tags.indexOf(unprefixed) >= 0) return unprefixed
return tag
}
function gitTags (cwd) {
return execFileSync('git', ['tag'], {
cwd, maxBuffer: 1024 * 1024 * 16, encoding: 'utf8'
}).split(/\r?\n/).filter(Boolean)
}
function repo (repository) {
return (repository && repository.url) || repository
}
function lastTagVersion (tags) {
const sorted = tags
.filter(t => t.startsWith('v'))
.sort(cmpVersion)
return sorted.length ? sorted[0].slice(1) : null
}
// TODO: there's a package that does this, can't find it
function github2 (repository) {
if (/^[a-z0-9-_]+\/[a-z0-9-_]+$/i.test(repository)) {
return 'https://github.com/' + repository
} else {
return github({ repository })
}
}
function isMapSorted (map, comparator) {
return isSorted(Array.from(map.keys()), { comparator })
}
function sortMap (map, comparator) {
const entries = Array.from(map.entries())
entries.sort((a, b) => comparator(a[0], b[0]))
return new Map(entries)
}
function releaseDate (Ctor) {
const date = new Ctor()
const yyyy = date.getFullYear()
const mm = twoDigits(date.getMonth() + 1)
const dd = twoDigits(date.getDate())
return `${yyyy}-${mm}-${dd}`
}
function twoDigits (n) {
return n < 10 ? `0${n}` : n
}