Class: Gergich::Review
- Inherits:
-
Object
- Object
- Gergich::Review
- Defined in:
- lib/gergich.rb
Instance Attribute Summary collapse
-
#commit ⇒ Object
readonly
Returns the value of attribute commit.
-
#draft ⇒ Object
readonly
Returns the value of attribute draft.
Instance Method Summary collapse
- #already_commented? ⇒ Boolean
- #anything_to_publish? ⇒ Boolean
- #cover_message ⇒ Object
- #cover_message_prefix ⇒ Object
-
#current_label ⇒ Object
currently, cover message only supports the GERGICH_REVIEW_LABEL.
- #current_label_date ⇒ Object
- #current_label_is_for_current_revision? ⇒ Boolean
-
#current_label_revision ⇒ Object
unfortunately, the revision is not a field in the label json.
- #current_score ⇒ Object
- #generate_payload ⇒ Object
- #generate_url ⇒ Object
-
#initialize(commit = Commit.new, draft = Draft.new(commit)) ⇒ Review
constructor
A new instance of Review.
- #multi_build_setup? ⇒ Boolean
- #my_messages ⇒ Object
- #my_messages_on_current_revision ⇒ Object
- #new_score? ⇒ Boolean
-
#publish!(allow_repost: false) ⇒ Object
Public: publish all draft comments/labels/messages.
- #review_info ⇒ Object
-
#status ⇒ Object
Public: show the current draft for this patchset.
- #unique_comment_prefix ⇒ Object
- #upcoming_score ⇒ Object
Constructor Details
#initialize(commit = Commit.new, draft = Draft.new(commit)) ⇒ Review
Returns a new instance of Review.
105 106 107 108 |
# File 'lib/gergich.rb', line 105 def initialize(commit = Commit.new, draft = Draft.new(commit)) @commit = commit @draft = draft end |
Instance Attribute Details
#commit ⇒ Object (readonly)
Returns the value of attribute commit.
103 104 105 |
# File 'lib/gergich.rb', line 103 def commit @commit end |
#draft ⇒ Object (readonly)
Returns the value of attribute draft.
103 104 105 |
# File 'lib/gergich.rb', line 103 def draft @draft end |
Instance Method Details
#already_commented? ⇒ Boolean
191 192 193 194 195 196 197 198 199 |
# File 'lib/gergich.rb', line 191 def already_commented? if multi_build_setup? .any? do || ["message"] =~ /^#{unique_comment_prefix}/ end else .any? end end |
#anything_to_publish? ⇒ Boolean
122 123 124 125 126 |
# File 'lib/gergich.rb', line 122 def anything_to_publish? !review_info[:comments].empty? || !review_info[:cover_message_parts].empty? || new_score? end |
#cover_message ⇒ Object
247 248 249 250 251 252 |
# File 'lib/gergich.rb', line 247 def parts = review_info[:cover_message_parts] prefix = parts.unshift prefix if prefix != "" parts.join("\n\n") end |
#cover_message_prefix ⇒ Object
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/gergich.rb', line 254 def 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_label ⇒ Object
currently, cover message only supports the GERGICH_REVIEW_LABEL. i.e., even if gergich has “Code-Review: -2”
213 214 215 216 217 218 219 |
# File 'lib/gergich.rb', line 213 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_date ⇒ Object
221 222 223 |
# File 'lib/gergich.rb', line 221 def current_label_date @current_label_date ||= current_label && current_label["date"] end |
#current_label_is_for_current_revision? ⇒ Boolean
239 240 241 |
# File 'lib/gergich.rb', line 239 def current_label_is_for_current_revision? current_label_revision == commit.revision_number end |
#current_label_revision ⇒ Object
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.
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/gergich.rb', line 228 def current_label_revision return nil if .empty? @current_label_revision ||= begin date = current_label_date comment_for_current_label = .find { || ["date"] == date } || .last comment_for_current_label["_revision_number"] end end |
#current_score ⇒ Object
243 244 245 |
# File 'lib/gergich.rb', line 243 def current_score (current_label && current_label["value"]) || 0 end |
#generate_payload ⇒ Object
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/gergich.rb', line 273 def generate_payload { 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_url ⇒ Object
269 270 271 |
# File 'lib/gergich.rb', line 269 def generate_url "/changes/#{commit.change_id}/revisions/#{commit.revision_id}/review" end |
#multi_build_setup? ⇒ Boolean
182 183 184 185 |
# File 'lib/gergich.rb', line 182 def multi_build_setup? # convert to boolean if this variable exists or not !ENV["GERGICH_COMMENT_PREFIX"].nil? end |
#my_messages ⇒ Object
206 207 208 209 |
# File 'lib/gergich.rb', line 206 def ||= API.get("/changes/#{commit.change_id}/detail")["messages"] .select { || ["author"] && ["author"]["username"] == GERGICH_USER } end |
#my_messages_on_current_revision ⇒ Object
201 202 203 204 |
# File 'lib/gergich.rb', line 201 def revision_number = commit.revision_number .select { || ["_revision_number"] == revision_number } end |
#new_score? ⇒ Boolean
128 129 130 131 132 133 134 |
# File 'lib/gergich.rb', line 128 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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gergich.rb', line 111 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_info ⇒ Object
265 266 267 |
# File 'lib/gergich.rb', line 265 def review_info @review_info ||= draft.info end |
#status ⇒ Object
Public: show the current draft for this patchset
145 146 147 148 149 150 151 152 153 154 155 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 |
# File 'lib/gergich.rb', line 145 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 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_prefix ⇒ Object
187 188 189 |
# File 'lib/gergich.rb', line 187 def unique_comment_prefix ENV["GERGICH_COMMENT_PREFIX"] end |
#upcoming_score ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/gergich.rb', line 136 def upcoming_score if current_label_is_for_current_revision? [current_score.to_i, review_info[:score]].min else review_info[:score] end end |