-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,393 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]; |
Oops, something went wrong.