Skip to content

Commit

Permalink
better naming, replace filter_map with filter, better tests for .is_s…
Browse files Browse the repository at this point in the history
…hallow_clone?
  • Loading branch information
anmarchenko committed Apr 10, 2024
1 parent 97bcfac commit 2bcb5f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 1 addition & 5 deletions lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ class << self
private

def filter_invalid_commits(commits)
commits.filter_map do |commit|
next unless Utils::Git.valid_commit_sha?(commit)

commit
end
commits.filter { |commit| Utils::Git.valid_commit_sha?(commit) }
end

def exec_git_command(cmd, stdin: nil)
Expand Down
14 changes: 8 additions & 6 deletions lib/datadog/ci/git/tree_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def call(repository_url)

begin
# ask the backend for the list of commits it already has
excluded_commits, included_commits = split_known_commits(repository_url, latest_commits)
known_commits, new_commits = fetch_known_commits_and_split(repository_url, latest_commits)
# if all commits are present in the backend, we don't need to upload anything
if included_commits.empty?
if new_commits.empty?
Datadog.logger.debug("No new commits to upload")
return
end
Expand All @@ -47,7 +47,7 @@ def call(repository_url)
Datadog.logger.debug("Detected shallow clone and unshallowed the repository, repeating commits search")

# re-run the search with the updated commit list after unshallowing
excluded_commits, included_commits = split_known_commits(
known_commits, new_commits = fetch_known_commits_and_split(
repository_url,
LocalRepository.git_commits
)
Expand All @@ -57,13 +57,13 @@ def call(repository_url)
return
end

Datadog.logger.debug { "Uploading packfiles for commits: #{included_commits}" }
Datadog.logger.debug { "Uploading packfiles for commits: #{new_commits}" }
uploader = UploadPackfile.new(
api: api,
head_commit_sha: head_commit,
repository_url: repository_url
)
Packfiles.generate(included_commits: included_commits, excluded_commits: excluded_commits) do |filepath|
Packfiles.generate(included_commits: new_commits, excluded_commits: known_commits) do |filepath|
uploader.call(filepath: filepath)
rescue UploadPackfile::ApiError => e
Datadog.logger.debug("Packfile upload failed with #{e}")
Expand All @@ -73,7 +73,9 @@ def call(repository_url)

private

def split_known_commits(repository_url, latest_commits)
# Split the latest commits list into known and new commits
# based on the backend response provided by /search_commits endpoint
def fetch_known_commits_and_split(repository_url, latest_commits)
Datadog.logger.debug { "Checking the latest commits list with backend: #{latest_commits}" }
backend_commits = SearchCommits.new(api: api).call(repository_url, latest_commits)
latest_commits.partition do |commit|
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/git/tree_uploader.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Datadog

private

def split_known_commits: (String repository_url, Array[String] latest_commits) -> [Array[String], Array[String]]
def fetch_known_commits_and_split: (String repository_url, Array[String] latest_commits) -> [Array[String], Array[String]]
end
end
end
Expand Down
30 changes: 24 additions & 6 deletions spec/datadog/ci/git/local_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ def with_custom_git_environment
end
end

describe ".git_shallow_clone?" do
subject { described_class.git_shallow_clone? }

it { is_expected.to be_falsey }
end

context "with git folder" do
include_context "with git fixture", "gitdir_with_commit"

Expand Down Expand Up @@ -353,4 +347,28 @@ def with_shallow_clone_git_dir
end
end
end

context "with full clone" do
let(:tmpdir) { Dir.mktmpdir }
after { FileUtils.remove_entry(tmpdir) }

before do
# shallow clone datadog-ci-rb repository
`cd #{tmpdir} && git clone https://github.com/DataDog/datadog-ci-rb`
end

def with_full_clone_git_dir
ClimateControl.modify("GIT_DIR" => File.join(tmpdir, "datadog-ci-rb/.git")) do
yield
end
end

describe ".git_shallow_clone?" do
subject do
with_full_clone_git_dir { described_class.git_shallow_clone? }
end

it { is_expected.to be_falsey }
end
end
end

0 comments on commit 2bcb5f0

Please sign in to comment.