Skip to content

Commit dd3e559

Browse files
committed
add proxy support for aws lambda
1 parent ff7a656 commit dd3e559

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

handler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ const {
22
createLambdaFunction,
33
createProbot
44
} = require('@probot/adapter-aws-lambda-serverless')
5+
const { getProbotOctoKit } = require('lib/proxyAwareProbotOctokit')
56

67
const appFn = require('./')
78

89
module.exports.webhooks = createLambdaFunction(appFn, {
9-
probot: createProbot()
10+
probot: createProbot({ octokit: getProbotOctoKit() })
1011
})
1112

1213
module.exports.scheduler = function () {
13-
const probot = createProbot()
14+
const probot = createProbot({ octokit: getProbotOctoKit() })
1415
const app = appFn(probot, {})
1516
return app.syncInstallation()
1617
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This custom plugin overrides the default ProbotOctokit plugin and support Http Proxy.
2+
const { Octokit } = require('@octokit/core')
3+
const { createProbotAuth } = require('octokit-auth-probot')
4+
const getProxiedFetch = require('../proxiedFetch')
5+
6+
const ProbotOctokit = Octokit.plugin().defaults((instanceOptions) => {
7+
const defaultOptions = {
8+
authStrategy: createProbotAuth,
9+
throttle: {
10+
onSecondaryRateLimit: (
11+
retryAfter,
12+
options,
13+
octokit
14+
) => {
15+
octokit.log.warn(
16+
`SecondaryRateLimit hit with "${options.method} ${options.url}", retrying in ${retryAfter} seconds.`
17+
)
18+
return true
19+
},
20+
onRateLimit: (
21+
retryAfter,
22+
options,
23+
octokit
24+
) => {
25+
octokit.log.warn(
26+
`Rate limit hit with "${options.method} ${options.url}", retrying in ${retryAfter} seconds.`
27+
)
28+
return true
29+
}
30+
},
31+
userAgent: 'probot',
32+
request: {
33+
fetch: getProxiedFetch(instanceOptions.baseUrl) || fetch
34+
}
35+
}
36+
// merge options deeply
37+
const options = Object.assign({}, defaultOptions, instanceOptions, {
38+
request: Object.assign({}, defaultOptions.request, instanceOptions.request),
39+
throttle: instanceOptions.throttle
40+
? Object.assign({}, defaultOptions.throttle, instanceOptions.throttle)
41+
: defaultOptions.throttle
42+
})
43+
return options
44+
})
45+
46+
function getProbotOctoKit () {
47+
return ProbotOctokit
48+
}
49+
module.exports = { ProbotOctokit, getProbotOctoKit }

lib/proxiedFetch.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { getProxyForUrl } = require('proxy-from-env')
2+
const { ProxyAgent, fetch: undiciFetch } = require('undici')
3+
4+
export function getProxiedFetch (url) {
5+
const proxyUrl = getProxyForUrl(url)
6+
if (proxyUrl) {
7+
// LOG.debug('Setting up proxy agent for ', url)
8+
return (url, options) => {
9+
return undiciFetch(url, {
10+
...options,
11+
dispatcher: new ProxyAgent(proxyUrl)
12+
})
13+
}
14+
}
15+
return null
16+
}

package-lock.json

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
"lodash": "^4.17.21",
3434
"node-cron": "^3.0.2",
3535
"octokit": "^4.1.2",
36-
"probot": "^13.4.4"
36+
"probot": "^13.4.4",
37+
"proxy-from-env": "^1.1.0",
38+
"undici": "^7.7.0"
3739
},
3840
"devDependencies": {
3941
"@eslint/eslintrc": "^3.1.0",

0 commit comments

Comments
 (0)