From 79642a476b255dff91ba70bb2f7f5aa0af166ab1 Mon Sep 17 00:00:00 2001 From: Alif Rachmawadi Date: Fri, 16 Aug 2019 16:10:53 +0700 Subject: [PATCH] use rest client for getting available versions --- lib/installer.js | 20 +++++++++----------- src/installer.ts | 29 ++++++++++++++++------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/installer.js b/lib/installer.js index dcc23e3..230eec6 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -23,7 +23,7 @@ const os = __importStar(require("os")); const path = __importStar(require("path")); const util = __importStar(require("util")); const semver = __importStar(require("semver")); -const httpm = __importStar(require("typed-rest-client/HttpClient")); +const restm = __importStar(require("typed-rest-client/RestClient")); let osPlat = os.platform(); let osArch = os.arch(); if (!tempDirectory) { @@ -155,24 +155,22 @@ function getLatestVersion(version) { // 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) { + core.debug('match not found'); return trimmedVersion; } + core.debug(`matched: ${versions[0]}`); return versions[0]; }); } -function unique(value, index, self) { - return self.indexOf(value) === index; -} function getAvailableVersions() { return __awaiter(this, void 0, void 0, function* () { - let http = new httpm.HttpClient('setup-go'); - let contents = yield (yield http.get('https://api.github.com/repos/golang/go/git/refs/tags')).readBody(); - const matches = contents.match(/go\d+\.[\w\.]+/g) || []; - const versions = matches - .map(version => version.replace('go', '')) - .filter(unique); - return versions; + 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) { diff --git a/src/installer.ts b/src/installer.ts index 6882f66..b007545 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -6,7 +6,7 @@ import * as os from 'os'; import * as path from 'path'; import * as util from 'util'; import * as semver from 'semver'; -import * as httpm from 'typed-rest-client/HttpClient'; +import * as restm from 'typed-rest-client/RestClient'; let osPlat: string = os.platform(); let osArch: string = os.arch(); @@ -155,29 +155,32 @@ async function getLatestVersion(version: string): Promise { const versions = await getPossibleVersions(trimmedVersion); + core.debug(`evaluating ${versions.length} versions`); + if (version.length === 0) { + core.debug('match not found'); return trimmedVersion; } + core.debug(`matched: ${versions[0]}`); + return versions[0]; } -function unique(value: string, index: number, self: string[]) { - return self.indexOf(value) === index; +interface IGoRef { + ref: string; } async function getAvailableVersions(): Promise { - let http: httpm.HttpClient = new httpm.HttpClient('setup-go'); - let contents = await (await http.get( - 'https://api.github.com/repos/golang/go/git/refs/tags' - )).readBody(); + let rest: restm.RestClient = new restm.RestClient('setup-go'); + let tags: IGoRef[] = + (await rest.get( + 'https://api.github.com/repos/golang/go/git/refs/tags' + )).result || []; - const matches = contents.match(/go\d+\.[\w\.]+/g) || []; - const versions = matches - .map(version => version.replace('go', '')) - .filter(unique); - - return versions; + 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 {