Compare commits

...

7 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
Imran Ismail
430fa071e8 Run jest tests sequentially 2020-09-25 13:06:21 +08:00
Imran Ismail
b0afa08459 Remove unused import 2020-09-25 12:58:37 +08:00
Imran Ismail
9503343f5a Update test.yml 2020-09-25 12:55:49 +08:00
Imran Ismail
51957eba2f Simplify installer logic 2020-09-25 12:53:36 +08:00
Imran Ismail
399ab9d4d6 Fix dependabot config typo 2020-09-25 10:53:18 +08:00
8 changed files with 120 additions and 232 deletions

View File

@@ -3,7 +3,7 @@ on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
- master
- 'releases/*'
jobs:

View File

@@ -10,7 +10,6 @@ process.env['RUNNER_TOOL_CACHE'] = toolDir
process.env['RUNNER_TEMP'] = tempDir
import * as installer from '../src/installer'
import * as semver from 'semver'
const IS_WINDOWS = os.platform() === 'win32'
@@ -18,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())
@@ -43,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())
@@ -59,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())
@@ -75,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

136
dist/index.js vendored
View File

@@ -9082,16 +9082,16 @@ exports.getKustomize = void 0;
// Load tempDirectory before it gets wiped by tool-cache
const rest_1 = __webpack_require__(889);
const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(533));
const os = __importStar(__webpack_require__(87));
const cache = __importStar(__webpack_require__(533));
const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(876));
const fs = __importStar(__webpack_require__(747));
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
const osPlat = os.platform();
const osArch = os.arch();
const octokit = new rest_1.Octokit();
const versionRegex = /\d+\.?\d*\.?\d*/;
const toolName = 'kustomize';
const platform = process.platform;
const arch = process.arch === 'x64' ? 'amd64' : process.arch;
if (!tempDirectory) {
let baseLocation;
if (process.platform === 'win32') {
@@ -9108,58 +9108,24 @@ if (!tempDirectory) {
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
}
function getKustomize(versionSpec) {
function getKustomize(targetVersion) {
return __awaiter(this, void 0, void 0, function* () {
// check cache
let toolPath;
toolPath = tc.find('kustomize', versionSpec);
// If not found in cache, download
if (!toolPath) {
let version;
const c = semver.clean(versionSpec) || '';
// If explicit version
if (semver.valid(c) != null) {
// version to download
version = versionSpec;
}
else {
// query kustomize for a matching version
const match = yield queryLatestMatch(versionSpec);
if (!match) {
throw new Error(`Unable to find Kustomize version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
}
version = match;
}
if (!toolPath) {
// download, extract, cache
toolPath = yield acquireKustomize(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);
}
core.addPath(toolPath);
return core.addPath(kustomizePath);
});
}
exports.getKustomize = getKustomize;
function queryLatestMatch(versionSpec) {
function getMaxSatisfyingVersion(targetVersion) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
let dataFileName;
switch (osPlat) {
case 'linux':
case 'darwin':
case 'win32':
dataFileName = osPlat;
break;
default:
throw new Error(`Unexpected OS '${osPlat}'`);
}
switch (osArch) {
case 'x64':
dataFileName = `${dataFileName}_amd64`;
break;
default:
dataFileName = `${dataFileName}_${osArch}`;
}
const versions = [];
const version = { target: targetVersion };
const availableVersions = new Map();
try {
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.repos.listReleases, {
owner: 'kubernetes-sigs',
@@ -9167,11 +9133,13 @@ function queryLatestMatch(versionSpec) {
})), _c; _c = yield _b.next(), !_c.done;) {
const response = _c.value;
for (const release of response.data) {
if (release.assets.some(asset => asset.name.includes(dataFileName) &&
asset.name.includes('kustomize'))) {
const version = (versionRegex.exec(release.name) || []).shift();
if (version != null) {
versions.push(version);
const matchingAsset = release.assets.find(asset => asset.name.includes('kustomize') &&
asset.name.includes(platform) &&
asset.name.includes(arch));
if (matchingAsset) {
const kustomizeVersion = (versionRegex.exec(release.name) || []).shift();
if (kustomizeVersion != null) {
availableVersions.set(kustomizeVersion, matchingAsset.browser_download_url);
}
}
}
@@ -9184,68 +9152,36 @@ function queryLatestMatch(versionSpec) {
}
finally { if (e_1) throw e_1.error; }
}
return semver.maxSatisfying(versions, versionSpec);
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 url = availableVersions.get(resolved);
return Object.assign(Object.assign({}, version), { resolved, url });
});
}
function acquireKustomize(version) {
function acquireVersion(version) {
return __awaiter(this, void 0, void 0, function* () {
version = semver.clean(version) || '';
let downloadUrl;
const toolFilename = process.platform === 'win32' ? `${toolName}.exe` : toolName;
let toolPath;
let toolFilename = 'kustomize';
const toolName = 'kustomize';
if (osPlat === 'win32') {
toolFilename = `${toolFilename}.exe`;
}
if (semver.gte(version, '3.3.0')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_v${version}_%{os}_%{arch}.tar.gz`;
}
else if (semver.gte(version, '3.2.1')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_kustomize.v${version}_%{os}_%{arch}`;
}
else {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/kustomize_${version}_%{os}_%{arch}`;
}
switch (osPlat) {
case 'win32':
if (semver.lte(version, '3.2.1'))
throw new Error(`Unexpected OS '${osPlat}'`);
downloadUrl = downloadUrl.replace('%{os}', 'windows');
if (semver.lt(version, '3.3.0'))
downloadUrl = `${downloadUrl}.exe`;
break;
case 'linux':
case 'darwin':
downloadUrl = downloadUrl.replace('%{os}', osPlat);
break;
default:
throw new Error(`Unexpected OS '${osPlat}'`);
}
switch (osArch) {
case 'x64':
downloadUrl = downloadUrl.replace('%{arch}', 'amd64');
break;
default:
throw new Error(`Unexpected Arch '${osArch}'`);
}
try {
toolPath = yield tc.downloadTool(downloadUrl);
toolPath = yield cache.downloadTool(version.url);
}
catch (err) {
core.debug(err);
throw new Error(`Failed to download version ${version}: ${err}`);
throw new Error(`Failed to download version ${version.target}: ${err}`);
}
if (downloadUrl.endsWith('.tar.gz')) {
toolPath = yield tc.extractTar(toolPath);
if (version.url.endsWith('.tar.gz')) {
toolPath = yield cache.extractTar(toolPath);
toolPath = path.join(toolPath, toolFilename);
}
switch (osPlat) {
switch (process.platform) {
case 'linux':
case 'darwin':
fs.chmodSync(toolPath, 0o755);
break;
}
return yield tc.cacheFile(toolPath, toolFilename, toolName, version);
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

@@ -10,7 +10,7 @@
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map",
"test": "jest",
"test": "jest -i",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"
},
"repository": {

View File

@@ -1,17 +1,17 @@
// Load tempDirectory before it gets wiped by tool-cache
import {Octokit} from '@octokit/rest'
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as os from 'os'
import * as cache from '@actions/tool-cache'
import * as path from 'path'
import * as semver from 'semver'
import * as fs from 'fs'
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''
const osPlat: string = os.platform()
const osArch: string = os.arch()
const octokit = new Octokit()
const versionRegex = /\d+\.?\d*\.?\d*/
const toolName = 'kustomize'
const platform = process.platform
const arch = process.arch === 'x64' ? 'amd64' : process.arch
if (!tempDirectory) {
let baseLocation
@@ -28,64 +28,31 @@ if (!tempDirectory) {
tempDirectory = path.join(baseLocation, 'actions', 'temp')
}
export async function getKustomize(versionSpec: string): Promise<void> {
// check cache
let toolPath: string
export async function getKustomize(targetVersion: string): Promise<void> {
if (!semver.validRange(targetVersion))
throw new Error(`invalid semver requested: ${targetVersion}`)
toolPath = tc.find('kustomize', versionSpec)
let kustomizePath = cache.find('kustomize', targetVersion)
// If not found in cache, download
if (!toolPath) {
let version: string
const c = semver.clean(versionSpec) || ''
// If explicit version
if (semver.valid(c) != null) {
// version to download
version = versionSpec
} else {
// query kustomize for a matching version
const match = await queryLatestMatch(versionSpec)
if (!match) {
throw new Error(
`Unable to find Kustomize version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
)
}
version = match
}
if (!toolPath) {
// download, extract, cache
toolPath = await acquireKustomize(version)
}
if (!kustomizePath) {
const version = await getMaxSatisfyingVersion(targetVersion)
kustomizePath = await acquireVersion(version)
}
core.addPath(toolPath)
return core.addPath(kustomizePath)
}
async function queryLatestMatch(versionSpec: string): Promise<string | null> {
let dataFileName: string
interface Version {
resolved: string
target: string
url: string
}
switch (osPlat) {
case 'linux':
case 'darwin':
case 'win32':
dataFileName = osPlat
break
default:
throw new Error(`Unexpected OS '${osPlat}'`)
}
switch (osArch) {
case 'x64':
dataFileName = `${dataFileName}_amd64`
break
default:
dataFileName = `${dataFileName}_${osArch}`
}
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,
@@ -95,86 +62,65 @@ async function queryLatestMatch(versionSpec: string): Promise<string | null> {
}
)) {
for (const release of response.data) {
if (
release.assets.some(
asset =>
asset.name.includes(dataFileName) &&
asset.name.includes('kustomize')
)
) {
const version = (versionRegex.exec(release.name) || []).shift()
const matchingAsset = release.assets.find(
asset =>
asset.name.includes('kustomize') &&
asset.name.includes(platform) &&
asset.name.includes(arch)
)
if (version != null) {
versions.push(version)
if (matchingAsset) {
const kustomizeVersion = (versionRegex.exec(release.name) || []).shift()
if (kustomizeVersion != null) {
availableVersions.set(
kustomizeVersion,
matchingAsset.browser_download_url
)
}
}
}
}
return semver.maxSatisfying(versions, versionSpec)
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 url = availableVersions.get(resolved) as string
return {...version, resolved, url}
}
async function acquireKustomize(version: string): Promise<string> {
version = semver.clean(version) || ''
let downloadUrl: string
async function acquireVersion(version: Version): Promise<string> {
const toolFilename =
process.platform === 'win32' ? `${toolName}.exe` : toolName
let toolPath: string
let toolFilename = 'kustomize'
const toolName = 'kustomize'
if (osPlat === 'win32') {
toolFilename = `${toolFilename}.exe`
}
if (semver.gte(version, '3.3.0')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_v${version}_%{os}_%{arch}.tar.gz`
} else if (semver.gte(version, '3.2.1')) {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${version}/kustomize_kustomize.v${version}_%{os}_%{arch}`
} else {
downloadUrl = `https://github.com/kubernetes-sigs/kustomize/releases/download/v${version}/kustomize_${version}_%{os}_%{arch}`
}
switch (osPlat) {
case 'win32':
if (semver.lte(version, '3.2.1'))
throw new Error(`Unexpected OS '${osPlat}'`)
downloadUrl = downloadUrl.replace('%{os}', 'windows')
if (semver.lt(version, '3.3.0')) downloadUrl = `${downloadUrl}.exe`
break
case 'linux':
case 'darwin':
downloadUrl = downloadUrl.replace('%{os}', osPlat)
break
default:
throw new Error(`Unexpected OS '${osPlat}'`)
}
switch (osArch) {
case 'x64':
downloadUrl = downloadUrl.replace('%{arch}', 'amd64')
break
default:
throw new Error(`Unexpected Arch '${osArch}'`)
}
try {
toolPath = await tc.downloadTool(downloadUrl)
toolPath = await cache.downloadTool(version.url)
} catch (err) {
core.debug(err)
throw new Error(`Failed to download version ${version}: ${err}`)
throw new Error(`Failed to download version ${version.target}: ${err}`)
}
if (downloadUrl.endsWith('.tar.gz')) {
toolPath = await tc.extractTar(toolPath)
if (version.url.endsWith('.tar.gz')) {
toolPath = await cache.extractTar(toolPath)
toolPath = path.join(toolPath, toolFilename)
}
switch (osPlat) {
switch (process.platform) {
case 'linux':
case 'darwin':
fs.chmodSync(toolPath, 0o755)
break
}
return await tc.cacheFile(toolPath, toolFilename, toolName, version)
return await cache.cacheFile(toolPath, toolFilename, toolName, version.target)
}