-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
70 lines (56 loc) · 2 KB
/
index.ts
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
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as semver from 'semver'
async function bash(command: string, options?: exec.ExecOptions) {
return await exec.exec('bash', ['-c', command], options)
}
async function main(): Promise<void> {
try {
await bash('git pull --depth=1 --all --tags')
await bash('git config --local user.email "[email protected]"')
await bash('git config --local user.name "GitHub Action"')
const versionTags = await getVersionTags()
const latestForMajor = getLatestForMajor(versionTags)
const tagsToPush = new Array<string>()
for (const [major, versionTag] of latestForMajor) {
await bash(`git tag --force -a -m 'Move v${major} tag to ${versionTag}' v${major} ${versionTag}`)
tagsToPush.push(`v${major}`)
}
await bash(`git push --force --tags origin ${tagsToPush.join(" ")}`)
} catch (error) {
core.setFailed((error as Error)?.message)
}
}
/** @returns an array of tuples of a major number of the version and its latest corresponding version tag. */
function getLatestForMajor(versionTags: string[]): [number, string][] {
const latestForMajor = new Map<number, string>()
versionTags.forEach((tag) => {
if (semver.valid(tag) == null) {
return
}
const version = new semver.SemVer(tag)
if (!latestForMajor.has(version.major)) {
latestForMajor.set(version.major, tag)
return
}
if (version.compare(latestForMajor.get(version.major) as string) > 0) {
latestForMajor.set(version.major, tag)
return
}
})
const ret = new Array<[number, string]>()
latestForMajor.forEach((tag, major) => ret.push([major, tag]))
return ret
}
/** @returns tags from the current git repository matching v*.*.* glob. */
async function getVersionTags(): Promise<string[]> {
const tags = new Array<string>()
const opts: exec.ExecOptions = {
listeners: {
stdline: (data) => tags.push(data),
},
}
await bash("git tag -l 'v*.*.*'", opts)
return tags
}
main()