Skip to content

Commit 205b37b

Browse files
committed
Use ActionState for getRegistryProxy
1 parent 6d70593 commit 205b37b

4 files changed

Lines changed: 36 additions & 35 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/api-client.test.ts‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as actionsUtil from "./actions-util";
77
import * as api from "./api-client";
88
import { DO_NOT_RETRY_STATUSES } from "./api-client";
99
import { ActionsEnvVars, RegistryProxyVars } from "./environment";
10-
import { getTestEnv, setupTests } from "./testing-utils";
10+
import { callee, getTestEnv, setupTests } from "./testing-utils";
1111
import * as util from "./util";
1212

1313
setupTests(test);
@@ -209,30 +209,28 @@ test.serial(
209209
);
210210

211211
test("getRegistryProxy - returns undefined if the proxy is not configured", async (t) => {
212+
const target = callee(api.getRegistryProxy).withArgs();
213+
212214
// Empty environment.
213-
t.is(api.getRegistryProxy(getTestEnv()), undefined);
215+
await target.passes(t.is, undefined);
214216
// Only the host.
215-
t.is(
216-
api.getRegistryProxy(
217-
getTestEnv({ [RegistryProxyVars.PROXY_HOST]: "localhost" }),
218-
),
219-
undefined,
220-
);
217+
await target
218+
.withEnv(getTestEnv({ [RegistryProxyVars.PROXY_HOST]: "localhost" }))
219+
.passes(t.is, undefined);
221220
// Only the port.
222-
t.is(
223-
api.getRegistryProxy(
224-
getTestEnv({ [RegistryProxyVars.PROXY_PORT]: "1234" }),
225-
),
226-
undefined,
227-
);
221+
await target
222+
.withEnv(getTestEnv({ [RegistryProxyVars.PROXY_PORT]: "1234" }))
223+
.passes(t.is, undefined);
228224
});
229225

230226
test("getRegistryProxy - returns value when both vars are set", async (t) => {
231-
const proxy = api.getRegistryProxy(
232-
getTestEnv({
233-
[RegistryProxyVars.PROXY_HOST]: "localhost",
234-
[RegistryProxyVars.PROXY_PORT]: "1234",
235-
}),
236-
);
237-
t.truthy(proxy);
227+
await callee(api.getRegistryProxy)
228+
.withArgs()
229+
.withEnv(
230+
getTestEnv({
231+
[RegistryProxyVars.PROXY_HOST]: "localhost",
232+
[RegistryProxyVars.PROXY_PORT]: "1234",
233+
}),
234+
)
235+
.passes(t.truthy);
238236
});

‎src/api-client.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
fetch as undiciFetch,
1010
} from "undici";
1111

12+
import type { ActionState } from "./action-common";
1213
import { getActionVersion, getRequiredInput } from "./actions-util";
1314
import {
1415
ActionsEnvVars,
@@ -60,14 +61,16 @@ export interface GitHubApiExternalRepoDetails {
6061
* Gets the configuration for the private registry authentication proxy,
6162
* if it is available in the environment.
6263
*
63-
* @param env The environment to query for the proxy host and port.
64+
* @param action The required Action state.
6465
* @returns A `ProxyAgent` corresponding to the private registry proxy,
6566
* or `undefined` if we couldn't retrieve the host and port.
6667
*/
67-
export function getRegistryProxy(env: ReadOnlyEnv): ProxyAgent | undefined {
68-
const host = env.getOptional(RegistryProxyVars.PROXY_HOST);
69-
const port = env.getOptional(RegistryProxyVars.PROXY_PORT);
70-
const cert = env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE);
68+
export function getRegistryProxy(
69+
action: ActionState<["Env"]>,
70+
): ProxyAgent | undefined {
71+
const host = action.env.getOptional(RegistryProxyVars.PROXY_HOST);
72+
const port = action.env.getOptional(RegistryProxyVars.PROXY_PORT);
73+
const cert = action.env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE);
7174

7275
if (host && port) {
7376
return new ProxyAgent({
@@ -102,10 +105,10 @@ export function makeProxyRequestOptions(
102105
* This will run API requests through the private registry authentication proxy
103106
* if it is configured.
104107
*
105-
* @param env The environment to query for the proxy host and port.
108+
* @param action The required Action state.
106109
*/
107-
export function getApiFetch(env: ReadOnlyEnv): typeof undiciFetch {
108-
const dispatcher = getRegistryProxy(env);
110+
export function getApiFetch(action: ActionState<["Env"]>): typeof undiciFetch {
111+
const dispatcher = getRegistryProxy(action);
109112

110113
const proxiedFetch = (req: RequestInfo, init?: RequestInit) => {
111114
return undiciFetch(req, { ...init, dispatcher });

‎src/config/file.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function getRemoteConfig(
7373
Feature.ProxyApiRequests,
7474
);
7575
const proxy = shouldProxyRequest
76-
? api.getRegistryProxy(actionState.env)
76+
? api.getRegistryProxy(actionState)
7777
: undefined;
7878

7979
const response = await api

0 commit comments

Comments
 (0)