Class: Gergich::Draft
- Inherits:
-
Object
- Object
- Gergich::Draft
- Defined in:
- lib/gergich.rb
Constant Summary collapse
- SEVERITY_MAP =
{ "info" => 0, "warn" => -1, "error" => -2 }.freeze
- POSITION_KEYS =
%w[end_character end_line start_character start_line].freeze
Instance Attribute Summary collapse
-
#commit ⇒ Object
readonly
Returns the value of attribute commit.
Instance Method Summary collapse
-
#add_comment(path, position, message, severity) ⇒ Object
Public: add an inline comment to the draft.
-
#add_label(name, score) ⇒ Object
Public: add a label to the draft.
-
#add_message(message) ⇒ Object
Public: add something to the cover message.
- #all_comments ⇒ Object
- #changed_files ⇒ Object
- #cover_message_parts ⇒ Object
- #create_db_schema! ⇒ Object
- #db ⇒ Object
- #db_file ⇒ Object
- #info ⇒ Object
-
#initialize(commit = Commit.new) ⇒ Draft
constructor
A new instance of Draft.
- #inline_comments ⇒ Object
- #labels ⇒ Object
- #messages ⇒ Object
- #min_comment_score ⇒ Object
- #orphaned_message ⇒ Object
- #other_comments ⇒ Object
- #reset! ⇒ Object
- #valid_position?(position) ⇒ Boolean
Constructor Details
Instance Attribute Details
#commit ⇒ Object (readonly)
Returns the value of attribute commit.
359 360 361 |
# File 'lib/gergich.rb', line 359 def commit @commit end |
Instance Method Details
#add_comment(path, position, message, severity) ⇒ Object
Public: add an inline comment to the draft
path - the relative file path, e.g. “app/models/user.rb” position - either a Fixnum (line number) or a Hash (range). If a
Hash, must have the following Fixnum properties:
* start_line
* start_character
* end_line
* end_character
message - the text of the comment severity - “info”|“warn”|“error” - this will automatically prefix
the comment (e.g. "[ERROR] message here"), and the most
severe comment will be used to determine the overall
Code-Review score (0, -1, or -2 respectively)
445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/gergich.rb', line 445 def add_comment(path, position, , severity) stripped_path = path.strip raise GergichError, "invalid position `#{position}`" unless valid_position?(position) position = position.to_json if position.is_a?(Hash) raise GergichError, "invalid severity `#{severity}`" unless SEVERITY_MAP.key?(severity) raise GergichError, "no message specified" unless .is_a?(String) && !.empty? db.execute "INSERT INTO comments (path, position, message, severity) VALUES (?, ?, ?, ?)", [stripped_path, position, , severity] end |
#add_label(name, score) ⇒ Object
Public: add a label to the draft
name - the label name, e.g. “Code-Review” score - the score, e.g. “-1”
You can set add the same label multiple times, but the lowest score for a given label will be used. This also applies to the inferred “Code-Review” score from comments; if it is non-zero, it will trump a higher score set here.
414 415 416 417 418 419 420 421 |
# File 'lib/gergich.rb', line 414 def add_label(name, score) score = score.to_i raise GergichError, "invalid score" if score < -2 || score > 1 raise GergichError, "can't set #{name}" if %w[Verified].include?(name) db.execute "INSERT INTO labels (name, score) VALUES (?, ?)", [name, score] end |
#add_message(message) ⇒ Object
Public: add something to the cover message
These messages will appear after the “-1” (or whatever)
426 427 428 |
# File 'lib/gergich.rb', line 426 def () db.execute "INSERT INTO messages (message) VALUES (?)", [] end |
#all_comments ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/gergich.rb', line 480 def all_comments @all_comments ||= begin comments = {} sql = "SELECT path, position, message, severity FROM comments" db.execute(sql).each do |row| inline = changed_files.include?(row["path"]) comments[row["path"]] ||= FileReview.new(row["path"], inline) comments[row["path"]].add_comment(row["position"], row["message"], row["severity"]) end comments.values end end |
#changed_files ⇒ Object
509 510 511 |
# File 'lib/gergich.rb', line 509 def changed_files @changed_files ||= commit.files + ["/COMMIT_MSG"] end |
#cover_message_parts ⇒ Object
549 550 551 552 553 |
# File 'lib/gergich.rb', line 549 def parts = parts << unless other_comments.empty? parts end |
#create_db_schema! ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/gergich.rb', line 383 def create_db_schema! db.execute <<-SQL CREATE TABLE comments ( path VARCHAR, position VARCHAR, message VARCHAR, severity VARCHAR ); SQL db.execute <<-SQL CREATE TABLE labels ( name VARCHAR, score INTEGER ); SQL db.execute <<-SQL CREATE TABLE messages ( message VARCHAR ); SQL end |
#db ⇒ Object
369 370 371 372 373 374 375 376 377 |
# File 'lib/gergich.rb', line 369 def db @db ||= begin db_exists = File.exist?(db_file) db = SQLite3::Database.new(db_file) db.results_as_hash = true create_db_schema! unless db_exists db end end |
#db_file ⇒ Object
365 366 367 |
# File 'lib/gergich.rb', line 365 def db_file @db_file ||= File.("/tmp/#{GERGICH_USER}-#{commit.revision_id}.sqlite3") end |
#info ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/gergich.rb', line 513 def info @info ||= begin comments = Hash[inline_comments.map { |file| [file.path, file.to_a] }] { comments: comments, cover_message_parts: , total_comments: all_comments.map(&:count).inject(&:+), score: labels[GERGICH_REVIEW_LABEL], labels: labels } end end |
#inline_comments ⇒ Object
497 498 499 |
# File 'lib/gergich.rb', line 497 def inline_comments all_comments.select(&:inline) end |
#labels ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/gergich.rb', line 468 def labels @labels ||= begin labels = { GERGICH_REVIEW_LABEL => 0 } db.execute("SELECT name, MIN(score) AS score FROM labels GROUP BY name").each do |row| labels[row["name"]] = row["score"] end score = min_comment_score labels[GERGICH_REVIEW_LABEL] = score if score < [0, labels[GERGICH_REVIEW_LABEL]].min labels end end |
#messages ⇒ Object
527 528 529 |
# File 'lib/gergich.rb', line 527 def db.execute("SELECT message FROM messages").map { |row| row["message"] } end |
#min_comment_score ⇒ Object
505 506 507 |
# File 'lib/gergich.rb', line 505 def min_comment_score all_comments.inject(0) { |acc, elem| [acc, elem.min_score].min } end |
#orphaned_message ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/gergich.rb', line 531 def = ["NOTE: I couldn't create inline comments for everything. " \ "Although this isn't technically part of your commit, you " \ "should still check it out (i.e. side effects or auto-" \ "generated from stuff you *did* change):"] other_comments.each do |file| file.comments.each do |position, comments| comments.each do |comment| line = position.is_a?(Integer) ? position : position["start_line"] << "#{file.path}:#{line}: #{comment}" end end end .join("\n\n") end |
#other_comments ⇒ Object
501 502 503 |
# File 'lib/gergich.rb', line 501 def other_comments all_comments.reject(&:inline) end |
#reset! ⇒ Object
379 380 381 |
# File 'lib/gergich.rb', line 379 def reset! FileUtils.rm_f(db_file) end |
#valid_position?(position) ⇒ Boolean
459 460 461 462 463 464 465 466 |
# File 'lib/gergich.rb', line 459 def valid_position?(position) ( position.is_a?(Integer) && position >= 0 ) || ( position.is_a?(Hash) && position.keys.sort == POSITION_KEYS && position.values.all? { |v| v.is_a?(Integer) && v >= 0 } ) end |