github_issues_cli 0.2.12 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5d5f699c1ca85db7643383177fe178c4678dc83
4
- data.tar.gz: 798ba7355e7832d27ef9e3a612aeacea9634cd4e
3
+ metadata.gz: 1eae2b08e31f053bc57a8509bb0f6d49328b46b1
4
+ data.tar.gz: ff998a2b853797aee5cf818a1cacde1ea056834d
5
5
  SHA512:
6
- metadata.gz: ed8404d6c7d5ef80b9e8ebd6617257f7bbe3b03d5ccb969476c2b8b1859c1a61a84a288c557fc5c2b6c8f7807f556567c048625787d3824db273bb1f3db172db
7
- data.tar.gz: 8d4c928d7197a1debf4fa32fc50889f07b5d9b671166db62daabe9995ebfd3550b8d1791cdc5fbd81f1933ea6a8b2eb42d5f14e8c7a4883fab0d2c5e6300f40a
6
+ metadata.gz: e18c131f6fff20450b9fa2c99c2ded9abae1878c9e1dcc5eb25028796ccfaac6bd2cf2789d0a7cfdd4937f2d5582aad1b362e5ec7297612daef232e7cfc7ae59
7
+ data.tar.gz: 5f75db94ababc441b5fe38b350738bec5bd5a2f3cead6ffa62c8cb8c3b1fc033cd296df26a79e15deb10f3221c932ed8e42d053407aaaa966f4c674e02643339
@@ -5,7 +5,9 @@ module GithubIssuesCli
5
5
  require 'github_api'
6
6
  require 'io/console'
7
7
  require 'json'
8
+ require 'pathname'
8
9
  require 'github_issues_cli/command'
10
+ require 'github_issues_cli/command/clone'
9
11
  require 'github_issues_cli/command/list'
10
12
  require 'github_issues_cli/command/checkout'
11
13
  require 'github_issues_cli/command/browse'
@@ -15,4 +17,5 @@ module GithubIssuesCli
15
17
  require 'github_issues_cli/command/push'
16
18
  require 'github_issues_cli/command/pull_request'
17
19
  require 'github_issues_cli/command_manager'
18
- end
20
+ require 'github_issues_cli/extensions/git'
21
+ end
@@ -35,6 +35,14 @@ module GithubIssuesCli
35
35
  end
36
36
  end
37
37
 
38
+ def get_git_push_target
39
+ git_repo = get_git_repo
40
+ branch_name = git_repo.current_branch
41
+ remote_name = git_repo.lib.command_proxy('config', ['--get', "branch.#{branch_name}.remote"]) || 'origin'
42
+ remote_ref = git_repo.lib.command_proxy('config', ['--get', "branch.#{branch_name}.merge"]) || branch_name
43
+ remote_name + '/' + remote_ref
44
+ end
45
+
38
46
  # @return [Git::Base]
39
47
  def get_git_repo
40
48
  unless @git_repo
@@ -57,7 +65,7 @@ module GithubIssuesCli
57
65
  $1
58
66
  end
59
67
 
60
- def get_github_repo
68
+ def get_upstream_repo
61
69
  url = get_git_repo.remote(:upstream).url
62
70
  if url.nil?
63
71
  raise 'No `upstream` remote found, please configure it first'
@@ -71,25 +79,35 @@ module GithubIssuesCli
71
79
  {:user => $1, :name => $2}
72
80
  end
73
81
 
74
- def get_source issue_number
75
- github_repo = get_github_repo
76
- pull_request = Github::PullRequests.new.get :user => github_repo[:user], :repo => github_repo[:name], :number => issue_number rescue return nil
77
- username = pull_request.head.repo.owner.login
78
- url = pull_request.head.repo.ssh_url
79
- branch = pull_request.head.ref
82
+ def get_pullrequest(issue_number)
83
+ upstream_repo = get_upstream_repo
84
+ request = {
85
+ :user => upstream_repo[:user],
86
+ :repo => upstream_repo[:name],
87
+ :number => issue_number,
88
+ }
89
+ Github::PullRequests.new.get(request) rescue nil
90
+ end
91
+
92
+ def get_source_branch(issue_number)
93
+ git_repo = get_git_repo
94
+ pullrequest = get_pullrequest(issue_number)
95
+ return nil if pullrequest.nil?
96
+
97
+ username = pullrequest.head.repo.owner.login
80
98
  remote_name = username == @username ? 'origin' : username
