class CitizenCodeScripts::Begin

Public Class Methods

description() click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 29
def self.description
  "Starts your Pivotal Tracker story and opens a new PR on Github"
end
help() click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 33
    def self.help
      <<-EOF
citizen begin #{colorize(:light_blue, "[story id] [branch name]")}

Example: $ #{colorize(:light_blue, 'citizen begin "#133717234"')}

This command will start your Pivotal Tracker story for you, open a pull
request on Github, and copy over the Pivotal Tracker story description to
the Github pull request description. As well, any tasks in your
Pivotal Tracker story will automatically become [x] checkbox tasks on the
Github PR.

* All branch names will be auto-converted to
  kebab-case, lowercase

* Passing story id/branch name as arguments are optional - if
  they are missing, you'll be prompted
EOF
    end

Public Instance Methods

run() click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 53
    def run
      story_id, branch_name = argv

      if !command?("hub")
        abort <<-EOF
You need to install `hub` before you can use this program.

brew install hub
EOF
      end

      pivotal = Pivotal.new(git_config("user.pivotalApiToken"))
      config_abort_if_blank!("user.pivotalApiToken", pivotal.token)

      project_id = git_config("user.pivotalProjectId")
      config_abort_if_blank!("user.pivotalProjectId", project_id)

      story_id ||= prompt("Please paste the Pivotal story ID here")
      story_id = story_id.gsub(/[^\d]/, '')

      story_url = "https://www.pivotaltracker.com/services/v5"\
        "/projects/#{project_id}/stories/#{story_id}"

      story = pivotal.request(story_url)

      default_branch_name = normalized_branch_name(story['name'])

      branch_name ||= prompt("Please enter a branch name (#{default_branch_name})")

      if branch_name.strip == ""
        branch_name = default_branch_name
      else
        branch_name = normalized_branch_name(branch_name)
      end

      # Start the story
      pivotal.request(story_url, method: "PUT", body: '{ "current_state":"started" }')

      silent "git checkout master",
        "git fetch origin",
        "git reset --hard origin/master"

      puts "==> Checking out #{branch_name}"

      silent "git checkout -b #{branch_name}",
        'git commit --allow-empty -m "Initial commit for story #' + story_id + '"',
        "git push origin #{branch_name}",
        "git branch --set-upstream #{branch_name} origin/#{branch_name}"

      tasks = pivotal.request(
      "https://www.pivotaltracker.com/services/v5"\
        "/projects/#{project_id}/stories/#{story_id}/tasks"
      )

      story_description = story['description']

      if story_description.nil?
        story_description = "_No story description given in Pivotal_"
      end

      description = <<-EOF
#{story['name']}

#{story['url']}

# Description

#{story_description}
EOF

      description.strip!

      unless tasks.empty?
        description += "\n\n## TODO\n\n"

        tasks.each do |task|
          description += "- [ ] #{task['description']}\n"
        end

        description.strip!
      end

      puts "==> Opening pull request on GitHub"

      tempfile = Tempfile.new('begin_pull_request')

      begin
        tempfile.write(description)
        tempfile.close

        labels = ['WIP', story['story_type']].join(',')

        url = `hub pull-request -F #{tempfile.path} -l "#{labels}" -a "" -o`

        # Copy it to your clipboard
        system("echo #{url} | pbcopy")
        puts url
      ensure
        tempfile.unlink
      end
    end

Private Instance Methods

command?(name) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 161
def command?(name)
  `which #{name}`
  $?.success?
end
config_abort_if_blank!(key, value) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 170
    def config_abort_if_blank!(key, value)
      if value.strip == ""
        abort <<-EOF
You need to set the #{key} value in your git config!

git config --local --add #{key} [value]
EOF
      end
    end
git_config(key) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 166
def git_config(key)
  `git config --local --get #{key}`.strip
end
normalized_branch_name(branch_name) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 187
def normalized_branch_name(branch_name)
  branch_name
    .gsub(/[^\w\s-]/, '')
    .gsub(/\s+/, '-')
    .downcase
    .gsub(/-*$/, '') # trailing dashes
end
prompt(msg) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 180
def prompt(msg)
  print "#{msg} > "
  value = STDIN.gets.strip
  puts
  value
end
silent(*cmds) click to toggle source
# File lib/citizen_code_scripts/begin.rb, line 157
def silent(*cmds)
  cmds.each { |cmd| system("#{cmd} >/dev/null 2>&1") }
end