From c5ac5834763d0f9a37d95611dc7583032852e6ea Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 16 Aug 2024 12:51:03 +0200 Subject: [PATCH] add different fallbacks for unshallowing remotes --- lib/datadog/ci/git/local_repository.rb | 45 ++++++++++++++------ spec/datadog/ci/git/local_repository_spec.rb | 32 ++++++++++++-- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/lib/datadog/ci/git/local_repository.rb b/lib/datadog/ci/git/local_repository.rb index e1a4803b..8c2f1a6e 100644 --- a/lib/datadog/ci/git/local_repository.rb +++ b/lib/datadog/ci/git/local_repository.rb @@ -238,23 +238,42 @@ def self.git_unshallow Telemetry.git_command(Ext::Telemetry::Command::UNSHALLOW) res = nil + unshallow_command = + "git fetch " \ + "--shallow-since=\"1 month ago\" " \ + "--update-shallow " \ + "--filter=\"blob:none\" " \ + "--recurse-submodules=no " \ + "$(git config --default origin --get clone.defaultRemoteName)" + + unshallow_remotes = [ + "$(git rev-parse HEAD)", + "$(git rev-parse --abbrev-ref --symbolic-full-name @{upstream})", + nil + ] + duration_ms = Core::Utils::Time.measure(:float_millisecond) do - res = exec_git_command( - "git fetch " \ - "--shallow-since=\"1 month ago\" " \ - "--update-shallow " \ - "--filter=\"blob:none\" " \ - "--recurse-submodules=no " \ - "$(git config --default origin --get clone.defaultRemoteName) $(git rev-parse HEAD)" - ) + unshallow_remotes.each do |remote| + unshallowing_errored = false + + res = + begin + exec_git_command( + "#{unshallow_command} #{remote}" + ) + rescue => e + log_failure(e, "git unshallow") + telemetry_track_error(e, Ext::Telemetry::Command::UNSHALLOW) + unshallowing_errored = true + nil + end + + break unless unshallowing_errored + end end - Telemetry.git_command_ms(Ext::Telemetry::Command::UNSHALLOW, duration_ms) + Telemetry.git_command_ms(Ext::Telemetry::Command::UNSHALLOW, duration_ms) res - rescue => e - log_failure(e, "git unshallow") - telemetry_track_error(e, Ext::Telemetry::Command::UNSHALLOW) - nil end # makes .exec_git_command private to make sure that this method diff --git a/spec/datadog/ci/git/local_repository_spec.rb b/spec/datadog/ci/git/local_repository_spec.rb index 622774cf..4b7add09 100644 --- a/spec/datadog/ci/git/local_repository_spec.rb +++ b/spec/datadog/ci/git/local_repository_spec.rb @@ -305,10 +305,34 @@ def with_clone_git_dir with_clone_git_dir { described_class.git_commits } end - it "unshallows the repository" do - expect(subject).to be_truthy - # additional commits plus the initial commit - expect(commits.size).to eq(commits_count + 1) + context "successful from the first try" do + before do + expect(Open3).to receive(:capture2e).and_call_original.at_most(2).times + end + + it "unshallows the repository" do + expect(subject).to be_truthy + # additional commits plus the initial commit + expect(commits.size).to eq(commits_count + 1) + end + end + + context "when unshallow command fails" do + before do + allow(Open3).to receive(:capture2e).and_call_original + allow(Open3).to receive(:capture2e) + .with("git fetch --shallow-since=\"1 month ago\" --update-shallow --filter=\"blob:none\" --recurse-submodules=no $(git config --default origin --get clone.defaultRemoteName) $(git rev-parse HEAD)", stdin_data: nil) + .and_return(["error", double(success?: false, to_i: 1)]) + end + + it "still unshallows the repository using the fallback command" do + expect(subject).to be_truthy + # additional commits plus the initial commit + expect(commits.size).to eq(commits_count + 1) + end + + # it signals error to the telemetry + it_behaves_like "emits telemetry metric", :inc, "git.command_errors", 1 end end end