futabooo blog

色々手をつけすぎてすぐに忘れるので備忘録

JIRAのMentionコメントをSlackのDMで通知するスクリプト

追記(2015/05/19)

このまま使うには一度botとDMのやりとりをしておく必要があります。

はじめに

JIRAのコメントの通知がメールでしか来ないと見逃すし、Slackに流れてきたら楽なのにって思ったので作った。

使い方

  1. JIRAのWebHooksに[your heroku app url]/hubot/jira-comment-dmのURLを登録し、issueのupdatedだけチェックを入れておく。
  2. JIRAのURLをheroku config:add HUBOT_JIRA_URL=[your jira url]環境変数に登録しておく。
  3. (必要であれば)JIRAのハンドルネームとSlackのハンドルネームが異なる場合はmapに登録しておく。

サンプル

こんな感じでDMが届く。仕事しろって言われてる。 f:id:futabooo:20150504192004p:plain

# Description:
#   hubot monitoring jira comments mention and slack DM
#
# Dependencies:
#   None
#
# Configuration:
#   HUBOT_JIRA_URL
#
# Commands:
#   None
#
# Author:
#   futabooo


# Use if the user name of jira and slack is different
# "jiraName": "slackName"
map =


module.exports = (robot) ->

  convertHandleName = (name) ->
    map[name] || name

  extractHandleName = (body) ->
    temp = body.match(/\[~.+?]/g)
    unless temp is null
      name = []
      for i in temp
        name.push("#{i}".replace(/[\[~\]]/g, ""))
      return name

  robot.router.post '/hubot/jira-comment-dm', (req, res) ->
    body = req.body
    if body.webhookEvent == 'jira:issue_updated' && body.comment
      issue = "#{body.issue.key} #{body.issue.fields.summary}"
      url = "#{process.env.HUBOT_JIRA_URL}/browse/#{body.issue.key}"
      handleNameList = extractHandleName(body.comment.body)

      unless handleNameList is null
        for i in handleNameList
          robot.send(room: convertHandleName(i),
            "*#{issue}* _(#{url})_\n@#{body.comment.author.name}'s comment:\n```#{body.comment.body}```")
    res.send 'OK'

さいごに

正規表現力が低くて一発で名前を抜けてないけど、動いてるから運用上問題ないと思われる。 人が増えたら超大変だろうなぁと思ってる。

参考

mnpk/hubot-jira-comment · GitHub