81
- repo = get_git_repo
82
- remote = repo.remote remote_name
83
- if remote.url.nil?
99
+ remote_url = pullrequest.head.repo.ssh_url
100
+ branch_name = pullrequest.head.ref
101
+
102
+ source_remote = git_repo.remote(remote_name)
103
+ if source_remote.url.nil?
84
104
  print 'Setting up remote `' + remote_name + '`...'
85
- remote = repo.add_remote remote_name, url
105
+ source_remote = git_repo.add_remote(remote_name, remote_url)
86
106
  puts ' Done'
87
107
  end
88
- if remote.url != url
89
- raise '`' + remote_name + '` remote\'s url differs from expected: `' + remote.url + ' != ' + url + '`'
90
- end
91
- remote.fetch
92
- remote.name + '/' + branch
108
+
109
+ source_remote.fetch
110
+ source_remote.name + '/' + branch_name
93
111
  end
94
112
 
95
113
  def run(arguments)
@@ -107,4 +125,4 @@ module GithubIssuesCli
107
125
  end
108
126
  end
109
127
  end
110
- end
128
+ end
@@ -2,7 +2,7 @@ module GithubIssuesCli
2
2
  class Command::Browse < Command
3
3
 
4
4
  def execute
5
- github_repo = get_github_repo
5
+ github_repo = get_upstream_repo
6
6
  url = 'https://github.com/' + github_repo[:user] + '/' + github_repo[:name] + '/issues/' + get_issue_number
7
7
  system('open ' + url)
8
8
  end
@@ -6,24 +6,35 @@ module GithubIssuesCli
6
6
  def execute
7
7
  branch_name = 'issue-' + issue_number
8
8
  repo = get_git_repo
9
- source = nil
10
- if repo.lib.branches_all.map(&:first).include? branch_name
9
+
10
+ is_local_branch = repo.lib.branches_all.map(&:first).include? branch_name
11
+ if is_local_branch
11
12
  repo.checkout branch_name
12
13
  else
13
- source = get_source issue_number
14
+ source = get_source_branch(issue_number)
15
+ target = source
14
16
  if source.nil?
15
- github_repo = get_github_repo
17
+ github_repo = get_upstream_repo
16
18
  request = {:user => github_repo[:user], :repo => github_repo[:name], :number => issue_number}
17
- Github::Issues.new.get request rescue raise 'Can\'t find issue #' + issue_number
19
+ Github::Issues.new.get(request) rescue raise "Can't find issue ##{issue_number}"
18
20
  repo.remote('upstream').fetch
19
21
  source = 'upstream/master'
22
+ target = 'origin/' + branch_name
20
23
  end
21
- repo.lib.checkout source, :new_branch => branch_name
24
+ repo.lib.command_proxy('checkout', ['-b', branch_name, source])
22
25
  end
23
26
  print on_green ' '
24
- print ' Checked out #' + issue_number
25
- print ' (' + source.split('/').first + ')' if source
27
+ print " Checked out ##{issue_number} "
28
+ print "(#{source})" unless is_local_branch
26
29
  puts
30
+
31
+ unless is_local_branch
32
+ print on_green ' '
33
+ puts " Setting upstream to (#{target})"
34
+ remote_name, branch_name = target.split('/')
35
+ repo.lib.command_proxy('config', ["branch.#{branch_name}.remote", remote_name])
36
+ repo.lib.command_proxy('config', ["branch.#{branch_name}.merge", "refs/heads/#{branch_name}"])
37
+ end
27
38
  end
28
39
  end
29
40
  end
