Export Trajectory Stories to CSV

I love using Trajectory to manage the development of our apps. Its interface is clean and straightforward and it satisfies all our needs for emergent planning. Most importantly, it's a great place for focused discussion with our clients, who have no trouble at all in using it fully.

We're at the stage in a project now where we've fleshed out the app's key features in user stories and have given them all points. At this point, we need to revisit the project as a whole to ensure that we can still deliver something great for the client within their budget. As nice as pure agile might be to follow, the commercial reality is that a client still likes (and deserves) some kind of up-front estimate. To do this, we need to tally up the points and work out how everything would translate into time and money.

This is all a rather long introduction so I can share with you a little Ruby script I wrote today to export Trajectory stories to CSV. Once they're in a spreadsheet, it's a lot easier to quickly manipulate the data and calculate total points. Trajectory doesn't currently have any kind of CSV export, but they do have the beginnings of a nice JSON API. We can use this to pull out the stories and output them as CSV. Here's how:

require 'rubygems'
require 'bundler/setup'
require 'csv'
require 'faraday'
require 'faraday_middleware'

API_KEY = ""
ACCOUNT_KEYWORD = ""
PROJECT_KEYWORD = ""

# Current story fields are:
#
# archived
# assignee_id
# branch
# created_at
# deleted
# design_needed
# development_needed
# id
# idea_id
# iteration_id
# points
# position
# state
# task_type
# title
# updated_at
# user_id
# comments_count
# assignee_name
# user_name
# state_events
# idea_subject

FIELDS = [
  'id',
  'task_type',
  'points',
  'title'
].freeze

connection = Faraday.new(:url => 'https://www.apptrajectory.com/') do |conn|
  conn.response :json, :content_type => /\bjson$/
  conn.adapter Faraday.default_adapter
end

response = connection.get("/api/#{API_KEY}/accounts/#{ACCOUNT_KEYWORD}/projects/#{PROJECT_KEYWORD}/stories.json")

csv_str = CSV.generate do |csv|
  csv << FIELDS

  response.body['stories'].each do |story|
    csv << FIELDS.map { |field_name| story[field_name] }
  end  
end

puts csv_str

And you'll want this Gemfile in place too:

source :rubygems

gem 'faraday', '>= 0.8.0.rc2'
gem 'faraday_middleware'

Run bundle, populate the constants at the start of the script, and then you can get your CSV:

ruby export.rb > stories.csv

This script is also a nice demonstration of the power of Faraday and its middlewares for consuming REST APIs. With just a single line, we get JSON parsing for free! Check out this great article if you want to learn more about Faraday.

This Trajectory CSV export script and its Gemfile are avilable as a Gist for your cloning and forking pleasure.