From 589f4176f85f8f7488e0de408daf710eae5e38f8 Mon Sep 17 00:00:00 2001 From: Vasilii Iakliushin Date: Thu, 16 Feb 2023 22:44:54 +0100 Subject: [PATCH] Refactor root sha access code Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/391900 **Problem** We raise, handle an exception and report an error to Sentry without a reason. **Solution** Verify if the root commit exists before requesting its sha. Changelog: other --- .../ci_configuration/base_create_service.rb | 13 +++++-------- .../create_service_shared_examples.rb | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/services/security/ci_configuration/base_create_service.rb b/app/services/security/ci_configuration/base_create_service.rb index 3e8865d3dfffde..c5fbabf487c493 100644 --- a/app/services/security/ci_configuration/base_create_service.rb +++ b/app/services/security/ci_configuration/base_create_service.rb @@ -51,7 +51,7 @@ def attributes end def existing_gitlab_ci_content - root_ref = root_ref_sha(project) + root_ref = root_ref_sha(project.repository) return if root_ref.nil? @gitlab_ci_yml ||= project.ci_config_for(root_ref) @@ -82,13 +82,10 @@ def track_event(attributes_for_commit) ) end - def root_ref_sha(project) - project.repository.root_ref_sha - rescue StandardError => e - # this might fail on the very first commit, - # and unfortunately it raises a StandardError - Gitlab::ErrorTracking.track_exception(e, project_id: project.id) - nil + def root_ref_sha(repository) + commit = repository.commit(repository.root_ref) + + commit&.sha end end end diff --git a/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb b/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb index 209be09c807fdd..8bfe57f45496ae 100644 --- a/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb +++ b/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb @@ -145,7 +145,7 @@ let_it_be(:repository) { project.repository } it 'is successful' do - expect(repository).to receive(:root_ref_sha).and_raise(StandardError) + expect(repository).to receive(:commit).and_return(nil) expect(result.status).to eq(:success) end end -- GitLab