diff options
author | Lukas Eipert <leipert@gitlab.com> | 2018-10-16 16:56:03 +0300 |
---|---|---|
committer | Lukas Eipert <leipert@gitlab.com> | 2018-10-22 12:04:55 +0300 |
commit | 253e1818aa3efafb05d9994aab0f778d339e26ac (patch) | |
tree | a453ba647ef7635157519adc96718e3369890755 /danger/plugins/helper.rb | |
parent | fdcdc36eecdbe904cf80fa130aebf4edd34c3f12 (diff) |
Create helper to get all changed files
Danger apparently has three different objects which could contain files
you often want to check:
- git.added_files
- git.modified_files
- git.renamed_files
The problem: If a file is renamed, `modified_files` contains the file
path before the rename. In some Danger checks we use `added_files` +
`modified_files`, which might contain the deleted paths of renamed
files, but missing the new paths of renamed files.
So we need to consider `renamed_files` as well.
Diffstat (limited to 'danger/plugins/helper.rb')
-rw-r--r-- | danger/plugins/helper.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/danger/plugins/helper.rb b/danger/plugins/helper.rb new file mode 100644 index 00000000000..f4eb9119266 --- /dev/null +++ b/danger/plugins/helper.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Danger + # Common helper functions for our danger scripts + # If we find ourselves repeating code in our danger files, we might as well put them in here. + class Helper < Plugin + # Returns a list of all files that have been added, modified or renamed. + # `git.modified_files` might contain paths that already have been renamed, + # so we need to remove them from the list. + # + # Considering these changes: + # + # - A new_file.rb + # - D deleted_file.rb + # - M modified_file.rb + # - R renamed_file_before.rb -> renamed_file_after.rb + # + # it will return + # ``` + # [ 'new_file.rb', 'modified_file.rb', 'renamed_file_after.rb' ] + # ``` + # + # @return [Array<String>] + def all_changed_files + Set.new + .merge(git.added_files.to_a) + .merge(git.modified_files.to_a) + .merge(git.renamed_files.map { |x| x[:after] }) + .subtract(git.renamed_files.map { |x| x[:before] }) + .to_a + .sort + end + end +end |