mirror of
https://github.com/Azure/setup-kubectl.git
synced 2025-12-13 13:41:15 +00:00
chore: fix Prettier formatting
This commit is contained in:
parent
0c053a1e47
commit
28c93170c9
2 changed files with 51 additions and 48 deletions
|
|
@ -2,7 +2,7 @@ jest.mock('@octokit/rest', () => {
|
||||||
const listTags = jest.fn()
|
const listTags = jest.fn()
|
||||||
return {
|
return {
|
||||||
Octokit: jest.fn().mockImplementation(() => ({
|
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 () => {
|
test('resolveKubectlVersion() - major.minor expanded to latest patch', async () => {
|
||||||
// mock GitHub tags list
|
// mock GitHub tags list
|
||||||
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
|
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
|
||||||
data: [
|
data: [{name: 'v1.27.13'}, {name: 'v1.27.15'}, {name: 'v1.26.6'}]
|
||||||
{name: 'v1.27.13'}, {name: 'v1.27.15'}, {name: 'v1.26.6'}
|
|
||||||
]
|
|
||||||
} as any)
|
} as any)
|
||||||
|
|
||||||
const tag = await run.resolveKubectlVersion('1.27', octo)
|
const tag = await run.resolveKubectlVersion('1.27', octo)
|
||||||
|
|
@ -197,15 +195,15 @@ describe('Testing all functions in run file.', () => {
|
||||||
} as any)
|
} as any)
|
||||||
const tag = await run.resolveKubectlVersion('v1.27.15', octo)
|
const tag = await run.resolveKubectlVersion('v1.27.15', octo)
|
||||||
expect(tag).toBe('v1.27.15')
|
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.
|
// When only one tag matches the provided major.minor, that tag is returned.
|
||||||
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
|
jest.spyOn(octo.repos, 'listTags').mockResolvedValueOnce({
|
||||||
data: [{name: 'v1.28.0'}, {name: 'v1.27.99'}, {name: 'v1.26.5'}]
|
data: [{name: 'v1.28.0'}, {name: 'v1.27.99'}, {name: 'v1.26.5'}]
|
||||||
} as any)
|
} as any)
|
||||||
const tag = await run.resolveKubectlVersion('1.27', octo)
|
const tag = await run.resolveKubectlVersion('1.27', octo)
|
||||||
expect(tag).toBe('v1.27.99')
|
expect(tag).toBe('v1.27.99')
|
||||||
})
|
})
|
||||||
test('run() - download specified version and set output', async () => {
|
test('run() - download specified version and set output', async () => {
|
||||||
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
|
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
|
||||||
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
||||||
|
|
@ -245,5 +243,4 @@ test('resolveKubectlVersion() - selects the only available matching version', as
|
||||||
path.join('pathToCachedTool', 'kubectl.exe')
|
path.join('pathToCachedTool', 'kubectl.exe')
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
||||||
22
src/run.ts
22
src/run.ts
|
|
@ -94,33 +94,39 @@ export async function downloadKubectl(version: string): Promise<string> {
|
||||||
return kubectlPath
|
return kubectlPath
|
||||||
}
|
}
|
||||||
export async function resolveKubectlVersion(
|
export async function resolveKubectlVersion(
|
||||||
version: string, octo:Octokit): Promise<string> {
|
version: string,
|
||||||
|
octo: Octokit
|
||||||
|
): Promise<string> {
|
||||||
const cleanedVersion = version.trim()
|
const cleanedVersion = version.trim()
|
||||||
|
|
||||||
/*------ detect "major.minor" only ----------------*/
|
/*------ 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) {
|
if (!mmMatch || !mmMatch.groups) {
|
||||||
// User already provided a full version such as 1.27.15 – do nothing.
|
// 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
|
const {major, minor} = mmMatch.groups
|
||||||
|
|
||||||
/* -------------------- fetch recent tags from GitHub ----------------- */
|
/* -------------------- fetch recent tags from GitHub ----------------- */
|
||||||
const resp= await octo.repos.listTags({
|
const resp = await octo.repos.listTags({
|
||||||
owner: 'kubernetes',
|
owner: 'kubernetes',
|
||||||
repo: 'kubernetes',
|
repo: 'kubernetes',
|
||||||
per_page: 100,
|
per_page: 100
|
||||||
})
|
})
|
||||||
|
|
||||||
/* -------------------- find newest patch within that line ------------ */
|
/* -------------------- find newest patch within that line ------------ */
|
||||||
const wantedPrefix = `${major}.${minor}.`
|
const wantedPrefix = `${major}.${minor}.`
|
||||||
const newest = resp.data
|
const newest = resp.data
|
||||||
.map(tag => tag.name.replace(/^v/, '')) // strip leading v
|
.map((tag) => tag.name.replace(/^v/, '')) // strip leading v
|
||||||
.filter(v => v.startsWith(wantedPrefix)) // keep only 1.27.*
|
.filter((v) => v.startsWith(wantedPrefix)) // keep only 1.27.*
|
||||||
.sort(semver.rcompare)[0] // newest first
|
.sort(semver.rcompare)[0] // newest first
|
||||||
|
|
||||||
if (!newest) {
|
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"
|
return `v${newest}` // always return with leading "v"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue