Last active
September 26, 2015 03:32
-
-
Save logemann/456f822bbd3301284346 to your computer and use it in GitHub Desktop.
Codebase Hook
This file contains hidden or 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
module['exports'] = function codebase(hook) { | |
console.log("Starting Hook: "+hook.params.hook); | |
// hook.io has a range of node modules available - see https://hook.io/modules. | |
// We use request (https://www.npmjs.com/package/request) for an easy HTTP request | |
var request = require('request'); | |
var payload = JSON.parse(hook.req.body.payload); | |
var title; | |
var jsonRequest; | |
if (hook.req.body.type === "ticket_creation") { | |
title = "Ticket erstellt:"; | |
jsonRequest = createTicketJson(payload, hook.params, hook.env, title); | |
} else if(hook.req.body.type === "ticket_update") { | |
console.log("create ticket json: "+payload.ticket); | |
title = "Ticket geändert:"; | |
jsonRequest = createTicketJson(payload.ticket, hook.params, hook.env, title); | |
} else if(hook.req.body.type === "push") { | |
title = "Pushed to the repository:"; | |
jsonRequest = createPushJson(payload, hook.params, hook.env, title); | |
} else if(hook.req.body.type === "deployment") { | |
title = "Deployment der Webseite: "; | |
jsonRequest = createDeploymentJson(payload, hook.params, hook.env, title); | |
} else { | |
console.log(""); | |
console.log("Kein Handler für aktuellen Request!"); | |
console.log(hook.params); | |
hook.res.end("No Handler. See logs."); | |
} | |
// Set up the options for the HTTP request. | |
var options = { | |
// Use the Webhook URL from the Slack Incoming Webhooks integration. | |
uri: hook.env.SLACK_WEBHOOK_URL, | |
method: 'POST', | |
// Slack expects a JSON payload with a "text" property. | |
json: true, | |
body: jsonRequest | |
}; | |
console.log("do the slack request"); | |
// Make the POST request to the Slack incoming webhook. | |
request(options, function(error, response, body) { | |
// Pass error back to client if request endpoint can't be reached. | |
if (error) { | |
hook.res.end(error.message); | |
} | |
hook.res.end("normally ended"); | |
}); | |
}; | |
function getMd5FromEmail(emailAddress) { | |
var md5 = require('md5'); | |
var hashedEmail = md5(emailAddress); | |
return hashedEmail; | |
} | |
function getChannel(codebaseProject) { | |
if(codebaseProject === "Logentis" || codebaseProject === "ASWO") { | |
return "#random"; | |
} else { | |
return ("#"+codebaseProject).toLowerCase(); | |
} | |
} | |
function createDeploymentJson(payload, params, env, title) { | |
return { | |
"channel": "#orga", | |
"username": params.hook, | |
"icon_url": env.PICTURE_BASE_URL + "/deployhqlogo.png", | |
"attachments": [{ | |
"mrkdwn_in": ["text", "pretext"], | |
"fallback": title + " " + payload.repository.name, | |
"color": "#FF32F8", | |
"author_name": "Codebase Deploy Roboter", | |
"title": "Repository Änderungen auf Server '"+payload.servers[0]+"' deployed!", | |
"title_link": "http://dev.logentis.de:8080", | |
"text": "Bitte überprüfe http://dev.logentis.de:8080 ob alle Änderungen ok sind." | |
}] | |
}; | |
} | |
function createPushJson(payload, params, env, title) { | |
var author_icon_url = "http://www.gravatar.com/avatar/"+getMd5FromEmail(payload.user.email); | |
var commitsText = ""; | |
for (commit of payload.commits) { | |
commitsText += "`"+commit.id.substring(30,40) + "` - "+commit.message +"\n"; | |
}; | |
var commit_url = payload.repository.url+"/compare/"+payload.after + "..."+payload.before; | |
return { | |
"channel": getChannel(payload.repository.project.name), | |
"username": params.hook, | |
"icon_url": env.PICTURE_BASE_URL + "/codebaselogo.png", | |
"attachments": [{ | |
"mrkdwn_in": ["text", "pretext"], | |
"fallback": title + " " + payload.repository.name, | |
"color": "#3E3EFF", | |
"author_name": payload.user.name+ " pushed to a repo in Project '"+payload.repository.project.name+"'", | |
"author_link": "http://flickr.com/bobby/", | |
"author_icon": author_icon_url, | |
"title": payload.commits.length +" commit pushed to '"+payload.ref+"' on '"+payload.repository.name+"'", | |
"title_link": commit_url, | |
"text": commitsText | |
}] | |
}; | |
} | |
function createTicketJson(ticket, params, env, title) { | |
var author_icon_url = "http://www.gravatar.com/avatar/"+getMd5FromEmail(ticket.reporter.email_address); | |
return { | |
"channel": getChannel(ticket.project.name), | |
"username": params.hook, | |
"icon_url": env.PICTURE_BASE_URL + "/codebaselogo.png", | |
"attachments": [{ | |
"fallback": title + " #" + ticket.id + " " +ticket.summary, | |
"color": "#36a64f", | |
"author_name": ticket.reporter.name, | |
"author_link": "http://flickr.com/bobby/", | |
"author_icon": author_icon_url, | |
"title": title + " #" + ticket.id + " " +ticket.summary, | |
"title_link": ticket.url, | |
"text": ticket.description != null ? ticket.description : "", | |
"fields": [{ | |
"title": "Type", | |
"value": ticket.type.name, | |
"short": true | |
}, { | |
"title": "Priority", | |
"value": ticket.priority.name, | |
"short": true | |
}, { | |
"title": "Status", | |
"value": ticket.status.name, | |
"short": true | |
}, { | |
"title": "Bearbeiter", | |
"value": ticket.assignee != null ? ticket.assignee.name : "", | |
"short": true | |
}] | |
//"image_url": "http://my-website.com/path/to/image.jpg", | |
//"thumb_url": "http://example.com/path/to/thumb.png" | |
}] | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment