Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add https proxy env support #329

Merged
merged 6 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"@octokit/core": "^3.0.0",
"@octokit/plugin-paginate-rest": "^2.2.4",
"@octokit/plugin-rest-endpoint-methods": "5.5.1",
"@octokit/types": "^6.16.1"
"@octokit/types": "^6.16.1",
"https-proxy-agent": "^5.0.0"
},
"devDependencies": {
"@pika/pack": "^0.5.0",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods
export { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { VERSION } from "./version";
import { autoProxyAgent } from "./plugins";

export const Octokit = Core.plugin(
paginateRest,
legacyRestEndpointMethods
legacyRestEndpointMethods,
autoProxyAgent
).defaults({
authStrategy: createActionAuth,
baseUrl: getApiBaseUrl(),
Expand Down
15 changes: 15 additions & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Octokit } from "@octokit/core";
import { OctokitOptions } from "@octokit/core/dist-types/types";
import { HttpsProxyAgent } from "https-proxy-agent";

export function autoProxyAgent(octokit: Octokit, options: OctokitOptions) {
const proxy = process.env["HTTPS_PROXY"] || process.env["https_proxy"];

if (!proxy) return;

const agent = new HttpsProxyAgent(proxy);

octokit.hook.before("request", (options) => {
options.request.agent = agent;
});
Copy link
Contributor

@gr2m gr2m Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options are mutable at this point. I think instead of hooking into the request lifecycle I'd just set options.request.agent = agent directly.

Suggested change
const proxy = process.env["HTTPS_PROXY"] || process.env["https_proxy"];
if (!proxy) return;
const agent = new HttpsProxyAgent(proxy);
octokit.hook.before("request", (options) => {
options.request.agent = agent;
});
const proxy = process.env["HTTPS_PROXY"] || process.env["https_proxy"];
if (!proxy) return;
options.request.agent = agent

I want to do a proper overall with the @octokit/action module, and turn it into something like @octokit/app which provides an octokit instance plus some app-specific APIs, instead of just being a customized Octokit constructor. That @octokit/action constructor should accept an env option which defaults to process.env, and then sets Octokit options based on that, including setting the request proxy agent. But that will be a bigger project

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response @gr2m, I tried removing the hooks code like you suggested, but I ran into an issue where the fetch wrapper in core.js wasn't receiving the updated request options from the plugin here.

I might be misunderstanding how core.js and plugins work, but it looks like @octokit/request is configured with the request options from the default options and those passed when octokit is constructed before any plugins are run
Request options setup: https://github.com/octokit/core.js/blob/master/src/index.ts#L108
Plugins setup: https://github.com/octokit/core.js/blob/master/src/index.ts#L165

I tried setting the agent in the defaults static method instead, but that led to some issues in the unit tests where the defaults always the HTTPS_PROXY environment variable value when src/index.ts was imported at the top of the test file, and didn't run again when changing environment variables in the test cases. This probably wouldn't be a big issue when using the module in practice, but I changed the defaults to use the function argument so that it'd read the process.env value when the Octokit class was instantiated in each test case instead of once when the module was imported.

How does that sound to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the defaults to use the function argument

that is a much more elegant solution 👏🏼

}
47 changes: 47 additions & 0 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import fetchMock from "fetch-mock";
import { RequestOptions } from "https";
import { HttpsProxyAgent } from "https-proxy-agent";

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

jest.mock("https-proxy-agent");

describe("Smoke test", () => {
beforeEach(() => {
delete process.env.GITHUB_TOKEN;
delete process.env.INPUT_GITHUB_TOKEN;
delete process.env.GITHUB_ACTION;
delete process.env.GITHUB_API_URL;
delete process.env.HTTPS_PROXY;
delete process.env.https_proxy;
});

it("happy path with GITHUB_TOKEN", () => {
Expand Down Expand Up @@ -171,4 +177,45 @@ describe("Smoke test", () => {

expect(data).toStrictEqual({ ok: true });
});

it.each(["HTTPS_PROXY", "https_proxy"])(
"Uses https-proxy-agent with %s env var",
async (https_proxy_env) => {
process.env.GITHUB_TOKEN = "secret123";
process.env.GITHUB_ACTION = "test";
process.env[https_proxy_env] = "https://127.0.0.1";

const fetchSandbox = fetchMock.sandbox();
const mock = fetchSandbox.post(
"path:/repos/octocat/hello-world/issues",
{ id: 1 },
{
body: {
title: "My test issue",
},
}
);

expect(Octokit).toBeInstanceOf(Function);
const octokit = new Octokit({
auth: "secret123",
request: {
fetch: mock,
},
});
await octokit.request("POST /repos/{owner}/{repo}/issues", {
owner: "octocat",
repo: "hello-world",
title: "My test issue",
});

expect(HttpsProxyAgent).toHaveBeenCalledWith("https://127.0.0.1");

const [call] = fetchSandbox.calls();
expect(call[0]).toEqual(
"https://api.github.com/repos/octocat/hello-world/issues"
);
expect((call[1] as RequestOptions).agent).toBeInstanceOf(HttpsProxyAgent);
}
);
});