Commit 69778de7 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch '218241-allow-files-to-have-multiple-categories' into 'master'

Allow files to belong to multiple categories in the roulette

Closes #218241

See merge request gitlab-org/gitlab!35660
parents 4eef5e5f 99ba0bc7
...@@ -73,16 +73,16 @@ module Gitlab ...@@ -73,16 +73,16 @@ module Gitlab
# @return [Hash<String,Array<String>>] # @return [Hash<String,Array<String>>]
def changes_by_category def changes_by_category
all_changed_files.each_with_object(Hash.new { |h, k| h[k] = [] }) do |file, hash| all_changed_files.each_with_object(Hash.new { |h, k| h[k] = [] }) do |file, hash|
hash[category_for_file(file)] << file categories_for_file(file).each { |category| hash[category] << file }
end end
end end
# Determines the category a file is in, e.g., `:frontend` or `:backend` # Determines the categories a file is in, e.g., `[:frontend]`, `[:backend]`, or `%i[frontend engineering_productivity]`.
# @return[Symbol] # @return Array<Symbol>
def category_for_file(file) def categories_for_file(file)
_, category = CATEGORIES.find { |regexp, _| regexp.match?(file) } _, categories = CATEGORIES.find { |regexp, _| regexp.match?(file) }
category || :unknown Array(categories || :unknown)
end end
# Returns the GFM for a category label, making its best guess if it's not # Returns the GFM for a category label, making its best guess if it's not
...@@ -125,10 +125,13 @@ module Gitlab ...@@ -125,10 +125,13 @@ module Gitlab
jest\.config\.js | jest\.config\.js |
package\.json | package\.json |
yarn\.lock | yarn\.lock |
config/.+\.js | config/.+\.js
\.gitlab/ci/frontend\.gitlab-ci\.yml
)\z}x => :frontend, )\z}x => :frontend,
%r{(\A|/)(
\.gitlab/ci/frontend\.gitlab-ci\.yml
)\z}x => %i[frontend engineering_productivity],
%r{\A(ee/)?db/(?!fixtures)[^/]+} => :database, %r{\A(ee/)?db/(?!fixtures)[^/]+} => :database,
%r{\A(ee/)?lib/gitlab/(database|background_migration|sql|github_import)(/|\.rb)} => :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{\A(app/models/project_authorization|app/services/users/refresh_authorized_projects_service)(/|\.rb)} => :database,
......
...@@ -165,125 +165,127 @@ RSpec.describe Gitlab::Danger::Helper do ...@@ -165,125 +165,127 @@ RSpec.describe Gitlab::Danger::Helper do
end end
end end
describe '#category_for_file' do describe '#categories_for_file' do
where(:path, :expected_category) do where(:path, :expected_categories) do
'doc/foo' | :none 'doc/foo' | [:none]
'CONTRIBUTING.md' | :none 'CONTRIBUTING.md' | [:none]
'LICENSE' | :none 'LICENSE' | [:none]
'MAINTENANCE.md' | :none 'MAINTENANCE.md' | [:none]
'PHILOSOPHY.md' | :none 'PHILOSOPHY.md' | [:none]
'PROCESS.md' | :none 'PROCESS.md' | [:none]
'README.md' | :none 'README.md' | [:none]
'ee/doc/foo' | :unknown 'ee/doc/foo' | [:unknown]
'ee/README' | :unknown 'ee/README' | [:unknown]
'app/assets/foo' | :frontend 'app/assets/foo' | [:frontend]
'app/views/foo' | :frontend 'app/views/foo' | [:frontend]
'public/foo' | :frontend 'public/foo' | [:frontend]
'scripts/frontend/foo' | :frontend 'scripts/frontend/foo' | [:frontend]
'spec/javascripts/foo' | :frontend 'spec/javascripts/foo' | [:frontend]
'spec/frontend/bar' | :frontend 'spec/frontend/bar' | [:frontend]
'vendor/assets/foo' | :frontend 'vendor/assets/foo' | [:frontend]
'babel.config.js' | :frontend 'babel.config.js' | [:frontend]
'jest.config.js' | :frontend 'jest.config.js' | [:frontend]
'package.json' | :frontend 'package.json' | [:frontend]
'yarn.lock' | :frontend 'yarn.lock' | [:frontend]
'config/foo.js' | :frontend 'config/foo.js' | [:frontend]
'config/deep/foo.js' | :frontend 'config/deep/foo.js' | [:frontend]
'ee/app/assets/foo' | :frontend 'ee/app/assets/foo' | [:frontend]
'ee/app/views/foo' | :frontend 'ee/app/views/foo' | [:frontend]
'ee/spec/javascripts/foo' | :frontend 'ee/spec/javascripts/foo' | [:frontend]
'ee/spec/frontend/bar' | :frontend 'ee/spec/frontend/bar' | [:frontend]
'app/models/foo' | :backend '.gitlab/ci/frontend.gitlab-ci.yml' | %i[frontend engineering_productivity]
'bin/foo' | :backend
'config/foo' | :backend 'app/models/foo' | [:backend]
'lib/foo' | :backend 'bin/foo' | [:backend]
'rubocop/foo' | :backend 'config/foo' | [:backend]
'spec/foo' | :backend 'lib/foo' | [:backend]
'spec/foo/bar' | :backend 'rubocop/foo' | [:backend]
'spec/foo' | [:backend]
'ee/app/foo' | :backend 'spec/foo/bar' | [:backend]
'ee/bin/foo' | :backend
'ee/spec/foo' | :backend 'ee/app/foo' | [:backend]
'ee/spec/foo/bar' | :backend 'ee/bin/foo' | [:backend]
'ee/spec/foo' | [:backend]
'generator_templates/foo' | :backend 'ee/spec/foo/bar' | [:backend]
'vendor/languages.yml' | :backend
'vendor/licenses.csv' | :backend 'generator_templates/foo' | [:backend]
'file_hooks/examples/' | :backend 'vendor/languages.yml' | [:backend]
'vendor/licenses.csv' | [:backend]
'Gemfile' | :backend 'file_hooks/examples/' | [:backend]
'Gemfile.lock' | :backend
'Rakefile' | :backend 'Gemfile' | [:backend]
'FOO_VERSION' | :backend 'Gemfile.lock' | [:backend]
'Rakefile' | [:backend]
'Dangerfile' | :engineering_productivity 'FOO_VERSION' | [:backend]
'danger/commit_messages/Dangerfile' | :engineering_productivity
'ee/danger/commit_messages/Dangerfile' | :engineering_productivity 'Dangerfile' | [:engineering_productivity]
'danger/commit_messages/' | :engineering_productivity 'danger/commit_messages/Dangerfile' | [:engineering_productivity]
'ee/danger/commit_messages/' | :engineering_productivity 'ee/danger/commit_messages/Dangerfile' | [:engineering_productivity]
'.gitlab-ci.yml' | :engineering_productivity 'danger/commit_messages/' | [:engineering_productivity]
'.gitlab/ci/cng.gitlab-ci.yml' | :engineering_productivity 'ee/danger/commit_messages/' | [:engineering_productivity]
'.gitlab/ci/ee-specific-checks.gitlab-ci.yml' | :engineering_productivity '.gitlab-ci.yml' | [:engineering_productivity]
'scripts/foo' | :engineering_productivity '.gitlab/ci/cng.gitlab-ci.yml' | [:engineering_productivity]
'lib/gitlab/danger/foo' | :engineering_productivity '.gitlab/ci/ee-specific-checks.gitlab-ci.yml' | [:engineering_productivity]
'ee/lib/gitlab/danger/foo' | :engineering_productivity 'scripts/foo' | [:engineering_productivity]
'.overcommit.yml.example' | :engineering_productivity 'lib/gitlab/danger/foo' | [:engineering_productivity]
'.editorconfig' | :engineering_productivity 'ee/lib/gitlab/danger/foo' | [:engineering_productivity]
'tooling/overcommit/foo' | :engineering_productivity '.overcommit.yml.example' | [:engineering_productivity]
'.codeclimate.yml' | :engineering_productivity '.editorconfig' | [:engineering_productivity]
'tooling/overcommit/foo' | [:engineering_productivity]
'lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml' | :backend '.codeclimate.yml' | [:engineering_productivity]
'ee/FOO_VERSION' | :unknown 'lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml' | [:backend]
'db/schema.rb' | :database 'ee/FOO_VERSION' | [:unknown]
'db/structure.sql' | :database
'db/migrate/foo' | :database 'db/schema.rb' | [:database]
'db/post_migrate/foo' | :database 'db/structure.sql' | [:database]
'ee/db/migrate/foo' | :database 'db/migrate/foo' | [:database]
'ee/db/post_migrate/foo' | :database 'db/post_migrate/foo' | [:database]
'ee/db/geo/migrate/foo' | :database 'ee/db/migrate/foo' | [:database]
'ee/db/geo/post_migrate/foo' | :database 'ee/db/post_migrate/foo' | [:database]
'app/models/project_authorization.rb' | :database 'ee/db/geo/migrate/foo' | [:database]
'app/services/users/refresh_authorized_projects_service.rb' | :database 'ee/db/geo/post_migrate/foo' | [:database]
'lib/gitlab/background_migration.rb' | :database 'app/models/project_authorization.rb' | [:database]
'lib/gitlab/background_migration/foo' | :database 'app/services/users/refresh_authorized_projects_service.rb' | [:database]
'ee/lib/gitlab/background_migration/foo' | :database 'lib/gitlab/background_migration.rb' | [:database]
'lib/gitlab/database.rb' | :database 'lib/gitlab/background_migration/foo' | [:database]
'lib/gitlab/database/foo' | :database 'ee/lib/gitlab/background_migration/foo' | [:database]
'ee/lib/gitlab/database/foo' | :database 'lib/gitlab/database.rb' | [:database]
'lib/gitlab/github_import.rb' | :database 'lib/gitlab/database/foo' | [:database]
'lib/gitlab/github_import/foo' | :database 'ee/lib/gitlab/database/foo' | [:database]
'lib/gitlab/sql/foo' | :database 'lib/gitlab/github_import.rb' | [:database]
'rubocop/cop/migration/foo' | :database 'lib/gitlab/github_import/foo' | [:database]
'lib/gitlab/sql/foo' | [:database]
'db/fixtures/foo.rb' | :backend 'rubocop/cop/migration/foo' | [:database]
'ee/db/fixtures/foo.rb' | :backend
'db/fixtures/foo.rb' | [:backend]
'qa/foo' | :qa 'ee/db/fixtures/foo.rb' | [:backend]
'ee/qa/foo' | :qa
'qa/foo' | [:qa]
'changelogs/foo' | :none 'ee/qa/foo' | [:qa]
'ee/changelogs/foo' | :none
'locale/gitlab.pot' | :none 'changelogs/foo' | [:none]
'ee/changelogs/foo' | [:none]
'FOO' | :unknown 'locale/gitlab.pot' | [:none]
'foo' | :unknown
'FOO' | [:unknown]
'foo/bar.rb' | :backend 'foo' | [:unknown]
'foo/bar.js' | :frontend
'foo/bar.txt' | :none 'foo/bar.rb' | [:backend]
'foo/bar.md' | :none 'foo/bar.js' | [:frontend]
'foo/bar.txt' | [:none]
'foo/bar.md' | [:none]
end end
with_them do with_them do
subject { helper.category_for_file(path) } subject { helper.categories_for_file(path) }
it { is_expected.to eq(expected_category) } it { is_expected.to eq(expected_categories) }
end end
end end
...@@ -296,6 +298,7 @@ RSpec.describe Gitlab::Danger::Helper do ...@@ -296,6 +298,7 @@ RSpec.describe Gitlab::Danger::Helper do
:frontend | '~frontend' :frontend | '~frontend'
:none | '' :none | ''
:qa | '~QA' :qa | '~QA'
:engineering_productivity | '~"Engineering Productivity" for CI, Danger'
end end
with_them do with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment