class Gub::CLI

Public Instance Methods

add_upstream() click to toggle source
# File lib/gub/cli.rb, line 184
def add_upstream
  repository = Gub::Repository.new(origin_name)
  repository.add_upstream
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
browse() click to toggle source
# File lib/gub/cli.rb, line 38
def browse
  repository = Gub::Repository.new(origin_name)
  repository.browse
end
clone(repo) click to toggle source
# File lib/gub/cli.rb, line 166
def clone repo
  if options.https
    url = "https://github.com/#{repo}"
  else
    url = "git@github.com:#{repo}"
  end
  Gub.log.info "Cloning from #{url}..."
  Gub.git.clone(url)
  `cd #{repo.split('/').last}`
  repository = Gub::Repository.new(origin_name)
  repository.add_upstream
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
finish(id = nil) click to toggle source
# File lib/gub/cli.rb, line 145
def finish id = nil
  id ||= `git rev-parse --abbrev-ref HEAD`.split('-').last.to_s.chop
  if id.nil?
    panic "Unable to guess issue ID from branch name. You might want to specify it explicitly."
  else
    repository = Repository.new(origin_name)
    issue = repository.issue(id)
    say 'Pushing branch...'
    Gub.git.push('origin', issue.branch)
    say "Creating pull-request for issue ##{id}..."
    issue.request_pull
    Gub.git.checkout('master')
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
info() click to toggle source
# File lib/gub/cli.rb, line 204
def info
  repo = Gub::Repository.new(origin_name)
  say "Github repository: #{repo.full_name}"
  say "Forked from: #{repo.parent}" if repo.parent
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
issue(id) click to toggle source
# File lib/gub/cli.rb, line 48
def issue(id)
  repository = Gub::Repository.new(origin_name)
  if options.comment
    repository.issue(id).comment(options.comment)
  elsif options.close
    repository.issue(id).close
  elsif options.assign
    repository.issue(id).assign
  elsif options.reopen
    repository.issue(id).reopen
  else
    issue = repository.issue(id)
    rows = []
    rows << ['Status:', issue.state]
    rows << ['Milestone:', issue.milestone.title]
    rows << ['Author:', issue.user.login]
    rows << ['Assignee:', (issue.assignee.nil? ? '-' : issue.assignee.login)]
    rows << ['Description:', word_wrap(issue.body, line_width: 70)]
    comments = []
    issue.comments.each do |comment|
      comments << [comment.user.login, "On #{comment.updated_at}: #{comment.body}"]
    end
    Gub.log.info "Hint: use 'gub start #{id}' to start working on this issue."
    puts table rows, ["Issue ##{id}:", issue.title]
    puts table comments, ['', 'Comments']
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
issues() click to toggle source
# File lib/gub/cli.rb, line 83
def issues
  args = {}
  repository = Gub::Repository.new(origin_name)
  if options.all || repository.full_name.nil?
    say "Listing all issues:"
    issues = Gub.github.user_issues
  else
    if options.mine
      args[:assignee] = Gub.github.user.login
    end
    if repository.has_issues?
      say "Listing issues for #{repository.full_name}:"
    else
      say "Issues disabled #{repository.full_name}.", :yellow
      say "Listing issues for #{repository.parent}:"
    end
    issues = repository.issues(args)
  end
  unless issues.nil?
    rows = []
    issues.each do |issue|
      row = []
      row << issue.number
      row << issue.title
      row << issue.user.login
      row << (issue.assignee.nil? ? '' : issue.assignee.login)
      row << issue.state
      rows << row
    end
    say table rows, ['ID', 'Title', 'Author', 'Assignee', 'Status']
    say "Found #{issues.count} issue(s)."
    say 'Hint: use "gub start" to start working on an issue.', :green
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
panic(message) click to toggle source
# File lib/gub/cli.rb, line 236
def panic message
  say message, :red
  exit 1
end
reauthorize() click to toggle source
# File lib/gub/cli.rb, line 232
def reauthorize
  say "Unable to find token. You might need to run 'gub setup'.", :red
end
repos() click to toggle source
# File lib/gub/cli.rb, line 14
def repos
  rows = []
  id = 0
  table = Terminal::Table.new(headings: ['#', 'Repository']) do |t|
    Gub.github.repos.each do |repo|
      id = id.next
      t.add_row [id, repo.full_name]
    end
    Gub.github.orgs.each do |org|
      t.add_separator
      Gub.github.organization_repositories(org.login).each do |repo|
        id = id.next
        t.add_row [id, repo.full_name]
      end
    end
  end
  puts table
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
setup() click to toggle source
# File lib/gub/cli.rb, line 215
def setup
  unless Gub.config.data && Gub.config.data.has_key?('token')
    hl = HighLine.new
    username = hl.ask 'Github username: '
    password = hl.ask('Github password (we will not store this): ') { |q| q.echo = "*" }
    gh = Gub::Github.new(login: username, password: password)
    token = gh.create_authorization(scopes: [:user, :repo, :gist], note: 'Gub').token
    Gub.config.add('token', token)
  end
end
start(id) click to toggle source
# File lib/gub/cli.rb, line 123
def start id
  if id.nil?
    panic 'Issue ID required.'
  else
    repository = Repository.new(origin_name)
    issue = repository.issue(id)
    if repository.branches.include?(issue.branch)
      Gub.git.checkout(issue.branch)
    else
      repository = Repository.new(origin_name)
      repository.sync
      issue.assign
      Gub.git.checkout('-b', issue.branch)
    end
  end
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
sync() click to toggle source
# File lib/gub/cli.rb, line 194
def sync
  Gub.log.info 'Synchroizing with upstream...'
  Gub::Repository.new(origin_name).sync
rescue Gub::Unauthorized
  reauthorize
rescue Gub::Disconnected
  panic 'Unable to connect to Github'
end
version() click to toggle source
# File lib/gub/cli.rb, line 227
def version
  say Gub::VERSION
end

Private Instance Methods

origin_name() click to toggle source
# File lib/gub/cli.rb, line 243
def origin_name
  `git remote -v | grep origin | grep fetch | awk '{print $2}' | cut -d ':' -f 2`.to_s
end
table(rows, header = []) click to toggle source
# File lib/gub/cli.rb, line 247
def table rows, header = []
  Terminal::Table.new headings: header, rows: rows
end
word_wrap(text, options = {}) click to toggle source

Source: github.com/rails/rails/actionpack/lib/action_view/helpers/text_helper.rb

# File lib/gub/cli.rb, line 252
def word_wrap(text, options = {})
  line_width = options.fetch(:line_width, 80)
  unless text.nil?
    text.split("\n").collect do |line|
      line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
    end * "\n"
  end
end