Use ActionState for getRegistryProxy

This commit is contained in:
Michael B. Gale
2026-07-14 16:17:51 +01:00
parent 6d70593fb7
commit 205b37b035
4 changed files with 36 additions and 35 deletions

View File

@@ -7,7 +7,7 @@ import * as actionsUtil from "./actions-util";
import * as api from "./api-client";
import { DO_NOT_RETRY_STATUSES } from "./api-client";
import { ActionsEnvVars, RegistryProxyVars } from "./environment";
import { getTestEnv, setupTests } from "./testing-utils";
import { callee, getTestEnv, setupTests } from "./testing-utils";
import * as util from "./util";
setupTests(test);
@@ -209,30 +209,28 @@ test.serial(
);
test("getRegistryProxy - returns undefined if the proxy is not configured", async (t) => {
const target = callee(api.getRegistryProxy).withArgs();
// Empty environment.
t.is(api.getRegistryProxy(getTestEnv()), undefined);
await target.passes(t.is, undefined);
// Only the host.
t.is(
api.getRegistryProxy(
getTestEnv({ [RegistryProxyVars.PROXY_HOST]: "localhost" }),
),
undefined,
);
await target
.withEnv(getTestEnv({ [RegistryProxyVars.PROXY_HOST]: "localhost" }))
.passes(t.is, undefined);
// Only the port.
t.is(
api.getRegistryProxy(
getTestEnv({ [RegistryProxyVars.PROXY_PORT]: "1234" }),
),
undefined,
);
await target
.withEnv(getTestEnv({ [RegistryProxyVars.PROXY_PORT]: "1234" }))
.passes(t.is, undefined);
});
test("getRegistryProxy - returns value when both vars are set", async (t) => {
const proxy = api.getRegistryProxy(
getTestEnv({
[RegistryProxyVars.PROXY_HOST]: "localhost",
[RegistryProxyVars.PROXY_PORT]: "1234",
}),
);
t.truthy(proxy);
await callee(api.getRegistryProxy)
.withArgs()
.withEnv(
getTestEnv({
[RegistryProxyVars.PROXY_HOST]: "localhost",
[RegistryProxyVars.PROXY_PORT]: "1234",
}),
)
.passes(t.truthy);
});

View File

@@ -9,6 +9,7 @@ import {
fetch as undiciFetch,
} from "undici";
import type { ActionState } from "./action-common";
import { getActionVersion, getRequiredInput } from "./actions-util";
import {
ActionsEnvVars,
@@ -60,14 +61,16 @@ export interface GitHubApiExternalRepoDetails {
* Gets the configuration for the private registry authentication proxy,
* if it is available in the environment.
*
* @param env The environment to query for the proxy host and port.
* @param action The required Action state.
* @returns A `ProxyAgent` corresponding to the private registry proxy,
* or `undefined` if we couldn't retrieve the host and port.
*/
export function getRegistryProxy(env: ReadOnlyEnv): ProxyAgent | undefined {
const host = env.getOptional(RegistryProxyVars.PROXY_HOST);
const port = env.getOptional(RegistryProxyVars.PROXY_PORT);
const cert = env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE);
export function getRegistryProxy(
action: ActionState<["Env"]>,
): ProxyAgent | undefined {
const host = action.env.getOptional(RegistryProxyVars.PROXY_HOST);
const port = action.env.getOptional(RegistryProxyVars.PROXY_PORT);
const cert = action.env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE);
if (host && port) {
return new ProxyAgent({
@@ -102,10 +105,10 @@ export function makeProxyRequestOptions(
* This will run API requests through the private registry authentication proxy
* if it is configured.
*
* @param env The environment to query for the proxy host and port.
* @param action The required Action state.
*/
export function getApiFetch(env: ReadOnlyEnv): typeof undiciFetch {
const dispatcher = getRegistryProxy(env);
export function getApiFetch(action: ActionState<["Env"]>): typeof undiciFetch {
const dispatcher = getRegistryProxy(action);
const proxiedFetch = (req: RequestInfo, init?: RequestInit) => {
return undiciFetch(req, { ...init, dispatcher });

View File

@@ -73,7 +73,7 @@ export async function getRemoteConfig(
Feature.ProxyApiRequests,
);
const proxy = shouldProxyRequest
? api.getRegistryProxy(actionState.env)
? api.getRegistryProxy(actionState)
: undefined;
const response = await api