Permalink
Browse files

Make Trajectory script also listen to ideas

  • Loading branch information...
1 parent de4993b commit e70274e4c7eaae653ab32d7af5e6c40ab6115455 @galfert galfert committed Aug 19, 2012
Showing with 31 additions and 12 deletions.
  1. +31 −12 src/scripts/trajectorystorylisten.coffee
View
43 src/scripts/trajectorystorylisten.coffee
@@ -1,5 +1,5 @@
# Description:
-# Listens for Trajectory story links.
+# Listens for Trajectory story and idea links.
#
# Dependencies:
# None
@@ -9,33 +9,52 @@
# HUBOT_TRAJECTORY_ACCOUNT: your Trajectory account number
#
# Commands:
-# <a Trajectory story URL> - sends back some story details
+# <a Trajectory story or idea URL> - sends back some details
#
# Author:
# galfert
module.exports = (robot) ->
- robot.hear /apptrajectory\.com\/\w+\/(\w+)\/stories\/(\d+)/i, (msg) ->
+ robot.hear /apptrajectory\.com\/\w+\/(\w+)\/(stories|ideas)\/(\d+)/i, (msg) ->
apiKey = process.env.HUBOT_TRAJECTORY_APIKEY
account = process.env.HUBOT_TRAJECTORY_ACCOUNT
unless apiKey && account
msg.send "Please set HUBOT_TRAJECTORY_APIKEY and HUBOT_TRAJECTORY_ACCOUNT correctly"
return
- project = msg.match[1]
- storyId = msg.match[2]
- storyURL = "https://www.apptrajectory.com/api/#{apiKey}/accounts/#{account}/projects/#{project}/stories/#{storyId}.json"
+ project = msg.match[1]
+ elementType = msg.match[2]
+ elementId = parseInt msg.match[3], 10
- msg.http(storyURL).get() (err, res, body) ->
+ baseURL = "https://www.apptrajectory.com/api/#{apiKey}/accounts/#{account}/projects/#{project}"
+ detailsURL = {
+ "stories": "#{baseURL}/stories/#{elementId}.json",
+ "ideas" : "#{baseURL}/ideas.json"
+ }[elementType]
+
+ msg.http(detailsURL).get() (err, res, body) ->
if err
msg.send "Trajectory says: #{err}"
return
unless res.statusCode is 200
msg.send "Got me a code #{res.statusCode}"
return
- story = JSON.parse body
- message = "\"#{story.title}\""
- message += ", assigned to #{story.assignee_name}" if story.assignee_name
- message += " (#{story.state} #{story.task_type.toLowerCase()})"
- msg.send message
+
+ details = JSON.parse body
+
+ if elementType == 'stories'
+ message = "\"#{details.title}\""
+ message += ", assigned to #{details.assignee_name}" if details.assignee_name
+ message += " (#{details.state} #{details.task_type.toLowerCase()})"
+ msg.send message
+
+ else if elementType == 'ideas'
+ for idea in details
+ if idea.id == elementId
+ message = "\"#{idea.subject}\""
+ message += ", created by #{idea.user.name}" if idea.user
+ message += " (#{idea.state})"
+ msg.send message
+ return
+ msg.send "I've got no idea what you are talking about"

0 comments on commit e70274e

Please sign in to comment.