Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, t)
const m = matrix('1.16', 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, t)
const m = matrix('1.16', true, false, false, t)
expect(m).toEqual([
'1.16beta1',
'1.16rc1',
Expand All @@ -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, t)
const m = matrix('1.16', false, true, false, t)
expect(m).toEqual([
'1.16',
'1.16.1',
Expand Down Expand Up @@ -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, t)
const m = matrix('1.16', true, true, false, t)
expect(m).toEqual([
'1.16beta1',
'1.16rc1',
Expand Down Expand Up @@ -123,3 +123,16 @@ test('test patch level, unstable version matrix', () => {
'1.18'
])
})

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)
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')
})
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
description: Include the patch levels on the versions (default is major.minor)
required: false
default: 'false'
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'
outputs:
go-mod-version:
description: The Go version specified by go.mod
Expand Down
28 changes: 27 additions & 1 deletion src/go-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ const getVersions = async withUnsupported => {
return result
}

const matrix = (min, withUnstable, withPatchLevel, tags) => {
const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => {
const minClean = semverCoerce(min)
if (minClean === null) {
throw new Error(`Minimal version isn't quite right: ${min}`)
}
if (withUnstable && withLatestPatches) {
throw new Error(
'The options "unstable" and "latest-patches-only" cannot be used together'
)
}
if (!withUnstable) {
tags = tags.filter(tag => tag.stable === true)
}
Expand All @@ -63,6 +68,27 @@ const matrix = (min, withUnstable, withPatchLevel, tags) => {
const parts = version.split('.')
return `${parts[0]}.${parts[1]}`
})
} else if (withLatestPatches) {
// Group by major.minor and keep only the latest patch version
const grouped = {}
versions.forEach(version => {
const parts = version.split('.')
const majorMinor = `${parts[0]}.${parts[1]}`
if (!grouped[majorMinor]) {
grouped[majorMinor] = []
}
grouped[majorMinor].push(version)
})
versions = Object.values(grouped).map(group => {
return group.reduce((acc, val) => {
const a = semverCoerce(acc)
const v = semverCoerce(val)
if (v !== null && a !== null && semverGte(v, a)) {
return val
}
return acc
})
})
}
versions = [...new Set(versions)]
return versions.reverse()
Expand Down
9 changes: 8 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ async function run() {
const withUnsupported = core.getBooleanInput('unsupported')
const withUnstable = core.getBooleanInput('unstable')
const withPatchLevel = core.getBooleanInput('patch-level')
const withLatestPatches = core.getBooleanInput('latest-patches-only')
const content = gomod(`${workingDirectory}/go.mod`)
const name = modulename(content)
const goModVersion = getGoModVersion(content)
const versions = await getVersions(withUnsupported)
const mat = matrix(goModVersion, withUnstable, withPatchLevel, versions)
const mat = matrix(
goModVersion,
withUnstable,
withPatchLevel,
withLatestPatches,
versions
)
const lat = latest(mat)
const min = minimal(mat)

Expand Down