class GitStoryid

Constants

TRACKER_CONFIG

Public Class Methods

new(*arguments) click to toggle source
# File lib/git_storyid.rb, line 22
def initialize(*arguments)
  @tracker = Configuration.build
  @git_options = []
  parser = OptionParser.new do |opts|
    opts.banner = "Do git commit with information from pivotal story"
    opts.on("-m", "--message [MESSAGE]", "Add addional MESSAGE to comit") do |custom_message|
      @custom_message = custom_message
    end
    opts.on("-f", "--finish", "Specify that this commit finishes a story or fixes a bug") do
      @finish_stories = true
    end
    opts.on("-d", "--deliver", "Specify that this commit delivers a story or a bug") do
      @deliver_stories = true
    end
  end
  parser.parse!(arguments)

  unless arguments.empty?
    @stories = arguments.map do |argument|
      @tracker.find_story_by_id(argument)
    end
  end
end
output(message) click to toggle source
# File lib/git_storyid.rb, line 13
def self.output(message)
  puts message
end
run(*args) click to toggle source
# File lib/git_storyid.rb, line 9
def self.run(*args)
  new(*args).run
end

Public Instance Methods

build_commit_message() click to toggle source
# File lib/git_storyid.rb, line 118
def build_commit_message
  message = @stories.map do |story|
    "#{finish_story_prefix(story)}##{story.id}"
  end.join(", ")
  message = "[#{message}] "
  if @custom_message && !@custom_message.empty?
    message += @custom_message.to_s + "\n\n"
  end
  message += @stories.map do |story|
    "#{story.type.capitalize}: " + story.name.strip
  end.join("\n\n")
  message
end
commit_changes() click to toggle source
# File lib/git_storyid.rb, line 114
def commit_changes
  output execute("git", "commit", "-m", build_commit_message)
end
ensure_changes_stashed!() click to toggle source
# File lib/git_storyid.rb, line 142
def ensure_changes_stashed!
  if execute("git", "diff", "--staged").empty?
    quit "No changes staged to commit."
  end
end
execute(*args) click to toggle source
# File lib/git_storyid.rb, line 138
def execute(*args)
  Open3.popen3(*args) {|i, o| return o.read }
end
fetch_story(index) click to toggle source
# File lib/git_storyid.rb, line 60
def fetch_story(index)
  if (1..100).include?(index.to_i)
    @tracker.all_stories[index.to_i - 1]
  else
    # Consider it a direct story id
    @tracker.find_story_by_id(index)
  end
end
finish_story_prefix(story) click to toggle source
# File lib/git_storyid.rb, line 132
def finish_story_prefix(story)
  return "Delivers " if @deliver_stories
  return "" unless @finish_stories
  story.type == "bug" ? "Fixes " : "Finishes "
end
output(message) click to toggle source
# File lib/git_storyid.rb, line 17
def output(message)
  self.class.output(message)
end
quit(message) click to toggle source
# File lib/git_storyid.rb, line 103
def quit(message)
  output message
  exit 1
end
quit_if_no_stories() click to toggle source
# File lib/git_storyid.rb, line 69
def quit_if_no_stories
  if @tracker.all_stories.empty?
    quit "No stories started and owned by you."
  end
end
readline() click to toggle source
# File lib/git_storyid.rb, line 99
def readline
  Readline.readline("Indexes: ", true)
end
readline_stories_if_not_present() click to toggle source
# File lib/git_storyid.rb, line 46
def readline_stories_if_not_present
  if !@stories || @stories.empty?
    quit_if_no_stories
    output stories_menu
    @stories = readline_story_ids.map do |index|
      fetch_story(index) || quit("Story #{index} was not found.")
    end
  end
rescue RefetchStories
  @tracker.reset
  @stories = nil
  readline_stories_if_not_present
end
readline_story_ids() click to toggle source
# File lib/git_storyid.rb, line 84
def readline_story_ids
  input = readline
  if input =~ /\A\s*re?f?r?e?s?h?\s*\z/
    raise RefetchStories
  end
  ids = input.split(/\s*,\s*/).reject do |string|
    string.empty?
  end
  if ids.empty?
    quit("Cancelling.")
  else
    ids
  end
end
run() click to toggle source
# File lib/git_storyid.rb, line 108
def run
  ensure_changes_stashed!
  readline_stories_if_not_present
  commit_changes
end
stories_menu() click to toggle source
# File lib/git_storyid.rb, line 75
def stories_menu
  result = ""
  @tracker.all_stories.each_with_index do |story, index|
    result << "[#{index + 1}] #{story.name}\n"
  end
  result << "\n"
  result
end