From 41b50dea4b380521e1a4c2232d597dba2890504d Mon Sep 17 00:00:00 2001 From: Shivam Gupta Date: Wed, 17 Mar 2021 13:39:16 +0530 Subject: [PATCH] ceffd --- lib/run.js | 70 ++++++++++++++++++-------------------------------- src/run.ts | 75 ++++++++++++++++++++---------------------------------- 2 files changed, 53 insertions(+), 92 deletions(-) diff --git a/lib/run.js b/lib/run.js index d46cc3e..e8aaf2c 100644 --- a/lib/run.js +++ b/lib/run.js @@ -34,13 +34,11 @@ const os = __importStar(require("os")); const path = __importStar(require("path")); const util = __importStar(require("util")); const fs = __importStar(require("fs")); -const semver = __importStar(require("semver")); const toolCache = __importStar(require("@actions/tool-cache")); const core = __importStar(require("@actions/core")); const graphql_1 = require("@octokit/graphql"); const helmToolName = 'helm'; const stableHelmVersion = 'v3.2.1'; -const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; const LATEST_HELM2_VERSION = '2.*'; const LATEST_HELM3_VERSION = '3.*'; function getExecutableExtension() { @@ -60,32 +58,6 @@ function getHelmDownloadURL(version) { return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version); } } -function getStableHelmVersion() { - return __awaiter(this, void 0, void 0, function* () { - try { - const downloadPath = yield toolCache.downloadTool(helmAllReleasesUrl); - const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); - let latestHelmVersion = semver.clean(stableHelmVersion); - responseArray.forEach(response => { - if (response && response.tag_name) { - let currentHelmVerison = semver.clean(response.tag_name.toString()); - if (currentHelmVerison) { - if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) { - //If current helm version is not a pre release and is greater than latest helm version - latestHelmVersion = currentHelmVerison; - } - } - } - }); - latestHelmVersion = "v" + latestHelmVersion; - return latestHelmVersion; - } - catch (error) { - core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion)); - } - return stableHelmVersion; - }); -} var walkSync = function (dir, filelist, fileToFind) { var files = fs.readdirSync(dir); filelist = filelist || []; @@ -105,7 +77,7 @@ var walkSync = function (dir, filelist, fileToFind) { function downloadHelm(version) { return __awaiter(this, void 0, void 0, function* () { if (!version) { - version = yield getStableHelmVersion(); + version = yield getLatestHelmVersionFor("v3"); } let cachedToolpath = toolCache.find(helmToolName, version); if (!cachedToolpath) { @@ -130,10 +102,9 @@ function downloadHelm(version) { } function getLatestHelmVersionFor(type) { return __awaiter(this, void 0, void 0, function* () { - console.log("Running graphql"); - console.log(type); const token = core.getInput('token', { 'required': true }); - const { repository } = yield graphql_1.graphql(` + try { + const { repository } = yield graphql_1.graphql(` { repository(name:"helm", owner:"helm") { releases(last: 100) { @@ -144,14 +115,28 @@ function getLatestHelmVersionFor(type) { } } `, { - headers: { - authorization: `token ${token}` - } - }); - console.log(repository.releases.nodes); + headers: { + authorization: `token ${token}` + } + }); + const releases = repository.releases.nodes.reverse(); + releases.forEach(release => { + if (isValidVersion(release.tagName, type)) + return release.tagName; + }); + } + catch (err) { + core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using defaul Helm version %s.", type, err.toString(), stableHelmVersion)); + } return stableHelmVersion; }); } +// isValidVersion checks if verison matches the specified type and is a stable release +function isValidVersion(version, type) { + if (!version.startsWith(type)) + return false; + return version.indexOf('rc') == -1; +} function findHelm(rootFolder) { fs.chmodSync(rootFolder, '777'); var filelist = []; @@ -166,16 +151,11 @@ function findHelm(rootFolder) { function run() { return __awaiter(this, void 0, void 0, function* () { let version = core.getInput('version', { 'required': true }); - if (version.toLocaleLowerCase() === 'latest') { - version = yield getStableHelmVersion(); + if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) { + version = yield getLatestHelmVersionFor("v3"); } else if (version === LATEST_HELM2_VERSION) { - const v = yield getLatestHelmVersionFor(2); - version = 'v' + v; - } - else if (version === LATEST_HELM3_VERSION) { - const v = yield getLatestHelmVersionFor(3); - version = v; + version = yield getLatestHelmVersionFor("v2"); } else if (!version.toLocaleLowerCase().startsWith('v')) { version = 'v' + version; diff --git a/src/run.ts b/src/run.ts index 458eff2..b0e6d2f 100644 --- a/src/run.ts +++ b/src/run.ts @@ -5,7 +5,6 @@ import * as os from 'os'; import * as path from 'path'; import * as util from 'util'; import * as fs from 'fs'; -import * as semver from 'semver'; import * as toolCache from '@actions/tool-cache'; import * as core from '@actions/core'; @@ -13,7 +12,6 @@ import { graphql } from '@octokit/graphql'; const helmToolName = 'helm'; const stableHelmVersion = 'v3.2.1'; -const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; const LATEST_HELM2_VERSION = '2.*'; const LATEST_HELM3_VERSION = '3.*'; @@ -38,32 +36,6 @@ function getHelmDownloadURL(version: string): string { } } -async function getStableHelmVersion(): Promise { - try { - const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl); - const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); - let latestHelmVersion = semver.clean(stableHelmVersion); - responseArray.forEach(response => { - if (response && response.tag_name) { - let currentHelmVerison = semver.clean(response.tag_name.toString()); - if (currentHelmVerison) { - if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) { - //If current helm version is not a pre release and is greater than latest helm version - latestHelmVersion = currentHelmVerison; - } - } - } - }); - latestHelmVersion = "v" + latestHelmVersion; - return latestHelmVersion; - } catch (error) { - core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion)); - } - - return stableHelmVersion; -} - - var walkSync = function (dir, filelist, fileToFind) { var files = fs.readdirSync(dir); filelist = filelist || []; @@ -82,7 +54,7 @@ var walkSync = function (dir, filelist, fileToFind) { }; async function downloadHelm(version: string): Promise { - if (!version) { version = await getStableHelmVersion(); } + if (!version) { version = await getLatestHelmVersionFor("v3"); } let cachedToolpath = toolCache.find(helmToolName, version); if (!cachedToolpath) { let helmDownloadPath; @@ -107,11 +79,10 @@ async function downloadHelm(version: string): Promise { } async function getLatestHelmVersionFor(type) { - console.log("Running graphql") - console.log(type) - const token = core.getInput('token', { 'required': true }); - const { repository } = await graphql( - ` + const token = core.getInput('token', { 'required': true }); + try { + const { repository } = await graphql( + ` { repository(name:"helm", owner:"helm") { releases(last: 100) { @@ -122,16 +93,30 @@ async function getLatestHelmVersionFor(type) { } } `, - { - headers: { - authorization: `token ${token}` + { + headers: { + authorization: `token ${token}` + } } - } - ); - console.log(repository.releases.nodes); + ); + const releases = repository.releases.nodes.reverse(); + releases.forEach(release => { + if (isValidVersion(release.tagName, type)) + return release.tagName; + }); + } catch (err) { + core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using defaul Helm version %s.", type, err.toString(), stableHelmVersion)); + } return stableHelmVersion; } +// isValidVersion checks if verison matches the specified type and is a stable release +function isValidVersion(version, type) { + if (!version.startsWith(type)) + return false; + return version.indexOf('rc') == -1 +} + function findHelm(rootFolder: string): string { fs.chmodSync(rootFolder, '777'); var filelist: string[] = []; @@ -146,14 +131,10 @@ function findHelm(rootFolder: string): string { async function run() { let version = core.getInput('version', { 'required': true }); - if (version.toLocaleLowerCase() === 'latest') { - version = await getStableHelmVersion(); + if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) { + version = await getLatestHelmVersionFor("v3"); } else if (version === LATEST_HELM2_VERSION) { - const v = await getLatestHelmVersionFor(2); - version = 'v' + v; - } else if (version === LATEST_HELM3_VERSION) { - const v = await getLatestHelmVersionFor(3); - version = v; + version = await getLatestHelmVersionFor("v2"); } else if (!version.toLocaleLowerCase().startsWith('v')) { version = 'v' + version; }