From 00094f7d0dca73a869b63f5793e1fd09fb0c2723 Mon Sep 17 00:00:00 2001 From: Cefatus Date: Fri, 20 Jan 2023 17:59:35 +0700 Subject: [PATCH 1/3] Fix wrong HTTP codes. Instead of always sending status 400, sending more appropriate status instead. --- index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 869438f..20425cb 100644 --- a/index.js +++ b/index.js @@ -6,15 +6,22 @@ var url = require('url'); var secret = 'amazingkey'; // secret key of the webhook var port = 8081; // port +function resHeadJson(res,code){ + var value = {"Content-Type": "application/json"}; + res.writeHead(code, value); +} + http.createServer(function(req, res){ - console.log("request received"); - res.writeHead(400, {"Content-Type": "application/json"}); - + + var path = url.parse(req.url).pathname; + console.log("request received at: ",path); + if(path!='/push' || req.method != 'POST'){ var data = JSON.stringify({"error": "invalid request"}); + resHeadJson(res,400); return res.end(data); } @@ -29,6 +36,7 @@ http.createServer(function(req, res){ if(hash != req.headers['x-hub-signature']){ console.log('invalid key'); var data = JSON.stringify({"error": "invalid key", key: hash}); + resHeadJson(res,403) return res.end(data); } @@ -40,11 +48,10 @@ http.createServer(function(req, res){ console.log(buff.toString('utf-8')); }); - - res.writeHead(400, {"Content-Type": "application/json"}); - + resHeadJson(res,202); var data = JSON.stringify({"success": true}); - return res.end(data); + + return res.end(data); }); From 4cc3d3cc121975cb2120336b9a9547fced45a00e Mon Sep 17 00:00:00 2001 From: Cefatus Date: Fri, 20 Jan 2023 18:05:14 +0700 Subject: [PATCH 2/3] add ssh login Login to Github with passwordless deploy key. --- hook.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hook.sh b/hook.sh index 88fbdc1..8781133 100755 --- a/hook.sh +++ b/hook.sh @@ -3,6 +3,16 @@ # DIRECTORY TO THE REPOSITORY REPOSITORY="../repo" +# This deploy key must not have challenge key. +# Uncomment this, if your keys doesn't have default names. +#SSH_KEY="/home//.ssh/" + +eval $(ssh-agent -s) +ssh-add $SSH_KEY + +# test login just in case. +ssh -T git@github.com + cd $REPOSITORY git pull From e305a298742b2256c0d80759573a2475cd54f40b Mon Sep 17 00:00:00 2001 From: Cefatus Date: Fri, 20 Jan 2023 18:16:09 +0700 Subject: [PATCH 3/3] add path config Use `_path` to control path instead of hardcode. So it's easier to config. --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 20425cb..a2fb85e 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var url = require('url'); var secret = 'amazingkey'; // secret key of the webhook var port = 8081; // port +var _path = '/git'; function resHeadJson(res,code){ var value = {"Content-Type": "application/json"}; @@ -19,7 +20,7 @@ http.createServer(function(req, res){ console.log("request received at: ",path); - if(path!='/push' || req.method != 'POST'){ + if(path!=_path || req.method != 'POST'){ var data = JSON.stringify({"error": "invalid request"}); resHeadJson(res,400); return res.end(data);