Commit 4ee55141 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch...

Merge branch '301086-make-danger-fails-when-a-feature-flag-is-removed-and-no-changelog-is-added' into 'master'

danger: Make sure a feature flag removal comes with a changelog [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!53393
parents 6882f4c0 03f7e89e
...@@ -46,7 +46,7 @@ def check_changelog_path(path) ...@@ -46,7 +46,7 @@ def check_changelog_path(path)
ee_changes = helper.all_ee_changes.dup ee_changes = helper.all_ee_changes.dup
ee_changes.delete(path) ee_changes.delete(path)
if ee_changes.any? && !changelog.ee_changelog? && !changelog.db_changes? if ee_changes.any? && !changelog.ee_changelog? && !changelog.required?
warn "This MR has a Changelog file outside `ee/`, but code changes in `ee/`. Consider moving the Changelog file into `ee/`." warn "This MR has a Changelog file outside `ee/`, but code changes in `ee/`. Consider moving the Changelog file into `ee/`."
end end
...@@ -54,7 +54,7 @@ def check_changelog_path(path) ...@@ -54,7 +54,7 @@ def check_changelog_path(path)
warn "This MR has a Changelog file in `ee/`, but no code changes in `ee/`. Consider moving the Changelog file outside `ee/`." warn "This MR has a Changelog file in `ee/`, but no code changes in `ee/`. Consider moving the Changelog file outside `ee/`."
end end
if ee_changes.any? && changelog.ee_changelog? && changelog.db_changes? if ee_changes.any? && changelog.ee_changelog? && changelog.required_reasons.include?(:db_changes)
warn "This MR has a Changelog file inside `ee/`, but there are database changes which [requires](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry) the Changelog placement to be outside of `ee/`. Consider moving the Changelog file outside `ee/`." warn "This MR has a Changelog file inside `ee/`, but there are database changes which [requires](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry) the Changelog placement to be outside of `ee/`. Consider moving the Changelog file outside `ee/`."
end end
end end
...@@ -69,7 +69,7 @@ if changelog_found ...@@ -69,7 +69,7 @@ if changelog_found
check_changelog_yaml(changelog_found) check_changelog_yaml(changelog_found)
check_changelog_path(changelog_found) check_changelog_path(changelog_found)
elsif changelog.required? elsif changelog.required?
fail changelog.required_text changelog.required_texts.each { |_, text| fail(text) } # rubocop:disable Lint/UnreachableLoop
elsif changelog.optional? elsif changelog.optional?
message changelog.optional_text message changelog.optional_text
end end
...@@ -57,8 +57,7 @@ the `author` field. GitLab team members **should not**. ...@@ -57,8 +57,7 @@ the `author` field. GitLab team members **should not**.
- Any change behind an enabled feature flag **should** have a changelog entry. - Any change behind an enabled feature flag **should** have a changelog entry.
- Any change that adds new usage data metrics and changes that needs to be documented in Product Intelligence [Event Dictionary](https://about.gitlab.com/handbook/product/product-intelligence-guide/#event-dictionary) **should** have a changelog entry. - Any change that adds new usage data metrics and changes that needs to be documented in Product Intelligence [Event Dictionary](https://about.gitlab.com/handbook/product/product-intelligence-guide/#event-dictionary) **should** have a changelog entry.
- A change that adds snowplow events **should** have a changelog entry - - A change that adds snowplow events **should** have a changelog entry -
- A change that [removes a feature flag](feature_flags/development.md) **should** have a changelog entry - - A change that [removes a feature flag](feature_flags/development.md) **must** have a changelog entry.
only if the feature flag did not default to true already.
- A fix for a regression introduced and then fixed in the same release (i.e., - A fix for a regression introduced and then fixed in the same release (i.e.,
fixing a bug introduced during a monthly release candidate) **should not** fixing a bug introduced during a monthly release candidate) **should not**
have a changelog entry. have a changelog entry.
......
...@@ -2,50 +2,78 @@ ...@@ -2,50 +2,78 @@
require_relative 'danger_spec_helper' require_relative 'danger_spec_helper'
require_relative '../../../tooling/danger/helper'
require_relative '../../../tooling/danger/changelog' require_relative '../../../tooling/danger/changelog'
RSpec.describe Tooling::Danger::Changelog do RSpec.describe Tooling::Danger::Changelog do
include DangerSpecHelper include DangerSpecHelper
let(:added_files) { nil } let(:change_class) { Tooling::Danger::Helper::Change }
let(:fake_git) { double('fake-git', added_files: added_files) } let(:changes_class) { Tooling::Danger::Helper::Changes }
let(:changes) { changes_class.new([]) }
let(:mr_labels) { nil } let(:mr_labels) { [] }
let(:mr_json) { nil } let(:sanitize_mr_title) { 'Fake Title' }
let(:fake_gitlab) { double('fake-gitlab', mr_labels: mr_labels, mr_json: mr_json) }
let(:changes_by_category) { nil } let(:fake_helper) { double('fake-helper', changes: changes, mr_iid: 1234, mr_title: sanitize_mr_title, mr_labels: mr_labels) }
let(:sanitize_mr_title) { nil }
let(:ee?) { false }
let(:fake_helper) { double('fake-helper', changes_by_category: changes_by_category, sanitize_mr_title: sanitize_mr_title, ee?: ee?) }
let(:fake_danger) { new_fake_danger.include(described_class) } let(:fake_danger) { new_fake_danger.include(described_class) }
subject(:changelog) { fake_danger.new(git: fake_git, gitlab: fake_gitlab, helper: fake_helper) } subject(:changelog) { fake_danger.new(helper: fake_helper) }
describe '#required_reasons' do
subject { changelog.required_reasons }
context "added files contain a migration" do
let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
it { is_expected.to include(:db_changes) }
end
context "removed files contains a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
it { is_expected.to include(:feature_flag_removed) }
end
context "added files do not contain a migration" do
let(:changes) { changes_class.new([change_class.new('foo', :added, :frontend)]) }
it { is_expected.to be_empty }
end
context "removed files do not contain a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :backend)]) }
it { is_expected.to be_empty }
end
end
describe '#required?' do describe '#required?' do
subject { changelog.required? } subject { changelog.required? }
context 'added files contain a migration' do context 'added files contain a migration' do
[ let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
'db/migrate/20200000000000_new_migration.rb',
'db/post_migrate/20200000000000_new_migration.rb'
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end end
context "removed files contains a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
it { is_expected.to be_truthy }
end end
context 'added files do not contain a migration' do context 'added files do not contain a migration' do
[ let(:changes) { changes_class.new([change_class.new('foo', :added, :frontend)]) }
'app/models/model.rb',
'app/assets/javascripts/file.js'
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
end end
context "removed files do not contain a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :backend)]) }
it { is_expected.to be_falsey }
end end
end end
...@@ -58,8 +86,7 @@ RSpec.describe Tooling::Danger::Changelog do ...@@ -58,8 +86,7 @@ RSpec.describe Tooling::Danger::Changelog do
subject { changelog.optional? } subject { changelog.optional? }
context 'when MR contains only categories requiring no changelog' do context 'when MR contains only categories requiring no changelog' do
let(:changes_by_category) { { category_without_changelog => nil } } let(:changes) { changes_class.new([change_class.new('foo', :modified, category_without_changelog)]) }
let(:mr_labels) { [] }
it 'is falsey' do it 'is falsey' do
is_expected.to be_falsy is_expected.to be_falsy
...@@ -67,7 +94,7 @@ RSpec.describe Tooling::Danger::Changelog do ...@@ -67,7 +94,7 @@ RSpec.describe Tooling::Danger::Changelog do
end end
context 'when MR contains a label that require no changelog' do context 'when MR contains a label that require no changelog' do
let(:changes_by_category) { { category_with_changelog => nil } } let(:changes) { changes_class.new([change_class.new('foo', :modified, category_with_changelog)]) }
let(:mr_labels) { [label_with_changelog, label_without_changelog] } let(:mr_labels) { [label_with_changelog, label_without_changelog] }
it 'is falsey' do it 'is falsey' do
...@@ -76,29 +103,28 @@ RSpec.describe Tooling::Danger::Changelog do ...@@ -76,29 +103,28 @@ RSpec.describe Tooling::Danger::Changelog do
end end
context 'when MR contains a category that require changelog and a category that require no changelog' do context 'when MR contains a category that require changelog and a category that require no changelog' do
let(:changes_by_category) { { category_with_changelog => nil, category_without_changelog => nil } } let(:changes) { changes_class.new([change_class.new('foo', :modified, category_with_changelog), change_class.new('foo', :modified, category_without_changelog)]) }
let(:mr_labels) { [] }
it 'is truthy' do context 'with no labels' do
is_expected.to be_truthy it 'is truthy' do
is_expected.to be_truthy
end
end end
end
context 'when MR contains a category that require changelog and a category that require no changelog with changelog label' do context 'with changelog label' do
let(:changes_by_category) { { category_with_changelog => nil, category_without_changelog => nil } } let(:mr_labels) { ['feature'] }
let(:mr_labels) { ['feature'] }
it 'is truthy' do it 'is truthy' do
is_expected.to be_truthy is_expected.to be_truthy
end
end end
end
context 'when MR contains a category that require changelog and a category that require no changelog with no changelog label' do context 'with no changelog label' do
let(:changes_by_category) { { category_with_changelog => nil, category_without_changelog => nil } } let(:mr_labels) { ['tooling'] }
let(:mr_labels) { ['tooling'] }
it 'is truthy' do it 'is truthy' do
is_expected.to be_falsey is_expected.to be_falsey
end
end end
end end
end end
...@@ -107,50 +133,35 @@ RSpec.describe Tooling::Danger::Changelog do ...@@ -107,50 +133,35 @@ RSpec.describe Tooling::Danger::Changelog do
subject { changelog.found } subject { changelog.found }
context 'added files contain a changelog' do context 'added files contain a changelog' do
[ let(:changes) { changes_class.new([change_class.new('foo', :added, :changelog)]) }
'changelogs/unreleased/entry.yml',
'ee/changelogs/unreleased/entry.yml'
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end
end end
context 'added files do not contain a changelog' do context 'added files do not contain a changelog' do
[ let(:changes) { changes_class.new([change_class.new('foo', :added, :backend)]) }
'app/models/model.rb',
'app/assets/javascripts/file.js' it { is_expected.to eq(nil) }
].each do |file_path|
let(:added_files) { [file_path] }
it { is_expected.to eq(nil) }
end
end end
end end
describe '#ee_changelog?' do describe '#ee_changelog?' do
subject { changelog.ee_changelog? } subject { changelog.ee_changelog? }
before do
allow(changelog).to receive(:found).and_return(file_path)
end
context 'is ee changelog' do context 'is ee changelog' do
let(:file_path) { 'ee/changelogs/unreleased/entry.yml' } let(:changes) { changes_class.new([change_class.new('ee/changelogs/unreleased/entry.yml', :added, :changelog)]) }
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end end
context 'is not ee changelog' do context 'is not ee changelog' do
let(:file_path) { 'changelogs/unreleased/entry.yml' } let(:changes) { changes_class.new([change_class.new('changelogs/unreleased/entry.yml', :added, :changelog)]) }
it { is_expected.to be_falsy } it { is_expected.to be_falsy }
end end
end end
describe '#modified_text' do describe '#modified_text' do
let(:mr_json) { { "iid" => 1234, "title" => sanitize_mr_title } }
subject { changelog.modified_text } subject { changelog.modified_text }
context "when title is not changed from sanitization", :aggregate_failures do context "when title is not changed from sanitization", :aggregate_failures do
...@@ -174,35 +185,42 @@ RSpec.describe Tooling::Danger::Changelog do ...@@ -174,35 +185,42 @@ RSpec.describe Tooling::Danger::Changelog do
end end
end end
describe '#required_text' do describe '#required_texts' do
let(:mr_json) { { "iid" => 1234, "title" => sanitize_mr_title } } let(:sanitize_mr_title) { 'Fake Title' }
subject { changelog.required_text }
context "when title is not changed from sanitization", :aggregate_failures do subject { changelog.required_texts }
let(:sanitize_mr_title) { 'Fake Title' }
shared_examples 'changelog required text' do |key|
specify do specify do
expect(subject).to include('CHANGELOG missing') expect(subject).to have_key(key)
expect(subject).to include('bin/changelog -m 1234 "Fake Title"') expect(subject[key]).to include('CHANGELOG missing')
expect(subject).not_to include('--ee') expect(subject[key]).to include('bin/changelog -m 1234 "Fake Title"')
expect(subject[key]).not_to include('--ee')
end end
end end
context "when title needs sanitization", :aggregate_failures do context 'with a new migration file' do
let(:sanitize_mr_title) { 'DRAFT: Fake Title' } let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
specify do context "when title is not changed from sanitization", :aggregate_failures do
expect(subject).to include('CHANGELOG missing') it_behaves_like 'changelog required text', :db_changes
expect(subject).to include('bin/changelog -m 1234 "Fake Title"') end
expect(subject).not_to include('--ee')
context "when title needs sanitization", :aggregate_failures do
let(:sanitize_mr_title) { 'DRAFT: Fake Title' }
it_behaves_like 'changelog required text', :db_changes
end end
end end
context 'with a removed feature flag file' do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
it_behaves_like 'changelog required text', :feature_flag_removed
end
end end
describe '#optional_text' do describe '#optional_text' do
let(:mr_json) { { "iid" => 1234, "title" => sanitize_mr_title } }
subject { changelog.optional_text } subject { changelog.optional_text }
context "when title is not changed from sanitization", :aggregate_failures do context "when title is not changed from sanitization", :aggregate_failures do
......
...@@ -10,13 +10,27 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -10,13 +10,27 @@ RSpec.describe Tooling::Danger::Helper do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
include DangerSpecHelper include DangerSpecHelper
let(:fake_git) { double('fake-git') }
let(:mr_author) { nil } let(:mr_author) { nil }
let(:fake_gitlab) { double('fake-gitlab', mr_author: mr_author) } let(:fake_gitlab) { double('fake-gitlab', mr_author: mr_author) }
let(:fake_danger) { new_fake_danger.include(described_class) } let(:fake_danger) { new_fake_danger.include(described_class) }
let(:added_files) { %w[added1] }
let(:modified_files) { %w[modified1] }
let(:deleted_files) { %w[deleted1] }
let(:renamed_before_file) { 'renamed_before' }
let(:renamed_after_file) { 'renamed_after' }
let(:renamed_files) { [{ before: renamed_before_file, after: renamed_after_file }] }
let(:fake_git) { double('fake-git') }
before do
allow(fake_git).to receive(:added_files) { added_files }
allow(fake_git).to receive(:modified_files) { modified_files }
allow(fake_git).to receive(:deleted_files) { deleted_files }
allow(fake_git).to receive(:renamed_files).at_least(:twice) { renamed_files }
end
subject(:helper) { fake_danger.new(git: fake_git, gitlab: fake_gitlab) } subject(:helper) { fake_danger.new(git: fake_git, gitlab: fake_gitlab) }
describe '#gitlab_helper' do describe '#gitlab_helper' do
...@@ -191,15 +205,16 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -191,15 +205,16 @@ RSpec.describe Tooling::Danger::Helper do
end end
describe '#changes_by_category' do describe '#changes_by_category' do
it 'categorizes changed files' do let(:added_files) { %w[foo foo.md foo.rb foo.js] }
expect(fake_git).to receive(:added_files) { %w[foo foo.md foo.rb foo.js db/migrate/foo lib/gitlab/database/foo.rb qa/foo ee/changelogs/foo.yml] } let(:modified_files) { %w[db/migrate/foo lib/gitlab/database/foo.rb] }
allow(fake_git).to receive(:modified_files) { [] } let(:renamed_files) { [{ before: '', after: 'qa/foo' }, { before: '', after: 'ee/changelogs/foo.yml' }] }
allow(fake_git).to receive(:renamed_files) { [] }
it 'categorizes changed files' do
expect(helper.changes_by_category).to eq( expect(helper.changes_by_category).to eq(
backend: %w[foo.rb], backend: %w[foo.rb],
database: %w[db/migrate/foo lib/gitlab/database/foo.rb], database: %w[db/migrate/foo lib/gitlab/database/foo.rb],
frontend: %w[foo.js], frontend: %w[foo.js],
migration: %w[db/migrate/foo],
none: %w[ee/changelogs/foo.yml foo.md], none: %w[ee/changelogs/foo.yml foo.md],
qa: %w[qa/foo], qa: %w[qa/foo],
unknown: %w[foo] unknown: %w[foo]
...@@ -207,6 +222,62 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -207,6 +222,62 @@ RSpec.describe Tooling::Danger::Helper do
end end
end end
describe 'Tooling::Danger::Helper::Changes', :aggregate_failures do
let(:added_files) { %w[db/migrate/foo ee/changelogs/unreleased/foo.yml] }
describe '#has_category?' do
it 'returns true when changes include given category, false otherwise' do
changes = helper.changes
expect(changes.has_category?(:migration)).to eq(true)
expect(changes.has_category?(:changelog)).to eq(true)
expect(changes.has_category?(:backend)).to eq(false)
end
end
describe '#by_category' do
it 'returns an array of Change objects' do
expect(helper.changes.by_category(:migration)).to all(be_an(described_class::Change))
end
it 'returns an array of Change objects with the given category' do
changes = helper.changes
expect(changes.by_category(:migration).files).to eq(['db/migrate/foo'])
expect(changes.by_category(:changelog).files).to eq(['ee/changelogs/unreleased/foo.yml'])
expect(changes.by_category(:backend)).to be_empty
end
end
describe '#categories' do
it 'returns an array of category symbols' do
expect(helper.changes.categories).to contain_exactly(:database, :migration, :changelog, :unknown)
end
end
describe '#files' do
it 'returns an array of files' do
expect(helper.changes.files).to include(*added_files)
end
end
end
describe '#changes' do
it 'returns an array of Change objects' do
expect(helper.changes).to all(be_an(described_class::Change))
end
it 'groups changes by change type' do
changes = helper.changes
expect(changes.added.files).to eq(added_files)
expect(changes.modified.files).to eq(modified_files)
expect(changes.deleted.files).to eq(deleted_files)
expect(changes.renamed_before.files).to eq([renamed_before_file])
expect(changes.renamed_after.files).to eq([renamed_after_file])
end
end
describe '#categories_for_file' do describe '#categories_for_file' do
before do before do
allow(fake_git).to receive(:diff_for_file).with('usage_data.rb') { double(:diff, patch: "+ count(User.active)") } allow(fake_git).to receive(:diff_for_file).with('usage_data.rb') { double(:diff, patch: "+ count(User.active)") }
...@@ -304,12 +375,10 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -304,12 +375,10 @@ RSpec.describe Tooling::Danger::Helper do
'db/schema.rb' | [:database] 'db/schema.rb' | [:database]
'db/structure.sql' | [:database] 'db/structure.sql' | [:database]
'db/migrate/foo' | [:database] 'db/migrate/foo' | [:database, :migration]
'db/post_migrate/foo' | [:database] 'db/post_migrate/foo' | [:database, :migration]
'ee/db/migrate/foo' | [:database] 'ee/db/geo/migrate/foo' | [:database, :migration]
'ee/db/post_migrate/foo' | [:database] 'ee/db/geo/post_migrate/foo' | [:database, :migration]
'ee/db/geo/migrate/foo' | [:database]
'ee/db/geo/post_migrate/foo' | [:database]
'app/models/project_authorization.rb' | [:database] 'app/models/project_authorization.rb' | [:database]
'app/services/users/refresh_authorized_projects_service.rb' | [:database] 'app/services/users/refresh_authorized_projects_service.rb' | [:database]
'lib/gitlab/background_migration.rb' | [:database] 'lib/gitlab/background_migration.rb' | [:database]
...@@ -400,6 +469,22 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -400,6 +469,22 @@ RSpec.describe Tooling::Danger::Helper do
end end
end end
describe '#mr_iid' do
it 'returns "" when `gitlab_helper` is unavailable' do
expect(helper).to receive(:gitlab_helper).and_return(nil)
expect(helper.mr_iid).to eq('')
end
it 'returns the MR IID when `gitlab_helper` is available' do
mr_iid = '1234'
expect(fake_gitlab).to receive(:mr_json)
.and_return('iid' => mr_iid)
expect(helper.mr_iid).to eq(mr_iid)
end
end
describe '#mr_title' do describe '#mr_title' do
it 'returns "" when `gitlab_helper` is unavailable' do it 'returns "" when `gitlab_helper` is unavailable' do
expect(helper).to receive(:gitlab_helper).and_return(nil) expect(helper).to receive(:gitlab_helper).and_return(nil)
...@@ -432,6 +517,22 @@ RSpec.describe Tooling::Danger::Helper do ...@@ -432,6 +517,22 @@ RSpec.describe Tooling::Danger::Helper do
end end
end end
describe '#mr_labels' do
it 'returns "" when `gitlab_helper` is unavailable' do
expect(helper).to receive(:gitlab_helper).and_return(nil)
expect(helper.mr_labels).to eq([])
end
it 'returns the MR labels when `gitlab_helper` is available' do
mr_labels = %w[foo bar baz]
expect(fake_gitlab).to receive(:mr_labels)
.and_return(mr_labels)
expect(helper.mr_labels).to eq(mr_labels)
end
end
describe '#mr_target_branch' do describe '#mr_target_branch' do
it 'returns "" when `gitlab_helper` is unavailable' do it 'returns "" when `gitlab_helper` is unavailable' do
expect(helper).to receive(:gitlab_helper).and_return(nil) expect(helper).to receive(:gitlab_helper).and_return(nil)
......
...@@ -30,25 +30,35 @@ module Tooling ...@@ -30,25 +30,35 @@ module Tooling
If this merge request [doesn't need a CHANGELOG entry](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry), feel free to ignore this message. If this merge request [doesn't need a CHANGELOG entry](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry), feel free to ignore this message.
MSG MSG
REQUIRED_CHANGELOG_REASONS = {
db_changes: 'introduces a database migration',
feature_flag_removed: 'removes a feature flag'
}.freeze
REQUIRED_CHANGELOG_MESSAGE = <<~MSG REQUIRED_CHANGELOG_MESSAGE = <<~MSG
To create a changelog entry, run the following: To create a changelog entry, run the following:
#{CREATE_CHANGELOG_COMMAND} #{CREATE_CHANGELOG_COMMAND}
This merge request requires a changelog entry because it [introduces a database migration](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry). This merge request requires a changelog entry because it [%<reason>s](https://docs.gitlab.com/ee/development/changelog.html#what-warrants-a-changelog-entry).
MSG MSG
def required_reasons
[].tap do |reasons|
reasons << :db_changes if helper.changes.added.has_category?(:migration)
reasons << :feature_flag_removed if helper.changes.deleted.has_category?(:feature_flag)
end
end
def required? def required?
git.added_files.any? { |path| path =~ %r{\Adb/(migrate|post_migrate)/} } required_reasons.any?
end end
alias_method :db_changes?, :required?
def optional? def optional?
categories_need_changelog? && without_no_changelog_label? categories_need_changelog? && without_no_changelog_label?
end end
def found def found
@found ||= git.added_files.find { |path| path =~ %r{\A(ee/)?(changelogs/unreleased)(-ee)?/} } @found ||= helper.changes.added.by_category(:changelog).files.first
end end
def ee_changelog? def ee_changelog?
...@@ -57,35 +67,34 @@ module Tooling ...@@ -57,35 +67,34 @@ module Tooling
def modified_text def modified_text
CHANGELOG_MODIFIED_URL_TEXT + CHANGELOG_MODIFIED_URL_TEXT +
format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title) format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: helper.mr_iid, mr_title: sanitized_mr_title)
end end
def required_text def required_texts
CHANGELOG_MISSING_URL_TEXT + required_reasons.each_with_object({}) do |required_reason, memo|
format(REQUIRED_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title) memo[required_reason] =
CHANGELOG_MISSING_URL_TEXT +
format(REQUIRED_CHANGELOG_MESSAGE, reason: REQUIRED_CHANGELOG_REASONS.fetch(required_reason), mr_iid: helper.mr_iid, mr_title: sanitized_mr_title)
end
end end
def optional_text def optional_text
CHANGELOG_MISSING_URL_TEXT + CHANGELOG_MISSING_URL_TEXT +
format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: mr_iid, mr_title: sanitized_mr_title) format(OPTIONAL_CHANGELOG_MESSAGE, mr_iid: helper.mr_iid, mr_title: sanitized_mr_title)
end end
private private
def mr_iid
gitlab.mr_json["iid"]
end
def sanitized_mr_title def sanitized_mr_title
TitleLinting.sanitize_mr_title(gitlab.mr_json["title"]) TitleLinting.sanitize_mr_title(helper.mr_title)
end end
def categories_need_changelog? def categories_need_changelog?
(helper.changes_by_category.keys - NO_CHANGELOG_CATEGORIES).any? (helper.changes.categories - NO_CHANGELOG_CATEGORIES).any?
end end
def without_no_changelog_label? def without_no_changelog_label?
(gitlab.mr_labels & NO_CHANGELOG_LABELS).empty? (helper.mr_labels & NO_CHANGELOG_LABELS).empty?
end end
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
require 'delegate'
require_relative 'teammate' require_relative 'teammate'
require_relative 'title_linting' require_relative 'title_linting'
...@@ -86,13 +88,84 @@ module Tooling ...@@ -86,13 +88,84 @@ module Tooling
end end
end end
# @return [Hash<String,Array<String>>] Change = Struct.new(:file, :change_type, :category)
class Changes < ::SimpleDelegator
def added
select_by_change_type(:added)
end
def modified
select_by_change_type(:modified)
end
def deleted
select_by_change_type(:deleted)
end
def renamed_before
select_by_change_type(:renamed_before)
end
def renamed_after
select_by_change_type(:renamed_after)
end
def has_category?(category)
any? { |change| change.category == category }
end
def by_category(category)
Changes.new(select { |change| change.category == category })
end
def categories
map(&:category).uniq
end
def files
map(&:file)
end
private
def select_by_change_type(change_type)
Changes.new(select { |change| change.change_type == change_type })
end
end
# @return [Hash<Symbol,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|
categories_for_file(file).each { |category| hash[category] << file } categories_for_file(file).each { |category| hash[category] << file }
end end
end end
# @return [Changes]
def changes
Changes.new([]).tap do |changes|
git.added_files.each do |file|
categories_for_file(file).each { |category| changes << Change.new(file, :added, category) }
end
git.modified_files.each do |file|
categories_for_file(file).each { |category| changes << Change.new(file, :modified, category) }
end
git.deleted_files.each do |file|
categories_for_file(file).each { |category| changes << Change.new(file, :deleted, category) }
end
git.renamed_files.map { |x| x[:before] }.each do |file|
categories_for_file(file).each { |category| changes << Change.new(file, :renamed_before, category) }
end
git.renamed_files.map { |x| x[:after] }.each do |file|
categories_for_file(file).each { |category| changes << Change.new(file, :renamed_after, category) }
end
end
end
# Determines the categories a file is in, e.g., `[:frontend]`, `[:backend]`, or `%i[frontend engineering_productivity]` # 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. # using filename regex and specific change regex if given.
# #
...@@ -130,6 +203,10 @@ module Tooling ...@@ -130,6 +203,10 @@ module Tooling
CATEGORIES = { CATEGORIES = {
[%r{usage_data\.rb}, %r{^(\+|-).*\s+(count|distinct_count|estimate_batch_distinct_count)\(.*\)(.*)$}] => [:database, :backend], [%r{usage_data\.rb}, %r{^(\+|-).*\s+(count|distinct_count|estimate_batch_distinct_count)\(.*\)(.*)$}] => [:database, :backend],
%r{\A(ee/)?config/feature_flags/} => :feature_flag,
%r{\A(ee/)?(changelogs/unreleased)(-ee)?/} => :changelog,
%r{\Adoc/.*(\.(md|png|gif|jpg))\z} => :docs, %r{\Adoc/.*(\.(md|png|gif|jpg))\z} => :docs,
%r{\A(CONTRIBUTING|LICENSE|MAINTENANCE|PHILOSOPHY|PROCESS|README)(\.md)?\z} => :docs, %r{\A(CONTRIBUTING|LICENSE|MAINTENANCE|PHILOSOPHY|PROCESS|README)(\.md)?\z} => :docs,
...@@ -159,6 +236,7 @@ module Tooling ...@@ -159,6 +236,7 @@ module Tooling
\.gitlab/ci/frontend\.gitlab-ci\.yml \.gitlab/ci/frontend\.gitlab-ci\.yml
)\z}x => %i[frontend engineering_productivity], )\z}x => %i[frontend engineering_productivity],
%r{\A(ee/)?db/(geo/)?(migrate|post_migrate)/} => [:database, :migration],
%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,
...@@ -214,6 +292,12 @@ module Tooling ...@@ -214,6 +292,12 @@ module Tooling
usernames.map { |u| Tooling::Danger::Teammate.new('username' => u) } usernames.map { |u| Tooling::Danger::Teammate.new('username' => u) }
end end
def mr_iid
return '' unless gitlab_helper
gitlab_helper.mr_json['iid']
end
def mr_title def mr_title
return '' unless gitlab_helper return '' unless gitlab_helper
...@@ -226,6 +310,12 @@ module Tooling ...@@ -226,6 +310,12 @@ module Tooling
gitlab_helper.mr_json['web_url'] gitlab_helper.mr_json['web_url']
end end
def mr_labels
return [] unless gitlab_helper
gitlab_helper.mr_labels
end
def mr_target_branch def mr_target_branch
return '' unless gitlab_helper return '' unless gitlab_helper
...@@ -257,10 +347,9 @@ module Tooling ...@@ -257,10 +347,9 @@ module Tooling
end end
def mr_has_labels?(*labels) def mr_has_labels?(*labels)
return false unless gitlab_helper
labels = labels.flatten.uniq labels = labels.flatten.uniq
(labels & gitlab_helper.mr_labels) == labels
(labels & mr_labels) == labels
end end
def labels_list(labels, sep: ', ') def labels_list(labels, sep: ', ')
......
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