Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for auto update via GH hooks. #17

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#/bin/sh

# Fetch the newest code
git fetch origin gh-pages

# Hard reset
git reset --hard origin/gh-pages

# Force pull
git pull origin gh-pages --force
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"homepage": "https://github.com/duckduckgo/privacy-test-pages#readme",
"dependencies": {
"express": "^4.17.1",
"node-cmd": "^4.0.0",
"ws": "^7.4.0"
}
}
30 changes: 22 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const ws = require('ws');
const app = express();
const port = process.env.PORT || 3000;
const url = require('url');
const cmd = require('node-cmd');
const crypto = require('crypto');

function fullUrl(req) {
return url.format({
Expand Down Expand Up @@ -35,14 +37,26 @@ app.use(express.static('.', {
}
}));

// // endpoint for updating the app
// app.post('/git', (req, res) => {
// if (req.headers['x-github-event'] == "push") {
// /* Here will be our updating code */
// }

// return res.sendStatus(200);
// });
// endpoint for updating the app (https://support.glitch.com/t/tutorial-how-to-auto-update-your-project-with-github/8124)
app.post('/git', (req, res) => {
const hmac = crypto.createHmac('sha1', process.env.SECRET);
const sig = 'sha1=' + hmac.update(JSON.stringify(req.body)).digest('hex');

if (req.headers['x-github-event'] == 'push' && sig == req.headers['x-hub-signature']) {
kdzwinel marked this conversation as resolved.
Show resolved Hide resolved
cmd.run('chmod 777 git.sh'); /* :/ Fix no perms after updating */
cmd.get('./git.sh', (err, data) => { // Run our script
if (data) console.log(data);
if (err) console.log(err);
});
cmd.run('refresh'); // Refresh project

console.log('> [GIT] Updated with origin/gh-pages');

return res.sendStatus(200);
} else {
return res.sendStatus(403);
}
});

// dummy websocket server
const wss = new ws.Server({server: listener, path: '/block-me/web-socket'});
Expand Down