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: support the no_proxy environment variable #591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {isNil, castArray} = require('lodash');
const resolveProxy = require('./resolve-proxy');

module.exports = (
{
Expand All @@ -15,21 +16,25 @@ module.exports = (
addReleases,
},
{env}
) => ({
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
githubUrl: githubUrl || env.GITHUB_API_URL || env.GH_URL || env.GITHUB_URL,
githubApiPathPrefix: githubApiPathPrefix || env.GH_PREFIX || env.GITHUB_PREFIX || '',
proxy: isNil(proxy) ? env.http_proxy || env.HTTP_PROXY || false : proxy,
assets: assets ? castArray(assets) : assets,
successComment,
failTitle: isNil(failTitle) ? 'The automated release is failing 🚨' : failTitle,
failComment,
labels: isNil(labels) ? ['semantic-release'] : labels === false ? false : castArray(labels),
assignees: assignees ? castArray(assignees) : assignees,
releasedLabels: isNil(releasedLabels)
? [`released<%= nextRelease.channel ? \` on @\${nextRelease.channel}\` : "" %>`]
: releasedLabels === false
? false
: castArray(releasedLabels),
addReleases: isNil(addReleases) ? false : addReleases,
});
) => {
githubUrl ||= env.GITHUB_API_URL || env.GH_URL || env.GITHUB_URL;

return {
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
githubUrl,
githubApiPathPrefix: githubApiPathPrefix || env.GH_PREFIX || env.GITHUB_PREFIX || '',
proxy: isNil(proxy) ? resolveProxy(githubUrl, env) : proxy,
assets: assets ? castArray(assets) : assets,
successComment,
failTitle: isNil(failTitle) ? 'The automated release is failing 🚨' : failTitle,
failComment,
labels: isNil(labels) ? ['semantic-release'] : labels === false ? false : castArray(labels),
assignees: assignees ? castArray(assignees) : assignees,
releasedLabels: isNil(releasedLabels)
? [`released<%= nextRelease.channel ? \` on @\${nextRelease.channel}\` : "" %>`]
: releasedLabels === false
? false
: castArray(releasedLabels),
addReleases: isNil(addReleases) ? false : addReleases,
};
};
24 changes: 24 additions & 0 deletions lib/resolve-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = (githubUrl, env) => {
githubUrl ||= 'https://api.github.com';
const proxy = env.http_proxy || env.HTTP_PROXY || false;
const noProxy = env.no_proxy || env.NO_PROXY;

if (proxy && noProxy) {
const {hostname} = new URL(githubUrl);
for (let noProxyHost of noProxy.split(',')) {
if (noProxyHost === '*') {
return false;
}

if (noProxyHost.startsWith('.')) {
noProxyHost = noProxyHost.slice(1);
}

if (hostname === noProxyHost || hostname.endsWith('.' + noProxyHost)) {
return false;
}
}
}

return proxy;
};
47 changes: 47 additions & 0 deletions test/resolve-proxy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const test = require('ava');
const resolveProxy = require('../lib/resolve-proxy');

test('Resolve proxy with no proxy configuration', (t) => {
t.is(resolveProxy(undefined, {}), false);
});

test('Resolve proxy with no exclusions', (t) => {
t.is(resolveProxy(undefined, {http_proxy: 'proxy.example.com'}), 'proxy.example.com');
});

test('Resolve proxy with no matching exclusion', (t) => {
t.is(
resolveProxy(undefined, {
http_proxy: 'proxy.example.com',
no_proxy: 'notapi.github.com,.example.org,example.net',
}),
'proxy.example.com'
);
});

test('Resolve proxy with matching exclusion', (t) => {
t.is(resolveProxy(undefined, {http_proxy: 'proxy.example.com', no_proxy: 'github.com'}), false);
});

test('Resolve proxy with matching exclusion (leading .)', (t) => {
t.is(resolveProxy(undefined, {http_proxy: 'proxy.example.com', no_proxy: '.github.com'}), false);
});

test('Resolve proxy with global exclusion', (t) => {
t.is(resolveProxy(undefined, {http_proxy: 'proxy.example.com', no_proxy: '*'}), false);
});

test('Resolve proxy with matching GitHub Enterprise exclusion', (t) => {
t.is(
resolveProxy('https://github.example.com/api/v3', {http_proxy: 'proxy.example.com', no_proxy: 'example.com'}),
false
);
});

test('Resolve proxy with uppercase environment variables', (t) => {
t.is(resolveProxy(undefined, {HTTP_PROXY: 'proxy.example.com', NO_PROXY: 'github.com'}), false);
t.is(
resolveProxy(undefined, {HTTP_PROXY: 'proxy.example.com', NO_PROXY: 'subdomain.github.com'}),
'proxy.example.com'
);
});