forked from Spatial-Quotient/pr-semver-bump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (117 loc) · 4.2 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
const core = require('@actions/core')
const github = require('@actions/github')
const semver = require('semver')
const { getConfig } = require('./config')
const {
extractPRNumber,
searchPRByCommit,
fetchPR,
getReleaseType,
getReleaseNotes,
} = require('./pr')
const { createRelease, getCurrentVersion } = require('./version')
// Returns true if the current context looks like an active PR.
function isActivePR() {
return (
github.context.eventName === 'pull_request'
&& github.context.payload.pull_request !== undefined
)
}
// Returns true if the current context looks like a merge commit.
function isMergeCommit() {
return (
github.context.eventName === 'push'
&& github.context.payload.head_commit !== undefined
)
}
// Ensures that the currently active PR contains the required release metadata.
async function validateActivePR(config) {
if (!isActivePR()) {
core.warning(
"in 'validate' mode, but this doesn't look like an active PR event (is your workflow misconfigured?)",
)
return
}
let pr
try {
pr = await fetchPR(github.context.payload.pull_request.number, config)
} catch (e) {
core.setFailed(e.message)
return
}
let releaseType
let releaseNotes
try {
releaseType = getReleaseType(pr, config)
releaseNotes = getReleaseNotes(pr, config)
} catch (e) {
core.setFailed(`PR validation failed: ${e.message}`)
return
}
const currentVersion = await getCurrentVersion(config)
const newVersion = semver.inc(currentVersion, releaseType)
core.info(`current version: ${config.v}${currentVersion}`)
core.info(`next version: ${config.v}${newVersion}`)
core.info(`release notes:\n${releaseNotes}`)
core.setOutput('old-version', `${config.v}${currentVersion}`)
core.setOutput('version', `${config.v}${newVersion}`)
core.setOutput('release-notes', releaseNotes)
}
// Increments the version according to the release type and tags a new version with release notes.
async function bumpAndTagNewVersion(config) {
if (!isMergeCommit()) {
core.warning(
"in 'bump' mode, but this doesn't look like a PR merge commit event (is your workflow misconfigured?)",
)
return
}
const num = extractPRNumber(github.context.payload.head_commit.message)
let pr
if (num == null) {
core.info(
'Unable to determine PR from commit msg, searching for PR by SHA',
)
// Try to search the commit sha for the PR number
pr = await searchPRByCommit(process.env.GITHUB_SHA, config)
if (pr == null) {
// Don't want to fail the job if some other commit comes in, but let's warn about it.
// Might be a good point for configuration in the future.
core.warning(
"head commit doesn't look like a PR merge, skipping version bumping and tagging",
)
return
}
} else {
pr = await fetchPR(num, config)
}
core.info(`Processing version bump for PR request #${pr.number}`)
const releaseType = getReleaseType(pr, config)
// If the release is skipped, we do not create a new tag.
const currentVersion = await getCurrentVersion(config)
if (releaseType !== 'skip') {
const releaseNotes = getReleaseNotes(pr, config)
const newVersion = semver.inc(currentVersion, releaseType)
const newTag = await createRelease(newVersion, releaseNotes, config)
core.info(
`Created release tag ${newTag} with the following release notes:\n${releaseNotes}\n`,
)
core.setOutput('version', newTag)
core.setOutput('release-notes', releaseNotes)
}
core.setOutput('old-version', `${config.v}${currentVersion}`)
core.setOutput('skipped', releaseType === 'skip')
}
async function run() {
try {
const config = getConfig()
if (config.mode === 'validate') {
await validateActivePR(config)
} else if (config.mode === 'bump') {
await bumpAndTagNewVersion(config)
}
} catch (e) {
core.info(e.stack)
core.setFailed(`unexpected error: ${e.message}`)
}
}
run()