I wrote the code to push notification to Slack when someone create an issue, when someone created a comment, etc. in Bitbucket.
Story
The mechanism to send notification to Slack when some action is taken in Bitbucket, is prepared in Bitbucket, called webhooks. It can be turned on in each project setting page.
In Slack, we can connect to Bitbucket easily in setting page. But Slack can only receive push notification of Bitbucket, can’t receive comment or issue notification, at that time ().
I want to be notified issue and comment creation rather than push action, and created such code in Google Apps Script.
Code
Copy the following script to Google Apps Script, and deploy as web app, which is in “publish” menu. Then, it’s url would be shown and set it as webhook url in Bitbucket console panel.
Bitbucket で何らかの操作が行われるたびに Google Apps Script の処理が実行され、 Slack に 投稿されるようになります。
If I had had time, I would’ve create object model and factory, etc. But in real, the code is not so clean.
(And I put the code to Github Bitbucket To Slack Gas. Contribution is welcomed.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
function createSlackMessage(contents) { var message = {}; if (contents.hasOwnProperty('push')) { // Push message['text'] = 'pushed to ' + contents.repository.name; var info = ''; var l = contents.push.changes.length; for (var i = 0; i < l; ++i) { info += '\n'; if (contents.push.changes[i].old == null) { info += 'created new branch: ' + contents.push.changes[i]['new'].name; } else { // deleted if(contents.push.changes[i]['new']==null) { info += 'deleted branch: '+contents.push.changes[i]['old'].name; }else{ info += 'from ' + contents.push.changes[i]['old'].name + ' to ' + contents.push.changes[i]['new'].name ; } } } message['text'] += info; } else if (contents.hasOwnProperty('fork')) { message['fork'] = 'forked.'; } else if (contents.hasOwnProperty('pullrequest')) { // PullRequest var pr = contents.pullrequest; var to = pr.destination.repository.full_name + '/' + pr.destination.branch.name; var from = pr.source.repository.full_name + '/' + pr.source.branch.name; if (contents.hasOwnProperty('approve')) { // Approve var prLink = '<' + contents.pullrequest.links.html.href + '|' + contents.pullrequest.title + '>'; message['text'] = 'approved the PR.\n' + prLink; } else if (contents.hasOwnProperty('comment')) { // Comment var prLink = '<' + contents.comment.links.html.href + '|' + contents.pullrequest.title + ' comment>'; message['text'] = 'commented on the PR.\n' + contents.comment.content.raw + '\n' + prLink; } else { var prLink = '<' + contents.pullrequest.links.html.href + '|' + contents.pullrequest.title + '>'; if (pr.updated_on == null) { // Create message['text'] = 'created new PR[' + from + '->' + to + '].\n' + prLink; } else { // Update or Merge message['text'] = 'updated PR (' + pr.state + ') [' + from + '->' + to + '].\n' + prLink; } } } else if (contents.hasOwnProperty('issue')) { // Issue var issueLink = '<' + contents.issue.links.html.href + '|#' + contents.issue.id + ' ' + contents.issue.title + '>'; if (contents.hasOwnProperty('comment')) { if (contents.hasOwnProperty('changes')) { message['text'] = 'updated issue: ' + issueLink; } else { // Comment created message['text'] = 'commented on ' + issueLink + '\n' + contents.comment.content.raw + '\n<' + contents.comment.links.html.href + '|More Detail>'; } } else { // Created message['text'] = 'created new issue: ' + issueLink; } } message['username'] = contents.actor.display_name; return message; } function post(payload, url) { var options; options = { method: 'post', payload: payload }; return UrlFetchApp.fetch(url, options); } function doPost(e) { var contents = JSON.parse(e.postData.contents); var slackMessage = createSlackMessage(contents); var url = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'; post(JSON.stringify(slackMessage), url); } |
First, Bitbucket send post request to the app and doPost will be executed. Then, it creates message to post to Slack in createSlackMessage
function, from the data posted.
After that, it posts to Slack.
Caution
createSlackMessage
では、 Issue の場合、 Pull Request の場合などパターン分けしていますが、 上で分けてある以上に 細かくパターンが分かれます。 今回は私の業務上必要だろうと思われる分岐にとどめています。 そのパターンを正確に判断するには Bitbucket から送信されるヘッダの中を確認するのがいいのですが、 Google Apps Script ではリクエストヘッダを確認することができないため ペイロード部分のデータで分岐を行っています。
実際のところ Pull Request の approved のあたりはまだ検証中のため、 正しく判別できているという保証がないです。
If you can use Heroku or AWS or other hosting service, it would be good to create in PHP, Ruby, etc. which can handle request header.