Compare commits

...

2 Commits

Author SHA1 Message Date
Imran Ismail
ff25099d8b Update manifests 2020-09-25 22:16:19 +08:00
Imran Ismail
359e84cbc9 Cache by requested version name 2020-09-25 22:14:20 +08:00
5 changed files with 76 additions and 59 deletions

View File

@@ -17,18 +17,24 @@ describe('installer tests', () => {
beforeAll(async () => {
await io.rmRF(toolDir)
await io.rmRF(tempDir)
}, 100000)
})
afterAll(async () => {
await io.rmRF(toolDir)
await io.rmRF(tempDir)
}, 100000)
it('Acquires the max satisfying version range', async () => {
await installer.getKustomize('~> 3.0')
const kustomizeDir = path.join(toolDir, 'kustomize', '~> 3.0', os.arch())
expect(fs.existsSync(`${kustomizeDir}.complete`)).toBe(true)
it('Acquires the latest kustomize version 3.x successfully', () => {
expect(async () => await installer.getKustomize('3.x')).not.toThrow()
}, 100000)
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize.exe'))).toBe(true)
} else {
expect(fs.existsSync(path.join(kustomizeDir, 'kustomize'))).toBe(true)
expect(() =>
fs.accessSync(path.join(kustomizeDir, 'kustomize'), fs.constants.X_OK)
).not.toThrow()
}
})
it('Acquires kustomize version 3.2.0 successfully', async () => {
it('Acquires kustomize version 3.2.0', async () => {
await installer.getKustomize('3.2.0')
const kustomizeDir = path.join(toolDir, 'kustomize', '3.2.0', os.arch())
@@ -42,9 +48,9 @@ describe('installer tests', () => {
fs.accessSync(path.join(kustomizeDir, 'kustomize'), fs.constants.X_OK)
).not.toThrow()
}
}, 100000)
})
it('Acquires kustomize version 3.2.1 successfully', async () => {
it('Acquires kustomize version 3.2.1', async () => {
await installer.getKustomize('3.2.1')
const kustomizeDir = path.join(toolDir, 'kustomize', '3.2.1', os.arch())
@@ -58,9 +64,9 @@ describe('installer tests', () => {
fs.accessSync(path.join(kustomizeDir, 'kustomize'), fs.constants.X_OK)
).not.toThrow()
}
}, 100000)
})
it('Acquires kustomize version 3.3.0 successfully', async () => {
it('Acquires kustomize version 3.3.0', async () => {
await installer.getKustomize('3.3.0')
const kustomizeDir = path.join(toolDir, 'kustomize', '3.3.0', os.arch())
@@ -74,7 +80,7 @@ describe('installer tests', () => {
fs.accessSync(path.join(kustomizeDir, 'kustomize'), fs.constants.X_OK)
).not.toThrow()
}
}, 100000)
})
it('Throws if no location contains correct kustomize version', async () => {
let thrown = false

42
dist/index.js vendored
View File

@@ -9108,23 +9108,24 @@ if (!tempDirectory) {
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
}
function getKustomize(versionSpec) {
function getKustomize(targetVersion) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath = cache.find('kustomize', versionSpec);
if (!toolPath) {
const version = yield getMaxSatisfyingVersion(versionSpec);
toolPath = yield acquireVersion(version);
if (!semver.validRange(targetVersion))
throw new Error(`invalid semver requested: ${targetVersion}`);
let kustomizePath = cache.find('kustomize', targetVersion);
if (!kustomizePath) {
const version = yield getMaxSatisfyingVersion(targetVersion);
kustomizePath = yield acquireVersion(version);
}
return core.addPath(toolPath);
return core.addPath(kustomizePath);
});
}
exports.getKustomize = getKustomize;
function getMaxSatisfyingVersion(versionSpec) {
function getMaxSatisfyingVersion(targetVersion) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const versionRange = semver.validRange(versionSpec);
const downloadUrls = new Map();
const versions = [];
const version = { target: targetVersion };
const availableVersions = new Map();
try {
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.repos.listReleases, {
owner: 'kubernetes-sigs',
@@ -9136,10 +9137,9 @@ function getMaxSatisfyingVersion(versionSpec) {
asset.name.includes(platform) &&
asset.name.includes(arch));
if (matchingAsset) {
const version = (versionRegex.exec(release.name) || []).shift();
if (version != null) {
downloadUrls.set(version, matchingAsset.browser_download_url);
versions.push(version);
const kustomizeVersion = (versionRegex.exec(release.name) || []).shift();
if (kustomizeVersion != null) {
availableVersions.set(kustomizeVersion, matchingAsset.browser_download_url);
}
}
}
@@ -9152,12 +9152,12 @@ function getMaxSatisfyingVersion(versionSpec) {
}
finally { if (e_1) throw e_1.error; }
}
const versionToDownload = semver.maxSatisfying(versions, versionRange);
if (!versionToDownload) {
throw new Error(`Unable to find Kustomize version '${versionSpec}' for platform '${platform}' and architecture ${arch}.`);
const resolved = semver.maxSatisfying([...availableVersions.keys()], version.target);
if (!resolved) {
throw new Error(`Unable to find Kustomize version '${version.target}' for platform '${platform}' and architecture ${arch}.`);
}
const downloadUrl = downloadUrls.get(versionToDownload);
return { name: versionToDownload, url: downloadUrl };
const url = availableVersions.get(resolved);
return Object.assign(Object.assign({}, version), { resolved, url });
});
}
function acquireVersion(version) {
@@ -9169,7 +9169,7 @@ function acquireVersion(version) {
}
catch (err) {
core.debug(err);
throw new Error(`Failed to download version ${version.name}: ${err}`);
throw new Error(`Failed to download version ${version.target}: ${err}`);
}
if (version.url.endsWith('.tar.gz')) {
toolPath = yield cache.extractTar(toolPath);
@@ -9181,7 +9181,7 @@ function acquireVersion(version) {
fs.chmodSync(toolPath, 0o755);
break;
}
return yield cache.cacheFile(toolPath, toolFilename, toolName, version.name);
return yield cache.cacheFile(toolPath, toolFilename, toolName, version.target);
});
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -4,8 +4,9 @@ module.exports = {
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
testTimeout: 60000,
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}
}

