LoginSignup
22
22

More than 5 years have passed since last update.

完了したタスクを自動で削除する(Slack x Trello)

Posted at

はじめに

Trelloはスクラム風のプロジェクト管理に便利です。どんなリストを用意しているかはプロジェクトによりますが、だいたいToDo、InProgress、Doneの3つくらいでしょうか?

Doneになったタスクは真面目なスクラム開発ならスプリントレビューで確認しますが、もっとライトなプロジェクト向けにDoneのタスクを自動で削除してくれるbotを作りました。

機能

以下の2つの機能があります。
* 毎週月曜日の朝9時にDoneにあるタスク一覧をSlackに投げる
* 午後6時になるとDoneにあるタスクを自動削除

本当にタスクが完了したのかを確認しないまま削除してしまうとさすがに問題があるので、朝1で一覧をSlackに投げるようにしています。これによって「この機能本当に終わったの?」とか言った会話が生まれ、いつの間にかタスクが消えていたという事態を防げます。

内容

hubotを作成し必要なパッケージをインストール

npm install cron time node-trello --save

以下のコードを/scriptsに配置。

/scripts/cron.coffee
# Description:
#   cron scripts
#

module.exports = (robot) ->
  Trello = require("node-trello")
  t = new Trello(process.env.HUBOT_TRELLO_KEY, process.env.HUBOT_TRELLO_TOKEN)

  cron = require('cron').CronJob

  # set cron: Show Done List
  new cron('0 0 9 * * 1', () ->
    postDoneCards()
  , null, true, 'Asia/Tokyo').start()

  # set cron: Close Done List
  new cron('0 0 18 * * 1', () ->
    closeDoneCards()
  , null, true, 'Asia/Tokyo').start()

  postDoneCards = () ->
    getCards (cards) ->
      if !cards.length
        postMessage "今回のスプリントでの完了タスクはありません。次がんばろう"
        return

      text = buildMessage cards
      postMessage text

  closeDoneCards = () ->
    getCards (cards) ->
      for card in cards
        closeCard(card)

      postMessage "完了したカードを削除しました"

  buildMessage = (cards) ->
    text = "今回のスプリントで完了したタスクがあります\n"
    text += "夜には自動で削除されます。本当に完了したかもう一度確認してください\n"
    text += "****************************************************\n"
    for card in cards
      text += "* #{card.name}\n"

    return text

  getCards = (callback) ->
    t.get "/1/lists/#{process.env.HUBOT_TRELLO_DONE_LIST}/cards", {}, (err, data) ->
      if err
        console.log err
        postMessage "Trelloからカードを取れませんでした..."
        return

      callback(data)

  closeCard = (card) ->
    t.put "/1/cards/#{card.id}/closed", {value: true}, (err, data) ->
      if (err)
        console.log err

  postMessage = (message) ->
    robot.messageRoom process.env.SLACK_CHANNEL_FOR_CRON, message

各種環境変数を設定。TrelloAPIの使い方は以下の記事で書いていますので参考にどうぞ。
http://qiita.com/KeitaMoromizato/items/8d56951f7e6fa57f9a93

HUBOT_TRELLO_KEY=xxxxxxxxxxxxxxx
HUBOT_TRELLO_TOKEN=xxxxxxxxxxxxxxx
HUBOT_TRELLO_DONE_LIST=xxxxxxxxxxxxxxx(削除対象のリストID)
SLACK_CHANNEL_FOR_CRON=xxxxxxxxxxxxxxx

完了!

22
22
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
22
22