/
index.js
225 lines (178 loc) · 7.57 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
const core = require('@actions/core')
const conventionalRecommendedBump = require('conventional-recommended-bump')
const path = require('path')
const getVersioning = require('./version')
const git = require('./helpers/git')
const changelog = require('./helpers/generateChangelog')
const requireScript = require('./helpers/requireScript')
async function handleVersioningByExtension(ext, file, versionPath, releaseType) {
const versioning = getVersioning(ext)
// File type not supported
if (versioning === null) {
throw new Error(`File extension "${ext}" from file "${file}" is not supported`)
}
versioning.init(path.resolve(process.cwd(), file), versionPath)
// Bump the version in the package.json
await versioning.bump(releaseType)
return versioning
}
async function run() {
try {
let gitCommitMessage = core.getInput('git-message')
const gitUserName = core.getInput('git-user-name')
const gitUserEmail = core.getInput('git-user-email')
const gitPush = core.getBooleanInput('git-push')
const tagPrefix = core.getInput('tag-prefix')
const preset = !core.getInput('config-file-path') ? core.getInput('preset') : ''
const preCommitFile = core.getInput('pre-commit')
const outputFile = core.getInput('output-file')
const releaseCount = core.getInput('release-count')
const versionFile = core.getInput('version-file')
const versionPath = core.getInput('version-path')
const skipGitPull = core.getBooleanInput('skip-git-pull')
const skipVersionFile = core.getBooleanInput('skip-version-file')
const skipCommit = core.getBooleanInput('skip-commit')
const skipEmptyRelease = core.getBooleanInput('skip-on-empty')
const conventionalConfigFile = core.getInput('config-file-path')
const preChangelogGenerationFile = core.getInput('pre-changelog-generation')
const gitUrl = core.getInput('git-url')
const skipCi = core.getBooleanInput('skip-ci')
if (skipCi) {
gitCommitMessage += " [skip ci]"
}
core.info(`Using "${preset}" preset`)
core.info(`Using "${gitCommitMessage}" as commit message`)
core.info(`Using "${gitUserName}" as git user.name`)
core.info(`Using "${gitUserEmail}" as git user.email`)
core.info(`Using "${releaseCount}" release count`)
core.info(`Using "${versionFile}" as version file`)
core.info(`Using "${versionPath}" as version path`)
core.info(`Using "${tagPrefix}" as tag prefix`)
core.info(`Using "${outputFile}" as output file`)
core.info(`Using "${conventionalConfigFile}" as config file`)
core.info(`Using "${gitUrl}" as gitUrl`)
if (preCommitFile) {
core.info(`Using "${preCommitFile}" as pre-commit script`)
}
if (preChangelogGenerationFile) {
core.info(`Using "${preChangelogGenerationFile}" as pre-changelog-generation script`)
}
core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`)
core.info(`Skipping the update of the version file is "${skipVersionFile ? 'enabled' : 'disabled'}"`)
if (!skipGitPull) {
core.info('Pull to make sure we have the full git history')
await git.pull()
}
const config = conventionalConfigFile && requireScript(conventionalConfigFile)
conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => {
if (error) {
core.setFailed(error.message)
return
}
core.info(`Recommended release type: ${recommendation.releaseType}`)
// If we have a reason also log it
if (recommendation.reason) {
core.info(`Because: ${recommendation.reason}`)
}
let newVersion
// If skipVersionFile or skipCommit is true we use GIT to determine the new version because
// skipVersionFile can mean there is no version file and skipCommit can mean that the user
// is only interested in tags
if (skipVersionFile || skipCommit) {
core.info('Using GIT to determine the new version')
const versioning = await handleVersioningByExtension(
'git',
versionFile,
versionPath,
recommendation.releaseType,
)
newVersion = versioning.newVersion
} else {
const files = versionFile.split(',').map((f) => f.trim())
core.info(`Files to bump: ${files.join(', ')}`)
const versioning = await Promise.all(
files.map((file) => {
const fileExtension = file.split('.').pop()
core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`)
return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType)
}),
)
newVersion = versioning[0].newVersion
}
let gitTag = `${tagPrefix}${newVersion}`
if (preChangelogGenerationFile) {
const preChangelogGenerationScript = requireScript(preChangelogGenerationFile)
// Double check if we want to update / do something with the tag
if (preChangelogGenerationScript && preChangelogGenerationScript.preTagGeneration) {
const modifiedTag = await preChangelogGenerationScript.preTagGeneration(gitTag)
if (modifiedTag) {
core.info(`Using modified tag "${modifiedTag}"`)
gitTag = modifiedTag
}
}
}
// Generate the string changelog
const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1, config)
core.info('Changelog generated')
core.info(stringChangelog)
// Removes the version number from the changelog
const cleanChangelog = stringChangelog.split('\n').slice(3).join('\n').trim()
if (skipEmptyRelease && cleanChangelog === '') {
core.info('Generated changelog is empty and skip-on-empty has been activated so we skip this step')
core.setOutput('skipped', 'true')
return
}
core.info(`New version: ${newVersion}`)
// If output file === 'false' we don't write it to file
if (outputFile !== 'false') {
// Generate the changelog
await changelog.generateFileChangelog(tagPrefix, preset, newVersion, outputFile, releaseCount, config)
}
if (!skipCommit) {
// Add changed files to git
if (preCommitFile) {
const preCommitScript = requireScript(preCommitFile)
// Double check if the file exists and the export exists
if (preCommitScript && preCommitScript.preCommit) {
await preCommitScript.preCommit({
tag: gitTag,
version: newVersion,
})
}
}
await git.add('.')
await git.commit(gitCommitMessage.replace('{version}', gitTag))
}
// Create the new tag
await git.createTag(gitTag)
if (gitPush) {
try {
core.info('Push all changes')
await git.push()
} catch (error) {
console.error(error)
core.setFailed(error)
return
}
} else {
core.info('We not going to push the GIT changes')
}
// Set outputs so other actions (for example actions/create-release) can use it
core.setOutput('changelog', stringChangelog)
core.setOutput('clean_changelog', cleanChangelog)
core.setOutput('version', newVersion)
core.setOutput('tag', gitTag)
core.setOutput('skipped', 'false')
try {
// If we are running in test mode we use this to validate everything still runs
git.testHistory()
} catch (error) {
console.error(error)
core.setFailed(error)
}
})
} catch (error) {
core.setFailed(error)
}
}
run()