#!/usr/bin/env ruby require 'open-uri' require 'json' BASE_URL = "https://www.pivotaltracker.com/services/v5/stories/" def get_story_info(story_id, flags = []) return nil if story_id.empty? begin include_owner_names = flags.any? { |f| f == '--owners' || f == '-O' } url = BASE_URL + "#{story_id}" + "/#{include_owner_names ? '?fields=:default,owners' : ''}" open( url, 'X-TrackerToken' => ENV['PIVOTAL_TRACKER_API_TOKEN'] ).read rescue StandardError => e JSON.generate({error: "could not get story info for story #{story_id}: #{e.message}\nstory info may be available at https://www.pivotaltracker.com/story/show/#{story_id}"}) end end flags = ARGV.reduce([]) do |reduced, a| reduced << a if a.include?('--') || a.include?('-') reduced end flags.each { |f| ARGV.delete f } stream = ARGV.any? ? ARGV : ARGF if stream == ARGF && $stdin.tty? puts "Usage:\nget-story-info-from-id STORY_ID\n" puts " OR\n" puts "echo STORY_ID | get-story-info-from-id" puts " OR\n" puts "use --w-owner-names to include owner names" exit 1 end infos = stream.reduce([]) do |reduced, story_id| next reduced unless story_id && story_id =~ /\d+/ reduced << get_story_info(story_id.strip, flags) end puts infos.join("\n")