Skip to content

Commit abaf716

Browse files
authored
Merge pull request #536 from colinjlacy/issue535_latest-patches
Adds option to only show latest patch versions #minor
2 parents 5db2317 + db5f2e1 commit abaf716

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

__tests__/main.test.js

Lines changed: 17 additions & 4 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, t)
31+
const m = matrix('1.16', 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, t)
37+
const m = matrix('1.16', true, 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, t)
55+
const m = matrix('1.16', false, true, 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, t)
88+
const m = matrix('1.16', true, true, false, t)
8989
expect(m).toEqual([
9090
'1.16beta1',
9191
'1.16rc1',
@@ -123,3 +123,16 @@ test('test patch level, unstable version matrix', () => {
123123
'1.18'
124124
])
125125
})
126+
127+
test('test patch level with latest patches only', () => {
128+
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
129+
const m = matrix('1.16', false, true, true, t)
130+
expect(m).toEqual(['1.16.15', '1.17.8', '1.18'])
131+
})
132+
133+
test('test patch level with latest patches only and unstable throws error', () => {
134+
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
135+
expect(() => {
136+
matrix('1.16', true, true, true, t)
137+
}).toThrow('The options "unstable" and "latest-patches-only" cannot be used together')
138+
})

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ inputs:
2222
description: Include the patch levels on the versions (default is major.minor)
2323
required: false
2424
default: 'false'
25+
latest-patches-only:
26+
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.
27+
required: false
28+
default: 'false'
2529
outputs:
2630
go-mod-version:
2731
description: The Go version specified by go.mod

src/go-versions.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ const getVersions = async withUnsupported => {
4141
return result
4242
}
4343

44-
const matrix = (min, withUnstable, withPatchLevel, tags) => {
44+
const matrix = (min, withUnstable, withPatchLevel, withLatestPatches, tags) => {
4545
const minClean = semverCoerce(min)
4646
if (minClean === null) {
4747
throw new Error(`Minimal version isn't quite right: ${min}`)
4848
}
49+
if (withUnstable && withLatestPatches) {
50+
throw new Error(
51+
'The options "unstable" and "latest-patches-only" cannot be used together'
52+
)
53+
}
4954
if (!withUnstable) {
5055
tags = tags.filter(tag => tag.stable === true)
5156
}
@@ -63,6 +68,27 @@ const matrix = (min, withUnstable, withPatchLevel, tags) => {
6368
const parts = version.split('.')
6469
return `${parts[0]}.${parts[1]}`
6570
})
71+
} else if (withLatestPatches) {
72+
// Group by major.minor and keep only the latest patch version
73+
const grouped = {}
74+
versions.forEach(version => {
75+
const parts = version.split('.')
76+
const majorMinor = `${parts[0]}.${parts[1]}`
77+
if (!grouped[majorMinor]) {
78+
grouped[majorMinor] = []
79+
}
80+
grouped[majorMinor].push(version)
81+
})
82+
versions = Object.values(grouped).map(group => {
83+
return group.reduce((acc, val) => {
84+
const a = semverCoerce(acc)
85+
const v = semverCoerce(val)
86+
if (v !== null && a !== null && semverGte(v, a)) {
87+
return val
88+
}
89+
return acc
90+
})
91+
})
6692
}
6793
versions = [...new Set(versions)]
6894
return versions.reverse()

src/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ async function run() {
2121
const withUnsupported = core.getBooleanInput('unsupported')
2222
const withUnstable = core.getBooleanInput('unstable')
2323
const withPatchLevel = core.getBooleanInput('patch-level')
24+
const withLatestPatches = core.getBooleanInput('latest-patches-only')
2425
const content = gomod(`${workingDirectory}/go.mod`)
2526
const name = modulename(content)
2627
const goModVersion = getGoModVersion(content)
2728
const versions = await getVersions(withUnsupported)
28-
const mat = matrix(goModVersion, withUnstable, withPatchLevel, versions)
29+
const mat = matrix(
30+
goModVersion,
31+
withUnstable,
32+
withPatchLevel,
33+
withLatestPatches,
34+
versions
35+
)
2936
const lat = latest(mat)
3037
const min = minimal(mat)
3138

0 commit comments

Comments
 (0)