View File

@@ -28,26 +28,31 @@ if (!tempDirectory) {
tempDirectory = path.join(baseLocation, 'actions', 'temp')
}
export async function getKustomize(versionSpec: string): Promise<void> {
let toolPath = cache.find('kustomize', versionSpec)
export async function getKustomize(targetVersion: string): Promise<void> {
if (!semver.validRange(targetVersion))
throw new Error(`invalid semver requested: ${targetVersion}`)
if (!toolPath) {
const version = await getMaxSatisfyingVersion(versionSpec)
toolPath = await acquireVersion(version)
let kustomizePath = cache.find('kustomize', targetVersion)
if (!kustomizePath) {
const version = await getMaxSatisfyingVersion(targetVersion)
kustomizePath = await acquireVersion(version)
}
return core.addPath(toolPath)
return core.addPath(kustomizePath)
}
interface Version {
name: string
resolved: string
target: string
url: string
}
async function getMaxSatisfyingVersion(versionSpec: string): Promise<Version> {
const versionRange = semver.validRange(versionSpec)
const downloadUrls: Map<string, string> = new Map()
const versions: string[] = []
async function getMaxSatisfyingVersion(
targetVersion: string
): Promise<Version> {
const version = {target: targetVersion}
const availableVersions: Map<string, string> = new Map()
for await (const response of octokit.paginate.iterator(
octokit.repos.listReleases,
@@ -65,27 +70,32 @@ async function getMaxSatisfyingVersion(versionSpec: string): Promise<Version> {
)
if (matchingAsset) {
const version = (versionRegex.exec(release.name) || []).shift()
const kustomizeVersion = (versionRegex.exec(release.name) || []).shift()
if (version != null) {
downloadUrls.set(version, matchingAsset.browser_download_url)
versions.push(version)
if (kustomizeVersion != null) {
availableVersions.set(
kustomizeVersion,
matchingAsset.browser_download_url
)
}
}
}
}
const versionToDownload = semver.maxSatisfying(versions, versionRange)
const resolved = semver.maxSatisfying(
[...availableVersions.keys()],
version.target
)
if (!versionToDownload) {
if (!resolved) {
throw new Error(
`Unable to find Kustomize version '${versionSpec}' for platform '${platform}' and architecture ${arch}.`
`Unable to find Kustomize version '${version.target}' for platform '${platform}' and architecture ${arch}.`
)
}
const downloadUrl = downloadUrls.get(versionToDownload) as string
const url = availableVersions.get(resolved) as string
return {name: versionToDownload, url: downloadUrl}
return {...version, resolved, url}
}
async function acquireVersion(version: Version): Promise<string> {
@@ -97,7 +107,7 @@ async function acquireVersion(version: Version): Promise<string> {
toolPath = await cache.downloadTool(version.url)
} catch (err) {
core.debug(err)
throw new Error(`Failed to download version ${version.name}: ${err}`)
throw new Error(`Failed to download version ${version.target}: ${err}`)
}
if (version.url.endsWith('.tar.gz')) {
@@ -112,5 +122,5 @@ async function acquireVersion(version: Version): Promise<string> {
break
}
return await cache.cacheFile(toolPath, toolFilename, toolName, version.name)
return await cache.cacheFile(toolPath, toolFilename, toolName, version.target)
}