4
0
Fork 0
mirror of https://github.com/Azure/setup-helm.git synced 2025-11-08 13:36:56 +00:00

Latest getLatestHelmVersion iteration

This commit is contained in:
Asa Gayle 2022-01-25 10:34:19 -05:00
parent 363e4253a0
commit f1588f7abf
4 changed files with 7544 additions and 55 deletions

View file

@ -22,31 +22,31 @@ describe('run.ts', () => {
test('getHelmDownloadURL() - return the URL to download helm for Linux', () => { test('getHelmDownloadURL() - return the URL to download helm for Linux', () => {
jest.spyOn(os, 'type').mockReturnValue('Linux'); jest.spyOn(os, 'type').mockReturnValue('Linux');
const kubectlLinuxUrl = 'https://get.helm.sh/helm-v3.7.2-linux-amd64.zip' const kubectlLinuxUrl = 'https://get.helm.sh/helm-v3.8.0-linux-amd64.zip'
expect(run.getHelmDownloadURL('v3.7.2')).toBe(kubectlLinuxUrl); expect(run.getHelmDownloadURL('v3.8.0')).toBe(kubectlLinuxUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => { test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin'); jest.spyOn(os, 'type').mockReturnValue('Darwin');
const kubectlDarwinUrl = 'https://get.helm.sh/helm-v3.7.2-darwin-amd64.zip' const kubectlDarwinUrl = 'https://get.helm.sh/helm-v3.8.0-darwin-amd64.zip'
expect(run.getHelmDownloadURL('v3.7.2')).toBe(kubectlDarwinUrl); expect(run.getHelmDownloadURL('v3.8.0')).toBe(kubectlDarwinUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getHelmDownloadURL() - return the URL to download helm for Windows', () => { test('getHelmDownloadURL() - return the URL to download helm for Windows', () => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
const kubectlWindowsUrl = 'https://get.helm.sh/helm-v3.7.2-windows-amd64.zip' const kubectlWindowsUrl = 'https://get.helm.sh/helm-v3.8.0-windows-amd64.zip'
expect(run.getHelmDownloadURL('v3.7.2')).toBe(kubectlWindowsUrl); expect(run.getHelmDownloadURL('v3.8.0')).toBe(kubectlWindowsUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getLatestHelmVersion() - return the latest version of HELM', async () => { test('getLatestHelmVersion() - return the latest version of HELM', async () => {
try{ try{
expect(run.getLatestHelmVersion()).toBe("v3.7.2"); expect(run.getLatestHelmVersion()).toBe("v3.8.0");
} catch (e){ } catch (e){
return e; return e;
} }
@ -76,9 +76,9 @@ describe('run.ts', () => {
jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download'); jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download');
jest.spyOn(core, 'warning').mockImplementation(); jest.spyOn(core, 'warning').mockImplementation();
expect(await run.getStableHelmVersion()).toBe('v3.7.2'); expect(await run.getStableHelmVersion()).toBe('v3.8.0');
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
expect(core.warning).toBeCalledWith("Cannot get the latest Helm info from https://api.github.com/repos/helm/helm/releases. Error Unable to download. Using default Helm version v3.7.2."); expect(core.warning).toBeCalledWith("Cannot get the latest Helm info from https://api.github.com/repos/helm/helm/releases. Error Unable to download. Using default Helm version v3.8.0.");
}); });
test('walkSync() - return path to the all files matching fileToFind in dir', () => { test('walkSync() - return path to the all files matching fileToFind in dir', () => {

View file

@ -18,11 +18,10 @@ const path = require("path");
const util = require("util"); const util = require("util");
const fs = require("fs"); const fs = require("fs");
const semver = require("semver"); const semver = require("semver");
const exec = require("@actions/exec");
const toolCache = require("@actions/tool-cache"); const toolCache = require("@actions/tool-cache");
const core = require("@actions/core"); const core = require("@actions/core");
const helmToolName = 'helm'; const helmToolName = 'helm';
const stableHelmVersion = 'v3.7.2'; const stableHelmVersion = 'v3.8.0';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
function getExecutableExtension() { function getExecutableExtension() {
if (os.type().match(/^Win/)) { if (os.type().match(/^Win/)) {
@ -113,27 +112,20 @@ function downloadHelm(version) {
}); });
} }
exports.downloadHelm = downloadHelm; exports.downloadHelm = downloadHelm;
// getLatestHelmVersion uses cURL and regex to scrape the latest version
function getLatestHelmVersion() { function getLatestHelmVersion() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
console.log("Test"); let helmJSONPath = yield toolCache.downloadTool("https://api.github.com/repos/helm/helm/releases");
const 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/".*//'`; let versions;
let latestHelm = ""; const helmJSONArray = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'));
let latestHelmErr = ""; for (const i in helmJSONArray) {
const options = {}; versions.push(helmJSONArray[i]["tag_name"]);
options.listeners = {
stdout: (data) => {
latestHelm += data.toString();
},
stderr: (data) => {
latestHelmErr += data.toString();
} }
}; for (const v in versions) {
yield exec.exec(command, [], options); if (isValidVersion(v)) {
console.log("latestHelm"); return v;
if (latestHelmErr !== "" || !isValidVersion(latestHelm)) }
return getStableHelmVersion(); }
return latestHelm; return stableHelmVersion;
}); });
} }
exports.getLatestHelmVersion = getLatestHelmVersion; exports.getLatestHelmVersion = getLatestHelmVersion;

7510
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8,13 +8,11 @@ import * as util from 'util';
import * as fs from 'fs'; import * as fs from 'fs';
import * as semver from 'semver'; 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 toolCache from '@actions/tool-cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
const helmToolName = 'helm'; const helmToolName = 'helm';
const stableHelmVersion = 'v3.7.2'; const stableHelmVersion = 'v3.8.0';
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'; const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases';
export function getExecutableExtension(): string { export function getExecutableExtension(): string {
@ -105,28 +103,23 @@ export async function downloadHelm(version: string): Promise<string> {
return helmpath; return helmpath;
} }
// getLatestHelmVersion uses cURL and regex to scrape the latest version export async function getLatestHelmVersion(): Promise<string> {
export async function getLatestHelmVersion(): Promise<string>{ let helmJSONPath:string = await toolCache.downloadTool("https://api.github.com/repos/helm/helm/releases");
console.log("Test"); let versions:Array<string>;
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 = {}; const helmJSONArray:JSON = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'))
options.listeners = { for(const i in helmJSONArray){
stdout: (data: Buffer) => { versions.push(helmJSONArray[i]["tag_name"]);
latestHelm += data.toString();
},
stderr: (data: Buffer) => {
latestHelmErr += data.toString();
} }
};
await exec.exec(command, [], options); for(const v in versions){
console.log("latestHelm") if(isValidVersion(v)){
if(latestHelmErr !== "" || !isValidVersion(latestHelm)) return getStableHelmVersion(); return v;
return latestHelm; }
}
return stableHelmVersion;
} }
// isValidVersion checks if verison matches the specified type and is a stable release // isValidVersion checks if verison matches the specified type and is a stable release