Swap to GraphQL and add logging (#83)

* swap to graphql

* add logs

* swap to group log

* fix test
This commit is contained in:
Oliver King
2022-07-11 10:12:11 -04:00
committed by GitHub
parent 6e32762c2e
commit 484a64052d
3 changed files with 40 additions and 36 deletions

View File

@ -74,12 +74,8 @@ describe('run.ts', () => {
expect(os.type).toBeCalled()
})
test('getLatestHelmVersion() - return the latest version of HELM', async () => {
try {
expect(await run.getLatestHelmVersion()).toBe('v3.8.0')
} catch (e) {
return e
}
test('getLatestHelmVersion() - return the stable version of HELM since its not authenticated', async () => {
expect(await run.getLatestHelmVersion()).toBe('v3.9.0')
})
test('walkSync() - return path to the all files matching fileToFind in dir', () => {

View File

@ -9,23 +9,25 @@ import * as fs from 'fs'
import * as toolCache from '@actions/tool-cache'
import * as core from '@actions/core'
import {graphql} from '@octokit/graphql'
const helmToolName = 'helm'
const stableHelmVersion = 'v3.8.0'
const helmAllReleasesUrl = 'https://api.github.com/repos/helm/helm/releases'
const stableHelmVersion = 'v3.9.0'
export async function run() {
let version = core.getInput('version', {required: true})
if (version !== 'latest' && version[0] !== 'v') {
core.info('Getting latest Helm version')
version = getValidVersion(version)
}
if (version.toLocaleLowerCase() === 'latest') {
version = await getLatestHelmVersion()
}
core.debug(util.format('Downloading %s', version))
let cachedPath = await downloadHelm(version)
core.startGroup(`Downloading ${version}`)
const cachedPath = await downloadHelm(version)
core.endGroup()
try {
if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) {
@ -35,41 +37,46 @@ export async function run() {
//do nothing, set as output variable
}
console.log(
`Helm tool version: '${version}' has been cached at ${cachedPath}`
)
core.info(`Helm tool version '${version}' has been cached at ${cachedPath}`)
core.setOutput('helm-path', cachedPath)
}
//Returns version with proper v before it
// Prefixes version with v
export function getValidVersion(version: string): string {
return 'v' + version
}
// Downloads the helm releases JSON and parses all the recent versions of helm from it.
// Defaults to sending stable helm version if none are valid or if it fails
// Gets the latest helm version or returns a default stable if getting latest fails
export async function getLatestHelmVersion(): Promise<string> {
const helmJSONPath: string = await toolCache.downloadTool(helmAllReleasesUrl)
try {
const helmJSON = JSON.parse(fs.readFileSync(helmJSONPath, 'utf-8'))
for (let i in helmJSON) {
if (isValidVersion(helmJSON[i].tag_name)) {
return helmJSON[i].tag_name
}
}
const {repository} = await graphql(
`
{
repository(name: "helm", owner: "helm") {
releases(last: 100) {
nodes {
tagName
}
}
}
}
`
)
const releases = repository.releases.nodes.reverse()
const latestValidRelease = releases.find((release: {tagName: string}) =>
isValidVersion(release.tagName)
)
if (latestValidRelease) return latestValidRelease
} catch (err) {
core.warning(
util.format(
'Error while fetching the latest Helm release. Error: %s. Using default Helm version %s',
err.toString(),
stableHelmVersion
)
`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
}
@ -135,10 +142,9 @@ export async function downloadHelm(version: string): Promise<string> {
)
} catch (exception) {
throw new Error(
util.format(
'Failed to download Helm from location',
getHelmDownloadURL(version)
)
`Failed to download Helm from location ${getHelmDownloadURL(
version
)}`
)
}