#!/usr/bin/env ruby require('json') require('ostruct') require('rest-client') stream = ARGV.any? ? ARGV : ARGF if stream == ARGF && $stdin.tty? puts "Usage: deliver-story STORY_INFO_JSON" puts "Examples:\n deliver-story '{\"id\":123,\"current_state\":\"started\"}'" puts %Q[ echo '{"id":123,"current_state":"started"}' | deliver-story] exit 1 end stream.each do |story_hash| story = OpenStruct.new(JSON.parse(story_hash)) story_state = story.current_state if story_state != 'finished' puts "Story #{story.id} is not finished, not delivering" next end story_url = "https://www.pivotaltracker.com/services/v5/stories/#{story.id}" begin response = RestClient::Request.execute( method: :put, url: story_url, payload: {current_state: 'delivered'}, headers: {'X-TrackerToken' => ENV['PIVOTAL_TRACKER_API_TOKEN']} ) puts response rescue RestClient::Exception => e puts "Error delivering story #{story.id}, #{e.message}:\n#{e.backtrace.join("\n")}" end end