Class: GitReview

Inherits:
Object
  • Object
show all
Defined in:
lib/git-review.rb

Constant Summary collapse

REVIEW_CACHE_FILE =
'.git/review_cache.json'

Instance Method Summary collapse

Instance Method Details

#browseObject

Open a browser window and review a specified request.



75
76
77
# File 'lib/git-review.rb', line 75

def browse
  Launchy.open(@review['html_url']) if review_exists?
end

#consoleObject

Start a console session (used for debugging).



133
134
135
136
137
138
139
# File 'lib/git-review.rb', line 133

def console
  puts 'Entering debug console.'
  require 'ruby-debug'
  Debugger.start
  debugger
  puts 'Leaving debug console.'
end

#createObject

Create a new request.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/git-review.rb', line 80

def create
  # Gather information.
  last_review_id = get_pull_info.collect{|review| review['number']}.sort.last
  target_repo = "#{@user}/#{@repo}"
  target_branch = 'master'
  source_branch = git('branch', false).match(/\*(.*)/)[0][2..-1]
  title = "Review request: #{} wants to merge changes from '#{source_branch}' into #{target_repo}'s branch '#{target_branch}'."
  # TODO: Insert commit messages (that are not yet in master) into body (since this will be displayed inside the mail that is sent out).
  body = 'my body'
  # Create the actual pull request.
  Octokit.create_pull_request(target_repo, target_branch, source_branch, title, body)
  # Switch back to target_branch and check for success.
  git "co #{target_branch}"
  update
  potential_new_review = get_pull_info.find{ |review| review['title'] == title}
  if potential_new_review['number'] > last_review_id
    puts 'Review request successfully created.'
  else
    puts 'Creating new review request failed.'
  end
end

#helpObject

Default command to show a quick reference of available commands.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/git-review.rb', line 12

def help
  puts 'Usage: git review <command>'
  puts 'Manage review workflow for projects hosted on GitHub (using pull requests).'
  puts ''
  puts 'Available commands:'
  puts '   list [--reverse]          List all open requests.'
  puts '   show <number> [--full]    Show details of a single request.'
  puts '   browse <number>           Open a browser window and review a specified request.'
  puts '   create                    Create a new request.'
  puts '   merge <number>            Sign off a specified request by merging it into master.'
end

#listObject

List all open requests.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/git-review.rb', line 25

def list
  puts "Open Pull Requests for #{@user}/#{@repo}"
  option = @args.shift
  open_requests = get_pull_info
  open_requests.reverse! if option == '--reverse'
  count = 0
  open_requests.each do |pull|
    line = []
    line << format_text(pull['number'], 4)
    line << format_text(Date.parse(pull['created_at']).strftime("%m/%d"), 5)
    line << format_text(pull['comments'], 2)
    line << format_text(pull['title'], 35)
    line << format_text(pull['head']['label'], 20)
    sha = pull['head']['sha']
    if not_merged?(sha)
      puts line.join ' '
      count += 1
    end
  end
  if count == 0
    puts ' -- no open pull requests --'
  end
end

#mergeObject

Sign off a specified request by merging it into master.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/git-review.rb', line 103

def merge
  return unless review_exists?
  option = @args.shift
  if @review['head']['repository']
    o = @review['head']['repository']['owner']
    r = @review['head']['repository']['name']
  else # they deleted the source repo
    o = @review['head']['user']['login']
    purl = @review['patch_url']
    puts "Sorry, #{o} deleted the source repository, git-review doesn't support this."
    puts "You can manually patch your repo by running:"
    puts
    puts "  curl #{purl} | git am"
    puts
    puts "Tell the contributor not to do this."
    return false
  end
  s = @review['head']['sha']
  message = "Merge pull request ##{@review['number']} from #{o}/#{r}\n\n---\n\n"
  message += @review['body'].gsub("'", '')
  if option == '--log'
    message += "\n\n---\n\nMerge Log:\n"
    puts cmd = "git merge --no-ff --log -m '#{message}' #{s}"
  else
    puts cmd = "git merge --no-ff -m '#{message}' #{s}"
  end
  exec(cmd)
end

#showObject

Show details of a single request.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git-review.rb', line 50

def show
  return unless review_exists?
  option = @args.shift
  puts "Number   : #{@review['number']}"
  puts "Label    : #{@review['head']['label']}"
  puts "Created  : #{@review['created_at']}"
  puts "Votes    : #{@review['votes']}"
  puts "Comments : #{@review['comments']}"
  puts
  puts "Title    : #{@review['title']}"
  puts "Body     :"
  puts
  puts @review['body']
  puts
  puts '------------'
  puts
  if option == '--full'
    exec "git diff --color=always HEAD...#{@review['head']['sha']}"
  else
    puts "cmd: git diff HEAD...#{@review['head']['sha']}"
    puts git("diff --stat --color=always HEAD...#{@review['head']['sha']}")
  end
end