hull 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/hull ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # resolve bin path, ignoring symlinks
4
+ require "pathname"
5
+ bin_file = Pathname.new(__FILE__).realpath
6
+
7
+ # add self to libpath
8
+ $:.unshift File.expand_path("../../lib", bin_file)
9
+
10
+ # start up the CLI
11
+ require "hull"
12
+ Hull::Hull.new(*ARGV).run
data/lib/hull.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'net/http'
2
+ require 'open-uri'
3
+ require 'time'
4
+ require 'json'
5
+ require 'grit'
6
+ require 'hull/command'
7
+ require 'hull/hull'
8
+ require 'hull/list'
9
+
10
+ module Hull
11
+ end
@@ -0,0 +1,86 @@
1
+ module Hull
2
+ class Command
3
+
4
+ BASH_COLORS = {
5
+ :reset => "0",
6
+ :red => "31",
7
+ :green => "32",
8
+ :yellow => "33",
9
+ :purple => "34",
10
+ :pink => "35",
11
+ :cyan => "36",
12
+ :white => "37",
13
+ }
14
+
15
+ def write(text, color)
16
+ if BASH_COLORS.keys.include?(color)
17
+ "\033[#{BASH_COLORS[color]}m#{text}\033[#{BASH_COLORS[:reset]}m"
18
+ else
19
+ text
20
+ end
21
+ end
22
+
23
+ def error(text); puts "#{write('error', :red) }: #{text}"; end
24
+ def warn(text); puts "#{write('warning', :purple)}: #{text}"; end
25
+ def note(text); puts "#{write('note', :green) }: #{text}"; end
26
+
27
+ def column_size
28
+ [ `stty size`.scan(/\d+/).last.to_i, 80 ].max
29
+ end
30
+
31
+ def pad_left(str, length)
32
+ "% #{length}s" % str[0, length]
33
+ end
34
+
35
+ def pad_right(str, length)
36
+ if str.length > length
37
+ "#{str[0, length - 3]}..."
38
+ elsif str.length < length
39
+ "#{str}#{' ' * (length - str.length)}"
40
+ else
41
+ str
42
+ end
43
+ end
44
+
45
+ def username
46
+ @username ||= repo.config['github.user']
47
+ end
48
+
49
+ def api_token
50
+ @api_token ||= repo.config['github.token']
51
+ end
52
+
53
+ def github_url
54
+ @github_url ||=
55
+ begin
56
+ _, owner, project = repo.config['remote.origin.url'].match(/^git@github.com:([^\/]*)\/(.*)\.git/).to_a
57
+ "https://github.com/api/v2/json/pulls/#{owner}/#{project}"
58
+ end
59
+ end
60
+
61
+ def repo
62
+ @repo ||= Grit::Repo.new(".")
63
+ unless valid_repository?(@repo)
64
+ raise InvalidRepository.new('Origin must be a GitHub repository')
65
+ end
66
+ @repo
67
+ end
68
+
69
+ def get_json(url)
70
+ options = {
71
+ :http_basic_authentication => [
72
+ "#{username}/token",
73
+ api_token
74
+ ]
75
+ }
76
+ JSON.load(open(url, options))
77
+ end
78
+
79
+ def valid_repository?(repository)
80
+ repository.remote_list.include?('origin') &&
81
+ repository.config['remote.origin.url'].match(/^git@github.com:/)
82
+ end
83
+ end
84
+
85
+ class InvalidRepository < RuntimeError; end
86
+ end
data/lib/hull/hull.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Hull
2
+ class Hull
3
+ attr_accessor :command, :number
4
+
5
+ def initialize(*argv)
6
+ parse_args(argv.clone)
7
+ end
8
+
9
+ def run
10
+ case command
11
+ when 'list'
12
+ List.new
13
+ when 'show'
14
+ Show.new(number)
15
+ when 'pull', 'checkout', 'co'
16
+ Pull.new(number)
17
+ when 'rm', 'remove', 'delete'
18
+ Remove.new(number)
19
+ else # help
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def parse_args(args)
26
+ self.command = args.shift.strip rescue 'help'
27
+ case command
28
+ when /^list$/
29
+ when /^(show|checkout|co|pull|rm|remove|delete)$/
30
+ self.number = args.shift.strip rescue ''
31
+ self.command = 'help' unless valid_number?
32
+ else
33
+ self.command = 'help'
34
+ end
35
+ end
36
+
37
+ def valid_number?
38
+ number.to_s.match(/\d+/)
39
+ end
40
+
41
+ end
42
+ end
data/lib/hull/list.rb ADDED
@@ -0,0 +1,57 @@
1
+ module Hull
2
+ class List < Command
3
+ def initialize
4
+ json = get_json(github_url)
5
+ if json['error'].to_s != ''
6
+ return error("Pull request ##{request_number} might not exist.")
7
+ end
8
+
9
+ pulls = json['pulls']
10
+ puts "Number of pull requests: #{pulls.length}"
11
+ return if pulls.length == 0
12
+
13
+ branch_names = pulls.map { |pull| pull['head']['label'].split(/:/) }
14
+ repo_len = [ branch_names.map(&:first).map(&:length).max, 15 ].min
15
+ branch_len = [ branch_names.map(&:last).map(&:length).max, 30 ].min
16
+
17
+ pulls.each do |pull|
18
+ puts pull_info(pull)
19
+ end
20
+ end
21
+
22
+ def pull_info(pull)
23
+ number = pull['number'].to_s
24
+ mergeable = pull['mergeable']
25
+ title = pull['title']
26
+ created_at = Time.parse(pull['issue_created_at'])
27
+ repo_name, branch_name = pull['head']['label'].split(/:/)
28
+
29
+ if repo_name == username
30
+ color = :yellow
31
+ else
32
+ if mergeable
33
+ is_old = repo.branches.map(&:name).any? { |branch| branch =~ /^. *cr\/#{number}/}
34
+ color = is_old ? :purple : :white
35
+ else
36
+ color = :red
37
+ end
38
+ end
39
+
40
+ number = pad_left(number, 6)
41
+ title = pad_right(title, column_size - repo_len - branch_len - 10)
42
+ repo_name = pad_left(repo_name, repo_len)
43
+ branch_name = pad_right(branch_name, branch_len)
44
+
45
+ [
46
+ write(number, color),
47
+ [
48
+ write(repo_name, :yellow),
49
+ branch_name.split(/\//).map do |branch|
50
+ write(branch, :green)
51
+ end.join('/'),
52
+ ].join('/'),
53
+ title,
54
+ ].join(' ')
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hull
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hc5duke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70167167752320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70167167752320
25
+ description: Code Review helper using GitHub's Pull Requests
26
+ email: hc5duke@gmail.com
27
+ executables:
28
+ - hull
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/hull/command.rb
33
+ - lib/hull/hull.rb
34
+ - lib/hull/list.rb
35
+ - lib/hull.rb
36
+ - bin/hull
37
+ homepage: https://github.com/hc5duke/hull
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.10
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Code Review helper using GitHub's Pull Requests
61
+ test_files: []