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

Conversation

copperwall
Copy link
Member

@copperwall copperwall commented Jul 24, 2021

When a there's either a HTTPS_PROXY or https_proxy environment variable,
the autoProxyAgent plugin creates a HttpsProxyAgent with the value of the
environment variable and includes it to the request options on the before
"request" hook.

This uses the plugin described here octokit/octokit.js#2098 (comment).

Fixes #300

When a there's either a HTTPS_PROXY or https_proxy environment variable,
the autoProxyAgent plugin creates a HttpsProxyAgent with the value of the
environment variable and includes it to the request options on the before
"request" hook.
@ghost ghost added this to Inbox in JS Jul 24, 2021
@wolfy1339 wolfy1339 added the Type: Feature New feature or request label Jul 24, 2021
@ghost ghost moved this from Inbox to Features in JS Jul 24, 2021
@wolfy1339 wolfy1339 changed the title Add https proxy env support feat: add https proxy env support Jul 24, 2021
@copperwall
Copy link
Member Author

Oops sorry I forgot to write the commit messages in a way for semantic-release to read them. @wolfy1339 would it be helpful if I amended those commit messages to follow the conventional commit/semantic release format?

@wolfy1339
Copy link
Member

That's fine. Once we merge, it will use the PR's title (since there are multiple commits) and squash all the commits

src/plugins.ts Outdated
Comment on lines 6 to 14
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 👏🏼

This removes the code that hooks into the request lifecycle and instead
just sets the agent as a default based on the HTTPS_PROXY environment
variable when the Octokit class is instantiated.

Setting the options.request.agent field in the plugin had an issue where
the agent field wasn't being passed to the fetch wrapper in core.js,
since the request options were being set before the plugins were run.

The reason the defaults are passed as a function value instead of an
object is that it was difficult to test different behavior based on env
variables in src/index.ts because it was only run once during the import
at the top of the test file. Using the function lets us change the
environment variable in between test cases and lets the change get
reflected when octokit is instantiated, and not when it's imported.
This lets a user override the agent proxy value when constructing a new octokit
instance
@gr2m gr2m merged commit 3cb8e37 into octokit:master Aug 1, 2021
JS automation moved this from Features to Done Aug 1, 2021
@github-actions
Copy link

github-actions bot commented Aug 1, 2021

🎉 This PR is included in version 3.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
No open projects
JS
  
Done
Development

Successfully merging this pull request may close these issues.

Respect https_proxy environment variables set by GitHub
3 participants