diff --git a/src/run.test.ts b/src/run.test.ts index e3e288b..3d49295 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -1,14 +1,14 @@ jest.mock('@octokit/rest', () => { - const listTags = jest.fn() - return { - Octokit: jest.fn().mockImplementation(() => ({ - repos: { listTags } - })) - } + const listTags = jest.fn() + return { + Octokit: jest.fn().mockImplementation(() => ({ + repos: {listTags} + })) + } }) import * as run from './run' -import {octo} from './run' +import {octo} from './run' import { getkubectlDownloadURL, getKubectlArch, @@ -25,7 +25,7 @@ describe('Testing all functions in run file.', () => { beforeEach(() => { jest.resetAllMocks() }) - + test('getExecutableExtension() - return .exe when os is Windows', () => { jest.spyOn(os, 'type').mockReturnValue('Windows_NT') expect(getExecutableExtension()).toBe('.exe') @@ -181,31 +181,29 @@ 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) expect(tag).toBe('v1.27.15') }) test('resolveKubectlVersion() - returns matching full version unchanged', async () => { - // When a full version (already with patch) is provided, assume it is returned as is. - // Even if the GitHub tag list contains the same version. - jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({ - data: [{name: 'v1.27.15'}, {name: 'v1.27.13'}] - } 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 () => { - // 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') -}) + // When a full version (already with patch) is provided, assume it is returned as is. + // Even if the GitHub tag list contains the same version. + jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({ + data: [{name: 'v1.27.15'}, {name: 'v1.27.13'}] + } 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 () => { + // 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') ) }) - }) diff --git a/src/run.ts b/src/run.ts index beb17bc..92ea198 100644 --- a/src/run.ts +++ b/src/run.ts @@ -94,34 +94,40 @@ export async function downloadKubectl(version: string): Promise { return kubectlPath } export async function resolveKubectlVersion( - version: string, octo:Octokit): Promise { + version: string, + octo: Octokit +): Promise { const cleanedVersion = version.trim() /*------ detect "major.minor" only ----------------*/ - const mmMatch=cleanedVersion.match(/^v?(?\d+)\.(?\d+)$/) + const mmMatch = cleanedVersion.match(/^v?(?\d+)\.(?\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}` + // User already provided a full version such as 1.27.15 – do nothing. + return cleanedVersion.startsWith('v') + ? cleanedVersion + : `v${cleanedVersion}` } const {major, minor} = mmMatch.groups - /* -------------------- fetch recent tags from GitHub ----------------- */ - const resp= await octo.repos.listTags({ - owner: 'kubernetes', - repo: 'kubernetes', - per_page: 100, - }) + /* -------------------- fetch recent tags from GitHub ----------------- */ + const resp = await octo.repos.listTags({ + owner: 'kubernetes', + repo: 'kubernetes', + 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.* - .sort(semver.rcompare)[0] // newest first + /* -------------------- 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.* + .sort(semver.rcompare)[0] // newest first - if (!newest) { - throw new Error(`Could not find any ${wantedPrefix}* tag in kubernetes/kubernetes`) - } + if (!newest) { + throw new Error( + `Could not find any ${wantedPrefix}* tag in kubernetes/kubernetes` + ) + } - return `v${newest}` // always return with leading "v" + return `v${newest}` // always return with leading "v" }