Commit d9ef910a authored by charlie ablett's avatar charlie ablett

Add widgitised requirement concern for issues

parent 24efb0b3
# frozen_string_literal: true
module IssueWidgets
module ActsLikeRequirement
extend ActiveSupport::Concern
included do
# This will mean that non-Requirement issues essentially ignore this relationship and always return []
has_many :test_reports, -> { joins(:requirement_issue).where(issues: { issue_type: Issue.issue_types[:requirement] }) },
foreign_key: :issue_id, inverse_of: :requirement_issue, class_name: 'RequirementsManagement::TestReport'
end
end
end
...@@ -18,6 +18,9 @@ module EE ...@@ -18,6 +18,9 @@ module EE
include IterationEventable include IterationEventable
include HealthStatus include HealthStatus
# widget supporting custom issue types - see https://gitlab.com/gitlab-org/gitlab/-/issues/292035
include IssueWidgets::ActsLikeRequirement
scope :order_blocking_issues_desc, -> { reorder(blocking_issues_count: :desc) } scope :order_blocking_issues_desc, -> { reorder(blocking_issues_count: :desc) }
scope :order_weight_desc, -> { reorder ::Gitlab::Database.nulls_last_order('weight', 'DESC') } scope :order_weight_desc, -> { reorder ::Gitlab::Database.nulls_last_order('weight', 'DESC') }
scope :order_weight_asc, -> { reorder ::Gitlab::Database.nulls_last_order('weight') } scope :order_weight_asc, -> { reorder ::Gitlab::Database.nulls_last_order('weight') }
......
...@@ -74,7 +74,7 @@ module RequirementsManagement ...@@ -74,7 +74,7 @@ module RequirementsManagement
end end
def only_requirement_type_issue def only_requirement_type_issue
errors.add(:requirement_issue, 'must be an issue of type `Requirement`') if requirement_issue && !requirement_issue.requirement? errors.add(:requirement_issue, "must be a `requirement`. You cannot associate a Test Report with a #{requirement_issue.issue_type}.") if requirement_issue && !requirement_issue.requirement? && will_save_change_to_issue_id?
end end
end end
end end
...@@ -14,6 +14,28 @@ RSpec.describe Issue do ...@@ -14,6 +14,28 @@ RSpec.describe Issue do
it { is_expected.to have_many(:resource_iteration_events) } it { is_expected.to have_many(:resource_iteration_events) }
it { is_expected.to have_one(:issuable_sla) } it { is_expected.to have_one(:issuable_sla) }
it { is_expected.to have_many(:metric_images) } it { is_expected.to have_many(:metric_images) }
it { is_expected.to have_many(:test_reports) }
context 'for an issue with associated test report' do
let_it_be(:requirement_issue) do
ri = create(:requirement_issue)
create(:test_report, requirement_issue: ri, requirement: nil)
ri
end
context 'for an issue of type Requirement' do
specify { expect(requirement_issue.test_reports.count).to eq(1) }
end
context 'for an issue of a different type' do
before do
requirement_issue.update_attribute(:issue_type, :incident)
end
specify { expect(requirement_issue.test_reports.count).to eq(0) }
end
end
end end
describe 'modules' do describe 'modules' do
......
...@@ -60,13 +60,31 @@ RSpec.describe RequirementsManagement::TestReport do ...@@ -60,13 +60,31 @@ RSpec.describe RequirementsManagement::TestReport do
specify { expect(subject).to be_valid } specify { expect(subject).to be_valid }
end end
context 'when requirement issue is non-requirement issue' do context 'when requirement issue is not of requirement type' do
let(:invalid_issue) { build(:issue) } let(:invalid_issue) { create(:issue) }
let(:requirement_issue_arg) { invalid_issue } let(:requirement_issue_arg) { invalid_issue }
specify do specify do
expect(subject).not_to be_valid expect(subject).not_to be_valid
expect(subject.errors.messages[:requirement_issue]).to include(/must be an issue of type `Requirement`/) expect(subject.errors.messages[:requirement_issue]).to include(/You cannot associate a Test Report with a issue/)
end
context 'when requirement issue is invalid but the type field is not dirty' do
let(:requirement_arg) { nil }
let(:requirement_issue_arg) { requirement_issue }
before do
subject.save!
# simulate the issue type changing in the background, which will be allowed
# the state is technically "invalid" (there are test reports associated with a non-requirement issue)
# but we don't want to prevent updating other fields
requirement_issue.update_attribute(:issue_type, :incident)
end
specify do
expect(subject).to be_valid
end
end end
end end
end end
......
...@@ -54,6 +54,7 @@ issues: ...@@ -54,6 +54,7 @@ issues:
- namespace - namespace
- note_authors - note_authors
- issue_email_participants - issue_email_participants
- test_reports
events: events:
- author - author
- project - project
......
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