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, 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) ⇒ Integer
Calculate the total size of deletions in modified files that match the file selector.
-
#diff_size(file_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) ⇒ 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, 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.
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: , type: report_type) if insertions_size(file_selector: file_selector) > max_size when :deletions reporter.report(message: , type: report_type) if deletions_size(file_selector: file_selector) > max_size when :all reporter.report(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.
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: , type: report_type) end |
#deletions_size(file_selector: nil) ⇒ Integer
Calculate the total size of deletions in modified files that match the file selector.
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.
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.
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 |