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

@@ -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;