class Redk::CapnPanda::CLI

Public Instance Methods

add(*args) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 27
def add(*args)
  issue = Redk::CapnPanda.jira.Issue.build

  issue_fields = Redk::CapnPanda.config.jira_new_issue_fields.merge({
    "summary"=> args.join(" "),
    "project"=>{
      "key" => Redk::CapnPanda.config.jira_default_project_key
    },
    "issuetype"=>{
      "name" => Redk::CapnPanda.config.jira_default_issuetype_name
    }
  })
  issue_fields["assignee"] = {"name" => Redk::CapnPanda.config.jira_username} if options[:auto_assign]
  issue.save({
    "fields" => issue_fields
  })
  issue.fetch

rescue Redk::CapnPanda::KeyChainError
  puts "#{Redk::CapnPanda.config.jira_domain} doesnt exist in your keychain".colorize(:red)
rescue Interrupt
  puts "\nInterrupt detected... terminating.".colorize(:yellow)
end
commit_type() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 235
def commit_type
  @commit_type ||= parsed_branch[:type]
end
config(key=nil, value=nil) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 10
def config(key=nil, value=nil)
  if options[:nuke]
    Redk::CapnPanda::Config.nuke
  end

  config = Redk::CapnPanda::Config.load
  if options[:write]
    config.set(key, value)
    config.write
  end

  puts config.to_hash
end
git_branch() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 227
def git_branch
  @git_branch ||= `git rev-parse --abbrev-ref HEAD`
end
has_change_id(message_file) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 211
def has_change_id(message_file)
  File.read(message_file).match('Change-Id: I')
