Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDTEST-126] add different fallbacks for unshallowing remotes #218

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions spec/datadog/ci/git/local_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading