Handle .x version syntax with latest release (#13)

* get latest release for .x syntax version

* added nock as dev dependency

* added test for .x syntax

* updated readme

* updated http client name

* use rest client for getting available versions

* more .x handling

* move nock to setup and teardown
This commit is contained in:
Alif Rachmawadi 2019-08-19 19:28:37 +07:00 committed by Danny McCormick
parent 5064ef8f2b
commit 632d18fc92
7 changed files with 2810 additions and 5 deletions

View File

@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-16.04 runs-on: ubuntu-16.04
strategy: strategy:
matrix: matrix:
go: [ '1.8', '1.9.3', '1.10' ] go: [ '1.8', '1.9.3', '1.10.x' ]
name: Go ${{ matrix.go }} sample name: Go ${{ matrix.go }} sample
steps: steps:
- uses: actions/checkout@master - uses: actions/checkout@master

File diff suppressed because it is too large Load Diff

View File

@ -2,9 +2,11 @@ import io = require('@actions/io');
import fs = require('fs'); import fs = require('fs');
import os = require('os'); import os = require('os');
import path = require('path'); import path = require('path');
import nock = require('nock');
const toolDir = path.join(__dirname, 'runner', 'tools'); const toolDir = path.join(__dirname, 'runner', 'tools');
const tempDir = path.join(__dirname, 'runner', 'temp'); const tempDir = path.join(__dirname, 'runner', 'temp');
const dataDir = path.join(__dirname, 'data');
process.env['RUNNER_TOOL_CACHE'] = toolDir; process.env['RUNNER_TOOL_CACHE'] = toolDir;
process.env['RUNNER_TEMP'] = tempDir; process.env['RUNNER_TEMP'] = tempDir;
@ -28,8 +30,8 @@ describe('installer tests', () => {
}, 100000); }, 100000);
it('Acquires version of go if no matching version is installed', async () => { it('Acquires version of go if no matching version is installed', async () => {
await installer.getGo('1.10'); await installer.getGo('1.10.8');
const goDir = path.join(toolDir, 'go', '1.10.0', os.arch()); const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
expect(fs.existsSync(`${goDir}.complete`)).toBe(true); expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
if (IS_WINDOWS) { if (IS_WINDOWS) {
@ -39,6 +41,55 @@ describe('installer tests', () => {
} }
}, 100000); }, 100000);
describe('the latest release of a go version', () => {
beforeEach(() => {
nock('https://api.github.com')
.get('/repos/golang/go/git/refs/tags')
.replyWithFile(200, path.join(dataDir, 'golang-tags.json'));
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('Acquires latest release version of go 1.10 if using 1.10 and no matching version is installed', async () => {
await installer.getGo('1.10');
const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
}
}, 100000);
it('Acquires latest release version of go 1.10 if using 1.10.x and no matching version is installed', async () => {
await installer.getGo('1.10.x');
const goDir = path.join(toolDir, 'go', '1.10.8', os.arch());
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
}
}, 100000);
it('Acquires latest release version of go if using 1.x and no matching version is installed', async () => {
await installer.getGo('1.x');
const goDir = path.join(toolDir, 'go', '1.13.0-beta1', os.arch());
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
}
}, 100000);
});
it('Throws if no location contains correct go version', async () => { it('Throws if no location contains correct go version', async () => {
let thrown = false; let thrown = false;
try { try {

View File

@ -22,6 +22,8 @@ const tc = __importStar(require("@actions/tool-cache"));
const os = __importStar(require("os")); const os = __importStar(require("os"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
const util = __importStar(require("util")); const util = __importStar(require("util"));
const semver = __importStar(require("semver"));
const restm = __importStar(require("typed-rest-client/RestClient"));
let osPlat = os.platform(); let osPlat = os.platform();
let osArch = os.arch(); let osArch = os.arch();
if (!tempDirectory) { if (!tempDirectory) {
@ -42,6 +44,10 @@ if (!tempDirectory) {
} }
function getGo(version) { function getGo(version) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const selected = yield determineVersion(version);
if (selected) {
version = selected;
}
// check cache // check cache
let toolPath; let toolPath;
toolPath = tc.find('go', normalizeVersion(version)); toolPath = tc.find('go', normalizeVersion(version));
@ -126,9 +132,74 @@ function normalizeVersion(version) {
//append minor and patch version if not available //append minor and patch version if not available
return version.concat('.0.0'); return version.concat('.0.0');
} }
else if (versionPart[2] == null) { else {
// handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) {
versionPart[1] = versionPart[1]
.replace('beta', '.0-beta')
.replace('rc', '.0-rc');
return versionPart.join('.');
}
}
if (versionPart[2] == null) {
//append patch version if not available //append patch version if not available
return version.concat('.0'); return version.concat('.0');
} }
else {
// handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) {
versionPart[2] = versionPart[2]
.replace('beta', '-beta')
.replace('rc', '-rc');
return versionPart.join('.');
}
}
return version; return version;
} }
function determineVersion(version) {
return __awaiter(this, void 0, void 0, function* () {
if (!version.endsWith('.x')) {
const versionPart = version.split('.');
if (versionPart[1] == null || versionPart[2] == null) {
return yield getLatestVersion(version.concat('.x'));
}
else {
return version;
}
}
return yield getLatestVersion(version);
});
}
function getLatestVersion(version) {
return __awaiter(this, void 0, void 0, function* () {
// clean .x syntax: 1.10.x -> 1.10
const trimmedVersion = version.slice(0, version.length - 2);
const versions = yield getPossibleVersions(trimmedVersion);
core.debug(`evaluating ${versions.length} versions`);
if (version.length === 0) {
throw new Error('unable to get latest version');
}
core.debug(`matched: ${versions[0]}`);
return versions[0];
});
}
function getAvailableVersions() {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient('setup-go');
let tags = (yield rest.get('https://api.github.com/repos/golang/go/git/refs/tags')).result || [];
return tags
.filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g))
.map(tag => tag.ref.replace('refs/tags/go', ''));
});
}
function getPossibleVersions(version) {
return __awaiter(this, void 0, void 0, function* () {
const versions = yield getAvailableVersions();
const possibleVersions = versions.filter(v => v.startsWith(version));
const versionMap = new Map();
possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v));
return Array.from(versionMap.keys())
.sort(semver.rcompare)
.map(v => versionMap.get(v));
});
}

