module PrSummary

Constants

MAX_FILE_NAME_LENGTH
VERSION

Public Class Methods

call(pull_request_number:, branch:, base:) click to toggle source
# File lib/pr_summary.rb, line 12
def call(pull_request_number:, branch:, base:)
  pr            = Git::PullRequest.new(number: pull_request_number)
  diff          = BranchDiff.new(base: base, branch: branch)
  markdown_text = create_md(diff, pr)
  save_to_file(markdown_text, pr)
end
create_md(diff, pr) click to toggle source
# File lib/pr_summary.rb, line 23
def create_md(diff, pr)
  grouped_files(diff).map do |type, files|
    files = files.map do |file|
      link = "[#{file.truncated_name}](#{pr.file_url(file.filename)})"
      "* **#{file.changes_display}** #{link}"
    end.join("\n")
    "## #{type}\n#{files}\n\n"
  end.join("\n")
end
group_files_by_type(diff) click to toggle source
# File lib/pr_summary.rb, line 53
def group_files_by_type(diff)
  Git::Diff.new(diff: diff).name_only.group_by do |filename|
    groups.keys.detect do |group|
      groups[group].any? { |reg| reg =~ filename }
    end
  end
end
grouped_files(diff) click to toggle source
# File lib/pr_summary.rb, line 33
def grouped_files(diff)
  grouped               = group_files_by_type(diff)
  sorted_files_in_group = sort_by_insertion_count(diff, grouped)
  listed_order.each_with_object({}) do |type, hash|
    files = sorted_files_in_group[type]
    next if files.nil?
    hash[type] = files
  end
end
groups() click to toggle source
# File lib/pr_summary.rb, line 65
def groups
  {
    database:          [/migrate/, /schema\.rb$/, /structure\.sql$/],
    fixtures_and_data: [/.\.yml$/, /.\.json$/, /.\.json\.erb$/],
    spec:              [/._spec\.rb$/, /spec\//],
    mocks:             [/._mock\.rb$/],
    code:              [/.\.rb$/],
    documentation:     [/.\.md$/],
    other:             [/.*/]
  }
end
listed_order() click to toggle source
# File lib/pr_summary.rb, line 61
def listed_order
  [:code, :spec, :fixtures_and_data, :database, :mocks, :other, :documentation]
end
save_to_file(markdown_text, pr) click to toggle source
# File lib/pr_summary.rb, line 19
def save_to_file(markdown_text, pr)
  File.open("#{pr.key}.md", "w") { |file| file.write(markdown_text) }
end
sort_by_insertion_count(diff, grouped) click to toggle source
# File lib/pr_summary.rb, line 43
def sort_by_insertion_count(diff, grouped)
  grouped.each do |type, files|
    grouped[type] = files.map do |file|
      FileDiff.new(filename: file, diff: diff)
    end
    grouped[type] = grouped[type].sort_by { |f| f.insertions || 0 }.reverse
  end
  grouped
end