Add action input to set download base url (#113)

* feat: add downloadBaseURL action input

* feat: downloadHelm and getHelmDownloadURL added baseURL function argument

* refactor: building the helm download url

* chore: format code

---------

Co-authored-by: Paul Vollmer <mail@paulvollmer.net>
This commit is contained in:
Paul Vollmer
2024-01-02 15:30:48 +01:00
committed by GitHub
parent ac5ee1fca8
commit d336b89352
3 changed files with 70 additions and 46 deletions

View File

@ -8,6 +8,10 @@ inputs:
token: token:
description: GitHub token. Required only if 'version' == 'latest' description: GitHub token. Required only if 'version' == 'latest'
required: false required: false
downloadBaseURL:
description: 'Set the download base URL'
required: false
default: 'https://get.helm.sh'
outputs: outputs:
helm-path: helm-path:
description: 'Path to the cached helm binary' description: 'Path to the cached helm binary'

View File

@ -21,41 +21,51 @@ describe('run.ts', () => {
}) })
test('getHelmDownloadURL() - return the URL to download helm for Linux', () => { test('getHelmDownloadURL() - return the URL to download helm for Linux', () => {
const downloadBaseURL = 'https://test.tld'
jest.spyOn(os, 'type').mockReturnValue('Linux') jest.spyOn(os, 'type').mockReturnValue('Linux')
jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') 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.type).toBeCalled()
expect(os.arch).toBeCalled() expect(os.arch).toBeCalled()
// arm64 // arm64
jest.spyOn(os, 'type').mockReturnValue('Linux') jest.spyOn(os, 'type').mockReturnValue('Linux')
jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64')
const helmLinuxArm64Url = const helmLinuxArm64Url = 'https://test.tld/helm-v3.8.0-linux-arm64.zip'
'https://get.helm.sh/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.type).toBeCalled()
expect(os.arch).toBeCalled() expect(os.arch).toBeCalled()
}) })
test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => { test('getHelmDownloadURL() - return the URL to download helm for Darwin', () => {
const downloadBaseURL = 'https://test.tld'
jest.spyOn(os, 'type').mockReturnValue('Darwin') jest.spyOn(os, 'type').mockReturnValue('Darwin')
jest.spyOn(os, 'arch').mockReturnValueOnce('unknown') 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.type).toBeCalled()
expect(os.arch).toBeCalled() expect(os.arch).toBeCalled()
// arm64 // arm64
jest.spyOn(os, 'type').mockReturnValue('Darwin') jest.spyOn(os, 'type').mockReturnValue('Darwin')
jest.spyOn(os, 'arch').mockReturnValueOnce('arm64') jest.spyOn(os, 'arch').mockReturnValueOnce('arm64')
const helmDarwinArm64Url = const helmDarwinArm64Url = 'https://test.tld/helm-v3.8.0-darwin-arm64.zip'
'https://get.helm.sh/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.type).toBeCalled()
expect(os.arch).toBeCalled() expect(os.arch).toBeCalled()
}) })
@ -65,10 +75,14 @@ describe('run.ts', () => {
}) })
test('getHelmDownloadURL() - return the URL to download helm for Windows', () => { test('getHelmDownloadURL() - return the URL to download helm for Windows', () => {
const downloadBaseURL = 'https://test.tld'
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
const helmWindowsUrl = 'https://get.helm.sh/helm-v3.8.0-windows-amd64.zip' const helmWindowsUrl = 'https://test.tld/helm-v3.8.0-windows-amd64.zip'
expect(run.getHelmDownloadURL('v3.8.0')).toBe(helmWindowsUrl) expect(run.getHelmDownloadURL(downloadBaseURL, 'v3.8.0')).toBe(
helmWindowsUrl
)
expect(os.type).toBeCalled() expect(os.type).toBeCalled()
}) })
@ -193,12 +207,14 @@ describe('run.ts', () => {
return {isDirectory: () => isDirectory} as fs.Stats 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') path.join('pathToCachedDir', 'helm.exe')
) )
expect(toolCache.find).toBeCalledWith('helm', 'v4.0.0') expect(toolCache.find).toBeCalledWith('helm', 'v4.0.0')
expect(toolCache.downloadTool).toBeCalledWith( 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(fs.chmodSync).toBeCalledWith('pathToTool', '777')
expect(toolCache.extractZip).toBeCalledWith('pathToTool') expect(toolCache.extractZip).toBeCalledWith('pathToTool')
@ -215,12 +231,14 @@ describe('run.ts', () => {
}) })
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
await expect(run.downloadHelm('v3.2.1')).rejects.toThrow( const baseURL = 'https://test.tld'
'Failed to download Helm from location https://get.helm.sh/helm-v3.2.1-windows-amd64.zip'
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.find).toBeCalledWith('helm', 'v3.2.1')
expect(toolCache.downloadTool).toBeCalledWith( 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 +246,9 @@ describe('run.ts', () => {
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedDir') jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedDir')
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {}) 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') path.join('pathToCachedDir', 'helm.exe')
) )
expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1') expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1')
@ -254,12 +274,14 @@ describe('run.ts', () => {
return {isDirectory: () => isDirectory} as fs.Stats 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' 'Helm executable not found in path pathToCachedDir'
) )
expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1') expect(toolCache.find).toBeCalledWith('helm', 'v3.2.1')
expect(toolCache.downloadTool).toBeCalledWith( 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(fs.chmodSync).toBeCalledWith('pathToTool', '777')
expect(toolCache.extractZip).toBeCalledWith('pathToTool') expect(toolCache.extractZip).toBeCalledWith('pathToTool')

View File

@ -26,8 +26,10 @@ export async function run() {
version = await getLatestHelmVersion() version = await getLatestHelmVersion()
} }
const downloadBaseURL = core.getInput('downloadBaseURL', {required: false})
core.startGroup(`Downloading ${version}`) core.startGroup(`Downloading ${version}`)
const cachedPath = await downloadHelm(version) const cachedPath = await downloadHelm(downloadBaseURL, version)
core.endGroup() core.endGroup()
try { try {
@ -105,53 +107,49 @@ const LINUX = 'Linux'
const MAC_OS = 'Darwin' const MAC_OS = 'Darwin'
const WINDOWS = 'Windows_NT' const WINDOWS = 'Windows_NT'
const ARM64 = 'arm64' const ARM64 = 'arm64'
export function getHelmDownloadURL(version: string): string { export function getHelmDownloadURL(baseURL: string, version: string): string {
const arch = os.arch() const arch = os.arch()
const operatingSystem = os.type() const operatingSystem = os.type()
let urlPath = ''
switch (true) { switch (true) {
case operatingSystem == LINUX && arch == ARM64: case operatingSystem == LINUX && arch == ARM64:
return util.format( urlPath = util.format(`/helm-%s-linux-arm64.zip`, version)
'https://get.helm.sh/helm-%s-linux-arm64.zip', break
version
)
case operatingSystem == LINUX: case operatingSystem == LINUX:
return util.format( urlPath = util.format(`/helm-%s-linux-amd64.zip`, version)
'https://get.helm.sh/helm-%s-linux-amd64.zip', break
version
)
case operatingSystem == MAC_OS && arch == ARM64: case operatingSystem == MAC_OS && arch == ARM64:
return util.format( urlPath = util.format(`/helm-%s-darwin-arm64.zip`, version)
'https://get.helm.sh/helm-%s-darwin-arm64.zip', break
version
)
case operatingSystem == MAC_OS: case operatingSystem == MAC_OS:
return util.format( urlPath = util.format(`/helm-%s-darwin-amd64.zip`, version)
'https://get.helm.sh/helm-%s-darwin-amd64.zip', break
version
)
case operatingSystem == WINDOWS: case operatingSystem == WINDOWS:
default: default:
return util.format( urlPath = util.format(`/helm-%s-windows-amd64.zip`, version)
'https://get.helm.sh/helm-%s-windows-amd64.zip',
version
)
} }
const url = new URL(urlPath, baseURL)
return url.toString()
} }
export async function downloadHelm(version: string): Promise<string> { export async function downloadHelm(
baseURL: string,
version: string
): Promise<string> {
let cachedToolpath = toolCache.find(helmToolName, version) let cachedToolpath = toolCache.find(helmToolName, version)
if (!cachedToolpath) { if (!cachedToolpath) {
let helmDownloadPath let helmDownloadPath
try { try {
helmDownloadPath = await toolCache.downloadTool( helmDownloadPath = await toolCache.downloadTool(
getHelmDownloadURL(version) getHelmDownloadURL(baseURL, version)
) )
} catch (exception) { } catch (exception) {
throw new Error( throw new Error(
`Failed to download Helm from location ${getHelmDownloadURL( `Failed to download Helm from location ${getHelmDownloadURL(
baseURL,
version version
)}` )}`
) )