Commit 72a518fe authored by Gary Holtz's avatar Gary Holtz

Using a method that was already there to simplify comparison

Also cleaning up the migration stubbed classes per review
parent 9d12db03
......@@ -131,35 +131,10 @@ class ApprovalMergeRequestRule < ApplicationRecord
refresh_license_scanning_approvals(project_approval_rule) if license_scanning?
end
def different_from_project_rule?
return unless approval_project_rule
different_groups? || different_users? || different_name_or_approvals_required? ? true : false
end
private
def different_groups?
group_ids = groups.collect(&:id)
apr_group_ids = approval_project_rule.groups.pluck(:id)
group_ids & apr_group_ids == group_ids
end
def different_users?
user_ids = users.collect(&:id)
apr_user_ids = approval_project_rule.users.pluck(:id)
user_ids & apr_user_ids == user_ids
end
def different_name_or_approvals_required?
true if approvals_required != approval_project_rule.approvals_required || name != approval_project_rule.name
end
def compare_with_project_rule
self.modified_from_project_rule =
different_from_project_rule? ? true : false
self.modified_from_project_rule = overridden? ? true : false
end
def validate_approval_project_rule
......
......@@ -321,7 +321,7 @@ RSpec.describe Gitlab::UsageData do
end
expect(described_class.usage_activity_by_stage_create({})).to include(
approval_project_rules: 8,
approval_project_rules: 10,
approval_project_rules_with_target_branch: 2,
approval_project_rules_with_more_approvers_than_required: 2,
approval_project_rules_with_less_approvers_than_required: 2,
......@@ -342,7 +342,7 @@ RSpec.describe Gitlab::UsageData do
total_number_of_locked_files: 14
)
expect(described_class.usage_activity_by_stage_create(described_class.last_28_days_time_period)).to include(
approval_project_rules: 8,
approval_project_rules: 10,
approval_project_rules_with_target_branch: 2,
approval_project_rules_with_more_approvers_than_required: 2,
approval_project_rules_with_less_approvers_than_required: 2,
......
......@@ -36,32 +36,6 @@ RSpec.describe ApprovalMergeRequestRule do
it 'is valid' do
expect(merge_request_rule).to be_valid
end
context 'the MR rule shows that its modified from the original' do
it 'by having different groups' do
first_group = create(:group)
second_group = create(:group)
third_group = create(:group)
approval_project_rule.groups = [first_group, second_group]
approval_project_rule.save!
merge_request_rule.groups = [first_group, third_group]
expect(merge_request_rule.different_from_project_rule?).to be true
end
it 'by having different users' do
first_user = create(:user)
second_user = create(:user)
third_user = create(:user)
approval_project_rule.users = [first_user, second_user]
approval_project_rule.save!
merge_request_rule.users = [first_user, third_user]
expect(merge_request_rule.different_from_project_rule?).to be true
end
end
end
context 'when the project of approval_project_rule and merge request does not match' do
......
......@@ -4,33 +4,37 @@ module Gitlab
module BackgroundMigration
# Compare all current rules to project rules
class AddModifiedToApprovalMergeRequestRule
# Stubbed class to access the Group table
class Group < ActiveRecord::Base
self.table_name = 'namespaces'
self.inheritance_column = :_type_disabled
end
# Stubbed class to access the ApprovalMergeRequestRule table
class ApprovalMergeRequestRule < ActiveRecord::Base
self.table_name = 'approval_merge_request_rules'
has_one :approval_merge_request_rule_source
has_one :approval_merge_request_rule_source, class_name: 'AddModifiedToApprovalMergeRequestRule::ApprovalMergeRequestRuleSource'
has_one :approval_project_rule, through: :approval_merge_request_rule_source
has_and_belongs_to_many :users
has_and_belongs_to_many :groups,
class_name: 'Group', join_table: "#{self.table_name}_groups"
class_name: 'AddModifiedToApprovalMergeRequestRule::Group', join_table: "#{self.table_name}_groups"
end
# Stubbed class to access the ApprovalProjectRule table
class ApprovalProjectRule < ActiveRecord::Base
self.table_name = 'approval_project_rules'
has_many :approval_merge_request_rule_sources
has_and_belongs_to_many :users
has_many :approval_merge_request_rule_sources, class_name: 'AddModifiedToApprovalMergeRequestRule::ApprovalMergeRequestRuleSource'
has_and_belongs_to_many :groups,
class_name: 'Group', join_table: "#{self.table_name}_groups"
class_name: 'AddModifiedToApprovalMergeRequestRule::Group', join_table: "#{self.table_name}_groups"
end
# Stubbed class to access the ApprovalMergeRequestRuleSource table
class ApprovalMergeRequestRuleSource < ActiveRecord::Base
self.table_name = 'approval_merge_request_rule_sources'
belongs_to :approval_merge_request_rule
belongs_to :approval_project_rule
belongs_to :approval_merge_request_rule, class_name: 'AddModifiedToApprovalMergeRequestRule::ApprovalMergeRequestRule'
belongs_to :approval_project_rule, class_name: 'AddModifiedToApprovalMergeRequestRule::ApprovalProjectRule'
end
def perform(start_id, stop_id)
......
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