class GitBranch

Private Class Methods

usage_message() click to toggle source
# File lib/pivotoolz/git_branch.rb, line 82
def self.usage_message
  "Usage : pv-git-branch '<pivotal_story_id>' '[<appended_branchname_values>...]'"
end

Public Instance Methods

generate(story_id, *append) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 4
def generate(story_id, *append)
  if !story_id
    puts "#{GitBranch.usage_message}"
    exit
  end

  story = get_pivotal_story(story_id)

  author      = set_author
  category    = set_category(story['story_type'])
  description = set_description(story['name'])
  pivotal_id  = story['id']
  append      = set_append_values(append)

  create_branch("#{author}/#{category}/#{description}-#{pivotal_id}#{append}")
end

Private Instance Methods

clean_text(string_value) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 74
def clean_text(string_value)
  string_value.tr!(':_,/.&!@#$%^*()[]\'`<>"', '') # remove unwanted punctuation
  string_value.tr!('-', ' ') # replace dashes with space
  string_value.downcase! # convert to lowercase
  string_value.gsub!(/ +/,'-') # collapse spacing and replace spaces with dashes
  string_value[0 .. 35].gsub(/-$/, '') # limit to 45 chars
end
create_branch(branch_name) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 23
def create_branch(branch_name)
  `git checkout -b #{branch_name}`
  if $?.exitstatus == 128
    puts 'Checking out existing branch...'
    `git checkout #{branch_name}`
  end
end
get_author_initials() click to toggle source
# File lib/pivotoolz/git_branch.rb, line 45
def get_author_initials
  initials = `git config user.initials`.strip
  initials unless initials.empty?
end
get_author_name() click to toggle source
# File lib/pivotoolz/git_branch.rb, line 50
def get_author_name
  name = `git config user.name`.strip
  name unless name.empty?
end
get_pivotal_story(pivotal_id) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 31
def get_pivotal_story(pivotal_id)
  unless ENV['PIVOTAL_TRACKER_API_TOKEN']
    puts "!: You need to set the 'PIVOTAL_TRACKER_API_TOKEN' environment variable"
    exit
  end

  story = JSON.parse(`get-story-info-from-id #{pivotal_id.tr('#', '')}`)
end
get_whoami() click to toggle source
# File lib/pivotoolz/git_branch.rb, line 55
def get_whoami
  you = `whoami`.strip
  you unless you.empty?
end
set_append_values(appendings) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 68
def set_append_values(appendings)
  return '' if appendings.empty?
  clean_appendings = appendings.map{|appending| clean_text(appending) }
  "-#{clean_appendings.join('-')}"
end
set_author() click to toggle source
# File lib/pivotoolz/git_branch.rb, line 40
def set_author
  author_name = get_author_initials || get_author_name || get_whoami || 'unknown'
  clean_text(author_name)
end
set_category(category) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 60
def set_category(category)
  category + 's'
end
set_description(description) click to toggle source
# File lib/pivotoolz/git_branch.rb, line 64
def set_description(description)
  clean_text(description)
end