diff --git a/lib/setup-julia.js b/lib/setup-julia.js index 93079c6..9863b10 100644 --- a/lib/setup-julia.js +++ b/lib/setup-julia.js @@ -18,11 +18,55 @@ Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const exec = __importStar(require("@actions/exec")); const tc = __importStar(require("@actions/tool-cache")); +const https = __importStar(require("https")); const os = __importStar(require("os")); const path = __importStar(require("path")); +const semver = __importStar(require("semver")); // Store information about the environment const osPlat = os.platform(); // possible values: win32 (Windows), linux (Linux), darwin (macOS) core.debug(`platform: ${osPlat}`); +function getJuliaReleases() { + return __awaiter(this, void 0, void 0, function* () { + // Wrap everything in a Promise so that it can be called with await. + return new Promise((resolve, reject) => { + const options = { + hostname: 'api.github.com', + path: '/repos/julialang/julia/releases?per_page=100', + method: 'GET', + headers: { + 'Accept': 'application/vnd.github.v3+json', + 'User-Agent': `${process.env.GITHUB_ACTION} Action running in ${process.env.GITHUB_REPOSITORY}` + } + }; + https.request(options, res => { + let data = ''; + res.on('data', d => { + data += d; + }); + res.on('end', () => { + resolve(JSON.parse(data).map((r) => r.tag_name)); + }); + }).on('error', err => { + reject(new Error(`Error while requesting Julia versions from GitHub:\n${err}`)); + }).end(); + }); + }); +} +function getJuliaVersion(versionInput) { + return __awaiter(this, void 0, void 0, function* () { + if (semver.valid(versionInput) == versionInput) { + // versionInput is a valid version, use it directly + return versionInput; + } + // use the highest available version that matches versionInput + const releases = yield getJuliaReleases(); + const version = semver.maxSatisfying(releases, versionInput); + if (version == null) { + throw `Could not find a Julia version that matches ${versionInput}`; + } + return version; + }); +} function getMajorMinorVersion(version) { return version.split('.').slice(0, 2).join('.'); } @@ -95,8 +139,9 @@ function installJulia(version, arch) { function run() { return __awaiter(this, void 0, void 0, function* () { try { - const version = core.getInput('version'); + const versionInput = core.getInput('version'); const arch = core.getInput('arch'); + const version = yield getJuliaVersion(versionInput); core.debug(`selected Julia version: ${arch}/${version}`); // Search in cache let juliaPath; diff --git a/package.json b/package.json index c3a5865..bec7122 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,11 @@ "license": "MIT", "dependencies": { "@actions/core": "^1.0.0", - "@actions/io": "^1.0.0", "@actions/exec": "^1.0.0", "@actions/github": "^1.0.0", + "@actions/io": "^1.0.0", "@actions/tool-cache": "^1.0.0", - "semver": "^6.1.1" + "semver": "^6.3.0" }, "devDependencies": { "@types/jest": "^24.0.13", diff --git a/src/setup-julia.ts b/src/setup-julia.ts index 8dc74ff..a098030 100644 --- a/src/setup-julia.ts +++ b/src/setup-julia.ts @@ -2,13 +2,61 @@ import * as core from '@actions/core' import * as exec from '@actions/exec' import * as tc from '@actions/tool-cache' +import * as https from 'https' import * as os from 'os' import * as path from 'path' +import * as semver from 'semver' + // Store information about the environment const osPlat = os.platform() // possible values: win32 (Windows), linux (Linux), darwin (macOS) core.debug(`platform: ${osPlat}`) +async function getJuliaReleases(): Promise { + // Wrap everything in a Promise so that it can be called with await. + return new Promise((resolve, reject) => { + const options = { + hostname: 'api.github.com', + path: '/repos/julialang/julia/releases?per_page=100', + method: 'GET', + headers: { + 'Accept': 'application/vnd.github.v3+json', // locks GitHub API version to v3 + 'User-Agent': `${process.env.GITHUB_ACTION} Action running in ${process.env.GITHUB_REPOSITORY}` + } + } + + https.request(options, res => { + let data = '' + + res.on('data', d => { + data += d + }) + + res.on('end', () => { + resolve(JSON.parse(data).map((r) => r.tag_name as string)) + }) + }).on('error', err => { + reject(new Error(`Error while requesting Julia versions from GitHub:\n${err}`)) + }).end() + }) +} + +async function getJuliaVersion(versionInput: string): Promise { + if (semver.valid(versionInput) == versionInput) { + // versionInput is a valid version, use it directly + return versionInput + } + + // use the highest available version that matches versionInput + const releases = await getJuliaReleases() + const version = semver.maxSatisfying(releases, versionInput) + if (version == null) { + throw `Could not find a Julia version that matches ${versionInput}` + } + + return version +} + function getMajorMinorVersion(version: string): string { return version.split('.').slice(0, 2).join('.') } @@ -81,8 +129,9 @@ async function installJulia(version: string, arch: string): Promise { async function run() { try { - const version = core.getInput('version') + const versionInput = core.getInput('version') const arch = core.getInput('arch') + const version = await getJuliaVersion(versionInput) core.debug(`selected Julia version: ${arch}/${version}`) // Search in cache