From 8c6cbda7709654876f554f510f13d17aef5b36e1 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Mon, 5 Aug 2024 23:21:26 +0900 Subject: [PATCH] Minimise GitHub API calls Signed-off-by: Sora Morimoto --- CHANGELOG.md | 4 + dist/index.js | 125 +++++++++++++------------- dist/post/index.js | 122 +++++++++++++------------ packages/setup-ocaml/src/cache.ts | 7 +- packages/setup-ocaml/src/constants.ts | 9 +- packages/setup-ocaml/src/installer.ts | 5 +- 6 files changed, 143 insertions(+), 129 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6769408..02af1415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to ## [unreleased] +### Changed + +- Minimise GitHub API calls. + ## [3.0.3] ### Fixed diff --git a/dist/index.js b/dist/index.js index 4d2d3c99..575ec7ce 100644 --- a/dist/index.js +++ b/dist/index.js @@ -48052,7 +48052,7 @@ module.exports = sort /***/ }), -/***/ 3529: +/***/ 3863: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { const parse = __nccwpck_require__(8257) @@ -48074,7 +48074,7 @@ const constants = __nccwpck_require__(9073) const SemVer = __nccwpck_require__(1490) const identifiers = __nccwpck_require__(8587) const parse = __nccwpck_require__(8257) -const valid = __nccwpck_require__(3529) +const valid = __nccwpck_require__(3863) const clean = __nccwpck_require__(7876) const inc = __nccwpck_require__(5009) const diff = __nccwpck_require__(3272) @@ -73410,7 +73410,7 @@ exports.authorizeRequestOnClaimChallenge = authorizeRequestOnClaimChallenge; /***/ }), -/***/ 3863: +/***/ 3230: /***/ ((__unused_webpack_module, exports) => { @@ -73870,7 +73870,7 @@ Object.defineProperty(exports, "serializationPolicy", ({ enumerable: true, get: Object.defineProperty(exports, "serializationPolicyName", ({ enumerable: true, get: function () { return serializationPolicy_js_1.serializationPolicyName; } })); var authorizeRequestOnClaimChallenge_js_1 = __nccwpck_require__(7037); Object.defineProperty(exports, "authorizeRequestOnClaimChallenge", ({ enumerable: true, get: function () { return authorizeRequestOnClaimChallenge_js_1.authorizeRequestOnClaimChallenge; } })); -var authorizeRequestOnTenantChallenge_js_1 = __nccwpck_require__(3863); +var authorizeRequestOnTenantChallenge_js_1 = __nccwpck_require__(3230); Object.defineProperty(exports, "authorizeRequestOnTenantChallenge", ({ enumerable: true, get: function () { return authorizeRequestOnTenantChallenge_js_1.authorizeRequestOnTenantChallenge; } })); //# sourceMappingURL=index.js.map @@ -92133,12 +92133,65 @@ var cache = __nccwpck_require__(6930); var lib_github = __nccwpck_require__(4005); // EXTERNAL MODULE: ../../node_modules/yaml/dist/index.js var dist = __nccwpck_require__(8447); +// EXTERNAL MODULE: ../../node_modules/semver/index.js +var semver = __nccwpck_require__(7546); +;// CONCATENATED MODULE: ./src/version.ts + + + + +function isSemverValidRange(semverVersion) { + return semver.validRange(semverVersion, { loose: true }) !== null; +} +async function getAllCompilerVersions() { + const octokit = lib_github.getOctokit(constants_GITHUB_TOKEN); + const { data: packages } = await octokit.rest.repos.getContent({ + owner: "ocaml", + repo: "opam-repository", + path: "packages/ocaml-base-compiler", + }); + const versions = new Set(); + if (Array.isArray(packages)) { + for (const { path: p } of packages) { + const basename = external_node_path_namespaceObject.basename(p); + const version = basename.replace("ocaml-base-compiler.", ""); + const parsed = semver.parse(version, { loose: true }); + if (parsed !== null) { + const { major, minor: _minor, patch } = parsed; + const minor = major < 5 && _minor < 10 + ? // ocaml-base-compiler.4.00.0, ocaml-base-compiler.4.01.0 + `0${_minor}` + : // ocaml-base-compiler.5.2.0, ocaml-base-compiler.4.14.0 + _minor; + const version = `${major}.${minor}.${patch}`; + versions.add(version); + } + } + } + return [...versions]; +} +async function resolveVersion(semverVersion) { + const compilerVersions = await getAllCompilerVersions(); + const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); + if (matchedFullCompilerVersion === null) { + throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); + } + return matchedFullCompilerVersion; +} +async function resolveCompiler(compiler) { + const resolvedCompiler = isSemverValidRange(compiler) + ? `ocaml-base-compiler.${await resolveVersion(compiler)}` + : compiler; + return resolvedCompiler; +} + ;// CONCATENATED MODULE: ./src/constants.ts + const ARCHITECTURE = (() => { switch (external_node_process_.arch) { case "arm": { @@ -92244,13 +92297,15 @@ const OPAM_REPOSITORIES = (() => { })); return Object.entries(repositories_yaml).reverse(); })(); +const RESOLVED_COMPILER = (async () => { + const resolvedCompiler = await resolveCompiler(OCAML_COMPILER); + return resolvedCompiler; +})(); ;// CONCATENATED MODULE: external "node:fs" const external_node_fs_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs"); // EXTERNAL MODULE: ../../node_modules/@actions/tool-cache/lib/tool-cache.js var tool_cache = __nccwpck_require__(834); -// EXTERNAL MODULE: ../../node_modules/semver/index.js -var semver = __nccwpck_require__(7546); ;// CONCATENATED MODULE: ./src/unix.ts @@ -92446,56 +92501,6 @@ async function repositoryRemoveAll() { }); } -;// CONCATENATED MODULE: ./src/version.ts - - - - -function isSemverValidRange(semverVersion) { - return semver.validRange(semverVersion, { loose: true }) !== null; -} -async function getAllCompilerVersions() { - const octokit = lib_github.getOctokit(constants_GITHUB_TOKEN); - const { data: packages } = await octokit.rest.repos.getContent({ - owner: "ocaml", - repo: "opam-repository", - path: "packages/ocaml-base-compiler", - }); - const versions = new Set(); - if (Array.isArray(packages)) { - for (const { path: p } of packages) { - const basename = external_node_path_namespaceObject.basename(p); - const version = basename.replace("ocaml-base-compiler.", ""); - const parsed = semver.parse(version, { loose: true }); - if (parsed !== null) { - const { major, minor: _minor, patch } = parsed; - const minor = major < 5 && _minor < 10 - ? // ocaml-base-compiler.4.00.0, ocaml-base-compiler.4.01.0 - `0${_minor}` - : // ocaml-base-compiler.5.2.0, ocaml-base-compiler.4.14.0 - _minor; - const version = `${major}.${minor}.${patch}`; - versions.add(version); - } - } - } - return [...versions]; -} -async function resolveVersion(semverVersion) { - const compilerVersions = await getAllCompilerVersions(); - const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); - if (matchedFullCompilerVersion === null) { - throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); - } - return matchedFullCompilerVersion; -} -async function resolveCompiler(compiler) { - const resolvedCompiler = isSemverValidRange(compiler) - ? `ocaml-base-compiler.${await resolveVersion(compiler)}` - : compiler; - return resolvedCompiler; -} - // EXTERNAL MODULE: ../../node_modules/@actions/http-client/lib/index.js var lib = __nccwpck_require__(7301); // EXTERNAL MODULE: ../../node_modules/@actions/io/lib/io.js @@ -109853,7 +109858,6 @@ async function setupCygwin() { - async function composeCygwinCacheKeys() { const cygwinVersion = await getCygwinVersion(); const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${cygwinVersion}`; @@ -109864,7 +109868,7 @@ async function composeDuneCacheKeys() { const platform = PLATFORM.replaceAll(/\W/g, "_"); const architecture = ARCHITECTURE.replaceAll(/\W/g, "_"); const { workflow, job } = lib_github.context; - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; const sha256 = external_node_crypto_namespaceObject.createHash("sha256"); const hash = sha256 .update([architecture, job, ocamlCompiler, platform, workflow].join("")) @@ -109876,7 +109880,7 @@ async function composeDuneCacheKeys() { async function composeOpamCacheKeys() { const { version: opamVersion } = await getLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; const repositories = OPAM_REPOSITORIES.map(([_, value]) => value).join(""); const sha256 = external_node_crypto_namespaceObject.createHash("sha256"); const hash = sha256 @@ -110069,7 +110073,6 @@ async function getOpamLocalPackages() { - async function installer() { if (lib_core.isDebug()) { lib_core.exportVariable("OPAMVERBOSE", 1); @@ -110109,7 +110112,7 @@ async function installer() { await setupOpam(); await repositoryRemoveAll(); await repositoryAddAll(OPAM_REPOSITORIES); - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; if (!opamCacheHit) { await installOcaml(ocamlCompiler); await saveOpamCache(); diff --git a/dist/post/index.js b/dist/post/index.js index a5ccb6ec..f6c51ed0 100644 --- a/dist/post/index.js +++ b/dist/post/index.js @@ -43572,7 +43572,7 @@ module.exports = sort /***/ }), -/***/ 3529: +/***/ 3863: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { const parse = __nccwpck_require__(8257) @@ -43594,7 +43594,7 @@ const constants = __nccwpck_require__(9073) const SemVer = __nccwpck_require__(1490) const identifiers = __nccwpck_require__(8587) const parse = __nccwpck_require__(8257) -const valid = __nccwpck_require__(3529) +const valid = __nccwpck_require__(3863) const clean = __nccwpck_require__(7876) const inc = __nccwpck_require__(5009) const diff = __nccwpck_require__(3272) @@ -68930,7 +68930,7 @@ exports.authorizeRequestOnClaimChallenge = authorizeRequestOnClaimChallenge; /***/ }), -/***/ 3863: +/***/ 3230: /***/ ((__unused_webpack_module, exports) => { @@ -69390,7 +69390,7 @@ Object.defineProperty(exports, "serializationPolicy", ({ enumerable: true, get: Object.defineProperty(exports, "serializationPolicyName", ({ enumerable: true, get: function () { return serializationPolicy_js_1.serializationPolicyName; } })); var authorizeRequestOnClaimChallenge_js_1 = __nccwpck_require__(7037); Object.defineProperty(exports, "authorizeRequestOnClaimChallenge", ({ enumerable: true, get: function () { return authorizeRequestOnClaimChallenge_js_1.authorizeRequestOnClaimChallenge; } })); -var authorizeRequestOnTenantChallenge_js_1 = __nccwpck_require__(3863); +var authorizeRequestOnTenantChallenge_js_1 = __nccwpck_require__(3230); Object.defineProperty(exports, "authorizeRequestOnTenantChallenge", ({ enumerable: true, get: function () { return authorizeRequestOnTenantChallenge_js_1.authorizeRequestOnTenantChallenge; } })); //# sourceMappingURL=index.js.map @@ -87472,12 +87472,65 @@ var lib_github = __nccwpck_require__(4005); var external_node_os_ = __nccwpck_require__(612); // EXTERNAL MODULE: ../../node_modules/yaml/dist/index.js var dist = __nccwpck_require__(8447); +// EXTERNAL MODULE: ../../node_modules/semver/index.js +var semver = __nccwpck_require__(7546); +;// CONCATENATED MODULE: ./src/version.ts + + + + +function isSemverValidRange(semverVersion) { + return semver.validRange(semverVersion, { loose: true }) !== null; +} +async function getAllCompilerVersions() { + const octokit = lib_github.getOctokit(GITHUB_TOKEN); + const { data: packages } = await octokit.rest.repos.getContent({ + owner: "ocaml", + repo: "opam-repository", + path: "packages/ocaml-base-compiler", + }); + const versions = new Set(); + if (Array.isArray(packages)) { + for (const { path: p } of packages) { + const basename = external_node_path_namespaceObject.basename(p); + const version = basename.replace("ocaml-base-compiler.", ""); + const parsed = semver.parse(version, { loose: true }); + if (parsed !== null) { + const { major, minor: _minor, patch } = parsed; + const minor = major < 5 && _minor < 10 + ? // ocaml-base-compiler.4.00.0, ocaml-base-compiler.4.01.0 + `0${_minor}` + : // ocaml-base-compiler.5.2.0, ocaml-base-compiler.4.14.0 + _minor; + const version = `${major}.${minor}.${patch}`; + versions.add(version); + } + } + } + return [...versions]; +} +async function resolveVersion(semverVersion) { + const compilerVersions = await getAllCompilerVersions(); + const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); + if (matchedFullCompilerVersion === null) { + throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); + } + return matchedFullCompilerVersion; +} +async function resolveCompiler(compiler) { + const resolvedCompiler = isSemverValidRange(compiler) + ? `ocaml-base-compiler.${await resolveVersion(compiler)}` + : compiler; + return resolvedCompiler; +} + ;// CONCATENATED MODULE: ./src/constants.ts + const constants_ARCHITECTURE = (() => { switch (external_node_process_.arch) { case "arm": { @@ -87556,7 +87609,7 @@ const DUNE_CACHE = lib_core.getBooleanInput("dune-cache", { required: false, trimWhitespace: true, }); -const constants_OCAML_COMPILER = lib_core.getInput("ocaml-compiler", { +const OCAML_COMPILER = lib_core.getInput("ocaml-compiler", { required: true, trimWhitespace: true, }); @@ -87583,58 +87636,10 @@ const constants_OPAM_REPOSITORIES = (() => { })); return Object.entries(repositories_yaml).reverse(); })(); - -// EXTERNAL MODULE: ../../node_modules/semver/index.js -var semver = __nccwpck_require__(7546); -;// CONCATENATED MODULE: ./src/version.ts - - - - -function isSemverValidRange(semverVersion) { - return semver.validRange(semverVersion, { loose: true }) !== null; -} -async function getAllCompilerVersions() { - const octokit = lib_github.getOctokit(GITHUB_TOKEN); - const { data: packages } = await octokit.rest.repos.getContent({ - owner: "ocaml", - repo: "opam-repository", - path: "packages/ocaml-base-compiler", - }); - const versions = new Set(); - if (Array.isArray(packages)) { - for (const { path: p } of packages) { - const basename = external_node_path_namespaceObject.basename(p); - const version = basename.replace("ocaml-base-compiler.", ""); - const parsed = semver.parse(version, { loose: true }); - if (parsed !== null) { - const { major, minor: _minor, patch } = parsed; - const minor = major < 5 && _minor < 10 - ? // ocaml-base-compiler.4.00.0, ocaml-base-compiler.4.01.0 - `0${_minor}` - : // ocaml-base-compiler.5.2.0, ocaml-base-compiler.4.14.0 - _minor; - const version = `${major}.${minor}.${patch}`; - versions.add(version); - } - } - } - return [...versions]; -} -async function resolveVersion(semverVersion) { - const compilerVersions = await getAllCompilerVersions(); - const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); - if (matchedFullCompilerVersion === null) { - throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); - } - return matchedFullCompilerVersion; -} -async function version_resolveCompiler(compiler) { - const resolvedCompiler = isSemverValidRange(compiler) - ? `ocaml-base-compiler.${await resolveVersion(compiler)}` - : compiler; +const constants_RESOLVED_COMPILER = (async () => { + const resolvedCompiler = await resolveCompiler(OCAML_COMPILER); return resolvedCompiler; -} +})(); ;// CONCATENATED MODULE: ./src/cache.ts @@ -87647,7 +87652,6 @@ async function version_resolveCompiler(compiler) { - async function composeCygwinCacheKeys() { const cygwinVersion = await getCygwinVersion(); const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${cygwinVersion}`; @@ -87658,7 +87662,7 @@ async function composeDuneCacheKeys() { const platform = constants_PLATFORM.replaceAll(/\W/g, "_"); const architecture = constants_ARCHITECTURE.replaceAll(/\W/g, "_"); const { workflow, job } = lib_github.context; - const ocamlCompiler = await version_resolveCompiler(constants_OCAML_COMPILER); + const ocamlCompiler = await constants_RESOLVED_COMPILER; const sha256 = external_node_crypto_namespaceObject.createHash("sha256"); const hash = sha256 .update([architecture, job, ocamlCompiler, platform, workflow].join("")) @@ -87670,7 +87674,7 @@ async function composeDuneCacheKeys() { async function composeOpamCacheKeys() { const { version: opamVersion } = await getLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; const repositories = OPAM_REPOSITORIES.map(([_, value]) => value).join(""); const sha256 = crypto.createHash("sha256"); const hash = sha256 diff --git a/packages/setup-ocaml/src/cache.ts b/packages/setup-ocaml/src/cache.ts index 62f194c7..754ce41a 100644 --- a/packages/setup-ocaml/src/cache.ts +++ b/packages/setup-ocaml/src/cache.ts @@ -11,14 +11,13 @@ import { CYGWIN_MIRROR, CYGWIN_ROOT, DUNE_CACHE_ROOT, - OCAML_COMPILER, OPAM_DISABLE_SANDBOXING, OPAM_REPOSITORIES, OPAM_ROOT, PLATFORM, + RESOLVED_COMPILER, } from "./constants.js"; import { getLatestOpamRelease } from "./opam.js"; -import { resolveCompiler } from "./version.js"; import { getCygwinVersion } from "./windows.js"; async function composeCygwinCacheKeys() { @@ -32,7 +31,7 @@ async function composeDuneCacheKeys() { const platform = PLATFORM.replaceAll(/\W/g, "_"); const architecture = ARCHITECTURE.replaceAll(/\W/g, "_"); const { workflow, job } = github.context; - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; const sha256 = crypto.createHash("sha256"); const hash = sha256 .update([architecture, job, ocamlCompiler, platform, workflow].join("")) @@ -45,7 +44,7 @@ async function composeDuneCacheKeys() { async function composeOpamCacheKeys() { const { version: opamVersion } = await getLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; const repositories = OPAM_REPOSITORIES.map(([_, value]) => value).join(""); const sha256 = crypto.createHash("sha256"); const hash = sha256 diff --git a/packages/setup-ocaml/src/constants.ts b/packages/setup-ocaml/src/constants.ts index 4b65ade0..31530696 100644 --- a/packages/setup-ocaml/src/constants.ts +++ b/packages/setup-ocaml/src/constants.ts @@ -1,9 +1,9 @@ import * as os from "node:os"; import * as path from "node:path"; import * as process from "node:process"; - import * as core from "@actions/core"; import * as yaml from "yaml"; +import { resolveCompiler } from "./version.js"; export const ARCHITECTURE = (() => { switch (process.arch) { @@ -98,7 +98,7 @@ export const DUNE_CACHE = core.getBooleanInput("dune-cache", { trimWhitespace: true, }); -export const OCAML_COMPILER = core.getInput("ocaml-compiler", { +const OCAML_COMPILER = core.getInput("ocaml-compiler", { required: true, trimWhitespace: true, }); @@ -131,3 +131,8 @@ export const OPAM_REPOSITORIES: [string, string][] = (() => { ) as Record; return Object.entries(repositories_yaml).reverse(); })(); + +export const RESOLVED_COMPILER = (async () => { + const resolvedCompiler = await resolveCompiler(OCAML_COMPILER); + return resolvedCompiler; +})(); diff --git a/packages/setup-ocaml/src/installer.ts b/packages/setup-ocaml/src/installer.ts index 375e11a1..9f38d94a 100644 --- a/packages/setup-ocaml/src/installer.ts +++ b/packages/setup-ocaml/src/installer.ts @@ -12,11 +12,11 @@ import { CYGWIN_ROOT_BIN, DUNE_CACHE, DUNE_CACHE_ROOT, - OCAML_COMPILER, OPAM_PIN, OPAM_REPOSITORIES, OPAM_ROOT, PLATFORM, + RESOLVED_COMPILER, } from "./constants.js"; import { installDune } from "./dune.js"; import { @@ -27,7 +27,6 @@ import { setupOpam, } from "./opam.js"; import { getOpamLocalPackages } from "./packages.js"; -import { resolveCompiler } from "./version.js"; import { setupCygwin } from "./windows.js"; export async function installer() { @@ -72,7 +71,7 @@ export async function installer() { await setupOpam(); await repositoryRemoveAll(); await repositoryAddAll(OPAM_REPOSITORIES); - const ocamlCompiler = await resolveCompiler(OCAML_COMPILER); + const ocamlCompiler = await RESOLVED_COMPILER; if (!opamCacheHit) { await installOcaml(ocamlCompiler); await saveOpamCache();