UF #github-activity Post
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
/* exported Script */ | |
String.prototype.capitalizeFirstLetter = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1); | |
} | |
// Begin embedded images | |
const gh_cmit_svg = '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" class="octicon octicon-git-commit" height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>'; | |
const gh_pr_svg = '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" class="octicon octicon-git-pull-request" height="16" version="1.1" viewBox="0 0 12 16" width="12"><path d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>'; | |
const gh_iss_svg = '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" class="octicon octicon-issue-opened" height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>'; | |
const svg_inline_prefix = 'data:image/svg+xml;ascii,'; | |
// 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: 'Now tracking a new repo! :thumbsup: ' + request.content.zen | |
} | |
}; | |
}, | |
/* NEW OR MODIFY ISSUE */ | |
issues(request) { | |
const user = request.content.sender; | |
if (request.content.action == "opened" || request.content.action == "reopened" || request.content.action == "edited") { | |
var body = request.content.issue.body; | |
} else if (request.content.action == "labeled") { | |
var body = "Current labels: " + getLabelsField(request.content.issue.labels).value; | |
} else if (request.content.action == "assigned" || request.content.action == "unassigned") { | |
// Note that the issues API only gives you one assignee. | |
var body = "Current assignee: " + request.content.issue.assignee.login; | |
} else if (request.content.action == "closed") { | |
if (request.content.issue.closed_by) { | |
var body = "Closed by: " + request.content.issue.closed_by.login; | |
} else { | |
var body = "Closed."; | |
} | |
} else { | |
return { | |
error: { | |
success: false, | |
message: 'Unsupported issue action' | |
} | |
}; | |
} | |
const action = request.content.action.capitalizeFirstLetter(); | |
const text = '_' + request.content.repository.full_name + '_\n' + | |
'**[' + action + ' issue #' + request.content.issue.number + | |
' - ' + request.content.issue.title + "**" + '](' + | |
request.content.issue.html_url + ')\n\n' + | |
body; | |
return { | |
content: { | |
attachments: [ | |
{ | |
thumb_url: user.avatar_url, | |
text: text, | |
fields: [] | |
} | |
] | |
} | |
}; | |
}, | |
/* COMMENT ON EXISTING ISSUE */ | |
issue_comment(request) { | |
const user = request.content.comment.user; | |
if (request.content.action == "edited") { | |
var action = "Edited comment "; | |
} else { | |
var action = "Comment " | |
} | |
const text = '_' + request.content.repository.full_name + '_\n' + | |
'**[' + action + ' on issue #' + request.content.issue.number + | |
' - ' + request.content.issue.title + '](' + | |
request.content.comment.html_url + ')**\n\n' + | |
request.content.comment.body; | |
return { | |
content: { | |
attachments: [ | |
{ | |
thumb_url: user.avatar_url, | |
text: text, | |
fields: [] | |
} | |
] | |
} | |
}; | |
}, | |
/* PUSH TO REPO */ | |
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; | |
var text = '**Pushed to ' + "["+request.content.repository.full_name+"]("+request.content.repository.url+"):" | |
+ request.content.ref.split('/').pop() + "**\n\n"; | |
for (var i = 0; i < commits.length; i++) { | |
var commit = commits[i]; | |
var shortID = commit.id.substring(0,7); | |
var a = '[' + shortID + '](' + commit.url + ') - ' + commit.message; | |
if ( commits.length > 1 ) { | |
output.push( a ); | |
} else { | |
var output = a; | |
} | |
} | |
if (commits.length > 1) { | |
text += output.reverse().join('\n'); | |
} else { | |
text += output; | |
} | |
return { | |
content: { | |
attachments: [ | |
{ | |
thumb_url: user.avatar_url, | |
text: text, | |
fields: [] | |
} | |
] | |
} | |
}; | |
}, // End Github Push | |
/* NEW PULL REQUEST */ | |
pull_request(request) { | |
const user = request.content.sender; | |
if (request.content.action == "opened" || request.content.action == "reopened" || request.content.action == "edited" || request.content.action == "synchronize") { | |
var body = request.content.pull_request.body; | |
} else if (request.content.action == "labeled") { | |
var body = "Current labels: " + getLabelsField(request.content.pull_request.labels).value; | |
} else if (request.content.action == "assigned" || request.content.action == "unassigned") { | |
// Note that the issues API only gives you one assignee. | |
var body = "Current assignee: " + request.content.pull_request.assignee.login; | |
} else if (request.content.action == "closed") { | |
if (request.content.pull_request.merged) { | |
var body = "Merged by: " + request.content.pull_request.merged_by.login; | |
} else { | |
var body = "Closed."; | |
} | |
} else { | |
return { | |
error: { | |
success: false, | |
message: 'Unsupported pull request action' | |
} | |
}; | |
} | |
const action = request.content.action.capitalizeFirstLetter(); | |
const text = '_' + request.content.repository.full_name + '_\n' + | |
'**[' + action + ' pull request #' + request.content.pull_request.number + | |
' - ' + request.content.pull_request.title + "**" + '](' + | |
request.content.pull_request.html_url + ')\n\n' + | |
body; | |
return { | |
content: { | |
attachments: [ | |
{ | |
thumb_url: user.avatar_url, | |
text: text, | |
fields: [] | |
} | |
] | |
} | |
}; | |
}, | |
}; | |
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