From b003a9a72372809c391a18476f65aef3c5820a47 Mon Sep 17 00:00:00 2001 From: Paul Vollmer Date: Sun, 25 Jun 2023 23:03:53 +0200 Subject: [PATCH 1/4] feat: add downloadBaseURL action input --- action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/action.yml b/action.yml index 78fa3852..cd0c99c7 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,10 @@ inputs: token: description: GitHub token. Required only if 'version' == 'latest' required: false + downloadBaseURL: + description: 'Set the download base URL' + required: false + default: 'https://get.helm.sh' outputs: helm-path: description: 'Path to the cached helm binary' From acb36743c6b7285caaeda797b5a8dfdc1d14409c Mon Sep 17 00:00:00 2001 From: Paul Vollmer Date: Sun, 25 Jun 2023 23:05:25 +0200 Subject: [PATCH 2/4] feat: downloadHelm and getHelmDownloadURL added baseURL function argument --- src/run.test.ts | 50 +++++++++++++++++++++++++++++++------------------ src/run.ts | 22 ++++++++++++---------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/run.test.ts b/src/run.test.ts index 5836f3d9..f4b2f0cf 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -21,11 +21,13 @@ describe('run.ts', () => { }) test('getHelmDownloadURL() - return the URL to download helm for Linux', () => { + const downloadBaseURL = 'https://test.tld' + jest.spyOn(os, 'type').mockReturnValue('Linux') jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') - const helmLinuxUrl = 'https://get.helm.sh/helm-v3.8.0-linux-amd64.zip' + const helmLinuxUrl = 'https://test.tld/helm-v3.8.0-linux-amd64.zip' - expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmLinuxUrl) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmLinuxUrl) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() @@ -33,19 +35,21 @@ describe('run.ts', () => { jest.spyOn(os, 'type').mockReturnValue('Linux') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') const helmLinuxArm64Url = - 'https://get.helm.sh/helm-v3.8.0-linux-arm64.zip' + 'https://test.tld/helm-v3.8.0-linux-arm64.zip' - expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmLinuxArm64Url) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmLinuxArm64Url) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() }) test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => { + const downloadBaseURL = 'https://test.tld' + jest.spyOn(os, 'type').mockReturnValue('Darwin') jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') - const helmDarwinUrl = 'https://get.helm.sh/helm-v3.8.0-darwin-amd64.zip' + const helmDarwinUrl = 'https://test.tld/helm-v3.8.0-darwin-amd64.zip' - expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmDarwinUrl) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmDarwinUrl) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() @@ -53,9 +57,9 @@ describe('run.ts', () => { jest.spyOn(os, 'type').mockReturnValue('Darwin') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') const helmDarwinArm64Url = - 'https://get.helm.sh/helm-v3.8.0-darwin-arm64.zip' + 'https://test.tld/helm-v3.8.0-darwin-arm64.zip' - expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmDarwinArm64Url) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmDarwinArm64Url) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() }) @@ -65,10 +69,12 @@ describe('run.ts', () => { }) test('getHelmDownloadURL() - return the URL to download helm for Windows', () => { + const downloadBaseURL = 'https://test.tld' + jest.spyOn(os, 'type').mockReturnValue('Windows_NT') - const helmWindowsUrl = 'https://get.helm.sh/helm-v3.8.0-windows-amd64.zip' - expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmWindowsUrl) + const helmWindowsUrl = 'https://test.tld/helm-v3.8.0-windows-amd64.zip' + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmWindowsUrl) expect(os.type).toBeCalled() }) @@ -193,12 +199,14 @@ describe('run.ts', () => { return {isDirectory: () => isDirectory} as fs.Stats }) - expect(await run.downloadHelm('v4.0.0')).toBe( + const baseURL = 'https://test.tld' + + expect(await run.downloadHelm(baseURL, 'v4.0.0')).toBe( path.join('pathToCachedDir', 'helm.exe') ) expect(toolCache.find).toBeCalledWith('helm', 'v4.0.0') expect(toolCache.downloadTool).toBeCalledWith( - 'https://get.helm.sh/helm-v4.0.0-windows-amd64.zip' + 'https://test.tld/helm-v4.0.0-windows-amd64.zip' ) expect(fs.chmodSync).toBeCalledWith('pathToTool', '777') expect(toolCache.extractZip).toBeCalledWith('pathToTool') @@ -215,12 +223,14 @@ describe('run.ts', () => { }) jest.spyOn(os, 'type').mockReturnValue('Windows_NT') - await expect(run.downloadHelm('v3.2.1')).rejects.toThrow( - 'Failed to download Helm from location https://get.helm.sh/helm-v3.2.1-windows-amd64.zip' + const baseURL = 'https://test.tld' + + await expect(run.downloadHelm(baseURL, 'v3.2.1')).rejects.toThrow( + 'Failed to download Helm from location https://test.tld/helm-v3.2.1-windows-amd64.zip' ) expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1') expect(toolCache.downloadTool).toBeCalledWith( - 'https://get.helm.sh/helm-v3.2.1-windows-amd64.zip' + 'https://test.tld/helm-v3.2.1-windows-amd64.zip' ) }) @@ -228,7 +238,9 @@ describe('run.ts', () => { jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedDir') jest.spyOn(fs, 'chmodSync').mockImplementation(() => {}) - expect(await run.downloadHelm('v3.2.1')).toBe( + const baseURL = 'https://test.tld' + + expect(await run.downloadHelm(baseURL, 'v3.2.1')).toBe( path.join('pathToCachedDir', 'helm.exe') ) expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1') @@ -254,12 +266,14 @@ describe('run.ts', () => { return {isDirectory: () => isDirectory} as fs.Stats }) - await expect(run.downloadHelm('v3.2.1')).rejects.toThrow( + const baseURL = 'https://test.tld' + + await expect(run.downloadHelm(baseURL, 'v3.2.1')).rejects.toThrow( 'Helm executable not found in path pathToCachedDir' ) expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1') expect(toolCache.downloadTool).toBeCalledWith( - 'https://get.helm.sh/helm-v3.2.1-windows-amd64.zip' + 'https://test.tld/helm-v3.2.1-windows-amd64.zip' ) expect(fs.chmodSync).toBeCalledWith('pathToTool', '777') expect(toolCache.extractZip).toBeCalledWith('pathToTool') diff --git a/src/run.ts b/src/run.ts index f4c5cbbe..9b4524b5 100644 --- a/src/run.ts +++ b/src/run.ts @@ -26,8 +26,10 @@ export async function run() { version = await getLatestHelmVersion() } + const downloadBaseURL = core.getInput('downloadBaseURL', {required: false}) + core.startGroup(`Downloading ${version}`) - const cachedPath = await downloadHelm(version) + const cachedPath = await downloadHelm(downloadBaseURL, version) core.endGroup() try { @@ -105,54 +107,54 @@ const LINUX = 'Linux' const MAC_OS = 'Darwin' const WINDOWS = 'Windows_NT' const ARM64 = 'arm64' -export function getHelmDownloadURL(version: string): string { +export function getHelmDownloadURL(baseURL: string, version: string): string { const arch = os.arch() const operatingSystem = os.type() switch (true) { case operatingSystem == LINUX && arch == ARM64: return util.format( - 'https://get.helm.sh/helm-%s-linux-arm64.zip', + `${baseURL}/helm-%s-linux-arm64.zip`, version ) case operatingSystem == LINUX: return util.format( - 'https://get.helm.sh/helm-%s-linux-amd64.zip', + `${baseURL}/helm-%s-linux-amd64.zip`, version ) case operatingSystem == MAC_OS && arch == ARM64: return util.format( - 'https://get.helm.sh/helm-%s-darwin-arm64.zip', + `${baseURL}/helm-%s-darwin-arm64.zip`, version ) case operatingSystem == MAC_OS: return util.format( - 'https://get.helm.sh/helm-%s-darwin-amd64.zip', + `${baseURL}/helm-%s-darwin-amd64.zip`, version ) case operatingSystem == WINDOWS: default: return util.format( - 'https://get.helm.sh/helm-%s-windows-amd64.zip', + `${baseURL}/helm-%s-windows-amd64.zip`, version ) } } -export async function downloadHelm(version: string): Promise { +export async function downloadHelm(baseURL: string, version: string): Promise { let cachedToolpath = toolCache.find(helmToolName, version) if (!cachedToolpath) { let helmDownloadPath try { helmDownloadPath = await toolCache.downloadTool( - getHelmDownloadURL(version) + getHelmDownloadURL(baseURL, version) ) } catch (exception) { throw new Error( `Failed to download Helm from location ${getHelmDownloadURL( - version + baseURL, version )}` ) } From 67a16f3bed94de1ed351e3541dcd1c620305c430 Mon Sep 17 00:00:00 2001 From: Paul Vollmer Date: Thu, 29 Jun 2023 07:44:24 +0200 Subject: [PATCH 3/4] refactor: building the helm download url --- src/run.ts | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/run.ts b/src/run.ts index 9b4524b5..a6341474 100644 --- a/src/run.ts +++ b/src/run.ts @@ -111,36 +111,28 @@ export function getHelmDownloadURL(baseURL: string, version: string): string { const arch = os.arch() const operatingSystem = os.type() + let urlPath = "" + switch (true) { case operatingSystem == LINUX && arch == ARM64: - return util.format( - `${baseURL}/helm-%s-linux-arm64.zip`, - version - ) + urlPath = util.format(`/helm-%s-linux-arm64.zip`, version) + break case operatingSystem == LINUX: - return util.format( - `${baseURL}/helm-%s-linux-amd64.zip`, - version - ) - + urlPath = util.format(`/helm-%s-linux-amd64.zip`, version) + break case operatingSystem == MAC_OS && arch == ARM64: - return util.format( - `${baseURL}/helm-%s-darwin-arm64.zip`, - version - ) + urlPath = util.format(`/helm-%s-darwin-arm64.zip`, version) + break case operatingSystem == MAC_OS: - return util.format( - `${baseURL}/helm-%s-darwin-amd64.zip`, - version - ) - + urlPath = util.format(`/helm-%s-darwin-amd64.zip`, version) + break case operatingSystem == WINDOWS: default: - return util.format( - `${baseURL}/helm-%s-windows-amd64.zip`, - version - ) + urlPath = util.format(`/helm-%s-windows-amd64.zip`, version) } + + const url = new URL(urlPath, baseURL) + return url.toString() } export async function downloadHelm(baseURL: string, version: string): Promise { From dc95ac6a4d4a5ff3e367aa00333a8bc87756e19f Mon Sep 17 00:00:00 2001 From: Paul Vollmer Date: Thu, 29 Jun 2023 07:46:24 +0200 Subject: [PATCH 4/4] chore: format code --- src/run.test.ts | 26 +++++++++++++++++--------- src/run.ts | 10 +++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/run.test.ts b/src/run.test.ts index f4b2f0cf..36ad1000 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -27,17 +27,20 @@ describe('run.ts', () => { jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') const helmLinuxUrl = 'https://test.tld/helm-v3.8.0-linux-amd64.zip' - expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmLinuxUrl) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe( + helmLinuxUrl + ) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() // arm64 jest.spyOn(os, 'type').mockReturnValue('Linux') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') - const helmLinuxArm64Url = - 'https://test.tld/helm-v3.8.0-linux-arm64.zip' + const helmLinuxArm64Url = 'https://test.tld/helm-v3.8.0-linux-arm64.zip' - expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmLinuxArm64Url) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe( + helmLinuxArm64Url + ) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() }) @@ -49,17 +52,20 @@ describe('run.ts', () => { jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') const helmDarwinUrl = 'https://test.tld/helm-v3.8.0-darwin-amd64.zip' - expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmDarwinUrl) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe( + helmDarwinUrl + ) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() // arm64 jest.spyOn(os, 'type').mockReturnValue('Darwin') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') - const helmDarwinArm64Url = - 'https://test.tld/helm-v3.8.0-darwin-arm64.zip' + const helmDarwinArm64Url = 'https://test.tld/helm-v3.8.0-darwin-arm64.zip' - expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmDarwinArm64Url) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe( + helmDarwinArm64Url + ) expect(os.type).toBeCalled() expect(os.arch).toBeCalled() }) @@ -74,7 +80,9 @@ describe('run.ts', () => { jest.spyOn(os, 'type').mockReturnValue('Windows_NT') const helmWindowsUrl = 'https://test.tld/helm-v3.8.0-windows-amd64.zip' - expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(helmWindowsUrl) + expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe( + helmWindowsUrl + ) expect(os.type).toBeCalled() }) diff --git a/src/run.ts b/src/run.ts index a6341474..4972eff5 100644 --- a/src/run.ts +++ b/src/run.ts @@ -111,7 +111,7 @@ export function getHelmDownloadURL(baseURL: string, version: string): string { const arch = os.arch() const operatingSystem = os.type() - let urlPath = "" + let urlPath = '' switch (true) { case operatingSystem == LINUX && arch == ARM64: @@ -135,7 +135,10 @@ export function getHelmDownloadURL(baseURL: string, version: string): string { return url.toString() } -export async function downloadHelm(baseURL: string, version: string): Promise { +export async function downloadHelm( + baseURL: string, + version: string +): Promise { let cachedToolpath = toolCache.find(helmToolName, version) if (!cachedToolpath) { let helmDownloadPath @@ -146,7 +149,8 @@ export async function downloadHelm(baseURL: string, version: string): Promise