Skip to content

Commit

Permalink
switching to fetching latest version from the dedicated file (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
elProxy authored Mar 1, 2024
1 parent efbd96d commit ec8dd7c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ inputs:
required: true
default: 'latest'
token:
description: GitHub token. Required only if 'version' == 'latest'
description: GitHub token. Used to be required to fetch the latest version
required: false
deprecationMessage: 'GitHub token is no longer required'
default: '${{ github.token }}'
downloadBaseURL:
description: 'Set the download base URL'
Expand Down
13 changes: 12 additions & 1 deletion src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,18 @@ describe('run.ts', () => {
expect(os.type).toHaveBeenCalled()
})

test('getLatestHelmVersion() - return the stable version of HELM since its not authenticated', async () => {
test('getLatestHelmVersion() - return the latest version of HELM', async () => {
const res = {
status: 200,
text: async () => 'v9.99.999'
} as Response
global.fetch = jest.fn().mockReturnValue(res)
expect(await run.getLatestHelmVersion()).toBe('v9.99.999')
})

test('getLatestHelmVersion() - return the stable version of HELM when simulating a network error', async () => {
const errorMessage: string = 'Network Error'
global.fetch = jest.fn().mockRejectedValueOnce(new Error(errorMessage))
expect(await run.getLatestHelmVersion()).toBe('v3.13.3')
})

Expand Down
30 changes: 3 additions & 27 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as fs from 'fs'

import * as toolCache from '@actions/tool-cache'
import * as core from '@actions/core'
import {Octokit} from '@octokit/action'

const helmToolName = 'helm'
const stableHelmVersion = 'v3.13.3'
Expand Down Expand Up @@ -51,38 +50,15 @@ export function getValidVersion(version: string): string {
// Gets the latest helm version or returns a default stable if getting latest fails
export async function getLatestHelmVersion(): Promise<string> {
try {
const octokit = new Octokit()
const response = await octokit.rest.repos.listReleases({
owner: 'helm',
repo: 'helm',
per_page: 100,
order: 'desc',
sort: 'created'
})

const releases = response.data
const latestValidRelease: string = releases.find(
({tag_name, draft, prerelease}) =>
isValidVersion(tag_name) && !draft && !prerelease
)?.tag_name

if (latestValidRelease) return latestValidRelease
const response = await fetch('https://get.helm.sh/helm-latest-version')
const release = (await response.text()).trim()
return release
} catch (err) {
core.warning(
`Error while fetching latest Helm release: ${err.toString()}. Using default version ${stableHelmVersion}`
)
return stableHelmVersion
}

core.warning(
`Could not find valid release. Using default version ${stableHelmVersion}`
)
return stableHelmVersion
}

// isValidVersion checks if verison is a stable release
function isValidVersion(version: string): boolean {
return version.indexOf('rc') == -1
}

export function getExecutableExtension(): string {
Expand Down

0 comments on commit ec8dd7c

Please sign in to comment.