Skip to content

Commit 3cb8e37

Browse files
authored
feat: add https proxy env support (#329)
1 parent dbb390f commit 3cb8e37

File tree

4 files changed

+117
-10
lines changed

4 files changed

+117
-10
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"@octokit/core": "^3.0.0",
2828
"@octokit/plugin-paginate-rest": "^2.2.4",
2929
"@octokit/plugin-rest-endpoint-methods": "5.5.2",
30-
"@octokit/types": "^6.16.1"
30+
"@octokit/types": "^6.16.1",
31+
"https-proxy-agent": "^5.0.0"
3132
},
3233
"devDependencies": {
3334
"@pika/pack": "^0.5.0",

src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,38 @@ import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods
55
export { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
66

77
import { VERSION } from "./version";
8+
import { OctokitOptions } from "@octokit/core/dist-types/types";
9+
import { HttpsProxyAgent } from "https-proxy-agent";
810

9-
export const Octokit = Core.plugin(
10-
paginateRest,
11-
legacyRestEndpointMethods
12-
).defaults({
11+
const DEFAULTS = {
1312
authStrategy: createActionAuth,
1413
baseUrl: getApiBaseUrl(),
1514
userAgent: `octokit-action.js/${VERSION}`,
15+
};
16+
17+
export const Octokit = Core.plugin(
18+
paginateRest,
19+
legacyRestEndpointMethods
20+
).defaults(function buildDefaults(options: OctokitOptions): OctokitOptions {
21+
return {
22+
...DEFAULTS,
23+
...options,
24+
request: {
25+
agent: getHttpsProxyAgent(),
26+
...options.request
27+
},
28+
};
1629
});
1730

1831
function getApiBaseUrl(): string {
1932
/* istanbul ignore next */
2033
return process.env["GITHUB_API_URL"] || "https://api.github.com";
2134
}
35+
36+
function getHttpsProxyAgent(): HttpsProxyAgent | undefined {
37+
const proxy = process.env["HTTPS_PROXY"] || process.env["https_proxy"];
38+
39+
if (!proxy) return undefined;
40+
41+
return new HttpsProxyAgent(proxy);
42+
}

test/smoke.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import fetchMock from "fetch-mock";
2+
import { RequestOptions } from "https";
3+
import { HttpsProxyAgent } from "https-proxy-agent";
24

35
import { Octokit } from "../src";
46

7+
jest.mock("https-proxy-agent");
8+
59
describe("Smoke test", () => {
610
beforeEach(() => {
711
delete process.env.GITHUB_TOKEN;
812
delete process.env.INPUT_GITHUB_TOKEN;
913
delete process.env.GITHUB_ACTION;
1014
delete process.env.GITHUB_API_URL;
15+
delete process.env.HTTPS_PROXY;
16+
delete process.env.https_proxy;
1117
});
1218

1319
it("happy path with GITHUB_TOKEN", () => {
@@ -171,4 +177,87 @@ describe("Smoke test", () => {
171177

172178
expect(data).toStrictEqual({ ok: true });
173179
});
180+
181+
it.each(["HTTPS_PROXY", "https_proxy"])(
182+
"Uses https-proxy-agent with %s env var",
183+
async (https_proxy_env) => {
184+
process.env.GITHUB_TOKEN = "secret123";
185+
process.env.GITHUB_ACTION = "test";
186+
process.env[https_proxy_env] = "https://127.0.0.1";
187+
188+
const fetchSandbox = fetchMock.sandbox();
189+
const mock = fetchSandbox.post(
190+
"path:/repos/octocat/hello-world/issues",
191+
{ id: 1 },
192+
{
193+
body: {
194+
title: "My test issue",
195+
},
196+
}
197+
);
198+
199+
expect(Octokit).toBeInstanceOf(Function);
200+
const octokit = new Octokit({
201+
auth: "secret123",
202+
request: {
203+
fetch: mock,
204+
},
205+
});
206+
await octokit.request("POST /repos/{owner}/{repo}/issues", {
207+
owner: "octocat",
208+
repo: "hello-world",
209+
title: "My test issue",
210+
});
211+
212+
expect(HttpsProxyAgent).toHaveBeenCalledWith("https://127.0.0.1");
213+
214+
const [call] = fetchSandbox.calls();
215+
expect(call[0]).toEqual(
216+
"https://api.github.com/repos/octocat/hello-world/issues"
217+
);
218+
expect((call[1] as RequestOptions).agent).toBeInstanceOf(HttpsProxyAgent);
219+
}
220+
);
221+
222+
it(
223+
"Uses the explicitly provided request.agent value if it's provided",
224+
async () => {
225+
process.env.GITHUB_TOKEN = "secret123";
226+
process.env.GITHUB_ACTION = "test";
227+
process.env.HTTPS_PROXY = "https://127.0.0.1";
228+
229+
const fetchSandbox = fetchMock.sandbox();
230+
const mock = fetchSandbox.post(
231+
"path:/repos/octocat/hello-world/issues",
232+
{ id: 1 },
233+
{
234+
body: {
235+
title: "My test issue",
236+
},
237+
}
238+
);
239+
240+
expect(Octokit).toBeInstanceOf(Function);
241+
const octokit = new Octokit({
242+
auth: "secret123",
243+
request: {
244+
fetch: mock,
245+
agent: null
246+
},
247+
});
248+
await octokit.request("POST /repos/{owner}/{repo}/issues", {
249+
owner: "octocat",
250+
repo: "hello-world",
251+
title: "My test issue",
252+
});
253+
254+
expect(HttpsProxyAgent).toHaveBeenCalledWith("https://127.0.0.1");
255+
256+
const [call] = fetchSandbox.calls();
257+
expect(call[0]).toEqual(
258+
"https://api.github.com/repos/octocat/hello-world/issues"
259+
);
260+
expect((call[1] as RequestOptions).agent).toBeNull();
261+
}
262+
);
174263
});

0 commit comments

Comments
 (0)