114
package-lock.json generated
View File

@ -530,6 +530,15 @@
"integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==", "integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==",
"dev": true "dev": true
}, },
"@types/nock": {
"version": "10.0.3",
"resolved": "https://registry.npmjs.org/@types/nock/-/nock-10.0.3.tgz",
"integrity": "sha512-OthuN+2FuzfZO3yONJ/QVjKmLEuRagS9TV9lEId+WHL9KhftYG+/2z+pxlr0UgVVXSpVD8woie/3fzQn8ft/Ow==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/node": { "@types/node": {
"version": "12.7.1", "version": "12.7.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.1.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.1.tgz",
@ -693,6 +702,12 @@
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
"dev": true "dev": true
}, },
"assertion-error": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
"integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==",
"dev": true
},
"assign-symbols": { "assign-symbols": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz", "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
@ -1007,6 +1022,20 @@
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=",
"dev": true "dev": true
}, },
"chai": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz",
"integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==",
"dev": true,
"requires": {
"assertion-error": "^1.1.0",
"check-error": "^1.0.2",
"deep-eql": "^3.0.1",
"get-func-name": "^2.0.0",
"pathval": "^1.1.0",
"type-detect": "^4.0.5"
}
},
"chalk": { "chalk": {
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
@ -1018,6 +1047,12 @@
"supports-color": "^5.3.0" "supports-color": "^5.3.0"
} }
}, },
"check-error": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
"integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=",
"dev": true
},
"ci-info": { "ci-info": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz",
@ -1263,6 +1298,21 @@
"integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=", "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=",
"dev": true "dev": true
}, },
"deep-eql": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
"integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
"dev": true,
"requires": {
"type-detect": "^4.0.0"
}
},
"deep-equal": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz",
"integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=",
"dev": true
},
"deep-is": { "deep-is": {
"version": "0.1.3", "version": "0.1.3",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz",
@ -2275,6 +2325,12 @@
"integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==", "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==",
"dev": true "dev": true
}, },
"get-func-name": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
"integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=",
"dev": true
},
"get-stdin": { "get-stdin": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-7.0.0.tgz", "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-7.0.0.tgz",
@ -3737,6 +3793,46 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true "dev": true
}, },
"nock": {
"version": "10.0.6",
"resolved": "https://registry.npmjs.org/nock/-/nock-10.0.6.tgz",
"integrity": "sha512-b47OWj1qf/LqSQYnmokNWM8D88KvUl2y7jT0567NB3ZBAZFz2bWp2PC81Xn7u8F2/vJxzkzNZybnemeFa7AZ2w==",
"dev": true,
"requires": {
"chai": "^4.1.2",
"debug": "^4.1.0",
"deep-equal": "^1.0.0",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.5",
"mkdirp": "^0.5.0",
"propagate": "^1.0.0",
"qs": "^6.5.1",
"semver": "^5.5.0"
},
"dependencies": {
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"dev": true,
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
}
}
},
"node-int64": { "node-int64": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz",
@ -4063,6 +4159,12 @@
"pify": "^3.0.0" "pify": "^3.0.0"
} }
}, },
"pathval": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
"integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=",
"dev": true
},
"performance-now": { "performance-now": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
@ -4190,6 +4292,12 @@
"sisteransi": "^1.0.3" "sisteransi": "^1.0.3"
} }
}, },
"propagate": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/propagate/-/propagate-1.0.0.tgz",
"integrity": "sha1-AMLa7t2iDofjeCs0Stuhzd1q1wk=",
"dev": true
},
"psl": { "psl": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.3.0.tgz", "resolved": "https://registry.npmjs.org/psl/-/psl-1.3.0.tgz",
@ -5093,6 +5201,12 @@
"prelude-ls": "~1.1.2" "prelude-ls": "~1.1.2"
} }
}, },
"type-detect": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
"integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
"dev": true
},
"type-fest": { "type-fest": {
"version": "0.6.0", "version": "0.6.0",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.6.0.tgz", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.6.0.tgz",

View File

@ -29,11 +29,13 @@
"devDependencies": { "devDependencies": {
"@actions/io": "^1.0.0", "@actions/io": "^1.0.0",
"@types/jest": "^24.0.13", "@types/jest": "^24.0.13",
"@types/nock": "^10.0.3",
"@types/node": "^12.0.4", "@types/node": "^12.0.4",
"@types/semver": "^6.0.0", "@types/semver": "^6.0.0",
"husky": "^2.3.0", "husky": "^2.3.0",
"jest": "^24.8.0", "jest": "^24.8.0",
"jest-circus": "^24.7.1", "jest-circus": "^24.7.1",
"nock": "^10.0.6",
"prettier": "^1.17.1", "prettier": "^1.17.1",
"ts-jest": "^24.0.2", "ts-jest": "^24.0.2",
"typescript": "^3.5.1" "typescript": "^3.5.1"

View File

@ -5,6 +5,8 @@ import * as tc from '@actions/tool-cache';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as util from 'util'; import * as util from 'util';
import * as semver from 'semver';
import * as restm from 'typed-rest-client/RestClient';
let osPlat: string = os.platform(); let osPlat: string = os.platform();
let osArch: string = os.arch(); let osArch: string = os.arch();
@ -25,6 +27,11 @@ if (!tempDirectory) {
} }
export async function getGo(version: string) { export async function getGo(version: string) {
const selected = await determineVersion(version);
if (selected) {
version = selected;
}
// check cache // check cache
let toolPath: string; let toolPath: string;
toolPath = tc.find('go', normalizeVersion(version)); toolPath = tc.find('go', normalizeVersion(version));
@ -122,9 +129,87 @@ function normalizeVersion(version: string): string {
if (versionPart[1] == null) { if (versionPart[1] == null) {
//append minor and patch version if not available //append minor and patch version if not available
return version.concat('.0.0'); return version.concat('.0.0');
} else if (versionPart[2] == null) { } else {
// handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) {
versionPart[1] = versionPart[1]
.replace('beta', '.0-beta')
.replace('rc', '.0-rc');
return versionPart.join('.');
}
}
if (versionPart[2] == null) {
//append patch version if not available //append patch version if not available
return version.concat('.0'); return version.concat('.0');
} else {
// handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) {
versionPart[2] = versionPart[2]
.replace('beta', '-beta')
.replace('rc', '-rc');
return versionPart.join('.');
} }
}
return version; return version;
} }
async function determineVersion(version: string): Promise<string> {
if (!version.endsWith('.x')) {
const versionPart = version.split('.');
if (versionPart[1] == null || versionPart[2] == null) {
return await getLatestVersion(version.concat('.x'));
} else {
return version;
}
}
return await getLatestVersion(version);
}
async function getLatestVersion(version: string): Promise<string> {
// clean .x syntax: 1.10.x -> 1.10
const trimmedVersion = version.slice(0, version.length - 2);
const versions = await getPossibleVersions(trimmedVersion);
core.debug(`evaluating ${versions.length} versions`);
if (version.length === 0) {
throw new Error('unable to get latest version');
}
core.debug(`matched: ${versions[0]}`);
return versions[0];
}
interface IGoRef {
ref: string;
}
async function getAvailableVersions(): Promise<string[]> {
let rest: restm.RestClient = new restm.RestClient('setup-go');
let tags: IGoRef[] =
(await rest.get<IGoRef[]>(
'https://api.github.com/repos/golang/go/git/refs/tags'
)).result || [];
return tags
.filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g))
.map(tag => tag.ref.replace('refs/tags/go', ''));
}
async function getPossibleVersions(version: string): Promise<string[]> {
const versions = await getAvailableVersions();
const possibleVersions = versions.filter(v => v.startsWith(version));
const versionMap = new Map();
possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v));
return Array.from(versionMap.keys())
.sort(semver.rcompare)
.map(v => versionMap.get(v));
}