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
Conversation
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.
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? |
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
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; | ||
}); |
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
🎉 This PR is included in version 3.12.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
When a there's either a
HTTPS_PROXY
orhttps_proxy
environment variable,the
autoProxyAgent
plugin creates aHttpsProxyAgent
with the value of theenvironment 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