Class: GithubIssuesCli::Command

Inherits:
Clamp::Command
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/github_issues_cli/command.rb

Direct Known Subclasses

Browse, Checkout, Comment, List, Open, Pull_request, Push, Show

Defined Under Namespace

Classes: Browse, Checkout, Comment, List, Open, Pull_request, Push, Show

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(invocation_path, context = {}, parent_attribute_values = {}) ⇒ Command

Returns a new instance of Command.



8
9
10
11
# File 'lib/github_issues_cli/command.rb', line 8

def initialize(invocation_path, context = {}, parent_attribute_values = {})
  super
  authenticate
end

Instance Attribute Details

#git_repoObject

Returns the value of attribute git_repo.



6
7
8
# File 'lib/github_issues_cli/command.rb', line 6

def git_repo
  @git_repo
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/github_issues_cli/command.rb', line 6

def username
  @username
end

Instance Method Details

#authenticateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github_issues_cli/command.rb', line 13

def authenticate
  config_dirname = ENV['HOME'] + '/.github-issues/'
  Dir.mkdir config_dirname unless Dir.exists? config_dirname

  config_path = config_dirname + 'config'
  if File.exists? config_path
    file = File.new(config_path, 'r')
    config = JSON.parse file.gets
    file.close
    @username, token = config.values_at 'username', 'token'
  else
    print 'Please provide GitHub token: '
    token = $stdin.gets.chomp
    @username = Github::Users.new.get(:oauth_token => token).
    config = {:username => @username, :token => token}
    file = File.new(config_path, 'w')
    file.puts config.to_json
    file.close
  end

  Github.configure do |c|
    c.oauth_token = token
  end
end

#get_git_repoGit::Base

Returns:

  • (Git::Base)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/github_issues_cli/command.rb', line 39

def get_git_repo
  unless @git_repo
    dir = Dir.getwd + '/'
    until Dir.exists? dir + '.git' do
      if dir == '/'
        raise StandardError, 'Git not found'
      end
      dir = File.dirname(dir)
    end
    @git_repo = Git.open dir
  end
  @git_repo
end

#get_github_repoObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/github_issues_cli/command.rb', line 60

def get_github_repo
  url = get_git_repo.remote(:upstream).url
  if url.nil?
    raise 'No `upstream` remote found, please configure it first'
  end
  unless url.start_with?('[email protected]:', 'https://github.com/')
    raise 'Remote upstream points to non-github url: ' + url
  end
  if url.match(/github.com[:\/]([^\/]+)\/([^\/]+)\.git$/).nil?
    raise 'Can\'t extract `user/repo` data from upstream remote'
  end
  {:user => $1, :name => $2}
end

#get_issue_numberObject



53
54
55
56
57
58
# File 'lib/github_issues_cli/command.rb', line 53

def get_issue_number
  if get_git_repo.current_branch.match(/issue-([0-9]+)/).nil?
    raise 'Is not branch issue. Issue branches match `issue-XXX` pattern'
  end
  $1
end

#get_source(issue_number) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/github_issues_cli/command.rb', line 74

def get_source issue_number
  github_repo = get_github_repo
  pull_request = Github::PullRequests.new.get :user => github_repo[:user], :repo => github_repo[:name], :number => issue_number rescue return nil
  username = pull_request.head.repo.owner.
  url = pull_request.head.repo.ssh_url
  branch = pull_request.head.ref
  remote_name = username == @username ? 'origin' : username
  repo = get_git_repo
  remote = repo.remote remote_name
  if remote.url.nil?
    print 'Setting up remote `' + remote_name + '`...'
    remote = repo.add_remote remote_name, url
    puts ' Done'
  end
  if remote.url != url
    raise '`' + remote_name + '` remote\'s url differs from expected: `' + remote.url + ' != ' + url + '`'
  end
  remote.fetch
  remote.name + '/' + branch
end

#run(arguments) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/github_issues_cli/command.rb', line 95

def run(arguments)
  begin
    super
  rescue Exception => e
    print on_red ' '
    print bold ' Error: '
    if e.message.empty?
      puts 'Unknown error/Interrupt'
    else
      puts e.message
    end
    exit 1
  end
end