lib/danger-packwerk/danger_packwerk.rb in danger-packwerk-0.14.1 vs lib/danger-packwerk/danger_packwerk.rb in danger-packwerk-0.14.2
- old
+ new
@@ -4,10 +4,11 @@
require 'danger'
require 'packwerk'
require 'parse_packwerk'
require 'sorbet-runtime'
require 'danger-packwerk/packwerk_wrapper'
+require 'danger-packwerk/private/git'
module DangerPackwerk
# Note that Danger names the plugin (i.e. anything that inherits from `Danger::Plugin`) by taking the name of the class and gsubbing out "Danger"
# Therefore this plugin is simply called "packwerk"
class DangerPackwerk < Danger::Plugin
@@ -41,21 +42,23 @@
max_comments: Integer,
fail_build: T::Boolean,
failure_message: String,
on_failure: OnFailure,
violation_types: T::Array[String],
- grouping_strategy: CommentGroupingStrategy
+ grouping_strategy: CommentGroupingStrategy,
+ root_path: T.nilable(String)
).void
end
def check(
offenses_formatter: nil,
max_comments: DEFAULT_MAX_COMMENTS,
fail_build: DEFAULT_FAIL,
failure_message: DEFAULT_FAILURE_MESSAGE,
on_failure: DEFAULT_ON_FAILURE,
violation_types: DEFAULT_VIOLATION_TYPES,
- grouping_strategy: CommentGroupingStrategy::PerConstantPerLocation
+ grouping_strategy: CommentGroupingStrategy::PerConstantPerLocation,
+ root_path: nil
)
offenses_formatter ||= Check::DefaultFormatter.new
repo_link = github.pr_json[:base][:repo][:html_url]
org_name = github.pr_json[:base][:repo][:owner][:login]
@@ -65,13 +68,16 @@
# the danger message will only be triggered by actual code that someone has actually written in their PR.
# Another example would be if someone changes the list of dependencies of a package (e.g. to resolve a cyclic dependency). This would not
# trigger the warning message, which is good, since we only want to trigger on new code.
github.dismiss_out_of_range_messages
+ git_filesystem = Private::GitFilesystem.new(git: git, root: root_path || '')
+
# https://github.com/danger/danger/blob/eca19719d3e585fe1cc46bc5377f9aa955ebf609/lib/danger/danger_core/plugins/dangerfile_git_plugin.rb#L80
- renamed_files_after = git.renamed_files.map { |f| f[:after] }
- targeted_files = (git.modified_files + git.added_files + renamed_files_after).select do |f|
+ renamed_files_after = git_filesystem.renamed_files.map { |f| f[:after] }
+
+ targeted_files = (git_filesystem.modified_files + git_filesystem.added_files + renamed_files_after).select do |f|
path = Pathname.new(f)
# We probably want to check the `include` key of `packwerk.yml`. By default, this value is "**/*.{rb,rake,erb}",
# so we hardcode this in for now. If this blocks a user, we can take that opportunity to read from `packwerk.yml`.
extension_is_targeted = ['.erb', '.rake', '.rb'].include?(path.extname)
@@ -90,11 +96,11 @@
current_comment_count = 0
packwerk_reference_offenses = PackwerkWrapper.get_offenses_for_files(targeted_files.to_a).compact
- renamed_files = git.renamed_files.map { |before_after_file| before_after_file[:after] }
+ renamed_files = git_filesystem.renamed_files.map { |before_after_file| before_after_file[:after] }
packwerk_reference_offenses_to_care_about = packwerk_reference_offenses.reject do |packwerk_reference_offense|
constant_name = packwerk_reference_offense.reference.constant.name
filepath_that_defines_this_constant = Private.constant_resolver.resolve(constant_name)&.location
# Ignore references that have been renamed
@@ -129,11 +135,10 @@
reference_offense = T.must(unique_packwerk_reference_offenses.first)
line_number = reference_offense.location&.line
referencing_file = reference_offense.reference.relative_path
message = offenses_formatter.format_offenses(unique_packwerk_reference_offenses, repo_link, org_name)
-
- markdown(message, file: referencing_file, line: line_number)
+ markdown(message, file: git_filesystem.convert_to_filesystem(referencing_file), line: line_number)
end
if current_comment_count > 0
fail(failure_message) if fail_build