Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from badging/payload-fix
Browse files Browse the repository at this point in the history
deployment fixes
  • Loading branch information
kaxada authored Mar 24, 2023
2 parents ef810e0 + 99637e1 commit 1275937
Show file tree
Hide file tree
Showing 20 changed files with 699 additions and 4,286 deletions.
8 changes: 0 additions & 8 deletions .dockerignore

This file was deleted.

76 changes: 0 additions & 76 deletions .github/workflows/deploy.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
npm-debug.log
*.pem
.env
test.js
logs
7 changes: 0 additions & 7 deletions Dockerfile

This file was deleted.

8 changes: 0 additions & 8 deletions docker-compose.yml

This file was deleted.

4 changes: 2 additions & 2 deletions githubBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
endReview,
assignChecklist,
updateReadme,
} = require("./src/logic/index");
} = require("./src/index");

const githubBot = async (name, octokit, payload) => {
// perform actions on application issues only
Expand Down Expand Up @@ -42,7 +42,7 @@ const githubBot = async (name, octokit, payload) => {
payload.action === "new_permissions_accepted"
) {
console.info("New permissions accepted");
} else {
} else if (name === "*"){
console.info(
`Webhook: ${name}.${payload.action} not yet automated or needed`
);
Expand Down
73 changes: 50 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const { App, createNodeMiddleware } = require("octokit");
const { App } = require("octokit");
require("dotenv").config();
const githubBot = require("./githubBot");
const SmeeClient = require("smee-client");
const express = require("express");
const logger = require('./utils/logger');
const path = require("path");
const fs = require("fs");

const app = express();
app.use(express.json());

// instantiate Github App
const app = new App({
const bot = new App({
appId: process.env.appId,
privateKey: process.env.privateKey,
oauth: {
Expand All @@ -14,27 +20,48 @@ const app = new App({
webhooks: { secret: process.env.webhookSecret },
});

/**
* Trigger the bot commands and the database initialization
* this works for routing too -> for the webhooks POST requests
*/
app.webhooks.onAny(async ({ id, name, payload }) => {
await payload;
const octokit = await app.getInstallationOctokit(payload.installation.id);
// Middleware function to log response details
app.use((req, res, next) => {
const oldSend = res.send;
res.send = function (data) {
logger.info(`Response for ${req.method} ${req.url}:`);
logger.info(data);
oldSend.apply(res, arguments);
};
next();
});

app.post('/', async (req, res) => {
const { headers: { 'x-github-event': name }, body: payload } = req;
const octokit = await bot.getInstallationOctokit(payload.installation.id);
githubBot(name, octokit, payload);
logger.info(`Received ${name} event from Github`);
res.send('ok');
});

// create local server to receive webhooks
require("http")
.createServer(createNodeMiddleware(app))
.listen(process.env.PORT, () =>
console.info(`App listening on PORT:${process.env.PORT}`)
);

//connect local server to network client in development
const smee = new SmeeClient({
source: process.env.webhookURL,
target: `http://localhost:${process.env.PORT}/api/github/webhooks`,
logger: console,
app.get('/logs', (req, res) => {
const logsDir = path.join(__dirname, 'logs');
fs.readdir(logsDir, (err, files) => {
if (err) {
logger.error(err);
return res.status(500).send('Error reading logs directory');
}
const logFiles = files.filter(file => file.startsWith('app.log'));
if (logFiles.length === 0) {
return res.status(404).send('No log files found');
}
const logFile = path.join(logsDir, logFiles[0]);
fs.readFile(logFile, 'utf8', (err, data) => {
if (err) {
logger.error(err);
return res.status(500).send('Error reading log file');
}
res.send(`<pre>${data}</pre>`);
});
});
});
smee.start();


app.listen(process.env.PORT, () =>
logger.info(`App listening on PORT:${process.env.PORT}`)
);
Loading

0 comments on commit 1275937

Please sign in to comment.