-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
executable file
·320 lines (274 loc) · 9.45 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
#!/usr/bin/env node
'use strict'
const request = require('axios')
const path = require('path')
const uri = require('lil-uri')
const fs = require('fs')
const program = require('commander')
const prompt = require('co-prompt')
const co = require('co')
const moment = require('moment')
const chalk = require('chalk')
const assert = require('assert')
const DEFAULT_FILE = 'CHANGES.md'
const JIRA_REGEX = /(?:)([A-Z0-9]{1,}-[0-9]+)(?=\s|-|_|\/|$)/g
let settings
program
.option('-o, --overwrite', 'regenerate the full changelog. OVERWRITES the current changelog')
.option('-i, --interactive', 'request username / password if not provided')
.option('-b, --branch [name]', 'base branch to look for merged pull requests (default: master)')
.option('-v, --verbose', 'verbose output')
.parse(process.argv)
co(function *() {
settings = yield getSettings(program)
const releases = yield buildReleases()
const contents = renderReleases(releases)
write(settings.file, contents)
complete()
})
.catch(e => error(e))
function *getSettings(program) {
const repoInfo = getRepoInfo()
const pkgInfo = getPackageInfo()
const settings = Object.assign({}, repoInfo, pkgInfo)
settings.verbose = !!program.verbose
const user = settings.username = process.env.BITBUCKET_USER
if (!user && program.interactive) settings.username = yield prompt('username: ')
const pswd = settings.password = process.env.BITBUCKET_PSWD
if (!pswd && program.interactive) settings.password = yield prompt.password('password: ')
if (!settings.basePath) settings.basePath = '/rest/api/1.0/projects'
settings.overwrite = !!program.overwrite
settings.branch = program.branch || 'master'
settings.file = settings.file ? path.resolve(settings.file) : path.resolve(DEFAULT_FILE)
settings.baseUrl = `${settings.bitbucket}${settings.basePath}/${settings.projectKey}/repos/${settings.repositoryKey}`
settings.fileContents = read(settings.file)
verifySettings(settings)
if (settings.verbose) {
const copy = Object.assign({}, settings)
delete copy.fileContents
delete copy.password
print('Settings:')
print(JSON.stringify(copy, null, 2))
}
return settings
}
function verifySettings(settings) {
assert.ok(settings.version, `Could not determine release version number. Is your package.json present?`)
assert.ok(settings.username, 'Please define username via `BITBUCKET_USER` env variable, or run in interactive mode.')
assert.ok(settings.password, 'Please define password via `BITBUCKET_PSWD` env variable, or run in interactive mode.')
assert.ok(settings.bitbucket, 'Please define your bitbucket base url in package.json. See README for more info.')
assert.ok(settings.projectKey, 'Please define your bitbucket projectKey in package.json. See README for more info.')
assert.ok(settings.repositoryKey, 'Please define your bitbucket repositoryKey in package.json. See README for more info.')
assert.ok(typeof settings.branch === 'string', 'Please specify a branch name when using the -b option.See README for more info.')
if (!settings.overwrite) {
const releaseTitle = renderReleaseTitle({ version: settings.version })
const regex = new RegExp(`^${releaseTitle}$`, 'm')
const present = settings.fileContents.match(regex)
assert.ok(!present, `Release ${settings.version} was already found in changelog. Aborting.`)
}
}
function getRepoInfo() {
try {
const gitConfig = fs.readFileSync(path.resolve('.git/config'), 'utf8')
const match = gitConfig.match(/(https:|ssh:).+\.git$/m)
const url = uri(match[0])
let host = url.host()
const i = host.lastIndexOf(':')
if (i != -1) host = host.substr(0, i)
const parts = url.path().split('/')
const repositoryKey = path.basename(parts.pop(), '.git')
const projectKey = parts.pop()
return { bitbucket: `https://${host}`, projectKey, repositoryKey }
}
catch(e) {
// Failed to get details from git repo so we'll have to rely on package.json config
}
return {}
}
function getPackageInfo() {
try {
const pkg = require(path.resolve('./package.json'))
return Object.assign({ version: pkg.version, name: pkg.name }, pkg.changelog)
}
catch(e) {
return {}
}
}
function getPullRequests(branch, state, since, start, size, results) {
return getPullRequestsPage(branch, state, start, size).then(res => {
let prs = res.values
if (since) {
prs = prs.filter(pr => pr.updatedDate > since)
}
results = (results || []).concat(prs)
if (res.isLastPage || prs.length < res.values.length) {
return results.filter(pr => pr.toRef.id === `refs/heads/${branch}`)
} else {
return getPullRequests(branch, state, since, start + size, size, results)
}
})
}
function getPullRequestsPage(branch, state, start, size) {
return serviceCall(`${settings.baseUrl}/pull-requests?state=${state}&order=NEWEST&at=refs/heads/${branch}&start=${start}&limit=${size}`)
}
function getTags(start, size, max, tags) {
return getTagsPage(start, size).then(res => {
tags = (tags || []).concat(res.values)
if (res.isLastPage || (max && tags.length >= max)) {
return tags
} else {
return getTags(start + 1, size, max, tags)
}
})
}
function getTagsPage(start, size) {
return serviceCall(`${settings.baseUrl}/tags?start=${start}&limit=${size}`)
}
function getCommit(hash) {
return serviceCall(`${settings.baseUrl}/commits/${hash}`)
}
function serviceCall(url) {
if (settings.verbose) print(url)
return request({
url,
headers: {
accept: 'application/json'
},
auth: {
username: settings.username,
password: settings.password
},
responseType: 'json'
})
.then(res => res.data)
}
function *buildReleases() {
const maxTags = settings.overwrite ? 0 : 1 // get all tags if overwriting
const tags = yield getTags(0, 25, maxTags)
const tagCommitPromises = tags.map(tag => getCommit(tag.hash || tag.latestCommit))
const tagCommits = yield tagCommitPromises
tags.forEach((tag, i) => tag.commit = tagCommits[i])
const since = (settings.overwrite || !tags.length) ? null : tags[0].commit.authorTimestamp
const prs = yield getPullRequests(settings.branch, 'MERGED', since, 0, 50)
const childPrPromises = prs.map(pr => getPullRequests(pr.fromRef.displayId, 'MERGED', null, 0, 25))
const childPrs = yield childPrPromises
prs.forEach((pr, i) => pr.children = childPrs[i])
let release = { version: settings.version, prs: [], date: Date.now() }
let lastTag = tags.shift()
const releases = []
while (prs.length) {
const pr = prs.shift()
if (!lastTag || (pr.updatedDate > lastTag.commit.authorTimestamp)) {
release.prs.push(pr)
} else {
// If this version is same as last version then must be generating
// full changelog, and not as part of new release, so discard any
// new merges as no new release for them to go with.
if (!lastTag || lastTag.displayId !== release.version) {
releases.push(release)
}
release = { prs: [ pr ] }
if (lastTag) {
release.version = lastTag.displayId
release.date = lastTag.commit.authorTimestamp
}
lastTag = tags.shift()
}
}
if (releases[releases.length - 1] !== release) {
releases.push(release)
}
return releases
}
function renderReleases(releases) {
return releases
.map(r => renderRelease(r))
.reduce((all, lines) => all.concat(lines), [])
.join('\n') + '\n'
}
function renderRelease(r) {
let lines = []
lines.push(renderReleaseTitle(r))
lines.push(`${renderDate(r.date, 'MMM Do YY')}\n`)
lines = lines.concat(renderPrs(r.prs, 0))
lines.push('')
return lines
}
function renderReleaseTitle(release) {
return `## ${release.version}`
}
function renderPrs(prs, indent) {
return prs.map(pr => renderPr(pr, indent))
.reduce((all, lines) => all.concat(lines), [])
}
function renderPr(pr, indent) {
const space = ' '.repeat(indent)
let lines = []
let line = `${space}- ${renderPrLink(pr)} ${pr.title} `
line += `<small>${renderAuthor(pr)}${renderJiras(pr)} - ${renderDate(pr.updatedDate, 'D/M/YY')}</small>`
lines.push(line)
if (pr.children && pr.children.length) {
lines = lines.concat(renderPrs(pr.children, indent + 4))
}
return lines
}
function renderPrLink(pr) {
return `[${pr.id}](${pr.links.self[0].href})`
}
function renderAuthor(pr) {
return `[${pr.author.user.displayName}](${pr.author.user.links.self[0].href})`
}
function renderDate(timestamp, format) {
return `${moment(timestamp).format(format)}`
}
function renderJiras(pr) {
const m1 = (pr.title || '').match(JIRA_REGEX)
const m2 = (pr.description || '').match(JIRA_REGEX)
const m3 = (pr.fromRef.displayId || '').match(JIRA_REGEX)
const comb = (m1 || []).concat(m2 ||[]).concat(m3 || [])
const jiras = [ ...new Set(comb) ]
if (jiras.length) {
if (settings.jira) {
return ` (${jiras.map(id => `[${id}](${settings.jira}/browse/${id})`).join(', ')})`
} else {
return ` (${jiras.join(', ')})`
}
} else {
return ''
}
}
function read(file) {
return stats(file) ? fs.readFileSync(file, 'utf8') : ''
}
function write(file, contents) {
const fileStats = stats(file)
if (settings.overwrite || !fileStats) {
fs.writeFileSync(file, contents, 'utf8')
} else {
fs.writeFileSync(file, contents + settings.fileContents, 'utf8')
}
}
function stats(file) {
try {
return !!fs.statSync(file)
} catch(e) {
return null
}
}
function complete() {
const filename = path.basename(settings.file)
const msg = `${settings.version} written to ${filename}`
print(chalk.bold.cyan(msg))
process.exit(0)
}
function error(e) {
let msg
if (e.status) msg = `${e.status}: ${e.statusText} - ${JSON.stringify(e.data, null, 2)}`
else if (e.message) msg = e.message
else msg = e
print(chalk.red(msg))
process.exit(1)
}
function print(msg) {
process.stdout.write(`${msg}\n`)
}