Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't make heavy API requests multiple times #945

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 161 additions & 161 deletions dist/index.js

Large diffs are not rendered by default.

205 changes: 102 additions & 103 deletions dist/post/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions packages/setup-ocaml/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import {
OPAM_REPOSITORIES,
OPAM_ROOT,
PLATFORM,
RESOLVED_COMPILER,
} from "./constants.js";
import { retrieveLatestOpamRelease } from "./opam.js";
import { retrieveCygwinVersion } from "./windows.js";
import { latestOpamRelease } from "./opam.js";
import { resolvedCompiler } from "./version.js";
import { cygwinVersion } from "./windows.js";

async function composeCygwinCacheKeys() {
const cygwinVersion = await retrieveCygwinVersion();
const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${CYGWIN_MIRROR_ENCODED_URI}-${cygwinVersion}`;
const version = await cygwinVersion;
const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${CYGWIN_MIRROR_ENCODED_URI}-${version}`;
const restoreKeys = [key];
return { key, restoreKeys };
}

async function composeDuneCacheKeys() {
const { workflow, job, runId } = github.context;
const ocamlCompiler = await RESOLVED_COMPILER;
const ocamlCompiler = await resolvedCompiler;
const plainKey = [ocamlCompiler, workflow, job].join();
const hash = crypto.createHash("sha256").update(plainKey).digest("hex");
const key = `${CACHE_PREFIX}-setup-ocaml-dune-${PLATFORM}-${ARCHITECTURE}-${hash}-${runId}`;
Expand All @@ -45,9 +45,9 @@ async function composeDuneCacheKeys() {
}

async function composeOpamCacheKeys() {
const { version: opamVersion } = await retrieveLatestOpamRelease();
const { version: opamVersion } = await latestOpamRelease;
const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox";
const ocamlCompiler = await RESOLVED_COMPILER;
const ocamlCompiler = await resolvedCompiler;
const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join();
const osInfo = await system.osInfo();
const plainKey = [
Expand Down
9 changes: 3 additions & 6 deletions packages/setup-ocaml/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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) {
Expand Down Expand Up @@ -99,7 +98,9 @@ export const GITHUB_TOKEN = core.getInput("github-token");

export const DUNE_CACHE = core.getBooleanInput("dune-cache");

const OCAML_COMPILER = core.getInput("ocaml-compiler", { required: true });
export const OCAML_COMPILER = core.getInput("ocaml-compiler", {
required: true,
});

export const OPAM_DISABLE_SANDBOXING =
// [TODO] unlock this once sandboxing is supported on Windows
Expand All @@ -115,7 +116,3 @@ export const OPAM_REPOSITORIES: [string, string][] = (() => {
) as Record<string, string>;
return Object.entries(repositoriesYaml).reverse();
})();

export const RESOLVED_COMPILER = (async () => {
return await resolveCompiler(OCAML_COMPILER);
})();
4 changes: 2 additions & 2 deletions packages/setup-ocaml/src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
OPAM_REPOSITORIES,
OPAM_ROOT,
PLATFORM,
RESOLVED_COMPILER,
} from "./constants.js";
import { installDune } from "./dune.js";
import {
Expand All @@ -30,6 +29,7 @@ import {
update,
} from "./opam.js";
import { retrieveOpamLocalPackages } from "./packages.js";
import { resolvedCompiler } from "./version.js";
import { setupCygwin } from "./windows.js";

export async function installer() {
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function installer() {
if (!opamCacheHit) {
await repositoryRemoveAll();
await repositoryAddAll(OPAM_REPOSITORIES);
const ocamlCompiler = await RESOLVED_COMPILER;
const ocamlCompiler = await resolvedCompiler;
await installOcaml(ocamlCompiler);
await saveOpamCache();
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/setup-ocaml/src/opam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
updateUnixPackageIndexFiles,
} from "./unix.js";

export async function retrieveLatestOpamRelease() {
export const latestOpamRelease = (async () => {
const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.4.0";
const octokit = github.getOctokit(GITHUB_TOKEN, undefined, retry);
const { data: releases } = await octokit.rest.repos.listReleases({
Expand Down Expand Up @@ -59,11 +59,11 @@ export async function retrieveLatestOpamRelease() {
version: latestRelease.tag_name,
browserDownloadUrl: matchedAssets.browser_download_url,
};
}
})();

async function acquireOpam() {
await core.group("Installing opam", async () => {
const { version, browserDownloadUrl } = await retrieveLatestOpamRelease();
const { version, browserDownloadUrl } = await latestOpamRelease;
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
const opam = PLATFORM !== "windows" ? "opam" : "opam.exe";
if (cachedPath === "") {
Expand Down
12 changes: 6 additions & 6 deletions packages/setup-ocaml/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "node:path";
import * as github from "@actions/github";
import { retry } from "@octokit/plugin-retry";
import * as semver from "semver";
import { GITHUB_TOKEN } from "./constants.js";
import { GITHUB_TOKEN, OCAML_COMPILER } from "./constants.js";

function isSemverValidRange(semverVersion: string) {
return semver.validRange(semverVersion, { loose: true }) !== null;
Expand Down Expand Up @@ -52,9 +52,9 @@ async function resolveVersion(semverVersion: string) {
return matchedFullCompilerVersion;
}

export async function resolveCompiler(compiler: string) {
const resolvedCompiler = isSemverValidRange(compiler)
? `ocaml-base-compiler.${await resolveVersion(compiler)}`
: compiler;
export const resolvedCompiler = (async () => {
const resolvedCompiler = isSemverValidRange(OCAML_COMPILER)
? `ocaml-base-compiler.${await resolveVersion(OCAML_COMPILER)}`
: OCAML_COMPILER;
return resolvedCompiler;
}
})();
Loading