Last active
August 29, 2015 14:12
-
-
Save ugate/9df717f7a0c278472ba4 to your computer and use it in GitHub Desktop.
Sends an email every time an issue is created or commented on using script.google.com and a GitHub Webhook
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
var secretToken = 'SECRET'; | |
var sendTo = '[email protected]'; | |
var org = 'node-forward'; | |
var repo = 'help'; | |
var prefix = '[' + org + '-' + repo + ']'; | |
var rx = new RegExp('\\[(' + org + ')-(' + repo + ')\\][\\s\\S]+\\(#(\\d+)', 'i'); | |
// recieving issue/comments from GitHub | |
function doPost(e) { | |
return toMailingList(e, JSON.parse(e.postData.getDataAsString())); | |
} | |
function toMailingList(e, ps) { | |
if (e.parameter.token !== secretToken) { | |
return rtn({ status: '400', message: 'Invalid token' }); | |
} else if (ps.repository['full_name'] !== org + '/' + repo) { | |
return rtn({ status: '400', message: 'Invalid repository: ' + ps.repository['full_name'] }); | |
} | |
var src = ps.comment || ps.issue; | |
var subject = prefix + ' ' + ps.issue.title + ' (#' + ps.issue.number + ' ' + ps.issue.state + ')'; | |
var body = getMsg(src.user.login, src.body); | |
GmailApp.sendEmail(sendTo, subject, body, { | |
name: org + '/' + repo, | |
htmlBody: body, | |
replyTo: org + '@gmail.com' | |
}); | |
return rtn({ status: '200', message: 'sent' }); | |
} | |
function rtn(json) { | |
//ContentService.createTextOutput(JSON.stringify(json)).setMimeType(ContentService.MimeType.JSON); | |
return HtmlService.createHtmlOutput(JSON.stringify(json)); | |
} | |
function getMsg(user, body) { | |
return '<b>' + user + '</b> <em>says</em>:<br/>' + body; | |
} | |
// uncomment setupTrig and add github api oauth to recieve responses from mailing list and add them as a comment | |
// setupTrig(); | |
function setupTrig() { | |
// would be nice to use trigger https://code.google.com/p/google-apps-script-issues/issues/detail?id=636 | |
var ts = ScriptApp.getProjectTriggers(); | |
for (var i = 0; i < ts.length; i++) { | |
if (ts[i].getEventType() == ScriptApp.EventType.CLOCK) { | |
return ts[i]; | |
} | |
} | |
// scan the inbox for new issue comments | |
return ScriptApp.newTrigger('toGitHubIssue').timeBased().everyHours(3).create(); | |
} | |
function toGitHubIssue() { | |
// authenticate using https://developer.github.com/v3/oauth/#non-web-application-flow | |
// and https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#addOAuthService(String) | |
var ts = GmailApp.search('is:unread subject:' + prefix); | |
for (var i = 0, il = ts.length, m; i < il; i++) { | |
for (var j = 0, jl = ts[i].length; j < jl; j++) { | |
if ((m = ts[i][j].getSubject().match(rx)) && m[3]) { | |
UrlFetchApp.fetch('https://api.github.com/repos/' + org + '/' + repo + '/issues/' + m[3] + '/comments', { | |
method: 'post', | |
payload: getMsg(ts[i][j].getFrom(), ts[i][j].getRawContent()) | |
}); | |
ts[i][j].markRead(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment