Class: Gergich::Review

Inherits:
Object
  • Object
show all
Defined in:
lib/gergich.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit = Commit.new, draft = Draft.new) ⇒ Review

Returns a new instance of Review.



92
93
94
95
# File 'lib/gergich.rb', line 92

def initialize(commit = Commit.new, draft = Draft.new)
  @commit = commit
  @draft = draft
end

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit.



90
91
92
# File 'lib/gergich.rb', line 90

def commit
  @commit
end

#draftObject (readonly)

Returns the value of attribute draft.



90
91
92
# File 'lib/gergich.rb', line 90

def draft
  @draft
end

Instance Method Details

#already_commented?Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
# File 'lib/gergich.rb', line 178

def already_commented?
  if multi_build_setup?
    my_messages_on_current_revision.any? do |message|
      message["message"] =~ /^#{unique_comment_prefix}/
    end
  else
    my_messages_on_current_revision.any?
  end
end

#anything_to_publish?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/gergich.rb', line 109

def anything_to_publish?
  !review_info[:comments].empty? ||
    !review_info[:cover_message_parts].empty? ||
    new_score?
end

#cover_messageObject



234
235
236
237
238
239
# File 'lib/gergich.rb', line 234

def cover_message
  parts = review_info[:cover_message_parts]
  prefix = cover_message_prefix
  parts.unshift prefix if prefix != ""
  parts.join("\n\n")
end

#cover_message_prefixObject



241
242
243
244
245
246
247
248
249
250
# File 'lib/gergich.rb', line 241

def cover_message_prefix
  score = upcoming_score
  prefix_parts = []
  prefix_parts << unique_comment_prefix if multi_build_setup?
  prefix_parts << score if score.negative?
  prefix_parts.join(":")
  # [].join(":") => ""
  # [-2].join(":") => "-2"
  # ["some build prefix", -2].join(":") => "some build prefix:-2"
end

#current_labelObject

currently, cover message only supports the GERGICH_REVIEW_LABEL. i.e., even if gergich has “Code-Review: -2”



200
201
202
203
204
205
206
# File 'lib/gergich.rb', line 200

def current_label
  @current_label ||= API.get("/changes/#{commit.change_id}/detail")["labels"]
    .fetch(GERGICH_REVIEW_LABEL, {})
    .fetch("all", [])
    .select { |label| label["username"] == GERGICH_USER }
    .first
end

#current_label_dateObject



208
209
210
# File 'lib/gergich.rb', line 208

def current_label_date
  @current_label_date ||= current_label && current_label["date"]
end

#current_label_is_for_current_revision?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/gergich.rb', line 226

def current_label_is_for_current_revision?
  current_label_revision == commit.revision_number
end

#current_label_revisionObject

unfortunately, the revision is not a field in the label json. however, we can match the label timestamp w/ one of our comment timestamps, then grab the comment’s revision.



215
216
217
218
219
220
221
222
223
224
# File 'lib/gergich.rb', line 215

def current_label_revision
  return nil if my_messages.empty?

  @current_label_revision ||= begin
    date = current_label_date
    comment_for_current_label = my_messages.find { |message| message["date"] == date } ||
                                my_messages.last
    comment_for_current_label["_revision_number"]
  end
end

#current_scoreObject



230
231
232
# File 'lib/gergich.rb', line 230

def current_score
  (current_label && current_label["value"]) || 0
end

#generate_payloadObject



260
261
262
263
264
265
266
267
268
269
# File 'lib/gergich.rb', line 260

def generate_payload
  {
    message: cover_message,
    labels: review_info[:labels],
    comments: review_info[:comments],
    # we don't want the post to fail if another
    # patchset was created in the interim
    strict_labels: false
  }.to_json
end

#generate_urlObject



256
257
258
# File 'lib/gergich.rb', line 256

def generate_url
  "/changes/#{commit.change_id}/revisions/#{commit.revision_id}/review"
end

#multi_build_setup?Boolean

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/gergich.rb', line 169

def multi_build_setup?
  # convert to boolean if this variable exists or not
  !ENV["GERGICH_COMMENT_PREFIX"].nil?
end

#my_messagesObject



193
194
195
196
# File 'lib/gergich.rb', line 193

def my_messages
  @my_messages ||= API.get("/changes/#{commit.change_id}/detail")["messages"]
    .select { |message| message["author"] && message["author"]["username"] == GERGICH_USER }
end

#my_messages_on_current_revisionObject



188
189
190
191
# File 'lib/gergich.rb', line 188

def my_messages_on_current_revision
  revision_number = commit.revision_number
  my_messages.select { |message| message["_revision_number"] == revision_number }
end

#new_score?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/gergich.rb', line 115

def new_score?
  if current_label_is_for_current_revision?
    review_info[:score] < current_score.to_i
  else
    true
  end
end

#publish!(allow_repost: false) ⇒ Object

Public: publish all draft comments/labels/messages



98
99
100
101
102
103
104
105
106
107
# File 'lib/gergich.rb', line 98

def publish!(allow_repost: false)
  # only publish if we have something to say or if our last score was negative
  return unless anything_to_publish?

  return if already_commented? && !allow_repost

  API.post(generate_url, generate_payload)

  review_info
end

#review_infoObject



252
253
254
# File 'lib/gergich.rb', line 252

def review_info
  @review_info ||= draft.info
end

#statusObject

Public: show the current draft for this patchset



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gergich.rb', line 132

def status
  puts "Gergich DB: #{draft.db_file}"
  unless anything_to_publish?
    puts "Nothing to publish"
    return
  end

  info = commit.info
  puts "Project: #{info[:project]}"
  puts "Branch: #{info[:branch]}"
  puts "Revision: #{info[:revision_id]} (##{commit.revision_number})"
  puts "ChangeId: #{commit.change_id}"
  puts "Files:"
  puts "  #{commit.files.join("\n  ")}"

  puts
  review_info[:labels].each do |name, score|
    puts "#{name}: #{score}"
  end

  puts
  puts "Cover Message:"
  puts cover_message

  return if review_info[:comments].empty?

  puts
  puts "Inline Comments:"
  puts

  review_info[:comments].each do |file, comments|
    comments.each do |comment|
      puts "#{file}:#{comment[:line] || comment[:range]['start_line']}\n#{comment[:message]}"
    end
  end
end

#unique_comment_prefixObject



174
175
176
# File 'lib/gergich.rb', line 174

def unique_comment_prefix
  ENV["GERGICH_COMMENT_PREFIX"]
end

#upcoming_scoreObject



123
124
125
126
127
128
129
# File 'lib/gergich.rb', line 123

def upcoming_score
  if current_label_is_for_current_revision?
    [current_score.to_i, review_info[:score]].min
  else
    review_info[:score]
  end
end