Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 76 lines (69 sloc) 3.041 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
# Description:
# Get current stories from PivotalTracker
#
# Dependencies:
# "xml2js": "0.1.14"
#
# Configuration:
# HUBOT_PIVOTAL_TOKEN
# HUBOT_PIVOTAL_PROJECT
#
# Commands:
# show me stories for <project> - shows current stories being worked on
# pivotal story <story_id> - shows story title, owner and status
#
# Author:
# assaf

Parser = require("xml2js").Parser

module.exports = (robot) ->
  robot.respond /show\s+(me\s+)?stories(\s+for\s+)?(.*)/i, (msg)->
    token = process.env.HUBOT_PIVOTAL_TOKEN
    project_name = msg.match[3]
    if project_name == ""
      project_name = RegExp(process.env.HUBOT_PIVOTAL_PROJECT, "i")
    else
      project_name = RegExp(project_name + ".*", "i")

    msg.http("http://www.pivotaltracker.com/services/v3/projects").headers("X-TrackerToken": token).get() (err, res, body) ->
      if err
        msg.send "Pivotal says: #{err}"
        return
      (new Parser).parseString body, (err, json)->
        for project in json.project
          if project_name.test(project.name)
            msg.http("https://www.pivotaltracker.com/services/v3/projects/#{project.id}/iterations/current").headers("X-TrackerToken": token).query(filter: "state:unstarted,started,finished,delivered").get() (err, res, body) ->
              if err
                msg.send "Pivotal says: #{err}"
                return
      
              (new Parser).parseString body, (err, json)->
                for story in json.iteration.stories.story
                  message = "##{story.id['#']} #{story.name}"
                  message += " (#{story.owned_by})" if story.owned_by
                  message += " is #{story.current_state}" if story.current_state && story.current_state != "unstarted"
                  msg.send message
            return
        msg.send "No project #{project_name}"

  robot.respond /(pivotal story)? (.*)/i, (msg)->
    token = process.env.HUBOT_PIVOTAL_TOKEN
    project_id = process.env.HUBOT_PIVOTAL_PROJECT
    story_id = msg.match[2]

    msg.http("http://www.pivotaltracker.com/services/v3/projects").headers("X-TrackerToken": token).get() (err, res, body) ->
      if err
        msg.send "Pivotal says: #{err}"
        return
      (new Parser).parseString body, (err, json)->
        for project in json.project
          msg.http("https://www.pivotaltracker.com/services/v3/projects/#{project.id}/stories/#{story_id}").headers("X-TrackerToken": token).get() (err, res, body) ->
            if err
              msg.send "Pivotal says: #{err}"
              return
            if res.statusCode != 500
              (new Parser).parseString body, (err, story)->
                if !story.id
                  return
                message = "##{story.id['#']} #{story.name}"
                message += " (#{story.owned_by})" if story.owned_by
                message += " is #{story.current_state}" if story.current_state && story.current_state != "unstarted"
                msg.send message
                storyReturned = true
                return
    return
Something went wrong with that request. Please try again.