chore: fix Prettier formatting

This commit is contained in:
Ogheneobukome Ejaife 2025-06-27 13:39:11 -04:00
parent 0c053a1e47
commit 28c93170c9
No known key found for this signature in database
GPG key ID: F29F0EA1A151DA11
2 changed files with 51 additions and 48 deletions

View file

@ -2,7 +2,7 @@ jest.mock('@octokit/rest', () => {
const listTags = jest.fn()
return {
Octokit: jest.fn().mockImplementation(() => ({
repos: { listTags }
repos: {listTags}
}))
}
})
@ -181,9 +181,7 @@ describe('Testing all functions in run file.', () => {
test('resolveKubectlVersion() - major.minor expanded to latest patch', async () => {
// mock GitHub tags list
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
data: [
{name: 'v1.27.13'}, {name: 'v1.27.15'}, {name: 'v1.26.6'}
]
data: [{name: 'v1.27.13'}, {name: 'v1.27.15'}, {name: 'v1.26.6'}]
} as any)
const tag = await run.resolveKubectlVersion('1.27', octo)
@ -197,15 +195,15 @@ describe('Testing all functions in run file.', () => {
} as any)
const tag = await run.resolveKubectlVersion('v1.27.15', octo)
expect(tag).toBe('v1.27.15')
})
test('resolveKubectlVersion() - selects the only available matching version', async () => {
})
test('resolveKubectlVersion() - selects the only available matching version', async () => {
// When only one tag matches the provided major.minor, that tag is returned.
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
data: [{name: 'v1.28.0'}, {name: 'v1.27.99'}, {name: 'v1.26.5'}]
} as any)
const tag = await run.resolveKubectlVersion('1.27', octo)
expect(tag).toBe('v1.27.99')
})
})
test('run() - download specified version and set output', async () => {
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
@ -245,5 +243,4 @@ test('resolveKubectlVersion() - selects the only available matching version', as
path.join('pathToCachedTool', 'kubectl.exe')
)
})
})

View file

@ -94,33 +94,39 @@ export async function downloadKubectl(version: string): Promise<string> {
return kubectlPath
}
export async function resolveKubectlVersion(
version: string, octo:Octokit): Promise<string> {
version: string,
octo: Octokit
): Promise<string> {
const cleanedVersion = version.trim()
/*------ detect "major.minor" only ----------------*/
const mmMatch=cleanedVersion.match(/^v?(?<major>\d+)\.(?<minor>\d+)$/)
const mmMatch = cleanedVersion.match(/^v?(?<major>\d+)\.(?<minor>\d+)$/)
if (!mmMatch || !mmMatch.groups) {
// User already provided a full version such as 1.27.15 do nothing.
return cleanedVersion.startsWith('v') ? cleanedVersion : `v${cleanedVersion}`
return cleanedVersion.startsWith('v')
? cleanedVersion
: `v${cleanedVersion}`
}
const {major, minor} = mmMatch.groups
/* -------------------- fetch recent tags from GitHub ----------------- */
const resp= await octo.repos.listTags({
const resp = await octo.repos.listTags({
owner: 'kubernetes',
repo: 'kubernetes',
per_page: 100,
per_page: 100
})
/* -------------------- find newest patch within that line ------------ */
const wantedPrefix = `${major}.${minor}.`
const newest = resp.data
.map(tag => tag.name.replace(/^v/, '')) // strip leading v
.filter(v => v.startsWith(wantedPrefix)) // keep only 1.27.*
.map((tag) => tag.name.replace(/^v/, '')) // strip leading v
.filter((v) => v.startsWith(wantedPrefix)) // keep only 1.27.*
.sort(semver.rcompare)[0] // newest first
if (!newest) {
throw new Error(`Could not find any ${wantedPrefix}* tag in kubernetes/kubernetes`)
throw new Error(
`Could not find any ${wantedPrefix}* tag in kubernetes/kubernetes`
)
}
return `v${newest}` // always return with leading "v"