Class: Danger::GitUtils
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::GitUtils
- Defined in:
- lib/dangermattic/plugins/common/git_utils.rb
Overview
Represents a utility plugin for working with Git: to check added and modified lines in Git diffs, to determine the type of change (added, removed, or other) in a diff line, and to retrieve lists of added, modified, and deleted files.
git_utils.change_type(diff_line: "- line removed")
#=> :removed
git_utils.change_type(diff_line: " context line")
#=> :other
Defined Under Namespace
Classes: MatchedData
Instance Method Summary collapse
-
#added_and_modified_files ⇒ Array<String>
Get the list of added and modified files in the current Pull Request.
-
#added_lines(diff_patch:) ⇒ String
Returns the lines that were added in the given diff patch.
-
#all_changed_files ⇒ Array<String>
Get the list of all files added, modified and deleted in the current Pull Request.
-
#change_type(diff_line:) ⇒ Symbol
Determine the type of change for a given line in a git diff.
-
#check_added_diff_lines(file_selector:, line_matcher:, message:, report_type: :warning) ⇒ void
Check added lines in a PR for a specific pattern and issue a warning or failure message when found.
-
#matching_lines_in_diff_files(files:, line_matcher:, change_type: nil) ⇒ Array<MatchedData>
Matches diff lines in the provided files based on the line matcher and change type.
-
#removed_lines(diff_patch:) ⇒ String
Returns the lines that were removed in the given diff patch.
-
#select_lines(diff_patch:, change_type:) ⇒ String
Selects lines of a specific change type (added or removed) from the given diff patch.
Instance Method Details
#added_and_modified_files ⇒ Array<String>
Get the list of added and modified files in the current Pull Request.
127 128 129 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 127 def added_and_modified_files danger.git.added_files + danger.git.modified_files end |
#added_lines(diff_patch:) ⇒ String
Returns the lines that were added in the given diff patch.
144 145 146 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 144 def added_lines(diff_patch:) select_lines(diff_patch: diff_patch, change_type: :added) end |
#all_changed_files ⇒ Array<String>
Get the list of all files added, modified and deleted in the current Pull Request.
135 136 137 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 135 def all_changed_files danger.git.added_files + danger.git.modified_files + danger.git.deleted_files end |
#change_type(diff_line:) ⇒ Symbol
Determine the type of change for a given line in a git diff.
114 115 116 117 118 119 120 121 122 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 114 def change_type(diff_line:) if diff_line.start_with?('+') && !diff_line.start_with?('+++ ') :added elsif diff_line.start_with?('-') && !diff_line.start_with?('--- ') :removed else :other end end |
#check_added_diff_lines(file_selector:, line_matcher:, message:, report_type: :warning) ⇒ void
This method returns an undefined value.
Check added lines in a PR for a specific pattern and issue a warning or failure message when found.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 54 def check_added_diff_lines(file_selector:, line_matcher:, message:, report_type: :warning) modified_files = added_and_modified_files.select(&file_selector) matches = matching_lines_in_diff_files( files: modified_files, line_matcher: line_matcher, change_type: :added ) matches.each do |match| match.lines.each do |line| = <<~MESSAGE #{} File `#{match.file}`: ```diff #{line.chomp} ``` MESSAGE reporter.report(message: , type: report_type) end end end |
#matching_lines_in_diff_files(files:, line_matcher:, change_type: nil) ⇒ Array<MatchedData>
Matches diff lines in the provided files based on the line matcher and change type
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 87 def matching_lines_in_diff_files(files:, line_matcher:, change_type: nil) change_types = Array(change_type).map(&:to_sym) matched_data = [] files.each do |file| matched_lines = [] diff = danger.git.diff_for_file(file) diff.patch.each_line do |line| matched_lines << line if line_matcher.call(line) && (change_type.nil? || change_types.include?(change_type(diff_line: line))) end matched_data << MatchedData.new(file, matched_lines) unless matched_lines.empty? end matched_data end |
#removed_lines(diff_patch:) ⇒ String
Returns the lines that were removed in the given diff patch.
153 154 155 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 153 def removed_lines(diff_patch:) select_lines(diff_patch: diff_patch, change_type: :removed) end |
#select_lines(diff_patch:, change_type:) ⇒ String
Selects lines of a specific change type (added or removed) from the given diff patch.
163 164 165 166 |
# File 'lib/dangermattic/plugins/common/git_utils.rb', line 163 def select_lines(diff_patch:, change_type:) selected_lines = diff_patch.lines.select { |line| change_type(diff_line: line) == change_type } selected_lines.map { |line| line[1..] }.join end |