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

@ -22,6 +22,8 @@ const tc = __importStar(require("@actions/tool-cache"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const util = __importStar(require("util"));
const semver = __importStar(require("semver"));
const restm = __importStar(require("typed-rest-client/RestClient"));
let osPlat = os.platform();
let osArch = os.arch();
if (!tempDirectory) {
@ -42,6 +44,10 @@ if (!tempDirectory) {
}
function getGo(version) {
return __awaiter(this, void 0, void 0, function* () {
const selected = yield determineVersion(version);
if (selected) {
version = selected;
}
// check cache
let toolPath;
toolPath = tc.find('go', normalizeVersion(version));
@ -126,9 +132,74 @@ function normalizeVersion(version) {
//append minor and patch version if not available
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
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;
}
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));
});
}