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

getLatestHelmVersion refactor

This commit is contained in:
Asa Gayle 2022-01-25 15:40:26 -05:00
parent 822c0c23d0
commit 45f20ed080
3 changed files with 32 additions and 108 deletions

View file

@ -52,35 +52,6 @@ describe('run.ts', () => {
}
});
test('getStableHelmVersion() - download stable version file, read version and return it', async () => {
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool');
const response = JSON.stringify(
[
{
'tag_name': 'v4.0.0'
}, {
'tag_name': 'v3.0.0'
}, {
'tag_name': 'v2.0.0'
}
]
);
jest.spyOn(fs, 'readFileSync').mockReturnValue(response);
expect(await run.getStableHelmVersion()).toBe('v4.0.0');
expect(toolCache.downloadTool).toBeCalled();
expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8');
});
test('getStableHelmVersion() - return default version if error occurs while getting latest version', async () => {
jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download');
jest.spyOn(core, 'warning').mockImplementation();
expect(await run.getStableHelmVersion()).toBe('v3.8.0');
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.8.0.");
});
test('walkSync() - return path to the all files matching fileToFind in dir', () => {
jest.spyOn(fs, 'readdirSync').mockImplementation((file, _) => {
if (file == 'mainFolder') return ['file1' as unknown as fs.Dirent, 'file2' as unknown as fs.Dirent, 'folder1' as unknown as fs.Dirent, 'folder2' as unknown as fs.Dirent];

View file

@ -12,12 +12,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.findHelm = exports.getLatestHelmVersion = exports.downloadHelm = exports.walkSync = exports.getStableHelmVersion = exports.getHelmDownloadURL = exports.getExecutableExtension = void 0;
exports.run = exports.findHelm = exports.getLatestHelmVersion = exports.downloadHelm = exports.walkSync = exports.getHelmDownloadURL = exports.getExecutableExtension = void 0;
const os = require("os");
const path = require("path");
const util = require("util");
const fs = require("fs");
const semver = require("semver");
const toolCache = require("@actions/tool-cache");
const core = require("@actions/core");
const helmToolName = 'helm';
@ -42,33 +41,6 @@ function getHelmDownloadURL(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;
}
}
}
});
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;
});
}
exports.getStableHelmVersion = getStableHelmVersion;
exports.walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
@ -88,7 +60,7 @@ exports.walkSync = function (dir, filelist, fileToFind) {
function downloadHelm(version) {
return __awaiter(this, void 0, void 0, function* () {
if (!version) {
version = yield getStableHelmVersion();
version = yield getLatestHelmVersion();
}
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
@ -112,18 +84,22 @@ function downloadHelm(version) {
});
}
exports.downloadHelm = downloadHelm;
// Downloads the helm release JSON and parses all the recent versions of helm from it.
// Defaults to sending stable helm version if none are valid.
function getLatestHelmVersion() {
return __awaiter(this, void 0, void 0, function* () {
let helmJSONPath = yield toolCache.downloadTool("https://api.github.com/repos/helm/helm/releases");
let versions;
const helmJSONArray = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'));
for (const i in helmJSONArray) {
versions.push(helmJSONArray[i]["tag_name"]);
let helmJSONPath = yield toolCache.downloadTool(helmAllReleasesUrl);
try {
const helmJSONArray = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'));
helmJSONArray.forEach(ver => {
if (isValidVersion(ver["tag_name"])) {
return ver;
}
});
}
for (const v in versions) {
if (isValidVersion(v)) {
return v;
}
catch (err) {
core.warning(util.format("Error while fetching the latest Helm release. Error: %s. Using default Helm version %s", err.toString(), stableHelmVersion));
return stableHelmVersion;
}
return stableHelmVersion;
});

View file

@ -36,31 +36,6 @@ export function getHelmDownloadURL(version: string): string {
}
}
export async function getStableHelmVersion(): Promise<string> {
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;
}
export var walkSync = function (dir, filelist, fileToFind) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
@ -79,7 +54,7 @@ export var walkSync = function (dir, filelist, fileToFind) {
};
export async function downloadHelm(version: string): Promise<string> {
if (!version) { version = await getStableHelmVersion(); }
if (!version) { version = await getLatestHelmVersion(); }
let cachedToolpath = toolCache.find(helmToolName, version);
if (!cachedToolpath) {
let helmDownloadPath;
@ -103,22 +78,24 @@ export async function downloadHelm(version: string): Promise<string> {
return helmpath;
}
// Downloads the helm release JSON and parses all the recent versions of helm from it.
// Defaults to sending stable helm version if none are valid.
export async function getLatestHelmVersion(): Promise<string> {
let helmJSONPath:string = await toolCache.downloadTool("https://api.github.com/repos/helm/helm/releases");
let versions:Array<string>;
const helmJSONArray:JSON = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'))
for(const i in helmJSONArray){
versions.push(helmJSONArray[i]["tag_name"]);
let helmJSONPath:string = await toolCache.downloadTool(helmAllReleasesUrl);
try{
const helmJSONArray = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'))
helmJSONArray.forEach(ver => {
if(isValidVersion(ver["tag_name"])){
return ver;
}
});
} catch(err){
core.warning(util.format("Error while fetching the latest Helm release. Error: %s. Using default Helm version %s", err.toString(), stableHelmVersion));
return stableHelmVersion;
}
for(const v in versions){
if(isValidVersion(v)){
return v;
}
}
return stableHelmVersion;
}