Skip to content

Commit 4bb4c65

Browse files
julianiagchingor13
andauthored
feat: Easy proxy configuration (googleapis#1639)
* support http and https proxy * renaming based on feedback Co-authored-by: Jeff Ching <[email protected]>
1 parent db51b29 commit 4bb4c65

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
"detect-indent": "^6.1.0",
8989
"diff": "^5.0.0",
9090
"figures": "^3.0.0",
91+
"http-proxy-agent": "^5.0.0",
92+
"https-proxy-agent": "^5.0.1",
9193
"js-yaml": "^4.0.0",
9294
"jsonpath": "^1.1.1",
9395
"node-html-parser": "^6.0.0",

src/github.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import {
4646
FileNotFoundError as MissingFileError,
4747
} from '@google-automations/git-file-utils';
4848
import {Logger} from 'code-suggester/build/src/types';
49+
import {HttpsProxyAgent} from 'https-proxy-agent';
50+
import {HttpProxyAgent} from 'http-proxy-agent';
4951

5052
// Extract some types from the `request` package.
5153
type RequestBuilderType = typeof request;
@@ -63,6 +65,11 @@ export interface GitHubOptions {
6365
logger?: Logger;
6466
}
6567

68+
interface ProxyOption {
69+
host: string;
70+
port: number;
71+
}
72+
6673
interface GitHubCreateOptions {
6774
owner: string;
6875
repo: string;
@@ -72,6 +79,7 @@ interface GitHubCreateOptions {
7279
octokitAPIs?: OctokitAPIs;
7380
token?: string;
7481
logger?: Logger;
82+
proxy?: ProxyOption;
7583
}
7684

7785
type CommitFilter = (commit: Commit) => boolean;
@@ -202,6 +210,24 @@ export class GitHub {
202210
this.logger = options.logger ?? defaultLogger;
203211
}
204212

213+
static createDefaultAgent(baseUrl: string, defaultProxy?: ProxyOption) {
214+
if (!defaultProxy) {
215+
return undefined;
216+
}
217+
218+
const {host, port} = defaultProxy;
219+
220+
return new URL(baseUrl).protocol.replace(':', '') === 'http'
221+
? new HttpProxyAgent({
222+
host,
223+
port,
224+
})
225+
: new HttpsProxyAgent({
226+
host,
227+
port,
228+
});
229+
}
230+
205231
/**
206232
* Build a new GitHub client with auto-detected default branch.
207233
*
@@ -224,6 +250,9 @@ export class GitHub {
224250
octokit: new Octokit({
225251
baseUrl: apiUrl,
226252
auth: options.token,
253+
request: {
254+
agent: this.createDefaultAgent(apiUrl, options.proxy),
255+
},
227256
}),
228257
request: request.defaults({
229258
baseUrl: apiUrl,

test/github.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {resolve} from 'path';
2222
import * as snapshot from 'snap-shot-it';
2323
import * as sinon from 'sinon';
2424

25-
import {GitHub, GitHubRelease} from '../src/github';
25+
import {GH_API_URL, GitHub, GitHubRelease} from '../src/github';
2626
import {PullRequest} from '../src/pull-request';
2727
import {TagName} from '../src/util/tag-name';
2828
import {Version} from '../src/version';
@@ -37,6 +37,8 @@ import {PullRequestBody} from '../src/util/pull-request-body';
3737
import {PullRequestTitle} from '../src/util/pull-request-title';
3838
import * as codeSuggester from 'code-suggester';
3939
import {RawContent} from '../src/updaters/raw-content';
40+
import {HttpsProxyAgent} from 'https-proxy-agent';
41+
import {HttpProxyAgent} from 'http-proxy-agent';
4042

4143
const fixturesPath = './test/fixtures';
4244
const sandbox = sinon.createSandbox();
@@ -88,8 +90,40 @@ describe('GitHub', () => {
8890
owner: 'some-owner',
8991
repo: 'some-repo',
9092
});
93+
9194
expect(github.repository.defaultBranch).to.eql('some-branch-from-api');
9295
});
96+
97+
it('default agent is undefined when no proxy option passed ', () => {
98+
expect(GitHub.createDefaultAgent('test_url')).eq(undefined);
99+
});
100+
101+
it('should return a https agent', () => {
102+
expect(
103+
GitHub.createDefaultAgent(GH_API_URL, {
104+
host: 'http://proxy.com',
105+
port: 3000,
106+
})
107+
).instanceof(HttpsProxyAgent);
108+
});
109+
110+
it('should throw error when baseUrl is an invalid url', () => {
111+
expect(() => {
112+
GitHub.createDefaultAgent('invalid_url', {
113+
host: 'http://proxy.com',
114+
port: 3000,
115+
});
116+
}).to.throw('Invalid URL');
117+
});
118+
119+
it('should return a http agent', () => {
120+
expect(
121+
GitHub.createDefaultAgent('http://www.github.com', {
122+
host: 'http://proxy.com',
123+
port: 3000,
124+
})
125+
).instanceof(HttpProxyAgent);
126+
});
93127
});
94128

95129
describe('findFilesByFilename', () => {

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./node_modules/gts/tsconfig-google.json",
33
"compilerOptions": {
4+
"allowSyntheticDefaultImports": true,
45
"lib": ["es2018", "dom"],
56
"rootDir": ".",
67
"outDir": "build",

0 commit comments

Comments
 (0)