public
Created

pivotal.rb

  • Download Gist
pivotal.rb
Ruby
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#!/usr/bin/env ruby
 
require 'pivotal-tracker'
require 'yaml'
 
TEMP_DIR = "#{ENV['HOME']}/.pivotal/"
TEMP_FILE = TEMP_DIR + "temp.yml"
CONFIG_FILE = TEMP_DIR + "config.yml"
 
unless File.directory? TEMP_DIR
Dir::mkdir TEMP_DIR
end
 
unless File.file? TEMP_FILE
f = File.open(TEMP_FILE, 'w')
f.write("id: -1")
end
 
unless File.file? CONFIG_FILE
f = File.open(CONFIG_FILE, 'w')
f.write("")
end
# TEMP FILE format:
# current_story_id: 123 OR -1
 
def story_info story
puts story.name
puts "id:\t\t#{story.id}"
puts "notes:\t\t#{story.description}"
puts "status:\t\t#{story.current_state}"
puts "estimate:\t#{(story.estimate == -1) ? 'unestimated' : story.estimate}"
end
 
def is_next string
string == 'next'
end
 
def id string
!string.match(/^[\d]+(\.[\d]+){0,1}$/).nil?
end
 
def current string
string == 'current'
end
 
def story_has_been_started
f = YAML.load_file(TEMP_FILE)
@current_id = f['id']
f['id'] != -1
end
 
def update_id id
f = File.open(TEMP_FILE, 'w')
f.write("id: #{id}")
@current_id = id
end
 
def set_config key, value
config = YAML::load(File.read(CONFIG_FILE)) || {}
config[key.to_sym] = value
open(CONFIG_FILE, 'w') { |f| YAML::dump(config, f) }
end
 
def authenticate
config = YAML::load(File.read(CONFIG_FILE)) || {}
has_email = true, has_password = true
 
unless config.has_key? :email
puts "\033[33mUse pivotal set email EMAIL to set your pivotal tracker email\033[0m\n"
has_email = false
end
 
unless config.has_key? :password
puts "\033[33mUse pivotal set password PASSWORD to set your pivotal tracker email\033[0m\n"
has_password = false
end
 
unless has_email and has_password
exit 0
end
PivotalTracker::Client.token(config[:email], config[:password])
end
 
@current_id = nil
 
begin
unless ARGV[0] == 'set'
authenticate
aidin = PivotalTracker::Project.all.first
end
rescue
puts "\033[33mInvalid Credentials\033[0m\n"
exit 0
end
 
case ARGV[0]
when "info"
# pivotal info next
# pivotal info id
# pivotal info current
 
if is_next(ARGV[1])
puts "\033[32mDisplaying information for next story\033[0m\n"
story_info(aidin.stories.all(owner: 'Chintan Parikh', state: 'unstarted').first)
 
elsif id(ARGV[1])
story = aidin.stories.find(Integer(ARGV[1]))
unless story.nil?
puts "\033[32mDisplaying information for story #{ARGV[1]}\033[0m\n"
story_info(story)
else
puts "\033[33mNo story with id #{ARGV[1]} exists\033[0m\n"
end
elsif current(ARGV[1])
if story_has_been_started
story = aidin.stories.find(@current_id)
puts "\033[32mDisplaying information for current story\033[0m\n"
story_info(story)
else
puts "\033[33mNo story has been started. Use pivotal start to start a story first\033[0m\n"
end
end
 
when "estimate"
# pivotal estimate next 1-8
# pivotal estimate id 1-8
# pivotal estimate current 1-8
 
when "start"
# pivotal start next
# pivotal start id
if story_has_been_started
current_branch = `git branch | grep "*" | sed "s/* //"`.chomp
status = `git status -s`
if !status.empty?
puts "\033[33mYou are currently working on story #{@current_id} and have uncommitted changes on #{current_branch}. If you continue, your uncommitted changes will be lost. Continue? (Y/N)\033[0m\n"
continue = false
while (!continue)
option = $stdin.gets.chomp
if option == 'N'
exit 1
elsif option != 'Y'
puts "Please enter either Y or N"
else
continue = true;
end
end
end
end
 
`git stash`
`git stash drop`
`git checkout develop`
`git pull`
 
if is_next(ARGV[1])
story = aidin.stories.all(owner: 'Chintan Parikh', state: 'unstarted').first
new_branch = "feature/#{story.id}_#{story.name.downcase.gsub(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}"
`git checkout -b #{new_branch}`
elsif id(ARGV[1])
story = aidin.stories.find(Integer(ARGV[1]))
unless story.nil?
new_branch = "feature/#{story.id}_#{story.name.downcase.gsub(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}"
`git checkout -b #{new_branch}`
else
puts "\033[33mNo story with id #{ARGV[1]} exists\033[0m\n"
end
end
 
update_id(story.id) unless story.nil?
story.update(current_state: 'started')
 
puts "\033[32mStory #{story.id} has been started\033[0m\n"
story_info(story)
 
when "complete"
if story_has_been_started
story = aidin.stories.find(@current_id)
branch = "feature/#{story.id}_#{story.name.downcase.gsub(' ', '_').gsub(/[^0-9A-Za-z_]/, '')}"
`git push origin #{branch}`
story.update(current_state: 'finished')
story.update(current_state: 'delivered')
puts "\033[32mStory #{story.id} has been completed\033[0m\n"
update_id(-1)
else
puts "\033[33mNo story has been started. Use pivotal start to start a story first\033[0m\n"
end
when 'abandon'
if story_has_been_started
story = aidin.stories.find(@current_id)
story.update(current_state: 'unstarted')
puts "\033[32mStory #{story.id} has been abandoned\033[0m\n"
update_id(-1)
else
puts "\033[33mNo story has been started. Use pivotal start to start a story first\033[0m\n"
end
 
when 'list'
stories = aidin.stories.all(owner: 'Chintan Parikh', current_state: ['unstarted', 'started', 'finished', 'delivered'])
stories.each do |story|
story_info(story)
end
when 'set'
# set email
# set password
set_config(ARGV[1], ARGV[2])
puts "\033[32m#{ARGV[1]} has been set to #{ARGV[2]}\033[0m\n"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.