2019-02-05 12:16:18 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative 'teammate'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Danger
|
|
|
|
module Helper
|
2019-04-04 07:26:01 -04:00
|
|
|
RELEASE_TOOLS_BOT = 'gitlab-release-tools-bot'
|
2020-07-16 11:09:38 -04:00
|
|
|
DRAFT_REGEX = /\A*#{Regexp.union(/(?i)(\[WIP\]\s*|WIP:\s*|WIP$)/, /(?i)(\[draft\]|\(draft\)|draft:|draft\s\-\s|draft$)/)}+\s*/i.freeze
|
2019-02-05 12:16:18 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-07-16 08:09:22 -04:00
|
|
|
# Returns a string containing changed lines as git diff
|
|
|
|
#
|
|
|
|
# Considering changing a line in lib/gitlab/usage_data.rb it will return:
|
|
|
|
#
|
|
|
|
# [ "--- a/lib/gitlab/usage_data.rb",
|
|
|
|
# "+++ b/lib/gitlab/usage_data.rb",
|
|
|
|
# "+ # Test change",
|
|
|
|
# "- # Old change" ]
|
|
|
|
def changed_lines(changed_file)
|
2020-09-10 08:08:54 -04:00
|
|
|
diff = git.diff_for_file(changed_file)
|
|
|
|
return [] unless diff
|
|
|
|
|
|
|
|
diff.patch.split("\n").select { |line| %r{^[+-]}.match?(line) }
|
2020-07-16 08:09:22 -04:00
|
|
|
end
|
|
|
|
|
2020-03-23 11:09:36 -04:00
|
|
|
def all_ee_changes
|
|
|
|
all_changed_files.grep(%r{\Aee/})
|
|
|
|
end
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
def ee?
|
2019-09-24 11:06:34 -04:00
|
|
|
# Support former project name for `dev` and support local Danger run
|
2020-07-22 17:09:50 -04:00
|
|
|
%w[gitlab gitlab-ee].include?(ENV['CI_PROJECT_NAME']) || Dir.exist?(File.expand_path('../../../ee', __dir__))
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
2019-09-06 07:21:53 -04:00
|
|
|
def gitlab_helper
|
|
|
|
# Unfortunately the following does not work:
|
|
|
|
# - respond_to?(:gitlab)
|
|
|
|
# - respond_to?(:gitlab, true)
|
|
|
|
gitlab
|
2020-12-01 10:09:28 -05:00
|
|
|
rescue NameError
|
2019-09-06 07:21:53 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-04-04 07:26:01 -04:00
|
|
|
def release_automation?
|
2019-09-06 07:21:53 -04:00
|
|
|
gitlab_helper&.mr_author == RELEASE_TOOLS_BOT
|
2019-04-04 07:26:01 -04:00
|
|
|
end
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
def project_name
|
2019-09-20 23:06:07 -04:00
|
|
|
ee? ? 'gitlab' : 'gitlab-foss'
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
2019-07-19 13:33:48 -04:00
|
|
|
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
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
# @return [Hash<String,Array<String>>]
|
|
|
|
def changes_by_category
|
|
|
|
all_changed_files.each_with_object(Hash.new { |h, k| h[k] = [] }) do |file, hash|
|
2020-07-02 11:09:08 -04:00
|
|
|
categories_for_file(file).each { |category| hash[category] << file }
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-16 08:09:22 -04:00
|
|
|
# Determines the categories a file is in, e.g., `[:frontend]`, `[:backend]`, or `%i[frontend engineering_productivity]`
|
|
|
|
# using filename regex and specific change regex if given.
|
|
|
|
#
|
2020-07-02 11:09:08 -04:00
|
|
|
# @return Array<Symbol>
|
|
|
|
def categories_for_file(file)
|
2020-07-16 08:09:22 -04:00
|
|
|
_, categories = CATEGORIES.find do |key, _|
|
|
|
|
filename_regex, changes_regex = Array(key)
|
|
|
|
|
|
|
|
found = filename_regex.match?(file)
|
|
|
|
found &&= changed_lines(file).any? { |changed_line| changes_regex.match?(changed_line) } if changes_regex
|
|
|
|
|
|
|
|
found
|
|
|
|
end
|
2019-02-05 12:16:18 -05:00
|
|
|
|
2020-07-02 11:09:08 -04:00
|
|
|
Array(categories || :unknown)
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the GFM for a category label, making its best guess if it's not
|
|
|
|
# a category we know about.
|
|
|
|
#
|
|
|
|
# @return[String]
|
|
|
|
def label_for_category(category)
|
|
|
|
CATEGORY_LABELS.fetch(category, "~#{category}")
|
|
|
|
end
|
|
|
|
|
|
|
|
CATEGORY_LABELS = {
|
2019-09-23 02:06:19 -04:00
|
|
|
docs: "~documentation", # Docs are reviewed along DevOps stages, so don't need roulette for now.
|
2019-02-14 11:45:53 -05:00
|
|
|
none: "",
|
2019-05-23 14:07:59 -04:00
|
|
|
qa: "~QA",
|
2019-10-31 08:06:26 -04:00
|
|
|
test: "~test ~Quality for `spec/features/*`",
|
2020-10-13 08:08:41 -04:00
|
|
|
engineering_productivity: '~"Engineering Productivity" for CI, Danger',
|
|
|
|
ci_template: '~"ci::templates"'
|
2019-02-05 12:16:18 -05:00
|
|
|
}.freeze
|
2020-04-02 23:07:58 -04:00
|
|
|
# First-match win, so be sure to put more specific regex at the top...
|
2019-02-05 12:16:18 -05:00
|
|
|
CATEGORIES = {
|
2020-07-20 11:09:10 -04:00
|
|
|
[%r{usage_data\.rb}, %r{^(\+|-).*(count|distinct_count)\(.*\)(.*)$}] => [:database, :backend],
|
2020-07-16 08:09:22 -04:00
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
%r{\Adoc/.*(\.(md|png|gif|jpg))\z} => :docs,
|
2020-07-07 05:08:57 -04:00
|
|
|
%r{\A(CONTRIBUTING|LICENSE|MAINTENANCE|PHILOSOPHY|PROCESS|README)(\.md)?\z} => :docs,
|
2019-02-05 12:16:18 -05:00
|
|
|
|
|
|
|
%r{\A(ee/)?app/(assets|views)/} => :frontend,
|
|
|
|
%r{\A(ee/)?public/} => :frontend,
|
2019-02-21 08:35:46 -05:00
|
|
|
%r{\A(ee/)?spec/(javascripts|frontend)/} => :frontend,
|
2019-02-05 12:16:18 -05:00
|
|
|
%r{\A(ee/)?vendor/assets/} => :frontend,
|
2019-10-31 08:06:26 -04:00
|
|
|
%r{\A(ee/)?scripts/frontend/} => :frontend,
|
2019-03-25 17:16:43 -04:00
|
|
|
%r{(\A|/)(
|
2019-03-25 14:24:24 -04:00
|
|
|
\.babelrc |
|
|
|
|
\.eslintignore |
|
|
|
|
\.eslintrc(\.yml)? |
|
|
|
|
\.nvmrc |
|
|
|
|
\.prettierignore |
|
|
|
|
\.prettierrc |
|
|
|
|
\.scss-lint.yml |
|
|
|
|
\.stylelintrc |
|
2019-05-13 18:16:17 -04:00
|
|
|
\.haml-lint.yml |
|
|
|
|
\.haml-lint_todo.yml |
|
2019-03-25 14:24:24 -04:00
|
|
|
babel\.config\.js |
|
|
|
|
jest\.config\.js |
|
|
|
|
package\.json |
|
2019-09-05 06:02:20 -04:00
|
|
|
yarn\.lock |
|
2020-07-02 11:09:08 -04:00
|
|
|
config/.+\.js
|
2019-03-25 14:24:24 -04:00
|
|
|
)\z}x => :frontend,
|
2019-02-05 12:16:18 -05:00
|
|
|
|
2020-07-02 11:09:08 -04:00
|
|
|
%r{(\A|/)(
|
|
|
|
\.gitlab/ci/frontend\.gitlab-ci\.yml
|
|
|
|
)\z}x => %i[frontend engineering_productivity],
|
|
|
|
|
2019-08-15 13:42:13 -04:00
|
|
|
%r{\A(ee/)?db/(?!fixtures)[^/]+} => :database,
|
2019-07-04 14:47:16 -04:00
|
|
|
%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,
|
2020-02-24 07:09:00 -05:00
|
|
|
%r{\A(ee/)?app/finders/} => :database,
|
2019-07-04 14:47:16 -04:00
|
|
|
%r{\Arubocop/cop/migration(/|\.rb)} => :database,
|
|
|
|
|
2019-10-31 08:06:26 -04:00
|
|
|
%r{\A(\.gitlab-ci\.yml\z|\.gitlab\/ci)} => :engineering_productivity,
|
2020-07-06 17:08:51 -04:00
|
|
|
%r{\A\.codeclimate\.yml\z} => :engineering_productivity,
|
2020-10-30 02:08:54 -04:00
|
|
|
%r{\Alefthook.yml\z} => :engineering_productivity,
|
2020-07-06 17:08:51 -04:00
|
|
|
%r{\A\.editorconfig\z} => :engineering_productivity,
|
2019-10-31 08:06:26 -04:00
|
|
|
%r{Dangerfile\z} => :engineering_productivity,
|
|
|
|
%r{\A(ee/)?(danger/|lib/gitlab/danger/)} => :engineering_productivity,
|
|
|
|
%r{\A(ee/)?scripts/} => :engineering_productivity,
|
2020-07-06 17:08:51 -04:00
|
|
|
%r{\Atooling/} => :engineering_productivity,
|
2020-07-22 11:09:28 -04:00
|
|
|
%r{(CODEOWNERS)} => :engineering_productivity,
|
2020-08-25 11:10:17 -04:00
|
|
|
%r{(tests.yml)} => :engineering_productivity,
|
2019-10-31 08:06:26 -04:00
|
|
|
|
2020-10-13 08:08:41 -04:00
|
|
|
%r{\Alib/gitlab/ci/templates} => :ci_template,
|
|
|
|
|
2020-08-11 02:10:03 -04:00
|
|
|
%r{\A(ee/)?spec/features/} => :test,
|
|
|
|
%r{\A(ee/)?spec/support/shared_examples/features/} => :test,
|
|
|
|
%r{\A(ee/)?spec/support/shared_contexts/features/} => :test,
|
|
|
|
%r{\A(ee/)?spec/support/helpers/features/} => :test,
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
%r{\A(ee/)?app/(?!assets|views)[^/]+} => :backend,
|
2019-10-31 08:06:26 -04:00
|
|
|
%r{\A(ee/)?(bin|config|generator_templates|lib|rubocop)/} => :backend,
|
2020-04-02 23:07:58 -04:00
|
|
|
%r{\A(ee/)?spec/} => :backend,
|
|
|
|
%r{\A(ee/)?vendor/} => :backend,
|
2020-03-20 08:10:03 -04:00
|
|
|
%r{\A(Gemfile|Gemfile.lock|Rakefile)\z} => :backend,
|
2019-02-05 12:16:18 -05:00
|
|
|
%r{\A[A-Z_]+_VERSION\z} => :backend,
|
2020-11-05 13:08:48 -05:00
|
|
|
%r{\A\.rubocop((_manual)?_todo)?\.yml\z} => :backend,
|
2020-06-10 23:08:30 -04:00
|
|
|
%r{\Afile_hooks/} => :backend,
|
2019-02-05 12:16:18 -05:00
|
|
|
|
|
|
|
%r{\A(ee/)?qa/} => :qa,
|
|
|
|
|
2019-02-14 11:45:53 -05:00
|
|
|
# Files that don't fit into any category are marked with :none
|
|
|
|
%r{\A(ee/)?changelogs/} => :none,
|
2019-02-27 05:52:19 -05:00
|
|
|
%r{\Alocale/gitlab\.pot\z} => :none,
|
2020-09-04 08:08:27 -04:00
|
|
|
%r{\Adata/whats_new/} => :none,
|
2019-02-14 11:45:53 -05:00
|
|
|
|
2020-10-20 11:08:57 -04:00
|
|
|
# GraphQL auto generated doc files and schema
|
|
|
|
%r{\Adoc/api/graphql/reference/} => :backend,
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
# Fallbacks in case the above patterns miss anything
|
|
|
|
%r{\.rb\z} => :backend,
|
2019-11-27 07:06:30 -05:00
|
|
|
%r{(
|
|
|
|
\.(md|txt)\z |
|
|
|
|
\.markdownlint\.json
|
|
|
|
)}x => :none, # To reinstate roulette for documentation, set to `:docs`.
|
2019-02-05 12:16:18 -05:00
|
|
|
%r{\.js\z} => :frontend
|
|
|
|
}.freeze
|
2019-05-30 06:50:40 -04:00
|
|
|
|
|
|
|
def new_teammates(usernames)
|
|
|
|
usernames.map { |u| Gitlab::Danger::Teammate.new('username' => u) }
|
|
|
|
end
|
2019-07-19 13:33:48 -04:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
def sanitize_mr_title(title)
|
2020-07-16 11:09:38 -04:00
|
|
|
title.gsub(DRAFT_REGEX, '').gsub(/`/, '\\\`')
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
def draft_mr?
|
|
|
|
return false unless gitlab_helper
|
|
|
|
|
|
|
|
DRAFT_REGEX.match?(gitlab_helper.mr_json['title'])
|
|
|
|
end
|
|
|
|
|
2020-01-03 19:07:49 -05:00
|
|
|
def security_mr?
|
|
|
|
return false unless gitlab_helper
|
|
|
|
|
|
|
|
gitlab_helper.mr_json['web_url'].include?('/gitlab-org/security/')
|
|
|
|
end
|
|
|
|
|
2020-07-02 20:09:23 -04:00
|
|
|
def cherry_pick_mr?
|
|
|
|
return false unless gitlab_helper
|
|
|
|
|
|
|
|
/cherry[\s-]*pick/i.match?(gitlab_helper.mr_json['title'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def stable_branch?
|
|
|
|
return false unless gitlab_helper
|
|
|
|
|
|
|
|
/\A\d+-\d+-stable-ee/i.match?(gitlab_helper.mr_json['target_branch'])
|
|
|
|
end
|
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
def mr_has_labels?(*labels)
|
|
|
|
return false unless gitlab_helper
|
|
|
|
|
|
|
|
labels = labels.flatten.uniq
|
|
|
|
(labels & gitlab_helper.mr_labels) == labels
|
|
|
|
end
|
|
|
|
|
2020-04-28 14:09:35 -04:00
|
|
|
def labels_list(labels, sep: ', ')
|
|
|
|
labels.map { |label| %Q{~"#{label}"} }.join(sep)
|
|
|
|
end
|
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
def prepare_labels_for_mr(labels)
|
|
|
|
return '' unless labels.any?
|
|
|
|
|
2020-04-28 14:09:35 -04:00
|
|
|
"/label #{labels_list(labels, sep: ' ')}"
|
2020-04-23 14:09:46 -04:00
|
|
|
end
|
|
|
|
|
2020-07-28 14:09:36 -04:00
|
|
|
def changed_files(regex)
|
|
|
|
all_changed_files.grep(regex)
|
|
|
|
end
|
|
|
|
|
2019-07-19 13:33:48 -04:00
|
|
|
def has_database_scoped_labels?(current_mr_labels)
|
|
|
|
current_mr_labels.any? { |label| label.start_with?('database::') }
|
|
|
|
end
|
2020-11-30 07:09:21 -05:00
|
|
|
|
|
|
|
def has_ci_changes?
|
|
|
|
changed_files(%r{\A(\.gitlab-ci\.yml|\.gitlab/ci/)}).any?
|
|
|
|
end
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|