Created
April 7, 2016 13:30
-
-
Save k0nsl/e410a6e735da150ebc9a61d1d4177452 to your computer and use it in GitHub Desktop.
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 */ | |
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: 'https://kebab.host/img/dboard/plainicon.com-50223-512px-5be.png', | |
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:' | |
}; | |
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: 'https://kebab.host/img/dboard/plainicon.com-50223-512px-5be.png', | |
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 = 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: 'https://kebab.host/img/dboard/plainicon.com-50223-512px-5be.png', | |
author_name: '#' + request.content.pull_request.number + ' - ' + request.content.pull_request.title, | |
author_link: request.content.pull_request.html_url | |
}; | |
let text; | |
switch (request.content.action) { | |
case 'assigned': | |
text += ' assigned to: ' + request.content.assignee.login; | |
break; | |
case 'unassigned': | |
text += ' unassigned ' + 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] | |
} | |
}; | |
} | |
}; | |
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