11import * as core from "@actions/core" ;
22import * as githubUtils from "@actions/github/lib/utils" ;
33import * as retry from "@octokit/plugin-retry" ;
4+ import {
5+ ProxyAgent ,
6+ RequestInfo ,
7+ RequestInit ,
8+ fetch as undiciFetch ,
9+ } from "undici" ;
410
511import { getActionVersion , getRequiredInput } from "./actions-util" ;
6- import { EnvVar , ReadOnlyEnv , ActionsEnvVars , getEnv } from "./environment" ;
12+ import {
13+ ActionsEnvVars ,
14+ EnvVar ,
15+ ReadOnlyEnv ,
16+ RegistryProxyVars ,
17+ getEnv ,
18+ } from "./environment" ;
719import { Logger } from "./logging" ;
820import { getRepositoryNwo , RepositoryNwo } from "./repository" ;
921import {
@@ -43,6 +55,47 @@ export interface GitHubApiExternalRepoDetails {
4355 apiURL : string | undefined ;
4456}
4557
58+ /**
59+ * Gets the configuration for the private registry authentication proxy,
60+ * if it is available in the environment.
61+ *
62+ * @param env The environment to query for the proxy host and port.
63+ * @returns A `ProxyAgent` corresponding to the private registry proxy,
64+ * or `undefined` if we couldn't retrieve the host and port.
65+ */
66+ export function getRegistryProxy ( env : ReadOnlyEnv ) : ProxyAgent | undefined {
67+ const host = env . getOptional ( RegistryProxyVars . PROXY_HOST ) ;
68+ const port = env . getOptional ( RegistryProxyVars . PROXY_PORT ) ;
69+ const cert = env . getOptional ( RegistryProxyVars . PROXY_CA_CERTIFICATE ) ;
70+
71+ if ( host && port ) {
72+ return new ProxyAgent ( {
73+ uri : `http://${ host } :${ port } ` ,
74+ keepAliveTimeout : 10 ,
75+ keepAliveMaxTimeout : 10 ,
76+ requestTls : cert ? { ca : cert } : undefined ,
77+ } ) ;
78+ }
79+
80+ return undefined ;
81+ }
82+
83+ /**
84+ * Returns an implementation of `fetch` to use for API requests.
85+ * This will run API requests through the private registry authentication proxy
86+ * if it is configured.
87+ *
88+ * @param env The environment to query for the proxy host and port.
89+ */
90+ export function getApiFetch ( env : ReadOnlyEnv ) : typeof undiciFetch {
91+ const dispatcher = getRegistryProxy ( env ) ;
92+
93+ const proxiedFetch = ( req : RequestInfo , init ?: RequestInit ) => {
94+ return undiciFetch ( req , { ...init , dispatcher } ) ;
95+ } ;
96+ return proxiedFetch ;
97+ }
98+
4699function createApiClientWithDetails (
47100 apiDetails : GitHubApiCombinedDetails ,
48101 { allowExternal = false } = { } ,
@@ -60,6 +113,7 @@ function createApiClientWithDetails(
60113 warn : core . warning ,
61114 error : core . error ,
62115 } ,
116+ request : { fetch : getApiFetch ( getEnv ( ) ) } ,
63117 retry : {
64118 doNotRetry : DO_NOT_RETRY_STATUSES ,
65119 } ,
0 commit comments