This commit is contained in:
Shivam Gupta 2021-03-17 13:39:16 +05:30
parent 93b6cbba4e
commit 41b50dea4b
2 changed files with 53 additions and 92 deletions

View file

@ -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<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;
}
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<string> {
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<string> {
}
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;
}