Skip to content

Commit

Permalink
Add more configurations
Browse files Browse the repository at this point in the history
- Add support for including patch levels in the versions (the default is still to only get major.minor)
- Add support for including unstable versions (betas, release candidates)
- Add support for only including the supported versions of Go

We also add `go-mod-version` as a new output variable.

In the default case this is identical to the `minimal` version. But if you add any of the new options they could differ.
  • Loading branch information
arnested committed Jun 6, 2022
1 parent fbb1c75 commit e30cd6d
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 45 deletions.
43 changes: 38 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,47 @@ jobs:
- run: |
npm run package
- uses: ./
id: go-version
id: go-version-1
with:
working-directory: ./__tests__/testdata
- run: |
test '${{ steps.go-version.outputs.module }}' == 'example.com/go/testmodule'
test '${{ steps.go-version-1.outputs.module }}' == 'example.com/go/testmodule'
- run: |
test '${{ steps.go-version.outputs.minimal }}' == '1.13'
test '${{ steps.go-version-1.outputs.go-mod-version }}' == '1.16'
- run: |
test '${{ steps.go-version.outputs.latest }}' == '1.18'
test '${{ steps.go-version-1.outputs.minimal }}' == '1.16'
- run: |
test '${{ steps.go-version.outputs.matrix }}' == '["1.13","1.14","1.15","1.16","1.17","1.18"]'
test '${{ steps.go-version-1.outputs.latest }}' == '1.18'
- run: |
test '${{ steps.go-version-1.outputs.matrix }}' == '["1.16","1.17","1.18"]'
- uses: ./
id: go-version-2
with:
working-directory: ./__tests__/testdata
unsupported: false
- run: |
test '${{ steps.go-version-2.outputs.module }}' == 'example.com/go/testmodule'
- run: |
test '${{ steps.go-version-2.outputs.go-mod-version }}' == '1.16'
- run: |
test '${{ steps.go-version-2.outputs.minimal }}' == '1.17'
- run: |
test '${{ steps.go-version-2.outputs.latest }}' == '1.18'
- run: |
test '${{ steps.go-version-2.outputs.matrix }}' == '["1.17","1.18"]'
- uses: ./
id: go-version-3
with:
working-directory: ./__tests__/testdata
unsupported: false
patch-level: true
- run: |
test '${{ steps.go-version-3.outputs.module }}' == 'example.com/go/testmodule'
- run: |
test '${{ steps.go-version-3.outputs.go-mod-version }}' == '1.16'
- run: |
test '${{ steps.go-version-3.outputs.minimal }}' == '1.17.11'
- run: |
test '${{ steps.go-version-3.outputs.latest }}' == '1.18.3'
- run: |
test '${{ steps.go-version-3.outputs.matrix }}' == '["1.17.11","1.18.3"]'
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,33 @@ working-directory:
description: Working direcory where you go.mod file is located
required: false
default: .
unsupported:
description: Include unsupported versions of Go
required: false
default: "true"
unstable:
description: Include unstable versions of Go (beta, release candidates)
required: false
default: "false"
patch-level:
description: Include the patch levels on the versions (default is major.minor)
required: false
default: "false"
```
## Outputs
```yaml
latest:
description: The latest go version
module:
description: The go module path (as specified by go.mod)
go-mod-version:
description: The go version specified by go.mod
minimal:
description: The minial go version (as specified by go.mod)
description: The minial go version
matrix:
description: An (stringified) array of go versions from the minimum supported version to the latest released version
module:
description: The go module name (as specified by go.mod)
description: An (stringified) array of go versions from the minimal supported version to the latest released version
latest:
description: The latest go version
```
## Examples
Expand Down
118 changes: 110 additions & 8 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,126 @@
import fs from 'fs'
import {gomod, latest, matrix, minimal, modulename} from '../src/go-versions'
import {
getGoModVersion,
getVersions,
gomod,
latest,
matrix,
minimal,
modulename
} from '../src/go-versions'

test('test module name', () => {
const content = gomod('__tests__/testdata/go.mod')
expect(modulename(content)).toEqual('example.com/go/testmodule')
})

test('test minimal version', () => {
test('test go mod version', () => {
const content = gomod('__tests__/testdata/go.mod')
expect(minimal(content)).toEqual('1.13')
expect(getGoModVersion(content)).toEqual('1.16')
})

test('test minimal version', () => {
expect(minimal(['1.16', '1.17', '1.18'])).toEqual('1.16')
})

test('test latest version', () => {
expect(latest(['1.13', '1.14', '1.15', '1.16', '1.17', '1.18'])).toEqual(
'1.18'
)
expect(latest(['1.16', '1.17', '1.18'])).toEqual('1.18')
})

test('test version matrix', () => {
const t = JSON.parse(fs.readFileSync('__tests__/testdata/dl.json', 'utf8'))
const m = matrix('1.13', t)
expect(m).toEqual(['1.13', '1.14', '1.15', '1.16', '1.17', '1.18'])
const m = matrix('1.16', 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)
expect(m).toEqual([
'1.16beta1',
'1.16rc1',
'1.16',
'1.17beta1',
'1.17rc1',
'1.17rc2',
'1.17',
'1.18beta1',
'1.18beta2',
'1.18rc1',
'1.18'
])
})

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)
expect(m).toEqual([
'1.16',
'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',
'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'
])
})

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)
expect(m).toEqual([
'1.16beta1',
'1.16rc1',
'1.16',
'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.17beta1',
'1.17rc1',
'1.17rc2',
'1.17',
'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.18beta1',
'1.18beta2',
'1.18rc1',
'1.18'
])
})
2 changes: 1 addition & 1 deletion __tests__/testdata/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module example.com/go/testmodule

go 1.13
go 1.16

require (
github.com/jimmyfrasche/autoreadme v0.0.0-20180503232641-58e67811d607
Expand Down
16 changes: 15 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ inputs:
description: Working direcory
required: false
default: .
unstable:
description: With unstable versions
required: false
default: "false"
unsupported:
description: With no longer supported versions
required: false
default: "true"
patch-level:
description: With patch level of the versions
required: false
default: "false"
outputs:
go-mod-version:
description: The minial go version (as specified by go.mod)
latest:
description: The latest go version
minimal:
description: The minial go version (as specified by go.mod)
description: The minial go version
matrix:
description: An (stringified) array of go versions from the minimal supported version to the latest released version
module:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

Binary file modified docs/job-summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 60 additions & 11 deletions src/go-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import fs from 'fs'
import fetch from 'node-fetch'
import semverCoerce from 'semver/functions/coerce'
import semverGte from 'semver/functions/gte'
import semverLte from 'semver/functions/lte'

interface Version {
stable: boolean
version: string
}

const gomod = (path: string): string => {
return fs.readFileSync(path, 'utf8')
}

const minimal = (content: string): string => {
const r = /\ngo ([0-9\\.]*)\n/s
const getGoModVersion = (content: string): string => {
const r = /\ngo ([^\n]*)\n/s

const matches = r.exec(content)

Expand All @@ -35,8 +37,14 @@ const modulename = (content: string): string => {
return matches[1]
}

const getVersions = async (): Promise<Version[]> => {
const response = await fetch('https://go.dev/dl/?mode=json&include=all')
const getVersions = async (withUnsupported: boolean): Promise<Version[]> => {
let url = 'https://go.dev/dl/?mode=json'

if (withUnsupported) {
url += '&include=all'
}

const response = await fetch(url)

if (!response.ok) {
throw new Error(
Expand All @@ -49,23 +57,43 @@ const getVersions = async (): Promise<Version[]> => {
return result
}

const matrix = (min: string, tags: Version[]): string[] => {
const matrix = (
min: string,
withUnstable: boolean,
withPatchLevel: boolean,
tags: Version[]
): string[] => {
const minClean = semverCoerce(min)
if (minClean === null) {
throw new Error(`Minimal version isn't quite right: ${min}`)
}

