-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrelease-notes.js
More file actions
187 lines (162 loc) Β· 5.21 KB
/
release-notes.js
File metadata and controls
187 lines (162 loc) Β· 5.21 KB
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
const hbs = require('handlebars')
const parseGitUrl = require('git-url-parse')
const dateFormat = require('dateformat')
const { find: findEmoji, emojify } = require('node-emoji')
const parseCommits = require('./helper/parse-commits')
const getCmpLink = require('./helper/get-cmp-link')
const debug = require('./helper/debug')
function proxifyCommitsMap (commits) {
return new Proxy(commits, {
get (target, prop) {
const emoji = findEmoji(prop)
if (emoji) {
return target[emoji.emoji]
}
return undefined
}
})
}
/**
* @type {string[]}
*/
const RELEASE_TYPES = require('./assets/release-types.json')
module.exports = class ReleaseNotes {
/**
* @param {object} context
*/
constructor (context, { issueResolution, commitUrlTemplate, commitBaseUrl, template, partials, helpers, semver } = {}) {
debug('Found %d commits', Object.keys(context.commits).length)
const { owner, name: repo, source } = parseGitUrl(context.options.repositoryUrl)
debug('Git remote: %s', source)
debug('Repository: %s/%s', owner, repo)
console.table(context)
this._semver = semver
this._template = template
this._partials = partials
this._helpers = {
datetime: function (format = 'UTC:yyyy-mm-dd') {
return dateFormat(new Date(), format)
},
...helpers
}
const parsedCommits = parseCommits(context.commits, { owner, repo, source, commitBaseUrl }, {
issues: { owner, repo, source, ...issueResolution },
semver: this._semver
})
this._context = {
owner,
repo,
source,
commits: Object.entries(parsedCommits).reduce((acc, [key, commits]) => {
acc[key] = commits.map(commit => {
return {
...commit,
url: commitUrlTemplate.replace('{source}', source)
.replace('{owner}', owner)
.replace('{repo}', repo)
.replace('{commit}', commit.commit.short)
}
})
return acc
}, {})
}
Object.values(this._context.commits)
.forEach(commits => commits.reverse())
debug('Gitmoji commits:')
Object.keys(this._context.commits)
.map(gitmoji => `* ${gitmoji}: ${this._context.commits[gitmoji].length} commit(s)`)
.forEach(msg => debug(msg))
}
getReleaseType (releaseSchema) {
// cache hits
if (this._rtype) return this._rtype
if (this._semver) {
for (const RTYPE of RELEASE_TYPES) {
debug('Testing against release type "%s"', RTYPE)
if (
this._context.commits[RTYPE] &&
this._context.commits[RTYPE].length > 0
) {
debug('Release type is now "%s".', RTYPE)
this._rtype = RTYPE
return RTYPE
}
}
} else {
for (const RTYPE of RELEASE_TYPES) {
debug('Testing against release type "%s"', RTYPE)
for (let gitmoji of releaseSchema[RTYPE]) {
gitmoji = emojify(gitmoji)
debug('Testing against gitmoji "%s "', gitmoji)
if (
this._context.commits[gitmoji] &&
this._context.commits[gitmoji].length > 0
) {
debug('Release type is now "%s".', RTYPE)
this._rtype = RTYPE
return RTYPE
}
}
}
}
}
/**
* @param {object} context
*/
async updateContext (context) {
const { lastRelease, nextRelease } = context
const prevTag = lastRelease.gitTag || lastRelease.gitHead
const nextTag = nextRelease.gitTag || nextRelease.gitHead
debug('Previous tag: "%s"', prevTag)
debug('Next tag: "%s"', nextTag)
const { source, owner, repo } = this._context
this._context = {
...this._context,
lastRelease,
nextRelease,
compareUrl: getCmpLink(source, owner, repo, prevTag, nextTag)
}
debug('Release notes context: %o', this._context)
const promises = Object.keys(this._partials).map(async (k, i) => {
const v = this._partials[k] instanceof Promise ? await this._partials[k] : this._partials[k]
hbs.registerPartial(k, v.toString())
debug('Partial registered: "%s" (#%d)', k, i + 1)
})
await Promise.all(promises)
let helperCount = 0
for (const helper of Object.keys(this._helpers)) {
hbs.registerHelper(helper, this._helpers[helper])
debug('Helper registered: "%s" (#%d)', helper, ++helperCount)
}
if (this._template instanceof Promise) {
this._template = await this._template
}
this._renderer = hbs.compile(this._template.toString())
}
toString () {
if (this._semver) {
return this._renderer({
...this._context,
commits: this._context.commits
}, {
allowProtoPropertiesByDefault: true
})
.replace(/\n{3,}/g, '\n\n') // allow a blank line
} else {
return this._renderer({
...this._context,
commits: proxifyCommitsMap(this._context.commits)
}, {
allowProtoPropertiesByDefault: true
})
.replace(/\n{3,}/g, '\n\n') // allow a blank line
}
}
/**
* @return {ReleaseNotes}
*/
static get (context, releaseNotesOptions) {
ReleaseNotes._instance = ReleaseNotes._instance || new ReleaseNotes(context, releaseNotesOptions)
return ReleaseNotes._instance
}
}