class OttInfra::CodeReview

Public Class Methods

new() click to toggle source
# File lib/ottinfra/codereview.rb, line 8
def initialize
  @g = Git.open( find_git_root )
end
run() click to toggle source
# File lib/ottinfra/codereview.rb, line 12
def self.run
  codereview = self.new
  review = codereview.get_review_info
  unless review[:reviewers].nil?
    OttInfra::SendMail.new.send(
      review[:reviewers],
      subject: "CodeReview",
      message: "Message",
      attach: [review[:patch]] )
  end
end

Public Instance Methods

get_review_info() click to toggle source
# File lib/ottinfra/codereview.rb, line 24
def get_review_info
  {
    :author => get_author,
    :reviewers => get_reviewers,
    :patch => get_patch
  }
end

Private Instance Methods

find_git_root( path = Dir.pwd ) click to toggle source
# File lib/ottinfra/codereview.rb, line 34
def find_git_root( path = Dir.pwd )
  path = File.expand_path( path )
  # Base Cases
  return nil if path == "/"
  return path if File.exist?( File.join(path,'.git') )
  # Recurse
  return find_git_root( File.dirname(path) )
end
get_author() click to toggle source
# File lib/ottinfra/codereview.rb, line 63
def get_author
  { :name => @g.log.first.author.name,
    :email => @g.log.first.author.email}
end
get_file_attrs( file ) click to toggle source
# File lib/ottinfra/codereview.rb, line 47
def get_file_attrs( file )
  result = Hash[
    @g.lib.checkattr(file).split("\n").map{ |i| i.split(': ')[1,2] }
  ]
  result.each { |attr, val| result[attr] = val.split(',') }
end
get_last_changes() click to toggle source
# File lib/ottinfra/codereview.rb, line 43
def get_last_changes
  @g.log.first.diff_parent.stats[:files].keys
end
get_patch() click to toggle source
# File lib/ottinfra/codereview.rb, line 68
def get_patch
  patch_path = "/tmp/#{@g.log.first.objectish}.patch"
  diff = @g.lib.diff_full( @g.log.first.parent, @g.log.first )
  File.open(patch_path, 'w') { |file| file.write(diff) }
  patch_path
end
get_reviewers() click to toggle source
# File lib/ottinfra/codereview.rb, line 54
def get_reviewers
  reviewers = []
  get_last_changes.each do |file|
    reviewer = get_file_attrs( file )["owner.mail"]
    reviewers += reviewer
  end
  reviewers.uniq
end