Last active
August 21, 2016 17:47
-
-
Save scriptnull/28a6d10cc5d06f1a703a82af58a17969 to your computer and use it in GitHub Desktop.
gitlab-notify url service
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
'use strict'; | |
var uuid = require('node-uuid'); | |
var url = require('url'); | |
var actionMap = { | |
'register' : { | |
allowedHttpMethod : 'POST', | |
executor : register | |
}, | |
'gitlab' : { | |
allowedHttpMethod : 'POST', | |
executor : gitlabHookHandler | |
} | |
}; | |
module.exports = function (hook) { | |
check({ | |
params : hook.params, | |
method : hook.req.method, | |
path : hook.req.path | |
}, function (err, executor) { | |
if (err) { | |
hook.res.writeHead(err.statusCode || 500, { | |
'Content-Type': 'application/json' | |
}); | |
hook.res.write(JSON.stringify(err) || { error : 'Internal Server Error'}); | |
return hook.res.end(); | |
} | |
else { | |
executor({ | |
hook : hook | |
}, function (err, response) { | |
if (err) { | |
hook.res.writeHead(err.statusCode || 500, { | |
'Content-Type': 'application/json' | |
}); | |
hook.res.write(JSON.stringify(err) || | |
{ error : 'Internal Server Error'}); | |
return hook.res.end(); | |
} | |
hook.res.writeHead(200, { | |
'Content-Type': 'application/json' | |
}); | |
hook.res.write(JSON.stringify(response)); | |
return hook.res.end(); | |
}); | |
} | |
}); | |
}; | |
function check(checkObj, callback) { | |
if (!checkObj) | |
return callback({ statusCode : 400, error : 'Invalid object in check'}); | |
if (!checkObj.params) | |
return callback({ statusCode : 400, error : 'params not found'}); | |
if (!checkObj.method) | |
return callback({ statusCode : 400, error : 'method not found'}); | |
if (!checkObj.params.id) | |
return callback({ statusCode : 400, error : 'params.id not found'}); | |
if (!checkObj.params.action) | |
return callback({ statusCode : 400, error : 'params.action not found'}); | |
if (checkObj.method !== actionMap[checkObj.params.action].allowedHttpMethod || | |
(typeof(actionMap[checkObj.params.action].executor) !== 'function')) { | |
return callback({ | |
statusCode : 404, | |
error : 'cannot ' + checkObj.method + ' on ' + checkObj.path | |
}); | |
} | |
return callback(null, actionMap[checkObj.params.action].executor); | |
} | |
// /:id/:action | |
// /:id/:register | |
function register(input, callback) { | |
var hook = input.hook; | |
var key = uuid.v4(); | |
if (!hook.params.tunnelUrl) | |
return callback({ statusCode : 400, error : 'method not found'}); | |
var storageObj = { | |
key : key, | |
tunnelUrl : hook.params.tunnelUrl | |
}; | |
hook.datastore.set(hook.params.id, | |
storageObj, | |
function (err, data) { | |
if (err) | |
return callback(err); | |
return callback(null, { | |
params : hook.params, | |
storedObj : storageObj, | |
storageStatus : data, | |
urls : { | |
gitlabHook : { | |
method : 'POST', | |
url : 'https://hook.io' + url.resolve(hook.req.path, 'gitlab') | |
} | |
} | |
}); | |
}); | |
} | |
// /:id/:gitlab | |
function gitlabHookHandler(input, callback) { | |
var hook = input.hook; | |
hook.datastore.get(hook.params.id, function (err, data) { | |
if (err) | |
return callback(err); | |
return callback({ | |
data : data | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment