Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gop-version supports branch #31

Merged
merged 5 commits into from
Nov 3, 2023
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
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,47 @@ jobs:
- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.gop-version }}"

test-versions:
name: Gop Versions Test

strategy:
matrix:
gop-version: ['1.1.7', '', 'latest', 'main', 'v1.0']
include:
- gop-version: '1.1.7'
gop-version-verified: 'true'
- gop-version: ''
gop-version-verified: 'true'
- gop-version: 'latest'
gop-version-verified: 'true'
- gop-version: 'main'
gop-version-verified: 'false'
- gop-version: 'v1.0'
gop-version-verified: 'false'

runs-on: ubuntu-latest

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Test Gop Version
id: test-gop-version
uses: ./
with:
gop-version: ${{ matrix.gop-version }}

- name: Test version verified
env:
VERIFIED: ${{ matrix.gop-version-verified }}
GOP_VERSION: ${{ matrix.gop-version }}
run: |
echo "gop-version: $GOP_VERSION"
echo "gop-version-verified: $VERIFIED"
if [[ "$VERIFIED" != "$VERIFIED" ]]; then
echo "gop-version-verified does not match expected value"
echo "expected: $VERIFIED, got: $VERIFIED"
exit 1
fi
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ outputs:
gop-version:
description:
'The installed Go+ version. Useful when given a version range as input.'
gop-version-verified:
description:
Whether the installed Go+ version checked, true if the installed version
is in the tags, false otherwise.
go-version:
description:
'The installed Go version. Useful when given a version range as input.'
Expand Down
69 changes: 53 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 61 additions & 16 deletions src/install-gop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,63 @@ const GOPLUS_REPO = 'https://github.com/goplus/gop.git'
export async function installGop(): Promise<void> {
try {
const versionSpec = resolveVersionInput() || ''
const versions = semver.rsort(fetchVersions().filter(v => semver.valid(v)))
const tagVersions = semver.rsort(fetchTags().filter(v => semver.valid(v)))
let version: string | null = null
if (!versionSpec || versionSpec === 'latest') {
version = versions[0]
version = tagVersions[0]
core.warning(`No gop-version specified, using latest version: ${version}`)
} else {
version = semver.maxSatisfying(versions, versionSpec)
version = semver.maxSatisfying(tagVersions, versionSpec)
if (!version) {
core.warning(
`No gop-version found that satisfies '${versionSpec}', trying branches...`
)
const branchVersions = fetchBranches()
if (!branchVersions.includes(versionSpec)) {
throw new Error(
`No gop-version found that satisfies '${versionSpec}' in branches or tags`
)
}
version = ''
}
}

if (!version) {
throw new Error(
`Unable to find a version that satisfies the version spec '${versionSpec}'`
let checkoutVersion = ''
if (version) {
core.info(`Selected version ${version} by spec ${versionSpec}`)
checkoutVersion = `v${version}`
core.setOutput('gop-version-verified', true)
} else {
core.warning(
`Unable to find a version that satisfies the version spec '${versionSpec}', trying branches...`
)
checkoutVersion = versionSpec
core.setOutput('gop-version-verified', false)
}
core.info(`Selected version ${version} by spec ${versionSpec}`)
const tagVersion = `v${version}`
const gopDir = clone(tagVersion)
const gopDir = cloneBranchOrTag(checkoutVersion)
install(gopDir)
test(tagVersion)
core.setOutput('gop-version', version)
if (version) {
checkVersion(version)
}
core.setOutput('gop-version', gopVersion())
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}

function clone(versionSpec: string): string {
export function selectVersion(
versions: string[],
versionSpec?: string
): string | null {
const sortedVersions = semver.rsort(versions.filter(v => semver.valid(v)))
if (!versionSpec || versionSpec === 'latest') {
return sortedVersions[0]
}
return semver.maxSatisfying(sortedVersions, versionSpec)
}

function cloneBranchOrTag(versionSpec: string): string {
// git clone https://github.com/goplus/gop.git with tag $versionSpec to $HOME/workdir/gop
const workDir = path.join(os.homedir(), 'workdir')
if (fs.existsSync(workDir)) {
Expand Down Expand Up @@ -69,19 +99,24 @@ function install(gopDir: string): void {
core.info('gop installed')
}

function test(versionSpec: string): void {
function checkVersion(versionSpec: string): string {
core.info(`Testing gop ${versionSpec} ...`)
const out = execSync('gop env GOPVERSION', { env: process.env })
const actualVersion = out.toString().trim()
const actualVersion = gopVersion()
if (actualVersion !== versionSpec) {
throw new Error(
`Installed gop version ${actualVersion} does not match expected version ${versionSpec}`
)
}
core.info(`Installed gop version ${actualVersion}`)
return actualVersion
}

function fetchVersions(): string[] {
function gopVersion(): string {
const out = execSync('gop env GOPVERSION', { env: process.env })
return out.toString().trim().replace(/^v/, '')
}

function fetchTags(): string[] {
const cmd = `git -c versionsort.suffix=- ls-remote --tags --sort=v:refname ${GOPLUS_REPO}`
const out = execSync(cmd).toString()
const versions = out
Expand All @@ -92,6 +127,16 @@ function fetchVersions(): string[] {
return versions
}

function fetchBranches(): string[] {
const cmd = `git -c versionsort.suffix=- ls-remote --heads --sort=v:refname ${GOPLUS_REPO}`
const out = execSync(cmd).toString()
const versions = out
.split('\n')
.filter(s => s)
.map(s => s.split('\t')[1].replace('refs/heads/', ''))
return versions
}

function resolveVersionInput(): string | undefined {
let version = process.env['INPUT_GOP_VERSION']
const versionFilePath = process.env['INPUT_GOP_VERSION_FILE']
Expand Down
Loading