This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
65 lines (57 loc) · 2.3 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
const core = require('@actions/core')
const github = require('@actions/github')
const authAction = require('@octokit/auth-action')
const semver = require('semver')
async function run() {
const level = core.getInput('level')
core.info(`Updating version with level "${level}"`)
// Generate minor version from date
const d = new Date()
const year = String(d.getUTCFullYear()).padStart(2, '0')
const month = String(d.getUTCMonth() + 1).padStart(2, '0')
const day = String(d.getUTCDate()).padStart(2, '0')
const minorVersion = Number(`${year}${month}${day}`)
// Get authenticated Octokit client
const actionAuth = authAction.createActionAuth()
const auth = await actionAuth()
const octokit = github.getOctokit(auth.token)
// Get existing tags and create a new tag ensuring we don't conflict
const tags = await octokit.rest.repos
.listTags({
...github.context.repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
})
.then((tags) => tags.data.map((tag) => tag.name).filter((tag) => Boolean(semver.valid(tag))))
const currentVersionStr = semver.sort(tags).at(-1) ?? 'v0.0.0'
const currentVersion = semver.parse(currentVersionStr)
core.info(`Found latest version: ${currentVersionStr}`)
const major = currentVersion.major + Number(level === 'major')
const minor = minorVersion
const patch = level !== 'major' && currentVersion.minor === minorVersion ? currentVersion.patch + 1 : 0
const newVersionParts = [major, minor, patch]
const newVersion = `v${newVersionParts.join('.')}`
const newVersionDNS = `v${newVersionParts.join('-')}`
// Commit the tag to the repo
const newTag = await octokit.rest.git.createTag({
...github.context.repo,
tag: newVersion,
message: `Version ${newVersion}`,
object: process.env.GITHUB_SHA,
type: 'commit',
})
await octokit.rest.git.createRef({
...github.context.repo,
ref: `refs/tags/${newTag.data.tag}`,
sha: newTag.data.sha,
})
core.info(`Tag "${newVersion}" created on commit SHA "${process.env.GITHUB_SHA}"`)
core.notice(`New version: ${newVersion}`)
core.setOutput('version_dns', newVersionDNS)
core.setOutput('version', newVersion)
core.setOutput('major', major)
core.setOutput('minor', minor)
core.setOutput('patch', patch)
}
run().catch((err) => core.setFailed(err.message))