@@ -0,0 +1,32 @@
1
+ module GithubIssuesCli
2
+ class Command::Clone < Command
3
+
4
+ parameter 'repository', 'name of the Github repository in owner/repo format', :attribute_name => :repository
5
+ parameter '[target]', 'target location for clone', :attribute_name => :target
6
+
7
+ def execute
8
+ owner, name = repository.split('/')
9
+ upstream_repo = Github::Repos.new.get(:user => owner, :repo => name)
10
+ if upstream_repo.owner.login == @username
11
+ origin_repo = upstream_repo
12
+ else
13
+ forks = Github::Repos::Forks.new.list(:user => owner, :repo => name)
14
+ fork = forks.find do |fork|
15
+ fork.owner.login == @username
16
+ end
17
+
18
+ unless fork
19
+ puts "Forking #{repository} for #{@username}"
20
+ fork = Github::Repos::Forks.new.create(:user => owner, :repo => name)
21
+ end
22
+ origin_repo = fork
23
+ end
24
+
25
+ target_directory = target || origin_repo.name
26
+ target_path = Pathname.new(target_directory).expand_path(Dir.getwd)
27
+ puts "Cloning #{repository} into #{target_path.to_s}"
28
+ git_repo = Git.clone(origin_repo.ssh_url, target_path.basename.to_s, :path => target_path.dirname.to_s)
29
+ git_repo.add_remote 'upstream', upstream_repo.ssh_url
30
+ end
31
+ end
32
+ end
@@ -4,7 +4,7 @@ module GithubIssuesCli
4
4
  parameter 'body', 'comment body', :attribute_name => :body
5
5
 
6
6
  def execute
7
- github_repo = get_github_repo
7
+ github_repo = get_upstream_repo
8
8
  issue_number = get_issue_number
9
9
  Github::Issues.new.comments.create :user => github_repo[:user], :repo => github_repo[:name], :issue_id => issue_number, :body => body
10
10
  print on_green ' '
@@ -4,7 +4,7 @@ module GithubIssuesCli
4
4
  option '--mine', :flag, 'show only mine issues'
5
5
 
6
6
  def execute
7
- github_repo = get_github_repo
7
+ github_repo = get_upstream_repo
8
8
  issues_client = Github::Issues.new
9
9
  request = {:user => github_repo[:user], :repo => github_repo[:name]}
10
10
  request.store(:assignee, @username) if mine?
@@ -12,11 +12,11 @@ module GithubIssuesCli
12
12
 
13
13
  issues.each do |issue|
14
14
  if not issue.assignee.nil? and issue.assignee.login == @username
15
- print on_yellow ' '
15
+ print yellow ''
16
16
  else
17
17
  print ' '
18
18
  end
19
- print bold ' ' + ('#' + issue.number.to_s).rjust(6)
19
+ print bold(issue.number.to_s.rjust(5) + ':')
20
20
  print ' ' + issue.title
21
21
  puts
22
22
  end
@@ -4,14 +4,20 @@ module GithubIssuesCli
4
4
  parameter 'summary', 'issue summary', :attribute_name => :summary
5
5
 
6
6
  def execute
7
- github_repo = get_github_repo
7
+ github_repo = get_upstream_repo
8
8
  issue = Github::Issues.new.create :user => github_repo[:user], :repo => github_repo[:name], :title => summary
9
9
  issue_number = issue[:number].to_s
10
10
 
11
- get_github_repo
11
+ get_upstream_repo
12
12
  git_repo = get_git_repo
13
13
  git_repo.remote('upstream').fetch
14
- git_repo.lib.checkout 'upstream/master', :new_branch => 'issue-' + issue_number
14
+ remote_name = 'origin'
15
+ branch_name = 'issue-' + issue_number
16
+
17
+ git_repo.lib.command_proxy('checkout', ['-b', branch_name, 'upstream/master'])
18
+ git_repo.lib.command_proxy('config', ["branch.#{branch_name}.remote", remote_name])
19
+ git_repo.lib.command_proxy('config', ["branch.#{branch_name}.merge", "refs/heads/#{branch_name}"])
20
+
15
21
  print on_green ' '
16
22
  print ' Checked out '
17
23
  puts bold '#' + issue_number
@@ -1,23 +1,30 @@
1
1
  module GithubIssuesCli
2
2
  class Command::Pull_request < Command
3
3
 
4
+ parameter '[base]', 'base for pull-request', :attribute_name => :base, :default => 'master'
5
+
4
6
  def execute
5
- github_repo = get_github_repo
6
7
  issue_number = get_issue_number
7
- source = @username + ':issue-' + issue_number
8
+ raise "Pull-request for issue ##{issue_number} already exists" if get_pullrequest(issue_number)
9
+
10
+ github_repo = get_upstream_repo
11
+ git_repo = get_git_repo
12
+ target = get_git_push_target
13
+ remote, ref = target.split('/', 2)
14
+ puts git_repo.lib.command_proxy('push', [remote, "#{git_repo.current_branch}:#{ref}"])
15
+
16
+ raise 'Cannot create pull-request for non-origin remotes' unless remote == 'origin'
17
+ source = @username + ':' + ref.split('/').last
8
18
  begin
