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.
This commit is contained in:
Lukas Eipert 2018-10-16 15:56:03 +02:00
parent fdcdc36eec
commit 253e1818aa
No known key found for this signature in database
GPG Key ID: 148BEA37CB35B2AC
6 changed files with 39 additions and 8 deletions

View File

@ -1,3 +1,4 @@
danger.import_plugin('danger/plugins/helper.rb')
danger.import_dangerfile(path: 'danger/metadata')
danger.import_dangerfile(path: 'danger/changes_size')
danger.import_dangerfile(path: 'danger/changelog')

View File

@ -39,8 +39,6 @@ def database_paths_requiring_review(files)
to_review
end
all_files = git.added_files + git.modified_files
non_geo_db_schema_updated = !git.modified_files.grep(%r{\Adb/schema\.rb}).empty?
geo_db_schema_updated = !git.modified_files.grep(%r{\Aee/db/geo/schema\.rb}).empty?
@ -55,7 +53,7 @@ if geo_migration_created && !geo_db_schema_updated
warn format(SCHEMA_NOT_UPDATED_MESSAGE, migrations: 'Geo migrations', schema: gitlab.html_link("ee/db/geo/schema.rb"))
end
db_paths_to_review = database_paths_requiring_review(all_files)
db_paths_to_review = database_paths_requiring_review(helper.all_changed_files)
unless db_paths_to_review.empty?
message 'This merge request adds or changes files that require a ' \

View File

@ -11,9 +11,7 @@ def docs_paths_requiring_review(files)
end
end
all_files = git.added_files + git.modified_files
docs_paths_to_review = docs_paths_requiring_review(all_files)
docs_paths_to_review = docs_paths_requiring_review(helper.all_changed_files)
unless docs_paths_to_review.empty?
message 'This merge request adds or changes files that require a ' \

View File

@ -7,7 +7,7 @@ def get_eslint_files(files)
end
end
eslint_candidates = get_eslint_files(git.added_files + git.modified_files)
eslint_candidates = get_eslint_files(helper.all_changed_files)
return if eslint_candidates.empty?

34
danger/plugins/helper.rb Normal file
View File

@ -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

View File

@ -6,7 +6,7 @@ def get_prettier_files(files)
end
end
prettier_candidates = get_prettier_files(git.added_files + git.modified_files)
prettier_candidates = get_prettier_files(helper.all_changed_files)
return if prettier_candidates.empty?