Skip to content

Commit

Permalink
feat: support system proxy (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alicia Lopez committed Jun 5, 2020
1 parent d0bb86e commit ac73304
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const ProxyAgent = require('proxy-agent');
const { globalAgent } = require('https');
const { spawnSync } = require('child_process');
const { getMergedConfig } = require('./config');

function proxy() {
let proxyUrl = getMergedConfig().proxy;
if (proxyUrl == null || proxyUrl === '') {
proxyUrl = spawnSync(
'git',
['config', '--get', '--path', 'https.proxy']
).stdout.toString();
}
if (proxyUrl == null || proxyUrl === '') {
return globalAgent;
} else {
return new ProxyAgent(proxyUrl);
}
}

module.exports = proxy;
5 changes: 5 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const { CI_DOMAIN } = require('./ci/ci_type_parser');
const proxy = require('./proxy');

class Request {
constructor(credentials) {
this.credentials = credentials;
this.proxyAgent = proxy();
}

loadQuery(file) {
Expand All @@ -16,6 +18,7 @@ class Request {
}

async text(url, options = {}) {
options.agent = this.proxyAgent;
if (url.startsWith(`https://${CI_DOMAIN}`)) {
options.headers = options.headers || {};
Object.assign(options.headers, this.getJenkinsHeaders());
Expand All @@ -24,6 +27,7 @@ class Request {
}

async json(url, options = {}) {
options.agent = this.proxyAgent;
options.headers = options.headers || {};
options.headers.Accept = 'application/json';
if (url.startsWith(`https://${CI_DOMAIN}`)) {
Expand Down Expand Up @@ -65,6 +69,7 @@ class Request {
}
const url = 'https://api.github.com/graphql';
const options = {
agent: this.proxyAgent,
method: 'POST',
headers: {
Authorization: `Basic ${githubCredentials}`,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"lodash": "^4.17.15",
"node-fetch": "^2.6.0",
"ora": "^4.0.4",
"proxy-agent": "^3.1.1",
"replace-in-file": "^6.0.0",
"rimraf": "^3.0.2",
"yargs": "^15.3.1"
Expand Down

0 comments on commit ac73304

Please sign in to comment.