diff --git a/README.md b/README.md index fa5073b..0ad6d57 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ latest-patches-only: 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. required: false default: 'false' +strict-semver: + default: Use strict semver version. E.g. `1.16` -> `1.16.0` and `1.18beta2` -> `1.18.0-beta.2`. + required: false + default: 'false' ``` ## Outputs diff --git a/__tests__/main.test.js b/__tests__/main.test.js index e62dec0..ef41967 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -28,13 +28,13 @@ test('test latest version', () => { test('test version matrix', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) - const m = matrix('1.16', false, false, false, t) + const m = matrix('1.16', false, false, false, false, t) expect(m).toEqual(['1.16', '1.17', '1.18']) }) test('test unstable version matrix', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) - const m = matrix('1.16', true, false, false, t) + const m = matrix('1.16', true, false, false, false, t) expect(m).toEqual([ '1.16beta1', '1.16rc1', @@ -52,7 +52,7 @@ test('test unstable version matrix', () => { test('test patch level version matrix', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) - const m = matrix('1.16', false, true, false, t) + const m = matrix('1.16', false, true, false, false, t) expect(m).toEqual([ '1.16', '1.16.1', @@ -85,7 +85,7 @@ test('test patch level version matrix', () => { test('test patch level, unstable version matrix', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) - const m = matrix('1.16', true, true, false, t) + const m = matrix('1.16', true, true, false, false, t) expect(m).toEqual([ '1.16beta1', '1.16rc1', @@ -124,15 +124,58 @@ test('test patch level, unstable version matrix', () => { ]) }) +test('test patch level, unstable, semver clean version matrix', () => { + const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) + const m = matrix('1.16', true, true, false, true, t) + expect(m).toEqual([ + '1.16.0-beta.1', + '1.16.0-rc.1', + '1.16.0', + '1.16.1', + '1.16.2', + '1.16.3', + '1.16.4', + '1.16.5', + '1.16.6', + '1.16.7', + '1.16.8', + '1.16.9', + '1.16.10', + '1.16.11', + '1.16.12', + '1.16.13', + '1.16.14', + '1.16.15', + '1.17.0-beta.1', + '1.17.0-rc.1', + '1.17.0-rc.2', + '1.17.0', + '1.17.1', + '1.17.2', + '1.17.3', + '1.17.4', + '1.17.5', + '1.17.6', + '1.17.7', + '1.17.8', + '1.18.0-beta.1', + '1.18.0-beta.2', + '1.18.0-rc.1', + '1.18.0' + ]) +}) + test('test patch level with latest patches only', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) - const m = matrix('1.16', false, true, true, t) + const m = matrix('1.16', false, true, true, false, t) expect(m).toEqual(['1.16.15', '1.17.8', '1.18']) }) test('test patch level with latest patches only and unstable throws error', () => { const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8')) expect(() => { - matrix('1.16', true, true, true, t) - }).toThrow('The options "unstable" and "latest-patches-only" cannot be used together') + matrix('1.16', true, true, true, false, t) + }).toThrow( + 'The options "unstable" and "latest-patches-only" cannot be used together' + ) }) diff --git a/action.yml b/action.yml index 743e080..d57849a 100644 --- a/action.yml +++ b/action.yml @@ -26,6 +26,10 @@ inputs: 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. required: false default: 'false' + strict-semver: + description: Use strict semver version in matrix. E.g. `1.16` -> `1.16.0` and `1.18beta2` -> `1.18.0-beta.2`. + required: false + default: 'false' outputs: go-mod-version: description: The Go version specified by go.mod diff --git a/src/go-versions.js b/src/go-versions.js index 9642341..4ef71e7 100644 --- a/src/go-versions.js +++ b/src/go-versions.js @@ -41,7 +41,14 @@ const getVersions = async withUnsupported => { return result } -const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => { +const matrix = ( + min, + withUnstable, + withPatchLevel, + withLatestPatches, + withStrictSemver, + tags +) => { const minClean = semverCoerce(min) if (minClean === null) { throw new Error(`Minimal version isn't quite right: ${min}`) @@ -63,6 +70,17 @@ const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => { const v2 = semverCoerce(v) return v2 !== null && semverGte(v2, minClean) }) + if (withStrictSemver) { + versions = versions.map(version => { + // Alpha and beta versions on https://go.dev/dl are named like + // `1.18beta2`. We rewrite into strict semver syntax: + // `1.18.0-beta.2`. + version = version.replace(/([^0-9\\.]+)/, '-$1.') + return semverCoerce(version, { + includePrerelease: true + }).version + }) + } if (!withPatchLevel) { versions = versions.map(version => { const parts = version.split('.') @@ -117,11 +135,11 @@ const minimal = versions => { } export { + getGoModVersion, + getVersions, gomod, latest, matrix, minimal, - modulename, - getGoModVersion, - getVersions + modulename } diff --git a/src/main.js b/src/main.js index 65a5429..b3c5735 100644 --- a/src/main.js +++ b/src/main.js @@ -1,12 +1,12 @@ import * as core from '@actions/core' import { + getGoModVersion, getVersions, gomod, latest, matrix, minimal, - modulename, - getGoModVersion + modulename } from './go-versions.js' async function run() { @@ -22,6 +22,7 @@ async function run() { const withUnstable = core.getBooleanInput('unstable') const withPatchLevel = core.getBooleanInput('patch-level') const withLatestPatches = core.getBooleanInput('latest-patches-only') + const withStrictSemver = core.getBooleanInput('strict-semver') const content = gomod(`${workingDirectory}/go.mod`) const name = modulename(content) const goModVersion = getGoModVersion(content) @@ -31,6 +32,7 @@ async function run() { withUnstable, withPatchLevel, withLatestPatches, + withStrictSemver, versions ) const lat = latest(mat)