Skip to content

Commit 1f82f7d

Browse files
authored
feat: add proxy support (#102)
Adds support for the following environment variables: - `https_proxy` - `HTTPS_PROXY` - `http_proxy` - `HTTP_PROXY` - `no_proxy` - `NO_PROXY`
1 parent 1f18aab commit 1f82f7d

File tree

8 files changed

+34737
-67
lines changed

8 files changed

+34737
-67
lines changed

dist/main.cjs

+17,352-31
Large diffs are not rendered by default.

dist/post.cjs

+17,344-23
Large diffs are not rendered by default.

lib/request.js

+34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1+
import core from "@actions/core";
12
import { request } from "@octokit/request";
3+
import { ProxyAgent, fetch as undiciFetch } from "undici";
4+
5+
const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");
6+
7+
// https://docs.github.com/actions/hosting-your-own-runners/managing-self-hosted-runners/using-a-proxy-server-with-self-hosted-runners
8+
const proxyUrl =
9+
process.env.https_proxy ||
10+
process.env.HTTPS_PROXY ||
11+
process.env.http_proxy ||
12+
process.env.HTTP_PROXY;
13+
14+
/* c8 ignore start */
15+
// Native support for proxies in Undici is under consideration: https://github.com/nodejs/undici/issues/1650
16+
// Until then, we need to use a custom fetch function to add proxy support.
17+
const proxyFetch = (url, options) => {
18+
const urlHost = new URL(url).hostname;
19+
const noProxy = (process.env.no_proxy || process.env.NO_PROXY || "").split(
20+
","
21+
);
22+
23+
if (!noProxy.includes(urlHost)) {
24+
options = {
25+
...options,
26+
dispatcher: new ProxyAgent(String(proxyUrl)),
27+
};
28+
}
29+
30+
return undiciFetch(url, options);
31+
};
32+
/* c8 ignore stop */
233

334
export default request.defaults({
435
headers: {
536
"user-agent": "actions/create-github-app-token",
637
},
38+
baseUrl,
39+
/* c8 ignore next */
40+
request: proxyUrl ? { fetch: proxyFetch } : {},
741
});

main.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ const skipTokenRevoke = Boolean(
3131
core.getInput("skip-token-revoke") || core.getInput("skip_token_revoke")
3232
);
3333

34-
const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");
35-
3634
main(
3735
appId,
3836
privateKey,
3937
owner,
4038
repositories,
4139
core,
4240
createAppAuth,
43-
request.defaults({ baseUrl }),
41+
request,
4442
skipTokenRevoke
4543
).catch((error) => {
4644
/* c8 ignore next 3 */

package-lock.json

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"@actions/core": "^1.10.1",
1616
"@octokit/auth-app": "^6.0.3",
1717
"@octokit/request": "^8.1.6",
18-
"p-retry": "^6.2.0"
18+
"p-retry": "^6.2.0",
19+
"undici": "^6.6.0"
1920
},
2021
"devDependencies": {
2122
"@sinonjs/fake-timers": "^11.2.2",
@@ -25,7 +26,6 @@
2526
"esbuild": "^0.20.0",
2627
"execa": "^8.0.1",
2728
"open-cli": "^8.0.0",
28-
"undici": "^6.6.0",
2929
"yaml": "^2.3.4"
3030
},
3131
"release": {

post.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import core from "@actions/core";
55
import { post } from "./lib/post.js";
66
import request from "./lib/request.js";
77

8-
const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");
9-
10-
post(core, request.defaults({ baseUrl })).catch((error) => {
8+
post(core, request).catch((error) => {
119
/* c8 ignore next 3 */
1210
console.error(error);
1311
core.setFailed(error.message);

tests/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const DEFAULT_ENV = {
66
GITHUB_REPOSITORY_OWNER: "actions",
77
GITHUB_REPOSITORY: "actions/create-github-app-token",
88
// inputs are set as environment variables with the prefix INPUT_
9-
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
9+
// https://docs.github.com/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
1010
"INPUT_GITHUB-API-URL": "https://api.github.com",
1111
"INPUT_APP-ID": "123456",
1212
// This key is invalidated. It’s from https://github.com/octokit/auth-app.js/issues/465#issuecomment-1564998327.

0 commit comments

Comments
 (0)