Class: Fuel::CLI::Gerrit

Inherits:
Base
  • Object
show all
Includes:
GerritCommon
Defined in:
lib/fuel/cli/gerrit.rb

Instance Method Summary collapse

Methods included from GerritCommon

#changes_endpoint, #fetch_change_id_or_fail, #gerrit_ref, #gerrit_url

Instance Method Details

#abandonObject



88
89
90
91
92
# File 'lib/fuel/cli/gerrit.rb', line 88

def abandon
  change_id = fetch_change_id_or_fail
  params = options['message'] ? { body: { message: options['message'] }.to_json } : {}
  result = self.class.post(changes_endpoint(change_id, 'abandon'), params).parsed_response
end

#alias(email, alias_name = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fuel/cli/gerrit.rb', line 61

def alias(email, alias_name = nil)
  if alias_name
    Config.set('gerrit', 'aliases', alias_name, email)
  else
    aliases = Config.get('gerrit', 'aliases') || {}
    aliases_found = aliases.map { |k, v| v == email ? k : nil }.compact
    if aliases_found.empty?
      say "No aliases created for #{email}", :yellow
    else
      say "Aliases for #{email}: #{aliases_found.join("; ")}"
    end
  end
end

#auth(user) ⇒ Object



76
77
78
# File 'lib/fuel/cli/gerrit.rb', line 76

def auth(user)
  set_credentials(user, ['gerrit', 'user'], ['gerrit', 'password'])
end

#checkout(email_or_alias) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/fuel/cli/gerrit.rb', line 114

def checkout(email_or_alias)
  email = to_email(email_or_alias)
  say "Group aliases are not supported", :red and return if email.is_a?(Array)
  result = self.class.get(changes_endpoint(nil, "?q=status:open+owner:#{email}&n=1&o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS")).parsed_response
  say "No open changes found for #{email_or_alias}", :yellow and return if result.empty?
  current_rev = result[0]['current_revision']
  command = result[0]['revisions'][current_rev]['fetch']['ssh']['commands']['Checkout']
  `#{command}`
end

#comments(file) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fuel/cli/gerrit.rb', line 143

def comments(file)
  comments, lines = comments_for_file(file)
  extra_lines = 0
  comments.each do |c|
    line = c['line'] + extra_lines
    comment_lines = format_comment(c)
    lines.insert(line, *comment_lines)
    extra_lines += comment_lines.size
  end
  exec("printf #{lines.join('\n').inspect} | less -F -X -R")
end

#cr(vote) ⇒ Object



132
133
134
# File 'lib/fuel/cli/gerrit.rb', line 132

def cr(vote)
  submit_review('Code-Review', vote)
end

#openObject



41
42
43
# File 'lib/fuel/cli/gerrit.rb', line 41

def open
  `open #{gerrit_change_url}`
end

#qa(vote) ⇒ Object



126
127
128
# File 'lib/fuel/cli/gerrit.rb', line 126

def qa(vote)
  submit_review('QA-Test-Passed', vote)
end

#rebaseObject



103
104
105
106
107
108
109
110
111
# File 'lib/fuel/cli/gerrit.rb', line 103

def rebase
  change_id = fetch_change_id_or_fail
  result = self.class.post(changes_endpoint(change_id, "rebase"))
  if result.response.code == "409"
    say "Rebase failed, conflicts found", :red
  else
    say "Rebased successfully", :green
  end
end

#respond(file) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fuel/cli/gerrit.rb', line 156

def respond(file)
  comments, lines, change_id, revision_id = comments_for_file(file)
  commented_on = {}
  comments_for_lines = comments.inject({}) do |h, c|
    h[c['line']] ||= []
    h[c['line']] << c
    h
  end
  comments_to_post = comments_for_lines.map do |line, cc|
    next if cc.all? { |c| c['author']['username'] == gerrit_user }
    system 'tput smcup'

    line_in_array = line - 1
    first_line = [0, line_in_array - 4].max
    if first_line == line_in_array
      say "Line #{line}:", :bold
    else
      say "Lines #{first_line + 1}-#{line}:", :bold
    end
    say lines[first_line..line_in_array].join("\n")
    cc.each do |c|
      say format_comment(c).join("\n")
    end
    reply = ask "Reply on this line (press enter to skip):"
    system 'tput rmcup'
    next if reply.strip.empty?
    { line: line, message: reply }
  end.compact
  say 'No comments to respond to!', :green and return if comments_to_post.empty?
  result = self.class.post(revisions_endpoint(change_id, revision_id, 'review'), body: { comments: { file => comments_to_post } }.to_json).parsed_response
end

#restoreObject



96
97
98
99
100
# File 'lib/fuel/cli/gerrit.rb', line 96

def restore
  change_id = fetch_change_id_or_fail
  params = options['message'] ? { body: { message: options['message'] }.to_json } : {}
  result = self.class.post(changes_endpoint(change_id, 'restore'), params).parsed_response
end

#review_add(*emails_or_aliases) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fuel/cli/gerrit.rb', line 46

def review_add(*emails_or_aliases)
  change_id = fetch_change_id_or_fail
  emails_or_aliases.each do |value|
    emails = Array(to_email(value))
    if emails.empty?
      say "No email found for alias #{value}; reviewer not added", :yellow
      next
    end
    emails.each do |email|
      result = self.class.post(reviewers_endpoint(change_id), body: { reviewer: email }.to_json).parsed_response
    end
  end
end

#statusObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fuel/cli/gerrit.rb', line 18

def status
  change_id = fetch_change_id_or_fail

  result = self.class.get(changes_endpoint(change_id, "?o=CURRENT_REVISION")).parsed_response
  current_rev = result['current_revision']

  say result["subject"], :bold
  table = [["Owner", result["owner"]["name"]]]
  table << ["Updated", format_time(result["updated"])]
  table << ["Status", result["status"]]
  table << ["Ref", gerrit_ref(result)]
  print_table table

  result = fetch_comments(change_id, current_rev)
  print_comments_info(result)
  puts
  result = self.class.get(reviewers_endpoint(change_id)).parsed_response
  table = result.map { |row| format_row(row) }
  table.unshift(["Reviewer", "CR", "QA", "V"])
  print_table table
end

#submitObject



81
82
83
84
# File 'lib/fuel/cli/gerrit.rb', line 81

def submit
  change_id = fetch_change_id_or_fail
  result = self.class.post(changes_endpoint(change_id, 'submit'), body: { wait_for_merge: true }.to_json)
end

#verified(vote) ⇒ Object



138
139
140
# File 'lib/fuel/cli/gerrit.rb', line 138

def verified(vote)
  submit_review('Verified', vote)
end