Skip to content

Commit b25d524

Browse files
committed
Add strict-semver option
The option rewrites version numbers in the matrix to adhere strictly to semver. On https://go.dev/dl alpha and beta versions are named like `1.18beta2`. We rewrite them into strict semver syntax: E.g. `1.16` is rewritten `1.16.0` and `1.18beta2` to `1.18.0-beta2`. The options default to being `false`.
1 parent 90b2bf6 commit b25d524

File tree

5 files changed

+84
-13
lines changed

5 files changed

+84
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ patch-level:
5656
description: Include the patch levels on the versions (default is major.minor)
5757
required: false
5858
default: 'false'
59+
strict-semver:
60+
default: Use strict semver version in matrix. E.g. `1.16` -> `1.16.0` and `1.18beta2` -> `1.18.0-beta.2`.
61+
required: false
62+
default: 'false'
5963
```
6064
6165
## Outputs

__tests__/main.test.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ test('test latest version', () => {
2828

2929
test('test version matrix', () => {
3030
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
31-
const m = matrix('1.16', false, false, false, t)
31+
const m = matrix('1.16', false, false, false, false, t)
3232
expect(m).toEqual(['1.16', '1.17', '1.18'])
3333
})
3434

3535
test('test unstable version matrix', () => {
3636
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
37-
const m = matrix('1.16', true, false, false, t)
37+
const m = matrix('1.16', true, false, false, false, t)
3838
expect(m).toEqual([
3939
'1.16beta1',
4040
'1.16rc1',
@@ -52,7 +52,7 @@ test('test unstable version matrix', () => {
5252

5353
test('test patch level version matrix', () => {
5454
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
55-
const m = matrix('1.16', false, true, false, t)
55+
const m = matrix('1.16', false, true, false, false, t)
5656
expect(m).toEqual([
5757
'1.16',
5858
'1.16.1',
@@ -85,7 +85,7 @@ test('test patch level version matrix', () => {
8585

8686
test('test patch level, unstable version matrix', () => {
8787
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
88-
const m = matrix('1.16', true, true, false, t)
88+
const m = matrix('1.16', true, true, false, false, t)
8989
expect(m).toEqual([
9090
'1.16beta1',
9191
'1.16rc1',
@@ -124,15 +124,58 @@ test('test patch level, unstable version matrix', () => {
124124
])
125125
})
126126

127+
test('test patch level, unstable, semver clean version matrix', () => {
128+
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
129+
const m = matrix('1.16', true, true, false, true, t)
130+
expect(m).toEqual([
131+
'1.16.0-beta.1',
132+
'1.16.0-rc.1',
133+
'1.16.0',
134+
'1.16.1',
135+
'1.16.2',
136+
'1.16.3',
137+
'1.16.4',
138+
'1.16.5',
139+
'1.16.6',
140+
'1.16.7',
141+
'1.16.8',
142+
'1.16.9',
143+
'1.16.10',
144+
'1.16.11',
145+
'1.16.12',
146+
'1.16.13',
147+
'1.16.14',
148+
'1.16.15',
149+
'1.17.0-beta.1',
150+
'1.17.0-rc.1',
151+
'1.17.0-rc.2',
152+
'1.17.0',
153+
'1.17.1',
154+
'1.17.2',
155+
'1.17.3',
156+
'1.17.4',
157+
'1.17.5',
158+
'1.17.6',
159+
'1.17.7',
160+
'1.17.8',
161+
'1.18.0-beta.1',
162+
'1.18.0-beta.2',
163+
'1.18.0-rc.1',
164+
'1.18.0'
165+
])
166+
})
167+
127168
test('test patch level with latest patches only', () => {
128169
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
129-
const m = matrix('1.16', false, true, true, t)
170+
const m = matrix('1.16', false, true, true, false, t)
130171
expect(m).toEqual(['1.16.15', '1.17.8', '1.18'])
131172
})
132173

133174
test('test patch level with latest patches only and unstable throws error', () => {
134175
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
135176
expect(() => {
136-
matrix('1.16', true, true, true, t)
137-
}).toThrow('The options "unstable" and "latest-patches-only" cannot be used together')
177+
matrix('1.16', true, true, true, false, t)
178+
}).toThrow(
179+
'The options "unstable" and "latest-patches-only" cannot be used together'
180+
)
138181
})

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ inputs:
2626
description: When patch-level is true, only include the latest patch version of each major.minor version in the matrix. Does nothing if patch-level is false. Cannot be used with unstable.
2727
required: false
2828
default: 'false'
29+
strict-semver:
30+
description: Use strict semver version in matrix. E.g. `1.16` -> `1.16.0` and `1.18beta2` -> `1.18.0-beta.2`.
31+
required: false
32+
default: 'false'
2933
outputs:
3034
go-mod-version:
3135
description: The Go version specified by go.mod

src/go-versions.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ const getVersions = async withUnsupported => {
4141
return result
4242
}
4343

44-
const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => {
44+
const matrix = (
45+
min,
46+
withUnstable,
47+
withPatchLevel,
48+
withLatestPatches,
49+
withStrictSemver,
50+
tags
51+
) => {
4552
const minClean = semverCoerce(min)
4653
if (minClean === null) {
4754
throw new Error(`Minimal version isn't quite right: ${min}`)
@@ -63,6 +70,17 @@ const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => {
6370
const v2 = semverCoerce(v)
6471
return v2 !== null && semverGte(v2, minClean)
6572
})
73+
if (withStrictSemver) {
74+
versions = versions.map(version => {
75+
// Alpha and beta versions on https://go.dev/dl are named like
76+
// `1.18beta2`. We rewrite into strict semver syntax:
77+
// `1.18.0-beta.2`.
78+
version = version.replace(/([^0-9\\.]+)/, '-$1.')
79+
return semverCoerce(version, {
80+
includePrerelease: true
81+
}).version
82+
})
83+
}
6684
if (!withPatchLevel) {
6785
versions = versions.map(version => {
6886
const parts = version.split('.')
@@ -117,11 +135,11 @@ const minimal = versions => {
117135
}
118136

119137
export {
138+
getGoModVersion,
139+
getVersions,
120140
gomod,
121141
latest,
122142
matrix,
123143
minimal,
124-
modulename,
125-
getGoModVersion,
126-
getVersions
144+
modulename
127145
}

src/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as core from '@actions/core'
22
import {
3+
getGoModVersion,
34
getVersions,
45
gomod,
56
latest,
67
matrix,
78
minimal,
8-
modulename,
9-
getGoModVersion
9+
modulename
1010
} from './go-versions.js'
1111

1212
async function run() {
@@ -22,6 +22,7 @@ async function run() {
2222
const withUnstable = core.getBooleanInput('unstable')
2323
const withPatchLevel = core.getBooleanInput('patch-level')
2424
const withLatestPatches = core.getBooleanInput('latest-patches-only')
25+
const withStrictSemver = core.getBooleanInput('strict-semver')
2526
const content = gomod(`${workingDirectory}/go.mod`)
2627
const name = modulename(content)
2728
const goModVersion = getGoModVersion(content)
@@ -31,6 +32,7 @@ async function run() {
3132
withUnstable,
3233
withPatchLevel,
3334
withLatestPatches,
35+
withStrictSemver,
3436
versions
3537
)
3638
const lat = latest(mat)

0 commit comments

Comments
 (0)