-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.mjs
267 lines (234 loc) · 9.57 KB
/
main.mjs
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
#!/usr/bin/env zx
let divider = "------"
let dateformat = process.env.dateformat || "%Y-%m-%d %H:%M:%S"
let prettygitformat = process.env.prettygitformat || "%s (%cn)"
let ticketMessageFormat = process.env.ticket_message_format || "(%ticket) %message"
let manualPreviousCommit = process.env.manual_previous_commit
let custom_branch_name = process.env.custom_branch_name || ""
let changelog = {
text: '',
convtext: '',
markdown: ''
}
let sections = {
features: [],
fixes: [],
maintenance: [],
format: [],
tests: [],
refactors: [],
documentation: [],
other: []
}
async function fetchCommits() {
let numTags = await quiet($`git tag -l | wc -l`)
if(manualPreviousCommit) {
let output = await quiet($`git log --no-merges --pretty=format:${prettygitformat} --date=format:${dateformat} ${manualPreviousCommit}..`)
return output.stdout.split('\n')
} else if(numTags > 1) {
let latest_tag_commit = await quiet($`git rev-list --tags --skip=0 --max-count=1`)
let latest_tag = await quiet($`git describe --abbrev=0 --tags ${latest_tag_commit}`)
let previous_tag_commit = await quiet($`git rev-list --tags --skip=1 --max-count=1`)
let previous_tag = await quiet($`git describe --abbrev=0 --tags ${previous_tag_commit}`)
let output = await quiet($`git log --no-merges --pretty=format:${prettygitformat} --date=format:${dateformat} ${latest_tag}...${previous_tag}`)
return output.stdout.split('\n')
} else {
let output = await quiet($`git log --no-merges --pretty=format:${prettygitformat} --date=format:${dateformat}`)
return output.stdout.split('\n')
}
}
function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
function formatCommit(re, commit, list) {
let matched = commit.match(re)
let ticket = matched.groups.ticket1 || matched.groups.ticket2
let message = capitalizeFirstLetter(matched.groups.message.trim())
if(ticket) {
let line = ticketMessageFormat
.replace("%ticket", ticket)
.replace("%message", message)
list.push(line)
} else {
list.push(message)
}
}
function fillSections(commits) {
let featureRegEx = /^(feat\((?<ticket1>.*)\)|feat|feature|feature\((?<ticket2>.*)\))\s?:(?<message>.*)$/
let fixRegEx = /^(fix\((?<ticket1>.*)\)|fix|bugfix|bugfix\((?<ticket2>.*)\))\s?:(?<message>.*)$/
let maintenanceRegEx = /^(chore\((?<ticket1>.*)\)|chore|build|build\((?<ticket2>.*)\))\s?:(?<message>.*)$/
let refactorRegEx = /^(refactor\((?<ticket1>.*)\)|refactor)\s?:(?<message>.*)$/
let formatRegEx = /^(format\((?<ticket1>.*)\)|format)\s?:(?<message>.*)$/
let testRegEx = /^(test\((?<ticket1>.*)\)|test|tests|tests\((?<ticket2>.*)\))\s?:(?<message>.*)$/
let docsRegEx = /^(docs\((?<ticket1>.*)\)|docs|doc|doc\((?<ticket2>.*)\))\s?:(?<message>.*)$/
/*
// For quick testing
commits = [
"feat(JRA-123): lollert (Johnny)",
"fix: something weird happened (Johnny)",
"fix : lolollld (Johnny)",
"build: something weird happened (Johnny)",
"test: something weird happened (Johnny)",
"refactor: something weird happened (Johnny)"
]
*/
commits.forEach(commit => {
if(featureRegEx.test(commit)) {
formatCommit(featureRegEx, commit, sections.features)
} else if(fixRegEx.test(commit)) {
formatCommit(fixRegEx, commit, sections.fixes)
} else if(maintenanceRegEx.test(commit)) {
formatCommit(maintenanceRegEx, commit, sections.maintenance)
} else if(refactorRegEx.test(commit)) {
formatCommit(refactorRegEx, commit, sections.refactors)
} else if(formatRegEx.test(commit)) {
formatCommit(formatRegEx, commit, sections.format)
} else if(testRegEx.test(commit)) {
formatCommit(testRegEx, commit, sections.tests)
} else if(docsRegEx.test(commit)) {
formatCommit(docsRegEx, commit, sections.documentation)
} else {
sections.other.push(capitalizeFirstLetter(commit))
}
});
}
async function buildChangelog(commits) {
let list = []
try {
let latest_tag = await getTitle()
if(latest_tag) {
list.push(latest_tag)
list.push(divider)
list.push('')
}
} catch {
// empty
}
commits.forEach((commit) => {
list.push(capitalizeFirstLetter(commit))
})
let text = list.join('\n')
changelog.text = text
}
async function buildConventionalChangelog() {
let list = []
try {
let latest_tag = await getTitle()
if(latest_tag) {
list.push(latest_tag)
list.push(divider)
list.push('')
}
} catch {
// empty
}
let featureTitle = process.env.custom_features_name || "Features"
let fixTitle = process.env.custom_bugfixes_name || "Bugfixes"
let maintenanceTitle = process.env.custom_maintenance_name || "Maintenance"
let refactorTitle = process.env.custom_refactor_name || "Refactors"
let formatTitle = process.env.custom_format_name || "Formatting"
let testsTitle = process.env.custom_test_name || "Tests"
let docsTitle = process.env.custom_documentation_name || "Documentation"
let otherTitle = process.env.custom_other_name || "Other changes"
if(sections.features.length > 0) addSection(list, sections.features, featureTitle)
if(sections.fixes.length > 0) addSection(list, sections.fixes, fixTitle)
if(sections.maintenance.length > 0) addSection(list, sections.maintenance, maintenanceTitle)
if(sections.refactors.length > 0) addSection(list, sections.refactors, refactorTitle)
if(sections.format.length > 0) addSection(list, sections.format, formatTitle)
if(sections.tests.length > 0) addSection(list, sections.tests, testsTitle)
if(sections.other.length > 0) addSection(list, sections.other, otherTitle)
if (list[list.length - 1] === '') {
list.pop()
}
let text = list.join('\n')
changelog.convtext = text
}
async function buildMarkdown() {
let list = []
try {
let latest_tag = await getTitle()
if(latest_tag) {
list.push("#" + latest_tag)
list.push(divider)
list.push('')
}
} catch {
// empty
}
let featureTitle = process.env.custom_features_name || "## 🎉 Features"
let fixTitle = process.env.custom_bugfixes_name || "## 🐛 Bugfixes"
let maintenanceTitle = process.env.custom_maintenance_name || "## 🔨Maintenance"
let refactorTitle = process.env.custom_refactor_name || "## 🧹 Refactors"
let formatTitle = process.env.custom_format_name || "## 📋 Formatting"
let testsTitle = process.env.custom_test_name || "## 📝 Tests"
let docsTitle = process.env.custom_documentation_name || "## 📄 Documentation"
let otherTitle = process.env.custom_other_name || "## 📚 Other changes"
if(sections.features.length > 0) addMarkdownSection(list, sections.features, featureTitle)
if(sections.fixes.length > 0) addMarkdownSection(list, sections.fixes, fixTitle)
if(sections.maintenance.length > 0) addMarkdownSection(list, sections.maintenance, maintenanceTitle)
if(sections.refactors.length > 0) addMarkdownSection(list, sections.refactors, refactorTitle)
if(sections.format.length > 0) addMarkdownSection(list, sections.format, formatTitle)
if(sections.tests.length > 0) addMarkdownSection(list, sections.tests, testsTitle)
if(sections.other.length > 0) addMarkdownSection(list, sections.other, otherTitle)
if (list[list.length - 1] === '') {
list.pop()
}
let md = list.join('\n')
changelog.markdown = md
}
function addSection(list, section, title) {
list.push(title)
list.push(divider)
section.forEach((c) => list.push(c))
list.push('')
}
function addMarkdownSection(list, section, title) {
list.push(title)
list.push(divider)
section.forEach((c) => list.push(" - " + c))
list.push('')
}
async function getTitle() {
let latest_tag_commit = await quiet($`git rev-list --tags --skip=0 --max-count=1`)
let latest_tag = await quiet($`git describe --abbrev=0 --tags ${latest_tag_commit}`)
return latest_tag.toString().trim()
}
async function fetchHistory() {
let numberOfLocalCommits = await quiet($`git rev-list --count --all`)
if(numberOfLocalCommits < 2) {
try {
await nothrow($`git fetch --unshallow`)
} catch(e) {
console.log('Failed fetching history...')
console.log(e.toString())
}
}
}
async function fetchTags() {
try {
if(custom_branch_name == "") {
await nothrow($`git fetch --tags`)
} else {
await nothrow($`git fetch --tags origin refs/heads/${custom_branch_name}`)
}
} catch(e) {
console.log('Failed fetching tags...')
console.log(e.toString())
}
}
// Let's try fetching history if depth is equal to 1
await fetchHistory()
// Let's fetch all tags as the default git clone step, doesn't do it
await fetchTags()
let commits = await fetchCommits();
fillSections(commits)
await buildChangelog(commits)
await buildMarkdown()
await buildConventionalChangelog()
console.log(changelog.text)
console.log(changelog.markdown)
// Set environment variable for bitrise
await nothrow($`envman add --key COMMIT_CHANGELOG_TEXT --value ${changelog.text}`)
await nothrow($`envman add --key COMMIT_CHANGELOG --value ${changelog.convtext}`)
await nothrow($`envman add --key COMMIT_CHANGELOG_MARKDOWN --value ${changelog.markdown}`)
await nothrow($`envman add --key COMMIT_CHANGELOG_SECTIONS --value ${JSON.stringify(sections)}`)