Skip to content

Commit e3aa18f

Browse files
committed
style: prettier
To stay consistent with `probot` and `@octokit/*` repositories
1 parent 5f68c28 commit e3aa18f

File tree

8 files changed

+118
-1061
lines changed

8 files changed

+118
-1061
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
test:
77
strategy:
88
matrix:
9-
node: ['12.x', '10.x', '8.x']
9+
node: ["12.x", "10.x", "8.x"]
1010

1111
runs-on: ubuntu-latest
1212

@@ -32,4 +32,4 @@ jobs:
3232
- run: npm ci
3333

3434
# Run tests
35-
- run: npm test
35+
- run: npm test

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## AWS Lambda Extension for Probot
2+
23
A [Probot](https://github.com/probot/probot) extension to make it easier to run your Probot Apps in AWS Lambda.
34

45
## Usage
@@ -9,23 +10,27 @@ $ npm install @probot/serverless-lambda
910

1011
```javascript
1112
// handler.js
12-
const { serverless } = require('@probot/serverless-lambda')
13-
const appFn = require('./')
14-
module.exports.probot = serverless(appFn)
13+
const { serverless } = require("@probot/serverless-lambda");
14+
const appFn = require("./");
15+
module.exports.probot = serverless(appFn);
1516
```
1617

1718
## Configuration
19+
1820
This package moves the functionality of `probot run` into a handler suitable for usage on AWS Lambda + API Gateway. Follow the documentation on [Environment Configuration](https://probot.github.io/docs/configuration/) to setup your app's environment variables. You can add these to `.env`, but for security reasons you may want to use the [AWS CLI](https://aws.amazon.com/cli/) or [Serverless Framework](https://github.com/serverless/serverless) to set Environment Variables for the function so you don't have to include any secrets in the deployed package.
1921

2022
For the private key, since AWS environment variables cannot be multiline strings, you could [Base64 encode](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) the `.pem` file you get from the GitHub App or use [KMS](https://aws.amazon.com/kms/) to encrypt and store the key.
2123

2224
## Differences from `probot run`
2325

2426
#### Local Development
27+
2528
Since Lambda functions do not start a normal node process, the best way we've found to test this out locally is to use [`serverless-offline`](https://github.com/dherault/serverless-offline). This plugin for the serverless framework emulates AWS Lambda and API Gateway on your local machine, allowing you to continue working from `https://localhost:3000/probot` before deploying your function to AWS.
2629

2730
#### Long running tasks
31+
2832
Some Probot Apps that depend on long running processes or intervals will not work with this extension. This is due to the inherent architecture of serverless functions, which are designed to respond to events and stop running as quickly as possible. For longer running apps we recommend using [other deployment options](https://probot.github.io/docs/deployment).
2933

3034
#### If you use [HTTP routes](https://probot.github.io/docs/http/) or [WEBHOOK_PATH](https://probot.github.io/docs/configuration/)
35+
3136
This extension is designed primarily for receiving webhooks from GitHub and responding back as a GitHub App. If you are using [HTTP Routes](https://probot.github.io/docs/http/) in your app to serve additional pages, you should take a look at [`serverless-http`](https://github.com/dougmoscrop/serverless-http), which can be used with Probot's [express server](https://github.com/probot/probot/blob/master/src/server.ts) by wrapping `probot.server`.

index.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
1-
const { Probot } = require('probot')
2-
const { resolve } = require('probot/lib/helpers/resolve-app-function')
3-
const { findPrivateKey } = require('probot/lib/helpers/get-private-key')
4-
const { template } = require('./views/probot')
1+
const { Probot } = require("probot");
2+
const { resolve } = require("probot/lib/helpers/resolve-app-function");
3+
const { findPrivateKey } = require("probot/lib/helpers/get-private-key");
4+
const { template } = require("./views/probot");
55

6-
let probot
6+
let probot;
77

88
const loadProbot = (appFn) => {
99
probot =
1010
probot ||
1111
new Probot({
1212
id: process.env.APP_ID,
1313
secret: process.env.WEBHOOK_SECRET,
14-
privateKey: findPrivateKey()
15-
})
14+
privateKey: findPrivateKey(),
15+
});
1616

17-
if (typeof appFn === 'string') {
18-
appFn = resolve(appFn)
17+
if (typeof appFn === "string") {
18+
appFn = resolve(appFn);
1919
}
2020

21-
probot.load(appFn)
21+
probot.load(appFn);
2222

23-
return probot
24-
}
23+
return probot;
24+
};
2525

2626
const lowerCaseKeys = (obj = {}) =>
2727
Object.keys(obj).reduce(
2828
(accumulator, key) =>
2929
Object.assign(accumulator, { [key.toLocaleLowerCase()]: obj[key] }),
3030
{}
31-
)
31+
);
3232

3333
module.exports.serverless = (appFn) => {
3434
return async (event, context) => {
3535
// 🤖 A friendly homepage if there isn't a payload
36-
if (event.httpMethod === 'GET' && event.path === '/probot') {
36+
if (event.httpMethod === "GET" && event.path === "/probot") {
3737
const res = {
3838
statusCode: 200,
3939
headers: {
40-
'Content-Type': 'text/html'
40+
"Content-Type": "text/html",
4141
},
42-
body: template
43-
}
44-
return res
42+
body: template,
43+
};
44+
return res;
4545
}
4646

4747
// Otherwise let's listen handle the payload
48-
probot = probot || loadProbot(appFn)
48+
probot = probot || loadProbot(appFn);
4949

5050
// Ends function immediately after callback
51-
context.callbackWaitsForEmptyEventLoop = false
51+
context.callbackWaitsForEmptyEventLoop = false;
5252

5353
// Determine incoming webhook event type
54-
const headers = lowerCaseKeys(event.headers)
55-
const e = headers['x-github-event']
54+
const headers = lowerCaseKeys(event.headers);
55+
const e = headers["x-github-event"];
5656
if (!e) {
5757
return {
5858
statusCode: 400,
59-
body: 'X-Github-Event header is missing'
60-
}
59+
body: "X-Github-Event header is missing",
60+
};
6161
}
6262

6363
// If body is expected to be base64 encoded, decode it and continue
6464
if (event.isBase64Encoded) {
65-
event.body = Buffer.from(event.body, 'base64').toString('utf8')
65+
event.body = Buffer.from(event.body, "base64").toString("utf8");
6666
}
6767

6868
// Convert the payload to an Object if API Gateway stringifies it
6969
event.body =
70-
typeof event.body === 'string' ? JSON.parse(event.body) : event.body
70+
typeof event.body === "string" ? JSON.parse(event.body) : event.body;
7171

7272
// Bail for null body
7373
if (!event.body) {
7474
return {
7575
statusCode: 400,
76-
body: 'Event body is null.'
77-
}
76+
body: "Event body is null.",
77+
};
7878
}
7979

8080
// Do the thing
8181
console.log(
82-
`Received event ${e}${event.body.action ? '.' + event.body.action : ''}`
83-
)
82+
`Received event ${e}${event.body.action ? "." + event.body.action : ""}`
83+
);
8484
if (event) {
8585
try {
8686
await probot.receive({
8787
name: e,
88-
payload: event.body
89-
})
88+
payload: event.body,
89+
});
9090
return {
9191
statusCode: 200,
9292
body: JSON.stringify({
93-
message: `Received ${e}.${event.body.action}`
94-
})
95-
}
93+
message: `Received ${e}.${event.body.action}`,
94+
}),
95+
};
9696
} catch (err) {
97-
console.error(err)
97+
console.error(err);
9898
return {
9999
statusCode: 500,
100-
body: JSON.stringify(err)
101-
}
100+
body: JSON.stringify(err),
101+
};
102102
}
103103
} else {
104-
console.error({ event, context })
105-
throw new Error('unknown error')
104+
console.error({ event, context });
105+
throw new Error("unknown error");
106106
}
107-
}
108-
}
107+
};
108+
};

0 commit comments

Comments
 (0)