const releaseTags = tags.filter(tag =>
tag.version.match(/^go[0-9]+\.[0-9]+$/)
)
if (!withUnstable) {
tags = tags.filter(tag => tag.stable === true)
}

const r = /^go(.*)$/

const releaseVersions = releaseTags.map(tag => tag.version.substr(2))
let versions: string[] = tags.map(tag => {
const matches = r.exec(tag.version)

const versions = releaseVersions.filter(v => {
return matches ? matches[1] : tag.version
})

versions = versions.filter(v => {
const v2 = semverCoerce(v)
return v2 !== null && semverGte(v2, minClean)
})

if (!withPatchLevel) {
versions = versions.map(version => {
const parts = version.split('.')
return `${parts[0]}.${parts[1]}`
})
}

versions = [...new Set(versions)]

return versions.reverse()
}

Expand All @@ -82,4 +110,25 @@ const latest = (versions: string[]): string => {
})
}

export {gomod, latest, matrix, minimal, modulename, getVersions}
const minimal = (versions: string[]): string => {
return versions.reduce((acc, val) => {
const a = semverCoerce(acc)
const v = semverCoerce(val)

if (v !== null && a !== null && semverLte(v, a)) {
return val
}

return acc
})
}

export {
gomod,
latest,
matrix,
minimal,
modulename,
getGoModVersion,
getVersions
}
Loading

0 comments on commit e30cd6d

Please sign in to comment.