forked from zwaldowski/semver-release-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (101 loc) · 3.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
const core = require('@actions/core')
const exec = require('@actions/exec');
const github = require('@actions/github')
const semver = require('semver')
async function getMostRecentRepoTag() {
console.log('Getting list of tags from repository')
const token = core.getInput('github_token', { required: true })
const prefix = core.getInput('prefix', {required: false}) || ""
const octokit = github.getOctokit(token)
const { data: refs } = await octokit.git.listMatchingRefs({
...github.context.repo,
ref: 'tags/'
})
const prx = new RegExp(`^${prefix}`,'g');
const versions = refs
.map(ref => ref.ref.replace(/^refs\/tags\//g, '').replace(prx, ''))
.map(tag => semver.parse(tag, { loose: true }))
.filter(version => version !== null)
.filter(version => (core.getInput('only_release') == 'true' && semver.prerelease(version) === null)
|| (core.getInput('only_release') !== 'true' && semver.prerelease(version) !== null) )
.sort(semver.rcompare)
return versions[0] || semver.parse('0.0.0')
}
async function getMostRecentBranchTag() {
console.log(`Getting list of tags from branch`)
let output = ''
let err = ''
const options = {}
options.listeners = {
stdout: (data) => {
output += data.toString()
},
stderr: (data) => {
err += data.toString()
}
};
options.cwd = '.'
let exitCode = await exec.exec('git', ['fetch', '--tags', '--quiet'], options)
if (exitCode != 0) {
console.log(err)
process.exit(exitCode)
}
exitCode = await exec.exec('git', ['tag', '--no-column', '--merged'], options)
if (exitCode != 0) {
console.log(err)
process.exit(exitCode)
}
const prefix = core.getInput('prefix', {required: false}) || ""
const prx = new RegExp(`^${prefix}`,'g');
const versions = output.split("\n")
.map(tag => semver.parse(tag.replace(prx, ''), { loose: true }))
.filter(version => version !== null)
.sort(semver.rcompare)
return versions[0] || semver.parse('0.0.0')
}
async function mostRecentTag() {
const perBranch = core.getInput('per_branch', { required: false })
if (perBranch === 'true') {
return getMostRecentBranchTag()
} else {
return getMostRecentRepoTag()
}
}
async function createTag(version) {
const token = core.getInput('github_token', { required: true })
const octokit = github.getOctokit(token)
const sha = core.getInput('sha') || github.context.sha
const ref = `refs/tags/${version}`
await octokit.git.createRef({
...github.context.repo,
ref,
sha
})
}
async function run() {
try {
let version = semver.parse(process.env.VERSION)
if (version === null) {
const bump = core.getInput('bump', { required: true })
const latestTag = await mostRecentTag()
const identifier = core.getInput('preid', { required: false }) || ""
console.log(`Using latest tag "${latestTag.toString()}" with identifier "${identifier}"`)
version = semver.inc(latestTag, bump, identifier)
}
const prefix = core.getInput('prefix', {required: false}) || ""
let version_tag = prefix + version.toString()
console.log(`Using tag prefix "${prefix}"`)
core.exportVariable('VERSION', version.toString())
core.setOutput('version', version.toString())
core.setOutput('version_optimistic', `${semver.major(version)}.${semver.minor(version)}`)
core.setOutput('version_tag', version_tag)
console.log(`Result: "${version.toString()}" (tag: "${version_tag}")`)
if (core.getInput('dry_run') !== 'true') {
await createTag(version_tag)
}
}
catch (error) {
core.setFailed(error.message)
}
}
run()