@@ -29,22 +29,25 @@ class DangerPackageTodoYmlChanges < Danger::Plugin
2929 offenses_formatter : T . nilable ( Update ::OffensesFormatter ) ,
3030 before_comment : BeforeComment ,
3131 max_comments : Integer ,
32- violation_types : T ::Array [ String ]
32+ violation_types : T ::Array [ String ] ,
33+ root_path : T . nilable ( String )
3334 ) . void
3435 end
3536 def check (
3637 offenses_formatter : nil ,
3738 before_comment : DEFAULT_BEFORE_COMMENT ,
3839 max_comments : DEFAULT_MAX_COMMENTS ,
39- violation_types : DEFAULT_VIOLATION_TYPES
40+ violation_types : DEFAULT_VIOLATION_TYPES ,
41+ root_path : nil
4042 )
4143 offenses_formatter ||= Update ::DefaultFormatter . new
4244 repo_link = github . pr_json [ :base ] [ :repo ] [ :html_url ]
4345 org_name = github . pr_json [ :base ] [ :repo ] [ :owner ] [ :login ]
4446
45- changed_package_todo_ymls = ( git . modified_files + git . added_files + git . deleted_files ) . grep ( PACKAGE_TODO_PATTERN )
47+ git_filesystem = Private ::GitFilesystem . new ( git : git , root : root_path || '' )
48+ changed_package_todo_ymls = ( git_filesystem . modified_files + git_filesystem . added_files + git_filesystem . deleted_files ) . grep ( PACKAGE_TODO_PATTERN )
4649
47- violation_diff = get_violation_diff ( violation_types )
50+ violation_diff = get_violation_diff ( violation_types , root_path : root_path )
4851
4952 before_comment . call (
5053 violation_diff ,
@@ -61,130 +64,30 @@ def check(
6164 markdown (
6265 offenses_formatter . format_offenses ( violations , repo_link , org_name ) ,
6366 line : location . line_number ,
64- file : location . file
67+ file : git_filesystem . convert_to_filesystem ( location . file )
6568 )
6669
6770 current_comment_count += 1
6871 end
6972 end
7073
71- sig { params ( violation_types : T ::Array [ String ] ) . returns ( ViolationDiff ) }
72- def get_violation_diff ( violation_types )
73- added_violations = T . let ( [ ] , T ::Array [ BasicReferenceOffense ] )
74- removed_violations = T . let ( [ ] , T ::Array [ BasicReferenceOffense ] )
75-
76- git . added_files . grep ( PACKAGE_TODO_PATTERN ) . each do |added_package_todo_yml_file |
77- # Since the file is added, we know on the base commit there are no violations related to this pack,
78- # and that all violations from this file are new
79- added_violations += BasicReferenceOffense . from ( added_package_todo_yml_file )
80- end
81-
82- git . deleted_files . grep ( PACKAGE_TODO_PATTERN ) . each do |deleted_package_todo_yml_file |
83- # Since the file is deleted, we know on the HEAD commit there are no violations related to this pack,
84- # and that all violations from this file are deleted
85- deleted_violations = get_violations_before_patch_for ( deleted_package_todo_yml_file )
86- removed_violations += deleted_violations
87- end
88-
89- # The format for git.renamed_files is a T::Array[{after: "some/path/new", before: "some/path/old"}]
90- renamed_files_before = git . renamed_files . map { |before_after_file | before_after_file [ :before ] }
91- renamed_files_after = git . renamed_files . map { |before_after_file | before_after_file [ :after ] }
92-
93- git . modified_files . grep ( PACKAGE_TODO_PATTERN ) . each do |modified_package_todo_yml_file |
94- # We skip over modified files if one of the modified files is a renamed `package_todo.yml` file.
95- # This allows us to rename packs while ignoring "new violations" in those renamed packs.
96- next if renamed_files_before . include? ( modified_package_todo_yml_file )
97-
98- head_commit_violations = BasicReferenceOffense . from ( modified_package_todo_yml_file )
99- base_commit_violations = get_violations_before_patch_for ( modified_package_todo_yml_file )
100- added_violations += head_commit_violations - base_commit_violations
101- removed_violations += base_commit_violations - head_commit_violations
102- end
103-
104- #
105- # This implementation creates some false negatives:
106- # That is – it doesn't capture some cases:
107- # 1) A file has been renamed without renaming a constant.
108- # That can happen if we change only the autoloaded portion of a filename.
109- # For example: `packs/foo/app/services/my_class.rb` (defines: `MyClass`)
110- # is changed to `packs/foo/app/public/my_class.rb` (still defines: `MyClass`)
111- #
112- # This implementation also doesn't cover these false positives:
113- # That is – it leaves a comment when it should not.
114- # 1) A CONSTANT within a class or module has been renamed.
115- # e.g. `class MyClass; MY_CONSTANT = 1; end` becomes `class MyClass; RENAMED_CONSTANT = 1; end`
116- # We would not detect based on file renames that `MY_CONSTANT` has been renamed.
117- #
118- renamed_constants = [ ]
119-
120- added_violations . each do |violation |
121- filepath_that_defines_this_constant = Private . constant_resolver . resolve ( violation . class_name ) &.location
122- renamed_constants << violation . class_name if renamed_files_after . include? ( filepath_that_defines_this_constant )
123- end
124-
125- relevant_added_violations = added_violations . reject do |violation |
126- renamed_files_after . include? ( violation . file ) ||
127- renamed_constants . include? ( violation . class_name ) ||
128- !violation_types . include? ( violation . type )
129- end
74+ sig do
75+ params (
76+ violation_types : T ::Array [ String ] ,
77+ root_path : T . nilable ( String )
78+ ) . returns ( ViolationDiff )
79+ end
80+ def get_violation_diff ( violation_types , root_path : nil )
81+ git_filesystem = Private ::GitFilesystem . new ( git : git , root : root_path || '' )
13082
131- relevant_removed_violations = removed_violations . select do | violation |
132- violation_types . include? ( violation . type )
133- end
83+ added_violations , removed_violations = Private :: TodoYmlChanges . get_reference_offenses (
84+ violation_types , git_filesystem
85+ )
13486
13587 ViolationDiff . new (
136- added_violations : relevant_added_violations ,
137- removed_violations : relevant_removed_violations
88+ added_violations : added_violations ,
89+ removed_violations : removed_violations
13890 )
13991 end
140-
141- private
142-
143- sig { params ( package_todo_yml_file : String ) . returns ( T ::Array [ BasicReferenceOffense ] ) }
144- def get_violations_before_patch_for ( package_todo_yml_file )
145- # The strategy to get the violations before this PR is to reverse the patch on each `package_todo.yml`.
146- # A previous strategy attempted to use `git merge-base --fork-point`, but there are many situations where it returns
147- # empty values. That strategy is fickle because it depends on the state of the `reflog` within the CI suite, which appears
148- # to not be reliable to depend on.
149- #
150- # Instead, just inverting the patch should hopefully provide a more reliable way to figure out what was the state of the file before
151- # the PR without needing to use git commands that interpret the branch history based on local git history.
152- #
153- # We apply the patch to the original file so that we can seamlessly reverse the patch applied to that file (since patches are coupled to
154- # the files they modify). After parsing the violations from that `package_todo.yml` file with the patch reversed,
155- # we use a temporary copy of the original file to rewrite to it with the original contents.
156- # Note that practically speaking, we don't need to rewrite the original contents (since we already fetched the
157- # original contents above and the CI file system should be ephemeral). However, we do this anyways in case we later change these
158- # assumptions, or another client's environment is different and expects these files not to be mutated.
159-
160- # Keep track of the original file contents. If the original file has been deleted, then we delete the file after inverting the patch at the end, rather than rewriting it.
161- package_todo_yml_file_copy = ( File . read ( package_todo_yml_file ) if File . exist? ( package_todo_yml_file ) )
162-
163- Tempfile . create do |patch_file |
164- # Normally we'd use `git.diff_for_file(package_todo_yml_file).patch` here, but there is a bug where it does not work for deleted files yet.
165- # I have a fix for that here: https://github.com/danger/danger/pull/1357
166- # Until that lands, I'm just using the underlying implementation of that method to get the diff for a file.
167- # Note that I might want to use a safe escape operator, `&.patch` and return gracefully if the patch cannot be found.
168- # However I'd be interested in why that ever happens, so for now going to proceed as is.
169- # (Note that better yet we'd have observability into these so I can just log under those circumstances rather than surfacing an error to the user,
170- # but we don't have that quite yet.)
171- patch_for_file = git . diff [ package_todo_yml_file ] . patch
172- # This appears to be a known issue that patches require new lines at the end. It seems like this is an issue with Danger that
173- # it gives us a patch without a newline.
174- # https://stackoverflow.com/questions/18142870/git-error-fatal-corrupt-patch-at-line-36
175- patch_file << "#{ patch_for_file } \n "
176- patch_file . rewind
177- # https://git-scm.com/docs/git-apply
178- _stdout , _stderr , _status = Open3 . capture3 ( "git apply --reverse #{ patch_file . path } " )
179- # https://www.rubyguides.com/2019/05/ruby-tempfile/
180- BasicReferenceOffense . from ( package_todo_yml_file )
181- end
182- ensure
183- if package_todo_yml_file_copy
184- File . write ( package_todo_yml_file , package_todo_yml_file_copy )
185- else
186- File . delete ( package_todo_yml_file )
187- end
188- end
18992 end
19093end
0 commit comments