summaryrefslogtreecommitdiff
path: root/danger/plugins/helper.rb
blob: 1a2710463ad65e6e41d8c035144bb79ed7bfa359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

    # @return [Hash<String,Array<String>>]
    def changes_by_category
      all_changed_files.inject(Hash.new { |h, k| h[k] = [] }) do |hsh, file|
        hsh[category_for_file(file)] << file
      end
    end

    def category_for_file(file)
      _, category = CATEGORIES.find { |regexp, _| regexp.match?(file) }

      category || :unknown
    end

    CATEGORIES = {
      %r{\Adoc/} => :documentation
    }
  end
end