Commit 081f0504 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '323779-requirement-widgetise' into 'master'

Relate Test Reports and Requirement Issues via requirement Issue widget

See merge request gitlab-org/gitlab!63965
parents c28ba2af d9ef910a
# 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
include IterationEventable
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_weight_desc, -> { reorder ::Gitlab::Database.nulls_last_order('weight', 'DESC') }
scope :order_weight_asc, -> { reorder ::Gitlab::Database.nulls_last_order('weight') }
......
......@@ -74,7 +74,7 @@ module RequirementsManagement
end
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
......@@ -14,6 +14,28 @@ RSpec.describe Issue do
it { is_expected.to have_many(:resource_iteration_events) }
it { is_expected.to have_one(:issuable_sla) }
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
describe 'modules' do
......
......@@ -60,13 +60,31 @@ RSpec.describe RequirementsManagement::TestReport do
specify { expect(subject).to be_valid }
end
context 'when requirement issue is non-requirement issue' do
let(:invalid_issue) { build(:issue) }
context 'when requirement issue is not of requirement type' do
let(:invalid_issue) { create(:issue) }
let(:requirement_issue_arg) { invalid_issue }
specify do
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
......
......@@ -54,6 +54,7 @@ issues:
- namespace
- note_authors
- issue_email_participants
- test_reports
events:
- author
- 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