Skip to content
This repository
beneidel July 20, 2012
file 165 lines (118 sloc) 4.436 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
# Description:
# Take notes on scrum daily meetings
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_SCRUMNOTES_PATH - if set, folder where daily notes should be saved as json files (otherwise they just stay on robot brain)
#
# Commands:
# hubot take scrum notes - Starts taking notes from all users in the room (records all messages starting with yesterday, today, tomorrow, sun, mon, tue, wed, thu, fri, sat, blocking)
# hubot stop taking notes - Stops taking scrum notes (if a path is configured saves day notes to a json file)
# hubot scrum notes - shows scrum notes taken so far
# hubot are you taking notes? - hubot indicates if he's currently taking notes
#
# Author:
# benjamin eidelman

env = process.env
fs = require('fs')

module.exports = (robot) ->
  
  # rooms where hubot is hearing for notes
  hearingRooms = {}
  messageKeys = ['blocking', 'blocker', 'yesterday', 'today', 'tomorrow', 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']

  getDate = ->
    today = new Date()
    dd = today.getDate()
    mm = today.getMonth()+1
    yyyy = today.getFullYear()
    if (dd<10)
      dd='0'+dd
    if (mm<10)
      mm='0'+mm
    return yyyy+'-'+mm+'-'+dd

  listener = null

  startHearing = ->

    if (listener)
      return

    listenersCount = robot.catchAll (msg) ->

      if (!hearingRooms[msg.message.user.room])
        return

      today = getDate()
      name = msg.message.user.name

      robot.brain.data.scrumNotes ?= {};
      notes = robot.brain.data.scrumNotes[today] ?= {}

      notes._raw ?= [];
      notes._raw.push([new Date().getTime(), name, msg.message.text])

      keyValue = /^([^ :\n\r\t]+)[ :\n\t](.+)$/m.exec(msg.message.text)
      if (keyValue)
        notes[name] ?= {}
        key = keyValue[1].toLowerCase()
        if (key in messageKeys)
          notes[name][key] ?= [];
          notes[name][key].push(keyValue[2])

    listener = robot.listeners[listenersCount - 1]

  stopHearing = ->

    if (!listener)
      return

    listenerIndex = -1
    for list, i in robot.listeners
      if list is listener
        listenerIndex = i
        break
    if (listenerIndex >= 0)
        setTimeout ->
          robot.listeners.splice(i, 1)
        , 0
    listener = null

  mkdir = (path, root) ->

    dirs = path.split('/')
    dir = dirs.shift()
    root = (root||'')+dir+'/'

    try
      fs.mkdirSync(root)
    catch e
        # dir wasn't made, something went wrong
        if (!fs.statSync(root).isDirectory())
          throw new Error(e)

    return !dirs.length || mkdir(dirs.join('/'), root)

  robot.respond /(?:show )?scrum notes/i, (msg) ->

    today = getDate()

    notes = robot.brain.data.scrumNotes?[today]

    if !notes
      msg.reply('no notes so far')
    else

      # build a pretty version
      response = []
      for own user, userNotes of notes
        if user != '_raw'
          response.push(user, ':\n')
          for key in messageKeys
            if userNotes[key]
              response.push(' ', key, ': ', userNotes[key].join(', '), '\n')

      msg.reply(response.join(''))

  robot.respond /take scrum notes/i, (msg) ->

    startHearing()

    hearingRooms[msg.message.user.room] = true

    msg.reply('taking scrum notes, I hear you');

  robot.respond /are you taking (scrum )?notes\?/i, (msg) ->

    takingNotes = !!hearingRooms[msg.message.user.room]

    msg.reply(if takingNotes then 'Yes, I\'m taking scrum notes' else 'No, I\'m not taking scrum notes')

  robot.respond /stop taking (?:scrum )?notes/i, (msg) ->

    delete hearingRooms[msg.message.user.room];

    msg.reply("not taking scrum notes anymore");

    today = getDate()
    notes = robot.brain.data.scrumNotes?[today]

    users = (user for user in Object.keys(notes) when user isnt '_raw')

    count = if notes then users.length else 0

    status = "I got no notes today"
    if count > 0
      status = ["I got notes from ", users.slice(0,Math.min(3, users.length - 1)).join(', '), " and ", if users.length > 3 then (users.length-3)+' more' else users[users.length-1]].join('')

    msg.reply(status);

    if (Object.keys(hearingRooms).length < 1)
      stopHearing()

    saveTo = process.env.HUBOT_SCRUMNOTES_PATH

    if (saveTo)
      mkdir(saveTo + '/scrumnotes')
      fs.writeFileSync(saveTo + '/scrumnotes/' + today + '.json', JSON.stringify(notes, null, 2))
      msg.send('scrum notes saved at: /scrumnotes/' + today + '.json')

Something went wrong with that request. Please try again.