Skip to content

Commit

Permalink
feat: Easy proxy configuration (#1639)
Browse files Browse the repository at this point in the history
* support http and https proxy

* renaming based on feedback

Co-authored-by: Jeff Ching <[email protected]>
  • Loading branch information
julianiag and chingor13 authored Sep 20, 2022
1 parent db51b29 commit 4bb4c65
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"detect-indent": "^6.1.0",
"diff": "^5.0.0",
"figures": "^3.0.0",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.1",
"js-yaml": "^4.0.0",
"jsonpath": "^1.1.1",
"node-html-parser": "^6.0.0",
Expand Down
29 changes: 29 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {
FileNotFoundError as MissingFileError,
} from '@google-automations/git-file-utils';
import {Logger} from 'code-suggester/build/src/types';
import {HttpsProxyAgent} from 'https-proxy-agent';
import {HttpProxyAgent} from 'http-proxy-agent';

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

interface ProxyOption {
host: string;
port: number;
}

interface GitHubCreateOptions {
owner: string;
repo: string;
Expand All @@ -72,6 +79,7 @@ interface GitHubCreateOptions {
octokitAPIs?: OctokitAPIs;
token?: string;
logger?: Logger;
proxy?: ProxyOption;
}

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

static createDefaultAgent(baseUrl: string, defaultProxy?: ProxyOption) {
if (!defaultProxy) {
return undefined;
}

const {host, port} = defaultProxy;

return new URL(baseUrl).protocol.replace(':', '') === 'http'
? new HttpProxyAgent({
host,
port,
})
: new HttpsProxyAgent({
host,
port,
});
}

/**
* Build a new GitHub client with auto-detected default branch.
*
Expand All @@ -224,6 +250,9 @@ export class GitHub {
octokit: new Octokit({
baseUrl: apiUrl,
auth: options.token,
request: {
agent: this.createDefaultAgent(apiUrl, options.proxy),
},
}),
request: request.defaults({
baseUrl: apiUrl,
Expand Down
36 changes: 35 additions & 1 deletion test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import * as sinon from 'sinon';

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

const fixturesPath = './test/fixtures';
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -88,8 +90,40 @@ describe('GitHub', () => {
owner: 'some-owner',
repo: 'some-repo',
});

expect(github.repository.defaultBranch).to.eql('some-branch-from-api');
});

it('default agent is undefined when no proxy option passed ', () => {
expect(GitHub.createDefaultAgent('test_url')).eq(undefined);
});

it('should return a https agent', () => {
expect(
GitHub.createDefaultAgent(GH_API_URL, {
host: 'http://proxy.com',
port: 3000,
})
).instanceof(HttpsProxyAgent);
});

it('should throw error when baseUrl is an invalid url', () => {
expect(() => {
GitHub.createDefaultAgent('invalid_url', {
host: 'http://proxy.com',
port: 3000,
});
}).to.throw('Invalid URL');
});

it('should return a http agent', () => {
expect(
GitHub.createDefaultAgent('http://www.github.com', {
host: 'http://proxy.com',
port: 3000,
})
).instanceof(HttpProxyAgent);
});
});

describe('findFilesByFilename', () => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"lib": ["es2018", "dom"],
"rootDir": ".",
"outDir": "build",
Expand Down

0 comments on commit 4bb4c65

Please sign in to comment.