Skip to content

Commit

Permalink
updated and documented
Browse files Browse the repository at this point in the history
  • Loading branch information
Jehfkemsy committed Jul 25, 2019
1 parent fe38036 commit c90176f
Show file tree
Hide file tree
Showing 7 changed files with 2,393 additions and 47 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# github-webhooks-nodejs
a little GitHub webhooks scripts using nodejs server
# Hooks

A little NodeJS server for managing GitHub events

## Available Commands

`yarn start` - Starts PM2 process with argument `--name`
`yarn stop` - stops process
`yarn restart` - restarts process
`yarn status` - logs process status
`yarn clear` - clear process' logs
`yarn delete` - deletes process

For more details see [package.json](https://github.com/UPE-FIU/Hooks/blob/master/package.json)

64 changes: 64 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require("dotenv").config();
const repos = require("./repositories");

//Docs: https://nodejs.org/api/http.html
const http = require("http");
//Docs: https://nodejs.org/api/crypto.html
const crypto = require("crypto");
//Docs: https://nodejs.org/api/child_process.html
const exec = require("child_process").exec;

const { PORT } = process.env;

/**
*Our server
*/
http
.createServer(function(req, res) {
let response = "UPDATES: "; //github log response
let { headers } = req;

req.on("error", function(error) {
response += error;
});

req.on("data", function(chunk) {
let remoteSig = headers["x-hub-signature"];

//choose updated repo
repos.map(repo => {
let { secret, path, needToBuild } = repo;
let currentSig =
"sha1=" +
crypto
.createHmac("sha1", secret)
.update(chunk.toString())
.digest("hex");

if (remoteSig === currentSig) {
//execute commands
exec("cd " + path + " && git pull", (error, stdout, stderr) => {
if (error) {
response += `exec error: ${error}`;
return;
}
response += `, stdout: ${stdout}`;
response += `, stderr: ${stderr}`;
});
if (needToBuild)
exec("cd " + path + "yarn build", (error, stdout, stderr) => {
if (error) {
response += `exec error: ${error}`;
return;
}
response += `, stdout: ${stdout}`;
response += `, stderr: ${stderr}`;
});
}
});
});
if (response === "UPDATES: ") response += "Nothing Was Executed...";
res.write(response);
res.end();
})
.listen(PORT || 8080);
9 changes: 9 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PORT=

#Repository secrets
WAVE_SECRET=
TURTLE_SECRET=
SHELL_SECRET=
ANCHOR_SECRET=
PRE_REGISTRATION_SECRET=
HOOKS_SECRET=
45 changes: 0 additions & 45 deletions hooks.js

This file was deleted.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "hooks",
"version": "1.0.0",
"description": "a little nodejs server for handling github events",
"main": "index.js",
"repository": "https://github.com/UPE-FIU/Hooks.git",
"author": "jehfkemsy <[email protected]>",
"license": "MIT",
"private": false,
"scripts": {
"start": "pm2 start app.js --name Hooks",
"stop": "pm2 stop Hooks",
"restart": "pm2 restart Hooks",
"status": "pm2 status Hooks",
"clear": "pm2 flush",
"delete": "pm2 delete Hooks"
},
"dependencies": {
"dotenv": "^8.0.0",
"pm2": "^3.5.1"
}
}
46 changes: 46 additions & 0 deletions repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Repositories Objects
*/
const path = require("path");

const {
WAVE_SECRET,
TURTLE_SECRET,
SHELL_SECRET,
ANCHOR_SECRET,
HOOKS_SECRET,
PRE_REGISTRATION_SECRET,
} = process.env;

module.exports = [
{
path: path.resolve(__dirname, "../Waves"),
secret: WAVE_SECRET,
needToBuild: true,
},
{
path: path.resolve(__dirname, "../Turtle"),
secret: TURTLE_SECRET,
needToBuild: true,
},
{
path: path.resolve(__dirname, "../Shell"),
secret: SHELL_SECRET,
needToBuild: true,
},
{
path: path.resolve(__dirname, "../Anchor"),
secret: ANCHOR_SECRET,
needToBuild: true,
},
{
path: path.resolve(__dirname),
secret: HOOKS_SECRET,
needToBuild: false,
},
{
path: path.resolve(__dirname, "../Shell-Pre-registration-2019"),
secret: PRE_REGISTRATION_SECRET,
needToBuild: false,
},
];
Loading

0 comments on commit c90176f

Please sign in to comment.