Class: Danger::PRSizeChecker
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::PRSizeChecker
- Defined in:
- lib/dangermattic/plugins/pr_size_checker.rb
Overview
Plugin to check the size of a Pull Request content and text body.
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
-
#check_diff_size(max_size:, file_selector: nil, line_selector: nil, type: :all, message: format(DEFAULT_DIFF_SIZE_MESSAGE_FORMAT, max_size), report_type: :warning) ⇒ void
Check the size of the PR diff against a specified maximum size.
-
#check_pr_body(min_length:, message: format(DEFAULT_MIN_PR_BODY_MESSAGE_FORMAT, min_length), report_type: :warning) ⇒ void
Check the size of the Pull Request description (PR body) against a specified minimum size.
-
#deletions_size(file_selector: nil, line_selector: nil) ⇒ Integer
Calculate the total size of deletions in modified files that match the file selector.
-
#diff_size(file_selector: nil, line_selector: nil) ⇒ Integer
Calculate the total size of changes (insertions and deletions) in modified files that match the file selector.
-
#insertions_size(file_selector: nil, line_selector: nil) ⇒ Integer
Calculate the total size of insertions in modified files that match the file selector.
Instance Method Details
#check_diff_size(max_size:, file_selector: nil, line_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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 59 def check_diff_size(max_size:, file_selector: nil, line_selector: nil, type: :all, message: format(DEFAULT_DIFF_SIZE_MESSAGE_FORMAT, max_size), report_type: :warning) size = case type when :insertions insertions_size(file_selector: file_selector, line_selector: line_selector) when :deletions deletions_size(file_selector: file_selector, line_selector: line_selector) when :all diff_size(file_selector: file_selector, line_selector: line_selector) else raise ArgumentError, "Unknown diff size type: #{type.inspect}. Use :insertions, :deletions, or :all." end reporter.report(message: , type: report_type) if size > max_size 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.
81 82 83 84 85 |
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 81 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: , type: report_type) end |
#deletions_size(file_selector: nil, line_selector: nil) ⇒ Integer
Calculate the total size of deletions in modified files that match the file selector.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 113 def deletions_size(file_selector: nil, line_selector: nil) return filtered_diff_size(file_selector: file_selector, line_selector: line_selector, change_types: [:removed]) if line_selector 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, line_selector: nil) ⇒ Integer
Calculate the total size of changes (insertions and deletions) in modified files that match the file selector.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 132 def diff_size(file_selector: nil, line_selector: nil) return filtered_diff_size(file_selector: file_selector, line_selector: line_selector, change_types: %i[added removed]) if line_selector 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, line_selector: nil) ⇒ Integer
Calculate the total size of insertions in modified files that match the file selector.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dangermattic/plugins/pr_size_checker.rb', line 93 def insertions_size(file_selector: nil, line_selector: nil) return filtered_diff_size(file_selector: file_selector, line_selector: line_selector, change_types: [:added]) if line_selector 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 |