LoginSignup
21
20

More than 5 years have passed since last update.

BacklogのタスクをSlackに通知する

Last updated at Posted at 2014-05-01

やりたいこと

backlogに新規タスクが追加されたら、slackに通知ポストする

使ったもの

xmlrpc
slack-notify

slackはclientはたくさんありますが、どれでやっても同じような感じだと思います

サンプルコード

require 'xmlrpc/client'
require "slack-notify"

class BacklogClient
  BACKLOG_HOST = 'xxxxxxxx'
  BACKLOG_USER = 'user'
  BACKLOG_PASS = 'pass'

  def initialize
    @client = XMLRPC::Client.new(BACKLOG_HOST, '/XML-RPC', 443, nil, nil, BACKLOG_USER, BACKLOG_PASS, true, 60)
  end

  def projectlist
    @client.call('backlog.getProjects')
  end

  def issuelist(project_id)
    @client.call('backlog.findIssue', {"projectId" => project_id})
  end

end

class SlackClient
  SLACK_TEAM  = 'test'
  SLACK_ROOM  = '#hoge'
  SLACK_NAME  = 'backlog'
  SLACK_TOKEN = 'xxxxxxxxxxx'

  def initialize
    @client = SlackNotify::Client.new(SLACK_TEAM, SLACK_TOKEN, {
      channel: SLACK_ROOM,
      username: SLACK_NAME,
    })
  end

  def message(msg, room = nil)
    @client.notify(msg, room)
  end
end

last_sent_date = Time.now.strftime("%Y%m%d%H%M%S").to_i

backlog = BacklogClient.new
slack = SlackClient.new
while true
  begin
    projects = backlog.projectlist
    projects.each{|project|
      issues = backlog.issuelist(project["id"])
      issues.each{|issue|
        if last_sent_date < issue["updated_on"].to_i
          action = (issue["created_on"] == issue["updated_on"]) ? "created" : "updated"
          mes = "[" + project["name"] + "]" + issue['summary'] + "(" + issue['url'] + ") " + action
          slack.message(mes)
          last_sent_date = issue["updated_on"].to_i
        end
      }
    }
  rescue => e
    puts e.message
  end
  sleep(10.0)
end

あくまでもサンプルです。

参照

こちらに続きます

21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20