From 102789f73dfab8c69f4a1e234bfdb99bc23198b9 Mon Sep 17 00:00:00 2001 From: Asa Gayle Date: Thu, 13 Jan 2022 13:40:37 -0500 Subject: [PATCH 1/2] Initial commit --- src/run-old.ts | 6 +++- src/run.ts | 91 ++++++++++++++++++-------------------------------- 2 files changed, 38 insertions(+), 59 deletions(-) diff --git a/src/run-old.ts b/src/run-old.ts index f685f45..c86ca51 100644 --- a/src/run-old.ts +++ b/src/run-old.ts @@ -8,6 +8,7 @@ import * as util from 'util'; import * as fs from 'fs'; import * as semver from 'semver'; +import * as exec from '@actions/exec'; import * as toolCache from '@actions/tool-cache'; import * as core from '@actions/core'; import { graphql } from '@octokit/graphql'; @@ -108,6 +109,9 @@ export async function downloadHelm(version: string): Promise { return helmpath; } +async function getLatestHelmVersionForTest(type: string): Promise{ + exec.exec(`curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`) +} async function getLatestHelmVersionFor(type: string): Promise { const token = core.getInput('token', { 'required': true }); try { @@ -199,4 +203,4 @@ export async function run() { core.setOutput('helm-path', cachedPath); } -run().catch(core.setFailed); +run().catch(core.setFailed); \ No newline at end of file diff --git a/src/run.ts b/src/run.ts index 2336538..45fbb94 100644 --- a/src/run.ts +++ b/src/run.ts @@ -8,16 +8,13 @@ import * as util from 'util'; import * as fs from 'fs'; import * as semver from 'semver'; +import * as exec from '@actions/exec'; +import { ExecOptions } from '@actions/exec/lib/interfaces'; import * as toolCache from '@actions/tool-cache'; import * as core from '@actions/core'; -import { graphql } from '@octokit/graphql'; const helmToolName = 'helm'; -const stableHelmVersion = 'v3.2.1'; -const stableHelm3Version = 'v3.5.3'; -const stableHelm2Version = 'v2.17.0'; -const LATEST_HELM2_VERSION = '2.*'; -const LATEST_HELM3_VERSION = '3.*'; +const stableHelmVersion = 'v3.7.1'; const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; export function getExecutableExtension(): string { @@ -50,7 +47,7 @@ export async function getStableHelmVersion(): Promise { 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 (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; } @@ -75,9 +72,8 @@ export var walkSync = function (dir, filelist, fileToFind) { } else { core.debug(file); - if (file === fileToFind) { + if (file == fileToFind) { filelist.push(path.join(dir, file)); - return filelist; } } }); @@ -109,40 +105,26 @@ export async function downloadHelm(version: string): Promise { return helmpath; } -async function getLatestHelmVersionFor(type: string): Promise { - const token = core.getInput('token', { 'required': true }); - try { - const { repository } = await graphql( - `{ - repository(name: "helm", owner: "helm") { - releases(last: 100) { - nodes { - tagName - } - } - } - }`, - { - headers: { - authorization: `token ${token}` - } - } - ); +async function getLatestHelmVersion(): Promise{ - const releases = repository.releases.nodes.reverse(); - let latestValidRelease = releases.find(release => isValidVersion(release.tagName, type)); - if (latestValidRelease) - return latestValidRelease.tagName; - } catch (err) { - core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using default Helm version %s.", type, err.toString(), getStableHelmVersionFor(type))); - return getStableHelmVersionFor(type); - } - core.warning(util.format("Could not find stable release for Helm %s. Using default Helm version %s.", type, getStableHelmVersionFor(type))); - return getStableHelmVersionFor(type); -} + let latestHelm: string = ""; + let latestHelmErr: string = ""; -function getStableHelmVersionFor(type: string) { - return type === "v2" ? stableHelm2Version : stableHelm3Version; + const options:ExecOptions = {}; + + options.listeners = { + stdout: (data: Buffer) => { + latestHelm += data.toString(); + }, + stderr: (data: Buffer) => { + latestHelmErr += data.toString(); + } + }; + + await exec.exec('curl', [`-Ls ${helmAllReleasesUrl} | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`], options); + + if(latestHelmErr !== "") return getStableHelmVersion(); + return latestHelm; } // isValidVersion checks if verison matches the specified type and is a stable release @@ -167,29 +149,22 @@ export function findHelm(rootFolder: string): string { export async function run() { let version = core.getInput('version', { 'required': true }); - if (process.env['HELM_INSTALLER_LEGACY_VERSIONING'] == 'true') { - if (version.toLocaleLowerCase() === 'latest') { - version = await getStableHelmVersion(); - } else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } - } else { - if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) { - version = await getLatestHelmVersionFor("v3"); - } else if (version === LATEST_HELM2_VERSION) { - version = await getLatestHelmVersionFor("v2"); - } else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } + if (version.toLocaleLowerCase() === 'latest') { + version = await getLatestHelmVersion(); } core.debug(util.format("Downloading %s", version)); let cachedPath = await downloadHelm(version); - if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { - core.addPath(path.dirname(cachedPath)); - } + try { + if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { + core.addPath(path.dirname(cachedPath)); + } + } + catch { + //do nothing, set as output variable + } console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`); core.setOutput('helm-path', cachedPath); From 91434c9853fbfff778e7d4b49b17f2c785165637 Mon Sep 17 00:00:00 2001 From: Asa Gayle Date: Thu, 13 Jan 2022 16:22:24 -0500 Subject: [PATCH 2/2] Compiled to js --- lib/run.js | 326 +++++++++++++++++++++++++++++------------------------ src/run.ts | 8 +- 2 files changed, 181 insertions(+), 153 deletions(-) diff --git a/lib/run.js b/lib/run.js index 9953953..af1485c 100644 --- a/lib/run.js +++ b/lib/run.js @@ -2,25 +2,6 @@ // Copyright (c) Microsoft Corporation. // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -30,29 +11,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.getStableHelmVersion = void 0; -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 stableHelm3Version = 'v3.5.3'; -const stableHelm2Version = 'v2.17.0'; -const LATEST_HELM2_VERSION = '2.*'; -const LATEST_HELM3_VERSION = '3.*'; -const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +exports.__esModule = true; +exports.run = exports.findHelm = exports.downloadHelm = exports.walkSync = exports.getStableHelmVersion = exports.getHelmDownloadURL = exports.getExecutableExtension = void 0; +var os = require("os"); +var path = require("path"); +var util = require("util"); +var fs = require("fs"); +var semver = require("semver"); +var exec = require("@actions/exec"); +var toolCache = require("@actions/tool-cache"); +var core = require("@actions/core"); +var helmToolName = 'helm'; +var stableHelmVersion = 'v3.7.1'; +var helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; function getExecutableExtension() { if (os.type().match(/^Win/)) { return '.exe'; } return ''; } +exports.getExecutableExtension = getExecutableExtension; function getHelmDownloadURL(version) { switch (os.type()) { case 'Linux': @@ -64,30 +69,39 @@ function getHelmDownloadURL(version) { return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version); } } +exports.getHelmDownloadURL = getHelmDownloadURL; 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; + return __awaiter(this, void 0, void 0, function () { + var downloadPath, responseArray, latestHelmVersion_1, error_1; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 2, , 3]); + return [4 /*yield*/, toolCache.downloadTool(helmAllReleasesUrl)]; + case 1: + downloadPath = _a.sent(); + responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); + latestHelmVersion_1 = semver.clean(stableHelmVersion); + responseArray.forEach(function (response) { + if (response && response.tag_name) { + var currentHelmVerison = semver.clean(response.tag_name.toString()); + if (currentHelmVerison) { + if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion_1)) { + //If current helm version is not a pre release and is greater than latest helm version + latestHelmVersion_1 = 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; + }); + latestHelmVersion_1 = "v" + latestHelmVersion_1; + return [2 /*return*/, latestHelmVersion_1]; + case 2: + error_1 = _a.sent(); + core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error_1, stableHelmVersion)); + return [3 /*break*/, 3]; + case 3: return [2 /*return*/, stableHelmVersion]; + } + }); }); } exports.getStableHelmVersion = getStableHelmVersion; @@ -96,7 +110,7 @@ var walkSync = function (dir, filelist, fileToFind) { filelist = filelist || []; files.forEach(function (file) { if (fs.statSync(path.join(dir, file)).isDirectory()) { - filelist = walkSync(path.join(dir, file), filelist, fileToFind); + filelist = (0, exports.walkSync)(path.join(dir, file), filelist, fileToFind); } else { core.debug(file); @@ -107,64 +121,80 @@ var walkSync = function (dir, filelist, fileToFind) { }); return filelist; }; +exports.walkSync = walkSync; function downloadHelm(version) { - return __awaiter(this, void 0, void 0, function* () { - if (!version) { - version = yield getLatestHelmVersionFor("v3"); - } - let cachedToolpath = toolCache.find(helmToolName, version); - if (!cachedToolpath) { - let helmDownloadPath; - try { - helmDownloadPath = yield toolCache.downloadTool(getHelmDownloadURL(version)); - } - catch (exception) { - throw new Error(util.format("Failed to download Helm from location ", getHelmDownloadURL(version))); - } - fs.chmodSync(helmDownloadPath, '777'); - const unzipedHelmPath = yield toolCache.extractZip(helmDownloadPath); - cachedToolpath = yield toolCache.cacheDir(unzipedHelmPath, helmToolName, version); - } - const helmpath = findHelm(cachedToolpath); - if (!helmpath) { - throw new Error(util.format("Helm executable not found in path ", cachedToolpath)); - } - fs.chmodSync(helmpath, '777'); - return helmpath; - }); -} -function getLatestHelmVersionFor(type) { - return __awaiter(this, void 0, void 0, function* () { - const token = core.getInput('token', { 'required': true }); - try { - const { repository } = yield graphql_1.graphql(`{ - repository(name: "helm", owner: "helm") { - releases(last: 100) { - nodes { - tagName - } + return __awaiter(this, void 0, void 0, function () { + var cachedToolpath, helmDownloadPath, exception_1, unzipedHelmPath, helmpath; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (!!version) return [3 /*break*/, 2]; + return [4 /*yield*/, getStableHelmVersion()]; + case 1: + version = _a.sent(); + _a.label = 2; + case 2: + cachedToolpath = toolCache.find(helmToolName, version); + if (!!cachedToolpath) return [3 /*break*/, 9]; + helmDownloadPath = void 0; + _a.label = 3; + case 3: + _a.trys.push([3, 5, , 6]); + return [4 /*yield*/, toolCache.downloadTool(getHelmDownloadURL(version))]; + case 4: + helmDownloadPath = _a.sent(); + return [3 /*break*/, 6]; + case 5: + exception_1 = _a.sent(); + throw new Error(util.format("Failed to download Helm from location", getHelmDownloadURL(version))); + case 6: + fs.chmodSync(helmDownloadPath, '777'); + return [4 /*yield*/, toolCache.extractZip(helmDownloadPath)]; + case 7: + unzipedHelmPath = _a.sent(); + return [4 /*yield*/, toolCache.cacheDir(unzipedHelmPath, helmToolName, version)]; + case 8: + cachedToolpath = _a.sent(); + _a.label = 9; + case 9: + helmpath = findHelm(cachedToolpath); + if (!helmpath) { + throw new Error(util.format("Helm executable not found in path", cachedToolpath)); } - } - }`, { - headers: { - authorization: `token ${token}` - } - }); - const releases = repository.releases.nodes.reverse(); - let latestValidRelease = releases.find(release => isValidVersion(release.tagName, type)); - if (latestValidRelease) - return latestValidRelease.tagName; - } - catch (err) { - core.warning(util.format("Error while fetching the latest Helm %s release. Error: %s. Using default Helm version %s.", type, err.toString(), getStableHelmVersionFor(type))); - return getStableHelmVersionFor(type); - } - core.warning(util.format("Could not find stable release for Helm %s. Using default Helm version %s.", type, getStableHelmVersionFor(type))); - return getStableHelmVersionFor(type); + fs.chmodSync(helmpath, '777'); + return [2 /*return*/, helmpath]; + } + }); }); } -function getStableHelmVersionFor(type) { - return type === "v2" ? stableHelm2Version : stableHelm3Version; +exports.downloadHelm = downloadHelm; +function getLatestHelmVersion() { + return __awaiter(this, void 0, void 0, function () { + var command, latestHelm, latestHelmErr, options; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + command = "curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*/helm/helm/releases/tag/tag/(v[0-9.]+)\".*/1/g' | head -1 | sed -E 's/.*tag///' | sed -E 's/\".*//'"; + latestHelm = ""; + latestHelmErr = ""; + options = {}; + options.listeners = { + stdout: function (data) { + latestHelm += data.toString(); + }, + stderr: function (data) { + latestHelmErr += data.toString(); + } + }; + return [4 /*yield*/, exec.exec(command, [], options)]; + case 1: + _a.sent(); + if (latestHelmErr !== "") + return [2 /*return*/, getStableHelmVersion()]; + return [2 /*return*/, latestHelm]; + } + }); + }); } // isValidVersion checks if verison matches the specified type and is a stable release function isValidVersion(version, type) { @@ -175,48 +205,46 @@ function isValidVersion(version, type) { function findHelm(rootFolder) { fs.chmodSync(rootFolder, '777'); var filelist = []; - walkSync(rootFolder, filelist, helmToolName + getExecutableExtension()); - if (!filelist) { - throw new Error(util.format("Helm executable not found in path ", rootFolder)); + (0, exports.walkSync)(rootFolder, filelist, helmToolName + getExecutableExtension()); + if (!filelist || filelist.length == 0) { + throw new Error(util.format("Helm executable not found in path", rootFolder)); } else { return filelist[0]; } } +exports.findHelm = findHelm; function run() { - return __awaiter(this, void 0, void 0, function* () { - let version = core.getInput('version', { 'required': true }); - if (process.env['HELM_INSTALLER_LEGACY_VERSIONING'] == 'true') { - if (version.toLocaleLowerCase() === 'latest') { - version = yield getStableHelmVersion(); + return __awaiter(this, void 0, void 0, function () { + var version, cachedPath; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + version = core.getInput('version', { 'required': true }); + if (!(version.toLocaleLowerCase() === 'latest')) return [3 /*break*/, 2]; + return [4 /*yield*/, getLatestHelmVersion()]; + case 1: + version = _a.sent(); + _a.label = 2; + case 2: + core.debug(util.format("Downloading %s", version)); + return [4 /*yield*/, downloadHelm(version)]; + case 3: + cachedPath = _a.sent(); + try { + if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { + core.addPath(path.dirname(cachedPath)); + } + } + catch (_b) { + //do nothing, set as output variable + } + console.log("Helm tool version: '".concat(version, "' has been cached at ").concat(cachedPath)); + core.setOutput('helm-path', cachedPath); + return [2 /*return*/]; } - else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } - } - else { - if (version.toLocaleLowerCase() === 'latest' || version === LATEST_HELM3_VERSION) { - version = yield getLatestHelmVersionFor("v3"); - } - else if (version === LATEST_HELM2_VERSION) { - version = yield getLatestHelmVersionFor("v2"); - } - else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } - } - core.debug(util.format("Downloading %s", version)); - let cachedPath = yield downloadHelm(version); - try { - if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { - core.addPath(path.dirname(cachedPath)); - } - } - catch (_a) { - //do nothing, set as output variable - } - console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`); - core.setOutput('helm-path', cachedPath); + }); }); } -run().catch(core.setFailed); +exports.run = run; +run()["catch"](core.setFailed); diff --git a/src/run.ts b/src/run.ts index 45fbb94..dcfbda5 100644 --- a/src/run.ts +++ b/src/run.ts @@ -106,10 +106,10 @@ export async function downloadHelm(version: string): Promise { } async function getLatestHelmVersion(): Promise{ - + const command:string = `curl -Ls https://api.github.com/repos/helm/helm/releases | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`; let latestHelm: string = ""; let latestHelmErr: string = ""; - + const options:ExecOptions = {}; options.listeners = { @@ -120,8 +120,8 @@ async function getLatestHelmVersion(): Promise{ latestHelmErr += data.toString(); } }; - - await exec.exec('curl', [`-Ls ${helmAllReleasesUrl} | grep 'v3.[0-9]*.[0-9]*' | sed -E 's/ .*\/helm\/helm\/releases\/tag\/tag\/(v[0-9\.]+)".*/\1/g' | head -1 | sed -E 's/.*tag\///' | sed -E 's/".*//'`], options); + + await exec.exec(command, [], options); if(latestHelmErr !== "") return getStableHelmVersion(); return latestHelm;