Skip to content

Commit 58e1bf5

Browse files
committed
Initial commit
0 parents  commit 58e1bf5

File tree

6 files changed

+1330
-0
lines changed

6 files changed

+1330
-0
lines changed

Diff for: .env.sample

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APP_ID="11"
2+
PRIVATE_KEY_PATH="very/secure/location/gh_app_key.pem"
3+
WEBHOOK_SECRET="secret"

Diff for: .gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
.env
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
pnpm-debug.log*
9+
lerna-debug.log*
10+
11+
node_modules
12+
dist
13+
dist-ssr
14+
*.local
15+
16+
# Editor directories and files
17+
.vscode/*
18+
!.vscode/extensions.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?

Diff for: app.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import dotenv from "dotenv";
2+
import fs from "fs";
3+
import http from "http";
4+
import { App } from "octokit";
5+
import { createNodeMiddleware } from "@octokit/webhooks";
6+
7+
// Load environment variables from .env file
8+
dotenv.config();
9+
10+
// Set configured values
11+
const appId = process.env.APP_ID;
12+
const privateKeyPath = process.env.PRIVATE_KEY_PATH;
13+
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
14+
const secret = process.env.WEBHOOK_SECRET;
15+
const messageForNewPRs = fs.readFileSync("./message.md", "utf8");
16+
17+
// Create an authenticated Octokit client authenticated as a GitHub App
18+
const app = new App({ appId, privateKey, webhooks: { secret }});
19+
20+
// Optional: Get & log the authenticated app's name
21+
const { data } = await app.octokit.request("/app");
22+
23+
// Read more about custom logging: https://github.com/octokit/core.js#logging
24+
app.octokit.log.debug(`Authenticated as '${ data.name }'`);
25+
26+
// Subscribe to the "pull_request.opened" webhook event
27+
app.webhooks.on("pull_request.opened", async ({ octokit, payload }) => {
28+
console.log(`Received a pull request event for #${ payload.pull_request.number }`);
29+
await octokit.rest.issues.createComment({
30+
owner: payload.repository.owner.login,
31+
repo: payload.repository.name,
32+
issue_number: payload.pull_request.number,
33+
body: messageForNewPRs,
34+
});
35+
});
36+
37+
// Optional: Handle errors
38+
app.webhooks.onError((error) => {
39+
if (error.name === "AggregateError") {
40+
// Log Secret verification errors
41+
console.log(`Error processing request: ${ error.event }`);
42+
} else {
43+
console.log(error);
44+
}
45+
});
46+
47+
// Launch a web server to listen for GitHub webhooks
48+
const port = process.env.PORT || 3000;
49+
const path = "/api/webhook";
50+
const localWebhookUrl = `http://localhost:${ port }${ path }`;
51+
52+
// See https://github.com/octokit/webhooks.js/#createnodemiddleware for all options
53+
const middleware = createNodeMiddleware(app.webhooks, { path });
54+
55+
http.createServer(middleware).listen(port, () => {
56+
console.log(`Server is listening for events at: ${localWebhookUrl}`);
57+
console.log('Press Ctrl + C to quit.')
58+
});

Diff for: message.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
👋 Hi there!
2+
3+
Thanks for opening a new PR. Please, consider following this [guide](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests) to make your PR easier to review.
4+
5+
Also, check this new feature to [use markdown helpers](https://github.blog/changelog/2023-03-15-introducing-the-github-markdown-helpers-public-beta/) in your PR.

0 commit comments

Comments
 (0)