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, Clone, Comment, List, Open, Pull_request, Push, Show

Defined Under Namespace

Classes: Browse, Checkout, Clone, 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_push_targetObject



38
39
40
41
42
43
44
# File 'lib/github_issues_cli/command.rb', line 38

def get_git_push_target
  git_repo = get_git_repo
  branch_name = git_repo.current_branch
  remote_name = git_repo.lib.command_proxy('config', ['--get', "branch.#{branch_name}.remote"]) || 'origin'
  remote_ref = git_repo.lib.command_proxy('config', ['--get', "branch.#{branch_name}.merge"]) || branch_name
  remote_name + '/' + remote_ref
end

#get_git_repoGit::Base

Returns:

  • (Git::Base)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/github_issues_cli/command.rb', line 47

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_issue_numberObject



61
62
63
64
65
66
# File 'lib/github_issues_cli/command.rb', line 61

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_pullrequest(issue_number) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/github_issues_cli/command.rb', line 82

def get_pullrequest(issue_number)
  upstream_repo = get_upstream_repo
  request = {
    :user => upstream_repo[:user],
    :repo => upstream_repo[:name],
    :number => issue_number,
  }
  Github::PullRequests.new.get(request) rescue nil
end

#get_source_branch(issue_number) ⇒ Object



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

def get_source_branch(issue_number)
  git_repo = get_git_repo
  pullrequest = get_pullrequest(issue_number)
  return nil if pullrequest.nil?

  username = pullrequest.head.repo.owner.
  remote_name = username == @username ? 'origin' : username
  remote_url = pullrequest.head.repo.ssh_url
  branch_name = pullrequest.head.ref

  source_remote = git_repo.remote(remote_name)
  if source_remote.url.nil?
    print 'Setting up remote `' + remote_name + '`...'
    source_remote = git_repo.add_remote(remote_name, remote_url)
    puts ' Done'
  end

  source_remote.fetch
  source_remote.name + '/' + branch_name
end

#get_upstream_repoObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/github_issues_cli/command.rb', line 68

def get_upstream_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

#run(arguments) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/github_issues_cli/command.rb', line 113

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