Class: Danger::PRSizeChecker

Inherits:
Plugin
  • Object
show all
Defined in:
lib/dangermattic/plugins/pr_size_checker.rb

Overview

Plugin to check the size of a Pull Request content and text body.

Examples:

Running a PR diff size check with default parameters


# Check the total size of changes in the PR using the default parameters, reporting a warning if the PR is larger than 500
pr_size_checker.check_diff_size(max_size: 500)

Running a PR diff size check customizing the size, message and type of report


# Check the total size of changes in the PR, reporting an error if the diff is larger than 1000 using the specified message
pr_size_checker.check_diff_size(max_size: 1000, message: 'PR too large, 1000 is the max!!', report_type: :error)

Running a PR diff size check on the specified files in part of the diff


# Check the size of insertions in the files selected by the file_selector
pr_size_checker.check_diff_size(file_selector: ->(file) { file.include?('/java/test/') }, type: :insertions)

Running a PR description length check


# Check the PR Body using the default parameters, reporting a warning if the PR is smaller than 10 characters
pr_size_checker.check_pr_body(min_length: 10)

Running a PR description length check with custom parameters


# Check if the minimum length of the PR body is smaller than 20 characters, reporting an error using a custom error message
pr_size_checker.check_pr_body(min_length: 20, message: 'Add a better description, 20 chars at least!!', report_type: :error)

See Also:

  • Automattic/dangermattic

Constant Summary collapse

DEFAULT_DIFF_SIZE_MESSAGE_FORMAT =
'This PR is larger than %d lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.'
DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT =
'The PR description appears very short, less than %d characters long. Please provide a summary of your changes in the PR description.'

Instance Method Summary collapse

Instance Method Details

#check_diff_size(max_size:, file_selector: nil, type: :all, message: format(DEFAULT_DIFF_SIZE_MESSAGE_FORMAT, max_size), report_type: :warning) ⇒ void

This method returns an undefined value.

Check the size of the PR diff against a specified maximum size.

Parameters:

  • max_size (Integer)

    The maximum allowed size for the diff.

  • file_selector (Proc) (defaults to: nil)

    Optional closure to filter the files in the diff to be used for size calculation.

  • type (:insertions, :deletions, :all) (defaults to: :all)

    The type of diff size to check. (default: :all)

  • message (String) (defaults to: format(DEFAULT_DIFF_SIZE_MESSAGE_FORMAT, max_size))

    The message to display if the diff size exceeds the maximum. (default: DEFAULT_DIFF_SIZE_MESSAGE)

  • report_type (Symbol) (defaults to: :warning)

    (optional) The type of report for the message. Types: :error, :warning (default), :message.



47
48
49
50
51
52
53
54
55
56
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 47

def check_diff_size(max_size:, file_selector: nil, type: :all, message: format(DEFAULT_DIFF_SIZE_MESSAGE_FORMAT, max_size), report_type: :warning)
  case type
  when :insertions
    reporter.report(message: message, type: report_type) if insertions_size(file_selector: file_selector) > max_size
  when :deletions
    reporter.report(message: message, type: report_type) if deletions_size(file_selector: file_selector) > max_size
  when :all
    reporter.report(message: message, type: report_type) if diff_size(file_selector: file_selector) > max_size
  end
end

#check_pr_body(min_length:, message: format(DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT, min_length), report_type: :warning) ⇒ void

This method returns an undefined value.

Check the size of the Pull Request description (PR body) against a specified minimum size.

Parameters:

  • min_length (Integer)

    The minimum allowed length for the PR body.

  • message (String) (defaults to: format(DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT, min_length))

    The message to display if the length of the PR body is smaller than the minimum. (default: DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT)

  • report_type (Boolean) (defaults to: :warning)

    If true, fail the PR check when the PR body length is too small. (default: false)



65
66
67
68
69
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 65

def check_pr_body(min_length:, message: format(DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT, min_length), report_type: :warning)
  return if danger.github.pr_body.length > min_length

  reporter.report(message: message, type: report_type)
end

#deletions_size(file_selector: nil) ⇒ Integer

Calculate the total size of deletions in modified files that match the file selector.

Parameters:

  • file_selector (Proc) (defaults to: nil)

    Select the files to be used for the deletions calculation.

Returns:

  • (Integer)

    The total size of deletions in the selected modified files.



93
94
95
96
97
98
99
100
101
102
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 93

def deletions_size(file_selector: nil)
  return danger.git.deletions unless file_selector

  filtered_files = git_utils.all_changed_files.select(&file_selector)

  filtered_files.sum do |file|
    # Use cached stats directly instead of calling info_for_file for each file
    danger.git.diff.stats[:files][file]&.[](:deletions).to_i
  end
end

#diff_size(file_selector: nil) ⇒ Integer

Calculate the total size of changes (insertions and deletions) in modified files that match the file selector.

Parameters:

  • file_selector (Proc) (defaults to: nil)

    Select the files to be used for the total insertions and deletions calculation.

Returns:

  • (Integer)

    The total size of changes in the selected modified files.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 109

def diff_size(file_selector: nil)
  return danger.git.lines_of_code unless file_selector

  filtered_files = git_utils.all_changed_files.select(&file_selector)

  filtered_files.sum do |file|
    # Use cached stats directly instead of calling info_for_file for each file
    stats = danger.git.diff.stats[:files][file]
    next 0 unless stats

    stats[:deletions].to_i + stats[:insertions].to_i
  end
end

#insertions_size(file_selector: nil) ⇒ Integer

Calculate the total size of insertions in modified files that match the file selector.

Parameters:

  • file_selector (Proc) (defaults to: nil)

    Select the files to be used for the insertions calculation.

Returns:

  • (Integer)

    The total size of insertions in the selected modified files.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 76

def insertions_size(file_selector: nil)
  return danger.git.insertions unless file_selector

  # Only check added and modified files - deleted files have 0 insertions
  filtered_files = git_utils.added_and_modified_files.select(&file_selector)

  filtered_files.sum do |file|
    # Use cached stats directly instead of calling info_for_file for each file
    danger.git.diff.stats[:files][file]&.[](:insertions).to_i
  end
end