Commit 462ad4e9 authored by charlie ablett's avatar charlie ablett

Merge branch 'add_build_reference' into 'master'

Add build reference to test reports

See merge request gitlab-org/gitlab!33184
parents 04d6297d 7882d7f3
---
title: Added build_id column to requirements_management_test_reports table
merge_request: 33184
author:
type: other
# frozen_string_literal: true
class AddRequirementsBuildReference < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_requirements_management_test_reports_on_build_id'
def up
add_column :requirements_management_test_reports, :build_id, :bigint
add_index :requirements_management_test_reports, :build_id, name: INDEX_NAME # rubocop:disable Migration/AddIndex
with_lock_retries do
add_foreign_key :requirements_management_test_reports, :ci_builds, column: :build_id, on_delete: :nullify # rubocop:disable Migration/AddConcurrentForeignKey
end
end
def down
with_lock_retries do
remove_column :requirements_management_test_reports, :build_id
end
end
end
......@@ -5831,7 +5831,8 @@ CREATE TABLE public.requirements_management_test_reports (
requirement_id bigint NOT NULL,
pipeline_id bigint,
author_id bigint,
state smallint NOT NULL
state smallint NOT NULL,
build_id bigint
);
CREATE SEQUENCE public.requirements_management_test_reports_id_seq
......@@ -10611,6 +10612,8 @@ CREATE UNIQUE INDEX index_repository_languages_on_project_and_languages_id ON pu
CREATE INDEX index_requirements_management_test_reports_on_author_id ON public.requirements_management_test_reports USING btree (author_id);
CREATE INDEX index_requirements_management_test_reports_on_build_id ON public.requirements_management_test_reports USING btree (build_id);
CREATE INDEX index_requirements_management_test_reports_on_pipeline_id ON public.requirements_management_test_reports USING btree (pipeline_id);
CREATE INDEX index_requirements_management_test_reports_on_requirement_id ON public.requirements_management_test_reports USING btree (requirement_id);
......@@ -12660,6 +12663,9 @@ ALTER TABLE ONLY public.approval_merge_request_rule_sources
ALTER TABLE ONLY public.prometheus_alerts
ADD CONSTRAINT fk_rails_e6351447ec FOREIGN KEY (prometheus_metric_id) REFERENCES public.prometheus_metrics(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.requirements_management_test_reports
ADD CONSTRAINT fk_rails_e67d085910 FOREIGN KEY (build_id) REFERENCES public.ci_builds(id) ON DELETE SET NULL;
ALTER TABLE ONLY public.merge_request_metrics
ADD CONSTRAINT fk_rails_e6d7c24d1b FOREIGN KEY (merge_request_id) REFERENCES public.merge_requests(id) ON DELETE CASCADE;
......@@ -14038,6 +14044,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200527092027
20200527094322
20200527095401
20200527135313
20200527151413
20200527152116
20200527152657
......
......@@ -7,9 +7,19 @@ module RequirementsManagement
belongs_to :requirement, inverse_of: :test_reports
belongs_to :author, inverse_of: :test_reports, class_name: 'User'
belongs_to :pipeline, class_name: 'Ci::Pipeline'
belongs_to :build, class_name: 'Ci::Build'
validates :requirement, :state, presence: true
validate :validate_pipeline_reference
enum state: { passed: 1 }
private
def validate_pipeline_reference
if pipeline_id != build&.pipeline_id
errors.add(:build, _('build pipeline reference mismatch'))
end
end
end
end
......@@ -4,7 +4,10 @@ FactoryBot.define do
factory :test_report, class: 'RequirementsManagement::TestReport' do
author
requirement
pipeline factory: :ci_pipeline
build factory: :ci_build
after(:build) do |report|
report.pipeline = report.build&.pipeline
end
state { :passed }
end
end
......@@ -9,6 +9,7 @@ describe RequirementsManagement::TestReport do
it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to belong_to(:requirement) }
it { is_expected.to belong_to(:pipeline) }
it { is_expected.to belong_to(:build) }
end
describe 'validations' do
......@@ -16,5 +17,22 @@ describe RequirementsManagement::TestReport do
it { is_expected.to validate_presence_of(:requirement) }
it { is_expected.to validate_presence_of(:state) }
describe 'pipeline reference' do
it { is_expected.to be_valid }
it 'is valid to if both build and pipeline are nil' do
subject.build = nil
subject.pipeline_id = nil
expect(subject).to be_valid
end
it 'is invalid if build references a different pipeline' do
subject.pipeline_id = nil
expect(subject).to be_invalid
end
end
end
end
......@@ -25813,6 +25813,9 @@ msgstr ""
msgid "branch name"
msgstr ""
msgid "build pipeline reference mismatch"
msgstr ""
msgid "by"
msgstr ""
......
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