Skip to content

Instantly share code, notes, and snippets.

@cs-sonar
Created March 11, 2015 02:00
Show Gist options
  • Save cs-sonar/4ac0e44dcb3e19ebeb24 to your computer and use it in GitHub Desktop.
Save cs-sonar/4ac0e44dcb3e19ebeb24 to your computer and use it in GitHub Desktop.
bitbucketに登録されたりコメントされたりしたissuesをslackで通知する
# -*- coding: utf-8 -*-
require 'net/http'
require "json"
require 'time'
########
# config
########
# crontabに登録は以下のような感じで
# */5 * * * * root /path/to/ruby -W0 /path/to/bitbucket_issues.rb
# bitbucket
rep_user = 'リポジトリのチーム名かユーザ名'
rep_name = 'リポジトリの名前'
user = 'bitbucketのユーザ名'
pass = 'bitbucketのパスワード'
# slack
channel = '#発言チャンネル'
icon_emoji = ':squirrel:'
slack_uri = 'slackのhookのURL'
########
# program
########
fp = File.expand_path(File.dirname($0))
# 実行時間の読み込み
if File.exist?(fp + "/unixtime.dat") then
lasttime = File.binread(fp + "/unixtime.dat")
else
lasttime = Time.now.to_i
end
# 実行時間の書き込み
unixtime = Time.now.to_i
File.binwrite(fp + "/unixtime.dat", unixtime)
# bitbucket api からissuesを取得
uri = 'curl -s -S https://api.bitbucket.org/1.0/repositories/' + rep_user + '/' + rep_name + '/issues/?limit=10 --user ' + user + ':' + pass + ''
result_json = `#{uri}`
result = JSON.load(result_json)
# 取得したissuesで最終実行時間より最新の更新日のものをslackで発言
result["issues"].each do | r |
if lasttime.to_i < Time.parse(r["utc_last_updated"]).to_i then
issues_uri = 'https://bitbucket.org/' + rep_user + '/' + rep_name + '/issue/' + r["local_id"].to_s + '/'
text = rep_name + ' Issues #' + r["local_id"].to_s + ' [' + r["title"] + '] が更新されました。 ' + issues_uri
slack_post = 'curl -s -S -X POST --data-urlencode \'payload={"channel": "' + channel + '", "username": "webhookbot", "text": "' + text + '", "icon_emoji": "' + icon_emoji + '"}\' ' + slack_uri + ''
`#{slack_post}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment