Last active
June 9, 2016 01:09
-
-
Save xenithorb/1232c2e1b23960e7edf3a8020d6f6cf8 to your computer and use it in GitHub Desktop.
Github Input Integration script for https://Rocket.Chat originally found https://github.com/xenithorb/Rocket.Chat.Docs/blob/master/4.%20Administrator%20Guides/Integrations/GitHub.md
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
/* exported Script */ | |
// Begin embedded images | |
const gh_cmit_svg = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGFyaWEtaGlkZGVuPSJ0cnVlIiBjbGFzcz0ib2N0aWNvbiBvY3RpY29uLWdpdC1jb21taXQiIGhlaWdodD0iMTYiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDE0IDE2IiB3aWR0aD0iMTQiPjxwYXRoIGQ9Ik0xMC44NiA3Yy0uNDUtMS43Mi0yLTMtMy44Ni0zLTEuODYgMC0zLjQxIDEuMjgtMy44NiAzSDB2MmgzLjE0Yy40NSAxLjcyIDIgMyAzLjg2IDMgMS44NiAwIDMuNDEtMS4yOCAzLjg2LTNIMTRWN2gtMy4xNHpNNyAxMC4yYy0xLjIyIDAtMi4yLS45OC0yLjItMi4yIDAtMS4yMi45OC0yLjIgMi4yLTIuMiAxLjIyIDAgMi4yLjk4IDIuMiAyLjIgMCAxLjIyLS45OCAyLjItMi4yIDIuMnoiPjwvcGF0aD48L3N2Zz4='; | |
const gh_pr_svg = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGFyaWEtaGlkZGVuPSJ0cnVlIiBjbGFzcz0ib2N0aWNvbiBvY3RpY29uLWdpdC1wdWxsLXJlcXVlc3QiIGhlaWdodD0iMTYiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDEyIDE2IiB3aWR0aD0iMTIiPjxwYXRoIGQ9Ik0xMSAxMS4yOFY1Yy0uMDMtLjc4LS4zNC0xLjQ3LS45NC0yLjA2QzkuNDYgMi4zNSA4Ljc4IDIuMDMgOCAySDdWMEw0IDNsMyAzVjRoMWMuMjcuMDIuNDguMTEuNjkuMzEuMjEuMi4zLjQyLjMxLjY5djYuMjhBMS45OTMgMS45OTMgMCAwIDAgMTAgMTVhMS45OTMgMS45OTMgMCAwIDAgMS0zLjcyem0tMSAyLjkyYy0uNjYgMC0xLjItLjU1LTEuMi0xLjIgMC0uNjUuNTUtMS4yIDEuMi0xLjIuNjUgMCAxLjIuNTUgMS4yIDEuMiAwIC42NS0uNTUgMS4yLTEuMiAxLjJ6TTQgM2MwLTEuMTEtLjg5LTItMi0yYTEuOTkzIDEuOTkzIDAgMCAwLTEgMy43MnY2LjU2QTEuOTkzIDEuOTkzIDAgMCAwIDIgMTVhMS45OTMgMS45OTMgMCAwIDAgMS0zLjcyVjQuNzJjLjU5LS4zNCAxLS45OCAxLTEuNzJ6bS0uOCAxMGMwIC42Ni0uNTUgMS4yLTEuMiAxLjItLjY1IDAtMS4yLS41NS0xLjItMS4yIDAtLjY1LjU1LTEuMiAxLjItMS4yLjY1IDAgMS4yLjU1IDEuMiAxLjJ6TTIgNC4yQzEuMzQgNC4yLjggMy42NS44IDNjMC0uNjUuNTUtMS4yIDEuMi0xLjIuNjUgMCAxLjIuNTUgMS4yIDEuMiAwIC42NS0uNTUgMS4yLTEuMiAxLjJ6Ij48L3BhdGg+PC9zdmc+'; | |
const svg_b64_pre = 'data:image/svg+xml;base64,'; | |
// end embedded images | |
const getLabelsField = (labels) => { | |
let labelsArray = []; | |
labels.forEach(function(label) { | |
labelsArray.push(label.name); | |
}); | |
labelsArray = labelsArray.join(', '); | |
return { | |
title: 'Labels', | |
value: labelsArray, | |
short: labelsArray.length <= 40 | |
}; | |
}; | |
const githubEvents = { | |
ping(request) { | |
return { | |
content: { | |
text: ':thumbsup: ' + request.content.zen | |
} | |
}; | |
}, | |
issues(request) { | |
const user = request.content.sender; | |
const attachment = { | |
author_icon: svg_b64_pre + gh_pr_svg, | |
author_name: '#' + request.content.issue.number + ' - ' + request.content.issue.title, | |
author_link: request.content.issue.html_url, | |
fields: [] | |
}; | |
if (request.content.issue.labels) { | |
attachment.fields.push(getLabelsField(request.content.issue.labels)); | |
} | |
if (request.content.issue.assignee) { | |
attachment.fields.push({ | |
title: 'Assignee', | |
value: request.content.issue.assignee.login, | |
short: true | |
}); | |
} | |
const actions = { | |
'assigned': ':inbox_tray:', | |
'unassigned': ':outbox_tray:', | |
'opened': ':triangular_flag_on_post:', | |
'closed': ':white_check_mark:', | |
'reopened': ':triangular_flag_on_post:', | |
'labeled': ':label:', | |
'unlabeled': ':label:', | |
'edited': ':pencil:' | |
}; | |
const text = actions[request.content.action] + ' issue'; | |
return { | |
content: { | |
icon_url: user.avatar_url, | |
alias: user.login, | |
text: text, | |
attachments: [attachment] | |
} | |
}; | |
}, | |
issue_comment(request) { | |
const user = request.content.comment.user; | |
var attachment = { | |
author_icon: svg_b64_pre + gh_pr_svg, | |
author_name: '#' + request.content.issue.number + ' - ' + request.content.issue.title, | |
author_link: request.content.comment.html_url, | |
fields: [] | |
}; | |
if (request.content.issue.labels) { | |
attachment.fields.push(getLabelsField(request.content.issue.labels)); | |
} | |
if (request.content.issue.assignee) { | |
attachment.fields.push({ | |
title: 'Assignee', | |
value: request.content.issue.assignee.login, | |
short: true | |
}); | |
} | |
const text = ':speech_balloon: ' + request.content.comment.body; | |
return { | |
content: { | |
icon_url: user.avatar_url, | |
alias: user.login, | |
text: text, | |
attachments: [attachment] | |
} | |
}; | |
}, | |
pull_request(request) { | |
const user = request.content.sender; | |
const attachment = { | |
author_icon: svg_b64_pre + gh_pr_svg, | |
author_name: '#' + request.content.pull_request.number + ' - ' + request.content.pull_request.title, | |
author_link: request.content.pull_request.html_url | |
}; | |
let text = 'Pull request'; | |
switch (request.content.action) { | |
case 'assigned': | |
text += ' assigned to: ' + request.content.assignee.login; | |
break; | |
case 'unassigned': | |
text += ' unassigned of ' + request.content.assignee.login; | |
break; | |
case 'opened': | |
text += ' opened'; | |
break; | |
case 'closed': | |
if (request.content.pull_request.merged) { | |
text += ' merged'; | |
} else { | |
text += ' closed'; | |
} | |
break; | |
case 'reopened': | |
text += ' reopened'; | |
break; | |
case 'labeled': | |
text += ' added label: "' + request.content.label.name + '" '; | |
break; | |
case 'unlabeled': | |
text += ' removed label: "' + request.content.label.name + '" '; | |
break; | |
case 'synchronize': | |
text += ' synchronized'; | |
} | |
return { | |
content: { | |
icon_url: user.avatar_url, | |
alias: user.login, | |
text: text, | |
attachments: [attachment] | |
} | |
}; | |
}, | |
//// Github push event | |
push(request) { | |
var commits = request.content.commits; | |
var multi_commit = "" | |
var is_short = true; | |
var changeset = 'Changeset'; | |
if ( commits.length > 1 ) { | |
var multi_commit = " [Multiple Commits]"; | |
var is_short = false; | |
var changeset = changeset + 's'; | |
var output = []; | |
} | |
const user = request.content.sender; | |
const attachment = { | |
author_icon: svg_b64_pre + gh_cmit_svg, | |
author_name: "Message: " + request.content.head_commit.message + multi_commit, | |
author_link: request.content.compare, | |
fields: [] | |
}; | |
if (request.content.repository.full_name) { | |
attachment.fields.push({ | |
title: 'Repo', | |
value: "["+request.content.repository.full_name+"]("+request.content.repository.url+")", | |
short: is_short | |
}); | |
} | |
for (var i = 0; i < commits.length; i++) { | |
var commit = commits[i]; | |
var shortID = commit.id.substring(0,7); | |
if ( commits.length > 1 ) { | |
a = '[' + shortID + '](' + commit.url + ') - ' + commit.message; | |
output.push( a ); | |
} else { | |
var output = "["+shortID+"]("+commit.url+")"; | |
} | |
} | |
if (commits.length > 1) { | |
attachment.fields.push({ | |
title: changeset, | |
value: output.reverse().join('<br />'), | |
short: is_short | |
}); | |
} else { | |
attachment.fields.push({ | |
title: changeset, | |
value: output, | |
short: is_short | |
}); | |
} | |
const text = ':ballot_box_with_check: Pushed to ' + "["+request.content.ref.split('/').pop()+"]"; | |
return { | |
content: { | |
icon_url: user.avatar_url, | |
alias: user.login, | |
text: text, | |
attachments: [attachment] | |
} | |
}; | |
}, // End Github Push | |
}; | |
class Script { | |
process_incoming_request({ request }) { | |
const header = request.headers['x-github-event']; | |
if (githubEvents[header]) { | |
return githubEvents[header](request); | |
} | |
return { | |
error: { | |
success: false, | |
message: 'Unsupported method' | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment