From e8bb1262d0e776e4a6d7f67a5bc350983145036b Mon Sep 17 00:00:00 2001
From: Tan Le <tle@gitlab.com>
Date: Mon, 27 Jun 2022 12:36:11 +0000
Subject: [PATCH] Bump shoulda-matchers to v5.1.0

This version enforces stricter validations around inverse relationship
which requires it is to be defined in both direction. Additionally,
`has_many..through` association also validates the declaration order.
---
 Gemfile                                                     | 2 +-
 Gemfile.lock                                                | 6 +++---
 .../incident_management/issuable_escalation_status.rb       | 2 +-
 app/models/project.rb                                       | 1 +
 ee/app/models/ee/iteration.rb                               | 1 +
 lib/gitlab/import_export/project/import_export.yml          | 2 ++
 spec/lib/gitlab/import_export/all_models.yml                | 1 +
 .../incident_management/issuable_escalation_status_spec.rb  | 4 +++-
 spec/models/project_spec.rb                                 | 1 +
 .../models/concerns/timebox_shared_examples.rb              | 2 +-
 10 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/Gemfile b/Gemfile
index 8ca232fc1e624..c554d509d0efb 100644
--- a/Gemfile
+++ b/Gemfile
@@ -436,7 +436,7 @@ group :test do
   gem 'capybara-screenshot', '~> 1.0.22'
   gem 'selenium-webdriver', '~> 3.142'
 
-  gem 'shoulda-matchers', '~> 4.0.1', require: false
+  gem 'shoulda-matchers', '~> 5.1.0', require: false
   gem 'email_spec', '~> 2.2.0'
   gem 'webmock', '~> 3.9.1'
   gem 'rails-controller-testing'
diff --git a/Gemfile.lock b/Gemfile.lock
index 2c6a35fe072d5..dd2b7196115a3 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1225,8 +1225,8 @@ GEM
     settingslogic (2.0.9)
     sexp_processor (4.15.1)
     shellany (0.0.1)
-    shoulda-matchers (4.0.1)
-      activesupport (>= 4.2.0)
+    shoulda-matchers (5.1.0)
+      activesupport (>= 5.2.0)
     sidekiq (6.4.0)
       connection_pool (>= 2.2.2)
       rack (~> 2.0)
@@ -1690,7 +1690,7 @@ DEPENDENCIES
   sentry-ruby (~> 5.1.1)
   sentry-sidekiq (~> 5.1.1)
   settingslogic (~> 2.0.9)
-  shoulda-matchers (~> 4.0.1)
+  shoulda-matchers (~> 5.1.0)
   sidekiq (~> 6.4)
   sidekiq-cron (~> 1.2)
   sigdump (~> 0.2.4)
diff --git a/app/models/incident_management/issuable_escalation_status.rb b/app/models/incident_management/issuable_escalation_status.rb
index fc881e62efdbb..3c581f0489add 100644
--- a/app/models/incident_management/issuable_escalation_status.rb
+++ b/app/models/incident_management/issuable_escalation_status.rb
@@ -7,7 +7,7 @@ class IssuableEscalationStatus < ApplicationRecord
     self.table_name = 'incident_management_issuable_escalation_statuses'
 
     belongs_to :issue
-    has_one :project, through: :issue, inverse_of: :incident_management_issuable_escalation_status
+    has_one :project, through: :issue, inverse_of: :incident_management_issuable_escalation_statuses
 
     validates :issue, presence: true, uniqueness: true
 
diff --git a/app/models/project.rb b/app/models/project.rb
index ea227743855dc..110f400d3ece3 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -260,6 +260,7 @@ def self.integration_association_name(name)
   has_many :merge_request_metrics, foreign_key: 'target_project', class_name: 'MergeRequest::Metrics', inverse_of: :target_project
   has_many :source_of_merge_requests, foreign_key: 'source_project_id', class_name: 'MergeRequest'
   has_many :issues
+  has_many :incident_management_issuable_escalation_statuses, through: :issues, inverse_of: :project, class_name: 'IncidentManagement::IssuableEscalationStatus'
   has_many :labels, class_name: 'ProjectLabel'
   has_many :integrations
   has_many :events
diff --git a/ee/app/models/ee/iteration.rb b/ee/app/models/ee/iteration.rb
index 0501b4b16cf77..c493b7ec8f007 100644
--- a/ee/app/models/ee/iteration.rb
+++ b/ee/app/models/ee/iteration.rb
@@ -37,6 +37,7 @@ def self.by_id(id)
       belongs_to :iterations_cadence, class_name: '::Iterations::Cadence', foreign_key: :iterations_cadence_id, inverse_of: :iterations
 
       has_many :issues, foreign_key: 'sprint_id'
+      has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
       has_many :merge_requests, foreign_key: 'sprint_id'
 
       has_internal_id :iid, scope: :project
diff --git a/lib/gitlab/import_export/project/import_export.yml b/lib/gitlab/import_export/project/import_export.yml
index 5a1787218f540..50ff6146174fb 100644
--- a/lib/gitlab/import_export/project/import_export.yml
+++ b/lib/gitlab/import_export/project/import_export.yml
@@ -761,6 +761,7 @@ excluded_attributes:
     - :exported_protected_branches
     - :repository_size_limit
     - :external_webhook_token
+    - :incident_management_issuable_escalation_statuses
   namespaces:
     - :runners_token
     - :runners_token_encrypted
@@ -819,6 +820,7 @@ excluded_attributes:
     - :upvotes_count
     - :work_item_type_id
     - :email_message_id
+    - :incident_management_issuable_escalation_status
   merge_request: &merge_request_excluded_definition
     - :milestone_id
     - :sprint_id
diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml
index 926347772514e..6a49e47e7614b 100644
--- a/spec/lib/gitlab/import_export/all_models.yml
+++ b/spec/lib/gitlab/import_export/all_models.yml
@@ -603,6 +603,7 @@ project:
 - incident_management_oncall_schedules
 - incident_management_oncall_rotations
 - incident_management_escalation_policies
+- incident_management_issuable_escalation_statuses
 - debian_distributions
 - merge_request_metrics
 - security_orchestration_policy_configuration
diff --git a/spec/models/incident_management/issuable_escalation_status_spec.rb b/spec/models/incident_management/issuable_escalation_status_spec.rb
index f956be3a04e9b..39d1fb325f53c 100644
--- a/spec/models/incident_management/issuable_escalation_status_spec.rb
+++ b/spec/models/incident_management/issuable_escalation_status_spec.rb
@@ -11,7 +11,9 @@
 
   describe 'associations' do
     it { is_expected.to belong_to(:issue) }
-    it { is_expected.to have_one(:project).through(:issue) }
+    it do
+      is_expected.to have_one(:project).through(:issue).inverse_of(:incident_management_issuable_escalation_statuses)
+    end
   end
 
   describe 'validatons' do
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index d2e281234fbb1..80a37ff0d8060 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -27,6 +27,7 @@
     it { is_expected.to have_many(:merge_requests) }
     it { is_expected.to have_many(:merge_request_metrics).class_name('MergeRequest::Metrics') }
     it { is_expected.to have_many(:issues) }
+    it { is_expected.to have_many(:incident_management_issuable_escalation_statuses).through(:issues).inverse_of(:project).class_name('IncidentManagement::IssuableEscalationStatus') }
     it { is_expected.to have_many(:milestones) }
     it { is_expected.to have_many(:iterations) }
     it { is_expected.to have_many(:project_members).dependent(:delete_all) }
diff --git a/spec/support/shared_examples/models/concerns/timebox_shared_examples.rb b/spec/support/shared_examples/models/concerns/timebox_shared_examples.rb
index a2b4cdc33d057..d06e8391a9a07 100644
--- a/spec/support/shared_examples/models/concerns/timebox_shared_examples.rb
+++ b/spec/support/shared_examples/models/concerns/timebox_shared_examples.rb
@@ -82,7 +82,7 @@
     it { is_expected.to belong_to(:group) }
     it { is_expected.to have_many(:issues) }
     it { is_expected.to have_many(:merge_requests) }
-    it { is_expected.to have_many(:labels) }
+    it { is_expected.to have_many(:labels).through(:issues) }
   end
 
   describe '#timebox_name' do
-- 
GitLab