Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
carturoch committed Mar 27, 2023
0 parents commit 58e1bf5
Show file tree
Hide file tree
Showing 6 changed files with 1,330 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
APP_ID="11"
PRIVATE_KEY_PATH="very/secure/location/gh_app_key.pem"
WEBHOOK_SECRET="secret"
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
.env
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
58 changes: 58 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import dotenv from "dotenv";
import fs from "fs";
import http from "http";
import { App } from "octokit";
import { createNodeMiddleware } from "@octokit/webhooks";

// Load environment variables from .env file
dotenv.config();

// Set configured values
const appId = process.env.APP_ID;
const privateKeyPath = process.env.PRIVATE_KEY_PATH;
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
const secret = process.env.WEBHOOK_SECRET;
const messageForNewPRs = fs.readFileSync("./message.md", "utf8");

// Create an authenticated Octokit client authenticated as a GitHub App
const app = new App({ appId, privateKey, webhooks: { secret }});

// Optional: Get & log the authenticated app's name
const { data } = await app.octokit.request("/app");

// Read more about custom logging: https://github.com/octokit/core.js#logging
app.octokit.log.debug(`Authenticated as '${ data.name }'`);

// Subscribe to the "pull_request.opened" webhook event
app.webhooks.on("pull_request.opened", async ({ octokit, payload }) => {
console.log(`Received a pull request event for #${ payload.pull_request.number }`);
await octokit.rest.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
body: messageForNewPRs,
});
});

// Optional: Handle errors
app.webhooks.onError((error) => {
if (error.name === "AggregateError") {
// Log Secret verification errors
console.log(`Error processing request: ${ error.event }`);
} else {
console.log(error);
}
});

// Launch a web server to listen for GitHub webhooks
const port = process.env.PORT || 3000;
const path = "/api/webhook";
const localWebhookUrl = `http://localhost:${ port }${ path }`;

// See https://github.com/octokit/webhooks.js/#createnodemiddleware for all options
const middleware = createNodeMiddleware(app.webhooks, { path });

http.createServer(middleware).listen(port, () => {
console.log(`Server is listening for events at: ${localWebhookUrl}`);
console.log('Press Ctrl + C to quit.')
});
5 changes: 5 additions & 0 deletions message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
👋 Hi there!

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.

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.
Loading

0 comments on commit 58e1bf5

Please sign in to comment.