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: add https proxy env support #329

Merged
merged 6 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"@octokit/core": "^3.0.0",
"@octokit/plugin-paginate-rest": "^2.2.4",
"@octokit/plugin-rest-endpoint-methods": "5.5.2",
"@octokit/types": "^6.16.1"
"@octokit/types": "^6.16.1",
"https-proxy-agent": "^5.0.0"
},
"devDependencies": {
"@pika/pack": "^0.5.0",
Expand Down
29 changes: 25 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@ import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods
export { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

import { VERSION } from "./version";
import { OctokitOptions } from "@octokit/core/dist-types/types";
import { HttpsProxyAgent } from "https-proxy-agent";

export const Octokit = Core.plugin(
paginateRest,
legacyRestEndpointMethods
).defaults({
const DEFAULTS = {
authStrategy: createActionAuth,
baseUrl: getApiBaseUrl(),
userAgent: `octokit-action.js/${VERSION}`,
};

export const Octokit = Core.plugin(
paginateRest,
legacyRestEndpointMethods
).defaults(function buildDefaults(options: OctokitOptions): OctokitOptions {
return {
...DEFAULTS,
...options,
request: {
agent: getHttpsProxyAgent(),
...options.request
},
};
});

function getApiBaseUrl(): string {
/* istanbul ignore next */
return process.env["GITHUB_API_URL"] || "https://api.github.com";
}

function getHttpsProxyAgent(): HttpsProxyAgent | undefined {
const proxy = process.env["HTTPS_PROXY"] || process.env["https_proxy"];

if (!proxy) return undefined;

return new HttpsProxyAgent(proxy);
}
89 changes: 89 additions & 0 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import fetchMock from "fetch-mock";
import { RequestOptions } from "https";
import { HttpsProxyAgent } from "https-proxy-agent";

import { Octokit } from "../src";

jest.mock("https-proxy-agent");

describe("Smoke test", () => {
beforeEach(() => {
delete process.env.GITHUB_TOKEN;
delete process.env.INPUT_GITHUB_TOKEN;
delete process.env.GITHUB_ACTION;
delete process.env.GITHUB_API_URL;
delete process.env.HTTPS_PROXY;
delete process.env.https_proxy;
});

it("happy path with GITHUB_TOKEN", () => {
Expand Down Expand Up @@ -171,4 +177,87 @@ describe("Smoke test", () => {

expect(data).toStrictEqual({ ok: true });
});

it.each(["HTTPS_PROXY", "https_proxy"])(
"Uses https-proxy-agent with %s env var",
async (https_proxy_env) => {
process.env.GITHUB_TOKEN = "secret123";
process.env.GITHUB_ACTION = "test";
process.env[https_proxy_env] = "https://127.0.0.1";

const fetchSandbox = fetchMock.sandbox();
const mock = fetchSandbox.post(
"path:/repos/octocat/hello-world/issues",
{ id: 1 },
{
body: {
title: "My test issue",
},
}
);

expect(Octokit).toBeInstanceOf(Function);
const octokit = new Octokit({
auth: "secret123",
request: {
fetch: mock,
},
});
await octokit.request("POST /repos/{owner}/{repo}/issues", {
owner: "octocat",
repo: "hello-world",
title: "My test issue",
});

expect(HttpsProxyAgent).toHaveBeenCalledWith("https://127.0.0.1");

const [call] = fetchSandbox.calls();
expect(call[0]).toEqual(
"https://api.github.com/repos/octocat/hello-world/issues"
);
expect((call[1] as RequestOptions).agent).toBeInstanceOf(HttpsProxyAgent);
}
);

it(
"Uses the explicitly provided request.agent value if it's provided",
async () => {
process.env.GITHUB_TOKEN = "secret123";
process.env.GITHUB_ACTION = "test";
process.env.HTTPS_PROXY = "https://127.0.0.1";

const fetchSandbox = fetchMock.sandbox();
const mock = fetchSandbox.post(
"path:/repos/octocat/hello-world/issues",
{ id: 1 },
{
body: {
title: "My test issue",
},
}
);

expect(Octokit).toBeInstanceOf(Function);
const octokit = new Octokit({
auth: "secret123",
request: {
fetch: mock,
agent: null
},
});
await octokit.request("POST /repos/{owner}/{repo}/issues", {
owner: "octocat",
repo: "hello-world",
title: "My test issue",
});

expect(HttpsProxyAgent).toHaveBeenCalledWith("https://127.0.0.1");

const [call] = fetchSandbox.calls();
expect(call[0]).toEqual(
"https://api.github.com/repos/octocat/hello-world/issues"
);
expect((call[1] as RequestOptions).agent).toBeNull();
}
);
});