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 proxy support #102

Merged
merged 6 commits into from
Feb 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17,383 changes: 17,352 additions & 31 deletions dist/main.cjs

Large diffs are not rendered by default.

17,367 changes: 17,344 additions & 23 deletions dist/post.cjs

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import core from "@actions/core";
import { request } from "@octokit/request";
import { ProxyAgent, fetch as undiciFetch } from "undici";

const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");

// https://docs.github.com/actions/hosting-your-own-runners/managing-self-hosted-runners/using-a-proxy-server-with-self-hosted-runners
const proxyUrl =
process.env.https_proxy ||
process.env.HTTPS_PROXY ||
process.env.http_proxy ||
process.env.HTTP_PROXY;

/* c8 ignore start */
// Native support for proxies in Undici is under consideration: https://github.com/nodejs/undici/issues/1650
// Until then, we need to use a custom fetch function to add proxy support.
const proxyFetch = (url, options) => {
const urlHost = new URL(url).hostname;
const noProxy = (process.env.no_proxy || process.env.NO_PROXY || "").split(
","
);

if (!noProxy.includes(urlHost)) {
options = {
...options,
dispatcher: new ProxyAgent(String(proxyUrl)),
};
}

return undiciFetch(url, options);
};
/* c8 ignore stop */

export default request.defaults({
headers: {
"user-agent": "actions/create-github-app-token",
},
baseUrl,
/* c8 ignore next */
request: proxyUrl ? { fetch: proxyFetch } : {},
});
4 changes: 1 addition & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ const skipTokenRevoke = Boolean(
core.getInput("skip-token-revoke") || core.getInput("skip_token_revoke")
);

const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");

main(
appId,
privateKey,
owner,
repositories,
core,
createAppAuth,
request.defaults({ baseUrl }),
request,
skipTokenRevoke
).catch((error) => {
/* c8 ignore next 3 */
Expand Down
6 changes: 2 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@actions/core": "^1.10.1",
"@octokit/auth-app": "^6.0.3",
"@octokit/request": "^8.1.6",
"p-retry": "^6.2.0"
"p-retry": "^6.2.0",
"undici": "^6.6.0"
},
"devDependencies": {
"@sinonjs/fake-timers": "^11.2.2",
Expand All @@ -25,7 +26,6 @@
"esbuild": "^0.20.0",
"execa": "^8.0.1",
"open-cli": "^8.0.0",
"undici": "^6.6.0",
"yaml": "^2.3.4"
},
"release": {
Expand Down
4 changes: 1 addition & 3 deletions post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import core from "@actions/core";
import { post } from "./lib/post.js";
import request from "./lib/request.js";

const baseUrl = core.getInput("github-api-url").replace(/\/$/, "");

post(core, request.defaults({ baseUrl })).catch((error) => {
post(core, request).catch((error) => {
/* c8 ignore next 3 */
console.error(error);
core.setFailed(error.message);
Expand Down
2 changes: 1 addition & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const DEFAULT_ENV = {
GITHUB_REPOSITORY_OWNER: "actions",
GITHUB_REPOSITORY: "actions/create-github-app-token",
// inputs are set as environment variables with the prefix INPUT_
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
// https://docs.github.com/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
"INPUT_GITHUB-API-URL": "https://api.github.com",
"INPUT_APP-ID": "123456",
// This key is invalidated. It’s from https://github.com/octokit/auth-app.js/issues/465#issuecomment-1564998327.
Expand Down
Loading