9
19
  request = {
10
- :user => github_repo[:user],
11
- :repo => github_repo[:name],
12
- :head => source,
13
- :base => 'master',
14
- :issue => issue_number
20
+ :user => github_repo[:user],
21
+ :repo => github_repo[:name],
22
+ :base => base,
23
+ :head => source,
24
+ :issue => issue_number
15
25
  }
16
- Github::PullRequests.new.create request
26
+ Github::PullRequests.new.create(request)
17
27
  rescue Exception => e
18
- unless get_source(issue_number).nil?
19
- raise 'Pull-request for issue #' + issue_number + ' already exists'
20
- end
21
28
  raise "Internal error: Cannot create pull-request.\n#{e.inspect}"
22
29
  end
23
30
  print 'Pull request for issue '
@@ -2,15 +2,10 @@ module GithubIssuesCli
2
2
  class Command::Push < Command
3
3
 
4
4
  def execute
5
- issue_number = get_issue_number
6
- source = get_source issue_number
7
- if source.nil?
8
- source = 'origin/issue-' + issue_number
9
- end
10
- print 'Pushing code to '
11
- puts bold source
12
- remote, branch = source.split('/')
13
- get_git_repo.push(remote, branch)
5
+ git_repo = get_git_repo
6
+ target = get_git_push_target
7
+ remote, ref = target.split('/', 2)
8
+ puts git_repo.lib.command_proxy('push', [remote, "#{git_repo.current_branch}:#{ref}"])
14
9
  end
15
10
  end
16
11
  end
@@ -2,7 +2,7 @@ module GithubIssuesCli
2
2
  class Command::Show < Command
3
3
 
4
4
  def execute
5
- github_repo = get_github_repo
5
+ github_repo = get_upstream_repo
6
6
  issue_number = get_issue_number
7
7
  issues_client = Github::Issues.new
8
8
  issue = issues_client.get :user => github_repo[:user], :repo => github_repo[:name], :number => issue_number
@@ -1,6 +1,7 @@
1
1
  module GithubIssuesCli
2
2
  class CommandManager < Clamp::Command
3
3
 
4
+ subcommand 'clone', 'Clones repository', Command::Clone
4
5
  subcommand 'list', 'Lists issues', Command::List
5
6
  subcommand 'checkout', 'Checkouts specific issue', Command::Checkout
6
7
  subcommand 'show', 'Show current issue details', Command::Show
@@ -0,0 +1,7 @@
1
+ module Git
2
+ class Lib
3
+ def command_proxy(cmd, opts = [], chdir = true, redirect = '', &block)
4
+ command(cmd, opts, chdir, redirect, &block)
5
+ end
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github_issues_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Durka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: git
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 1.2.0
33
+ version: 1.2.8
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 1.2.0
40
+ version: 1.2.8
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: github_api
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +78,7 @@ files:
78
78
  - lib/github_issues_cli/command.rb
79
79
  - lib/github_issues_cli/command/browse.rb
80
80
  - lib/github_issues_cli/command/checkout.rb
81
+ - lib/github_issues_cli/command/clone.rb
81
82
  - lib/github_issues_cli/command/comment.rb
82
83
  - lib/github_issues_cli/command/list.rb
83
84
  - lib/github_issues_cli/command/open.rb
@@ -85,6 +86,7 @@ files:
85
86
  - lib/github_issues_cli/command/push.rb
86
87
  - lib/github_issues_cli/command/show.rb
87
88
  - lib/github_issues_cli/command_manager.rb
89
+ - lib/github_issues_cli/extensions/git.rb
88
90
  homepage: https://github.com/tomaszdurka/github_issues_cli.git
89
91
  licenses:
90
92
  - MIT
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  requirements: []
107
109
  rubyforge_project:
108
- rubygems_version: 2.2.2
110
+ rubygems_version: 2.4.6
109
111
  signing_key:
110
112
  specification_version: 4
111
113
  summary: Command line tool for managing issues, pull-requests on GitHub platform