Module: Gitlab::Danger::Helper

Defined in:
lib/gitlab_roulette/danger/helper.rb

Constant Summary collapse

RELEASE_TOOLS_BOT =
'gitlab-release-tools-bot'
CATEGORY_LABELS =
{
  docs: "~documentation", # Docs are reviewed along DevOps stages, so don't need roulette for now.
  none: "",
  qa: "~QA",
  test: "~test ~Quality for `spec/features/*`",
  engineering_productivity: '~"Engineering Productivity" for CI, Danger'
}.freeze
CATEGORIES =
{
  %r{\Adoc/} => :none, # To reinstate roulette for documentation, set to `:docs`.
  %r{\A(CONTRIBUTING|LICENSE|MAINTENANCE|PHILOSOPHY|PROCESS|README)(\.md)?\z} => :none, # To reinstate roulette for documentation, set to `:docs`.

  %r{\A(ee/)?app/(assets|views)/} => :frontend,
  %r{\A(ee/)?public/} => :frontend,
  %r{\A(ee/)?spec/(javascripts|frontend)/} => :frontend,
  %r{\A(ee/)?vendor/assets/} => :frontend,
  %r{\A(ee/)?scripts/frontend/} => :frontend,
  %r{(\A|/)(
    \.babelrc |
    \.eslintignore |
    \.eslintrc(\.yml)? |
    \.nvmrc |
    \.prettierignore |
    \.prettierrc |
    \.scss-lint.yml |
    \.stylelintrc |
    \.haml-lint.yml |
    \.haml-lint_todo.yml |
    babel\.config\.js |
    jest\.config\.js |
    karma\.config\.js |
    webpack\.config\.js |
    package\.json |
    yarn\.lock |
    \.gitlab/ci/frontend\.gitlab-ci\.yml
  )\z}x => :frontend,

  %r{\A(ee/)?db/(?!fixtures)[^/]+} => :database,
  %r{\A(ee/)?lib/gitlab/(database|background_migration|sql|github_import)(/|\.rb)} => :database,
  %r{\A(app/models/project_authorization|app/services/users/refresh_authorized_projects_service)(/|\.rb)} => :database,
  %r{\Arubocop/cop/migration(/|\.rb)} => :database,

  %r{\A(\.gitlab-ci\.yml\z|\.gitlab\/ci)} => :engineering_productivity,
  %r{Dangerfile\z} => :engineering_productivity,
  %r{\A(ee/)?(danger/|lib/gitlab/danger/)} => :engineering_productivity,
  %r{\A(ee/)?scripts/} => :engineering_productivity,

  %r{\A(ee/)?app/(?!assets|views)[^/]+} => :backend,
  %r{\A(ee/)?(bin|config|generator_templates|lib|rubocop)/} => :backend,
  %r{\A(ee/)?spec/features/} => :test,
  %r{\A(ee/)?spec/(?!javascripts|frontend)[^/]+} => :backend,
  %r{\A(ee/)?vendor/(?!assets)[^/]+} => :backend,
  %r{\A(ee/)?vendor/(languages\.yml|licenses\.csv)\z} => :backend,
  %r{\A(Gemfile|Gemfile.lock|Procfile|Rakefile)\z} => :backend,
  %r{\A[A-Z_]+_VERSION\z} => :backend,
  %r{\A\.rubocop(_todo)?\.yml\z} => :backend,

  %r{\A(ee/)?qa/} => :qa,

  # Files that don't fit into any category are marked with :none
  %r{\A(ee/)?changelogs/} => :none,
  %r{\Alocale/gitlab\.pot\z} => :none,

  # Fallbacks in case the above patterns miss anything
  %r{\.rb\z} => :backend,
  %r{(
    \.(md|txt)\z |
    \.markdownlint\.json
  )}x => :none, # To reinstate roulette for documentation, set to `:docs`.
  %r{\.js\z} => :frontend
}.freeze

Instance Method Summary collapse

Instance Method Details

#all_changed_filesArray<String>

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’

“‘

Returns:

  • (Array<String>)


27
28
29
30
31
32
33
34
35
# File 'lib/gitlab_roulette/danger/helper.rb', line 27

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

#category_for_file(file) ⇒ Object

Determines the category a file is in, e.g., ‘:frontend` or `:backend` @return



78
79
80
81
82
# File 'lib/gitlab_roulette/danger/helper.rb', line 78

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

  category || :unknown
end

#changes_by_categoryHash<String,Array<String>>

Returns:

  • (Hash<String,Array<String>>)


70
71
72
73
74
# File 'lib/gitlab_roulette/danger/helper.rb', line 70

def changes_by_category
  all_changed_files.each_with_object(Hash.new { |h, k| h[k] = [] }) do |file, hash|
    hash[category_for_file(file)] << file
  end
end

#ee?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/gitlab_roulette/danger/helper.rb', line 37

def ee?
  # Support former project name for `dev` and support local Danger run
  %w[gitlab gitlab-ee].include?(ENV['CI_PROJECT_NAME']) || Dir.exist?('../../ee')
end

#gitlab_helperObject



42
43
44
45
46
47
48
49
# File 'lib/gitlab_roulette/danger/helper.rb', line 42

def gitlab_helper
  # Unfortunately the following does not work:
  # - respond_to?(:gitlab)
  # - respond_to?(:gitlab, true)
  gitlab
rescue NoMethodError
  nil
end

#label_for_category(category) ⇒ Object

Returns the GFM for a category label, making its best guess if it’s not a category we know about.

@return



88
89
90
# File 'lib/gitlab_roulette/danger/helper.rb', line 88

def label_for_category(category)
  CATEGORY_LABELS.fetch(category, "~#{category}")
end

#markdown_list(items) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/gitlab_roulette/danger/helper.rb', line 59

def markdown_list(items)
  list = items.map { |item| "* `#{item}`" }.join("\n")

  if items.size > 10
    "\n<details>\n\n#{list}\n\n</details>\n"
  else
    list
  end
end

#missing_database_labels(current_mr_labels) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/gitlab_roulette/danger/helper.rb', line 167

def missing_database_labels(current_mr_labels)
  labels = if has_database_scoped_labels?(current_mr_labels)
             ['database']
           else
             ['database', 'database::review pending']
           end

  labels - current_mr_labels
end

#new_teammates(usernames) ⇒ Object



163
164
165
# File 'lib/gitlab_roulette/danger/helper.rb', line 163

def new_teammates(usernames)
  usernames.map { |u| Gitlab::Danger::Teammate.new('username' => u) }
end

#project_nameObject



55
56
57
# File 'lib/gitlab_roulette/danger/helper.rb', line 55

def project_name
  ENV["CI_PROJECT_NAME"]
end

#release_automation?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/gitlab_roulette/danger/helper.rb', line 51

def release_automation?
  gitlab_helper&.mr_author == RELEASE_TOOLS_BOT
end