growl sample - HideakiSaito/benkyoukai4app-3.3 GitHub Wiki

### Redmine通知の個人設定
# redmine_hostname:					REDMINEのURL固定
# redmine_user:,redmine_password:	redmineのユーザのログインID、パスワード => redmineで利用するものです。
# api_key:							redmineの個人設定画面からAPIキーを貼り付けてください。
redmine_hostname: url
redmine_user: ////
redmine_password: ///
api_key: ///




##"C:\Program Files\Growl for Windows\growlnotify.exe" sample
##http://kakakikikeke.blogspot.jp/2013/11/growlredmineruby.html
require 'json'
require 'open-uri'
require 'date'
require 'openssl'
require 'thread'
require 'yaml'

this_query = "35"   #更新
title_prefix = "[更新]".encode('CP932')

config = YAML.load_file('C:\\Redmine@IT統\\個人設定.yml')
# configure 個人情報
redmine_user = config["redmine_user"]
redmine_password = config["redmine_password"]
redmine_hostname = config["redmine_hostname"]
api_key = config["api_key"]
target_url = "http://" + redmine_hostname + "/issues.json?query_id=" + this_query + "&KEY=" + api_key + "&include=journals&limit=100&page=1"

# configure GROWLデザイン
iconRed = '/i:"C:\\Redmine@IT統\\files\\redmine-logo.png"'
titleRed = '/t:"[更新]Redmine@IT統"'.encode('CP932')

# configure チケットの間隔、条件
before_minute = Time.now - 60*10

messages = ''.encode('CP932')
ticket_count = 0
res = open(target_url ,
    :http_basic_authentication => [redmine_user, redmine_password],
    :redirect => false,
	:proxy => nil).read
result = JSON.parse(res)

#取得したい情報を加工する。
result['issues'].each do |issues|
  update_time = Time.parse(issues['updated_on'])
  if update_time >= before_minute then
    message = "".encode('CP932')
    message << "<< " + issues['status']["name"].to_s.encode('CP932') + " >>"
    message << "\n".encode('CP932') 
    message << issues['project']["name"].encode('CP932')
    message << "\n".encode('CP932')
    message << "#" + issues['id'].to_s.encode('CP932')
    message << "\n".encode('CP932')
     time = update_time.strftime("%H時%M分").encode('CP932')
    message << "更新時間:".encode('CP932') + time
    message << "\n".encode('CP932')

	##ジャーナル情報の取得>>>
	target_url = "http://" + redmine_hostname + "/issues/" + issues['id'].to_s + ".json?KEY=" + api_key + "&include=journals&limit=100&page=1"
	res = open(target_url ,
	    :http_basic_authentication => [redmine_user, redmine_password],
	    :redirect => false,
		:proxy => nil).read
	journals = JSON.parse(res)
	last_note = ""
	cnt = 0
	journals['issue']['journals'].each do |journal|
	  created_time = Time.parse(journal['created_on'])
	  cnt += 1
	  if created_time >= before_minute then
	  	message << "\n".encode('CP932')
	  	message << journal['user']['name'].to_s.encode('CP932') + ">>>「".encode('CP932')
	    message << journal['notes'].to_s.encode('CP932') + "」".encode('CP932')
	    message << "\n".encode('CP932')
	    journal['details'].each do |detail|
	    	if detail['property'].to_s == "attr"
	    		#日本語ヵ
	    		attrname = detail['name']
	    		replacenames = 
	    			{	"done_ratio" => "進捗率", 
						"description" => "説明", 
						"assigned_to_id" => "担当", 
						"status_id" => "ステータス", 
						"priority_id" => "優先度", 
						"estimated_hours" => "説明", 
						"parent_id" => "親チケット", 
						"fixed_version_id" => "バージョン", 
						"checklist" => "チェック", 
						"start_date" => "開始日", 
	    			 	"due_date" => "期限"}
	    		replacenames.each do |key,val|
	    			attrname.gsub!(key,val)
				end
	    		message << "・".encode('CP932')  + attrname.encode('CP932')  + ": "
	    		#message << detail['old_value'].encode('CP932') + " -> "
	    		message << detail['new_value'].encode('CP932') 
	    		message << "\n".encode('CP932')
	    	end
	    	if detail['property'].to_s == "attachment"
	    		message << detail['new_value'].encode('CP932') + ":ファイルを添付しました。".encode('CP932')  
	    		message << "\n".encode('CP932')
	    	end
	    	if detail['property'].to_s == "relation"
	    		message << "関連するチケット ".encode('CP932')
	    		message << detail['name'].encode('CP932')  + ": "
	    		message << detail['old_value'].encode('CP932')  if detail['old_value']
	    		message << detail['new_value'].encode('CP932')  if detail['new_value']
	    		message << "\n".encode('CP932')
	    	end
	    end
	    message << "\n".encode('CP932')
	    last_note = "#note-" + cnt.to_s
	  end
	end
	##ジャーナル情報の取得<<<

    #message << "担当:" + issues['assigned_to']["name"].to_s.encode('CP932')
    #1件づつだすときは。message
    assigned_to = issues['assigned_to']["id"]
    subject = issues['subject'].encode('CP932')
    tracker = "[" + issues['tracker']["name"] + "]"
    title = '/t:"'.encode('CP932') + tracker.encode('CP932') + subject + '"'
    ##title = titleRed
    if assigned_to 
    	icon = '/i:"http://dev-its:8087/redmine/account/get_avatar/' + assigned_to.to_s + '"'
    else
    	icon = iconRed
    end
    types ='/r:"全体通知","更新通知","作成通知","チェック待ち通知"'
    type = '/n:"全体通知"'
    url='/cu:"http://dev-its:8087/redmine/issues/' + issues['id'].to_s + last_note + '"' 
    priority = '/p:"0"'
    sticky =  '/s:false'
    appicon = '/ai:"C:\\Redmine@IT統\\files\\redmine-logo.png"'.encode('CP932')
    Thread.new{
      system("C:\\Program Files\\Growl for Windows\\growlnotify.exe" ,  title ,types,type ,icon,sticky ,url,priority, appicon, message)
    }
    messages << message.encode('CP932')
    ticket_count += 1
  end
end
p ticket_count
if ticket_count == 0 then
  exit(0)
end