Commit 81d14eac authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Fix PopulateLatestPipelineIds background migration

This change fixes a constant namespace resolution error by using the
absolute constant path for `Gitlab::Git::Repository::NoRepository`
error on rescue block.

Changelog: fixed
EE: true
parent 526e99ae
......@@ -135,6 +135,10 @@ module EE
return unless latest_pipeline_id
[id, DEFAULT_LETTER_GRADE, latest_pipeline_id, quoted_time, quoted_time].join(', ').then { |s| "(#{s})" }
rescue StandardError => e
::Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
nil
end
private
......@@ -199,7 +203,7 @@ module EE
def root_ref
raw_repository&.root_ref
rescue Gitlab::Git::Repository::NoRepository
rescue ::Gitlab::Git::Repository::NoRepository
end
def empty?
......
......@@ -31,6 +31,8 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateLatestPipelineIds do
let!(:project_2) { projects.create!(namespace_id: namespace.id, name: 'Foo 2') }
let!(:project_3) { projects.create!(namespace_id: namespace.id, name: 'Foo 3') }
let!(:project_4) { projects.create!(namespace_id: namespace.id, name: 'Foo 4') }
let!(:project_5) { projects.create!(namespace_id: namespace.id, name: 'Foo 5') }
let!(:project_6) { projects.create!(namespace_id: namespace.id, name: 'Foo 6') }
let!(:project_1_pipeline) { pipelines.create!(project_id: project_1.id, ref: 'master', sha: 'adf43c3a', status: 'success') }
let!(:project_1_latest_pipeline) { pipelines.create!(project_id: project_1.id, ref: 'master', sha: 'adf43c3a', status: 'failed') }
......@@ -51,6 +53,8 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateLatestPipelineIds do
project_settings.create!(project_id: project_2.id, has_vulnerabilities: true)
project_settings.create!(project_id: project_3.id)
project_settings.create!(project_id: project_4.id, has_vulnerabilities: true)
project_settings.create!(project_id: project_5.id, has_vulnerabilities: true)
project_settings.create!(project_id: project_6.id, has_vulnerabilities: true)
# Create security builds
create_security_build_for(project_1_pipeline, file_type: file_types[:sast])
......@@ -63,7 +67,40 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateLatestPipelineIds do
end
describe '#perform' do
subject(:populate_latest_pipeline_ids) { migrator.perform(project_1.id, project_4.id) }
subject(:populate_latest_pipeline_ids) { migrator.perform(project_1.id, project_6.id) }
before do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
# Raise an error while retreiving the `root_ref` for the `project_5`
mock_raw_repository = instance_double(::Gitlab::Git::Repository)
allow(mock_raw_repository).to receive(:root_ref).and_raise(::Gitlab::Git::Repository::NoRepository.new)
raw_repository_call_count = 0
allow_next_instance_of(described_class::Repository) do |repository_instance|
original_raw_repository = repository_instance.method(:raw_repository)
allow(repository_instance).to receive(:raw_repository) do
raw_repository_call_count += 1
raw_repository_call_count == 3 ? mock_raw_repository : original_raw_repository.call
end
end
# Raise a RuntimeError while retreiving the `pipeline_with_reports` for the `project_6`
pipeline_with_reports_call_count = 0
allow_next_found_instance_of(described_class::Project) do |project_instance|
original_pipeline_with_reports = project_instance.method(:pipeline_with_reports)
allow(project_instance).to receive(:pipeline_with_reports) do
pipeline_with_reports_call_count += 1
pipeline_with_reports_call_count == 4 ? raise("Foo") : original_pipeline_with_reports.call
end
end
end
it 'sets the latest_pipeline_id' do
expect { populate_latest_pipeline_ids }.to change { project_4_stats.reload.latest_pipeline_id }.from(nil).to(project_4_pipeline.id)
......@@ -71,6 +108,8 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateLatestPipelineIds do
.and change { vulnerability_statistics.find_by(project_id: project_1.id) }.from(nil)
.and change { vulnerability_statistics.find_by(project_id: project_1.id)&.latest_pipeline_id }.from(nil).to(project_1_latest_pipeline.id)
.and not_change { project_2_stats.reload.latest_pipeline_id }.from(project_2_pipeline.id)
expect(Gitlab::ErrorTracking).to have_received(:track_and_raise_for_dev_exception).once
end
end
......
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