end
list() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 81
def list
  issues_by_status = {}
  return puts Redk::CapnPanda.jira.my_issues.each {|d| puts d.to_json} if options[:raw]
  my_changes = Redk::CapnPanda.gerrit.my_changes

  Redk::CapnPanda.jira.my_issues.each_with_index do |issue, index|
    status = issue.status.statusCategory['name']
    issues_by_status[status] ||= []
    issues_by_status[status].push my_issues_index: index, issue: issue
  end

  status_colors = {
    :unknown => [:white, :light_white],
    "To Do" => [:yellow, :light_yellow],
    "In Progress" => [:blue, :light_blue]
  }

  issues_by_status.each do |status, issues|
    colors = status_colors[status] || status_colors[:unknown]
    puts "\n #{status}".colorize(colors[0]).bold
    issues.each_with_index do |issue, local_index|
      my_issues_index = issue[:my_issues_index] + 1
      issue = issue[:issue]
      color = colors[local_index % 2]

      change_field = issue.send(Redk::CapnPanda.config.jira_gerrit_field)
      change_id = change_field && change_field.split("\n")[0].chomp
      gerrit = my_changes.select {|ch| ch["change_id"] == change_id}.map do |change|

        status = change['labels'].sort.map do |key, label|
          m1 = label["rejected"] && !label["rejected"]["blocking"]
          m2 = label["rejected"] && label["rejected"]["blocking"]
          p1 = label["approved"]

          res = if m2
            "-2"
          elsif p1
            "+2"
          elsif m1
            "-1"
          else
            "--"
          end

          {
            key => res
          }
        end.reduce({}, :merge)

        res = [status.values.join("\t"),"<https://#{Redk::CapnPanda.config.gerrit_domain}/#/c/#{change['_number']}/>"]
        res << "<https://#{Redk::CapnPanda.config.firework_domain}/runs/gerrit/#{change['_number']}/>" if Redk::CapnPanda.config.firework_domain
        res.join("\t")
      end.join("\n")

      code_status = issue.has_branch? ? "⟁ " : ""

      key = "#{issue.key}".colorize(color).underline
      res = [%Q{  #{my_issues_index} #{code_status}#{key} #{issue.summary}}, %Q{    <https://#{Redk::CapnPanda.config.jira_domain}/browse/#{issue.key}>}]
      res << "\tCR\tNA\tPR\tQA\tV \n\t" + gerrit if gerrit != ""
      puts "#{res.join("\n")}\n".colorize(color)
    end
  end
rescue Redk::CapnPanda::KeyChainError
  puts "#{Redk::CapnPanda.config.jira_domain} doesnt exist in your keychain".colorize(:red)
end
message_template() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 243
def message_template
  @message_template ||= (
    issue = Redk::CapnPanda.jira.Issue.find(ticket_number)
    message_template = []

    message_template << issue.summary if commit_type == 'issue'
    message_template << ''
    message_template << WordWrap.ww(issue.description || '', 72)
    message_template << ''
    message_template << "Fixes #{ticket_number}"
    message_template << "# <https://instructure.atlassian.net/browse/#{ticket_number}>"
    message_template << ''
    message_template << 'Test Plan:'
    message_template << WordWrap.ww(issue.customfield_10500, 72) if issue.customfield_10500
    message_template << 'You do have a test plan, right?'
    message_template
  )
end
parsed_branch() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 231
def parsed_branch
  @parsed_branch ||=  git_branch.match(/(?<type>[^\/]+)\/?(?<ticket>[A-Z]+-[0-9]+)/)
end
prepare_commit_msg(message_file) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 183
def prepare_commit_msg(message_file)
  # we dont want to modify messages that have already been generated
  return if has_change_id message_file
  return unless parsed_branch
  write_commit_message message_file
end
prune() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 148
def prune
  issue_keys = Redk::CapnPanda.jira.my_issues.map do |issue|
    issue.key
  end

  issue_branches = Redk::CapnPanda.git.branches.local.map do |branch|
    branch_name = nil
    parsed_branch = branch.name.match(/(?<type>[^\/]+)\/?(?<ticket>[A-Z]+-[0-9]+)/)
    if parsed_branch
      ticket_number = parsed_branch[:ticket]
      branch_name = branch.name unless issue_keys.include? ticket_number
    end
    branch_name
  end.compact

  issue_branches.each do |branch|
      puts branch
  end

  if issue_branches.empty?
    puts "Nothing to prune".colorize(:yellow)
  elsif yes?("Continue? The above branches will be removed")
    Redk::CapnPanda.git.branch('master').checkout
    issue_branches.each do |branch|
        puts branch
        Redk::CapnPanda.git.branch(branch).delete
    end
  end
rescue Redk::CapnPanda::KeyChainError
  puts "#{Redk::CapnPanda.config.jira_domain} doesnt exist in your keychain".colorize(:red)
rescue Interrupt
  puts "\nInterrupt detected... terminating.".colorize(:yellow)
end
setup() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 191
def setup()
  hook_file = '.git/hooks/prepare-commit-msg'
  if File.exist?(hook_file)
    puts "It looks like you already have a 'prepare-commit-msg' git hook."
    puts "You'll have to manually invoke capn_panda from your existing hook"
    puts "\t'capn_panda prepare_commit_msg $1'"
  else
    File.open(hook_file, 'w') do |file|
      file << '#!/bin/bash'
      file << 'capn_panda prepare_commit_msg $1'
    end
  end
end
start(ticket_match=nil) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 53
def start(ticket_match=nil)
  return Redk::CapnPanda.git.branch("master").checkout if ticket_match == "-"
  issue = Redk::CapnPanda.jira.my_issues.find {|d| d.key.include?(ticket_match)} if ticket_match

  unless issue
    list
    ticket_item = ask ("Select a ticket")  { |q| q.echo = true }
    ticket_index = ticket_item.to_i - 1
    issue = Redk::CapnPanda.jira.my_issues[ticket_index]
  end

  issue.checkout

  if issue.status.statusCategory['name'] != "In Progress"
    if yes?("Would you like to move this issue to 'In Progress'? ")
      issue.move_to_in_progress
    end
  end

rescue Redk::CapnPanda::KeyChainError
  puts "#{Redk::CapnPanda.config.jira_domain} doesnt exist in your keychain".colorize(:red)
rescue Interrupt
  puts "\nInterrupt detected... terminating.".colorize(:yellow)
end
ticket_number() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 239
def ticket_number
  @ticket_number ||= parsed_branch[:ticket]
end
version() click to toggle source
# File lib/redk/capn_panda/cli.rb, line 206
def version()
  puts Redk::CapnPanda::VERSION
end
write_commit_message(message_file) click to toggle source
# File lib/redk/capn_panda/cli.rb, line 215
def write_commit_message(message_file)
  original_file_contents = File.read(message_file)
  temp_message_file = "#{(message_file)}.tmp"
  File.open(temp_message_file, 'w') do |file|
    file << message_template.join("\n")
    file << "\n"
    file << original_file_contents
  end

  File.rename(temp_message_file, message_file)
end