Skip to content

Commit 7db34ae

Browse files
committed
Refactor looking up proxy env vars into getRegistryProxyConfig.
This makes that function testable.
1 parent 8125f87 commit 7db34ae

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

lib/entry-points.js

Lines changed: 9 additions & 4 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,20 @@ test("getRegistryProxy - returns value when both vars are set", async (t) => {
234234
)
235235
.passes(t.truthy);
236236
});
237+
238+
test("getRegistryProxyConfig - gets the configuration from the env vars", async (t) => {
239+
const host = "localhost";
240+
const port = "1234";
241+
const ca = "cert";
242+
243+
await callee(api.getRegistryProxyConfig)
244+
.withArgs()
245+
.withEnv(
246+
getTestEnv({
247+
[RegistryProxyVars.PROXY_HOST]: host,
248+
[RegistryProxyVars.PROXY_PORT]: port,
249+
[RegistryProxyVars.PROXY_CA_CERTIFICATE]: ca,
250+
}),
251+
)
252+
.passes(t.like, { host, port, ca });
253+
});

src/api-client.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,28 @@ export interface GitHubApiExternalRepoDetails {
6565
* if it is available in the environment.
6666
*
6767
* @param action The required Action state.
68+
* @returns The hostname, port, and CA retrieved from the corresponding environment variables.
69+
*/
70+
export function getRegistryProxyConfig(action: ActionState<["ReadOnlyEnv"]>) {
71+
return {
72+
host: action.env.getOptional(RegistryProxyVars.PROXY_HOST),
73+
port: action.env.getOptional(RegistryProxyVars.PROXY_PORT),
74+
ca: action.env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE),
75+
};
76+
}
77+
78+
/**
79+
* Gets the configuration for the private registry authentication proxy,
80+
* and uses it to initialise a corresponding `ProxyAgent`.
81+
*
82+
* @param action The required Action state.
6883
* @returns A `ProxyAgent` corresponding to the private registry proxy,
6984
* or `undefined` if we couldn't retrieve the host and port.
7085
*/
7186
export function getRegistryProxy(
72-
action: ActionState<["Logger", "Env"]>,
87+
action: ActionState<["Logger", "ReadOnlyEnv"]>,
7388
): ProxyAgent | undefined {
74-
const host = action.env.getOptional(RegistryProxyVars.PROXY_HOST);
75-
const port = action.env.getOptional(RegistryProxyVars.PROXY_PORT);
76-
const cert = action.env.getOptional(RegistryProxyVars.PROXY_CA_CERTIFICATE);
89+
const { host, port, ca } = getRegistryProxyConfig(action);
7790

7891
if (host && port) {
7992
const uri = `http://${host}:${port}`;
@@ -84,7 +97,7 @@ export function getRegistryProxy(
8497
uri,
8598
keepAliveTimeout: 10,
8699
keepAliveMaxTimeout: 10,
87-
requestTls: cert ? { ca: cert } : undefined,
100+
requestTls: ca ? { ca } : undefined,
88101
});
89102
}
90103

0 commit comments

Comments
 (0)