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

Avoid nesting cloned OGM repos inside themselves #148

Merged
merged 3 commits into from
Mar 30, 2023
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
28 changes: 18 additions & 10 deletions lib/geo_combine/harvester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,35 @@ def docs_to_index
end
end

# Update a repository via git, or all repositories if none specified.
# If the repository doesn't exist, clone it. Return the count of repositories updated.
def pull(repo = nil)
return repositories.map(&method(:pull)).reduce(:+) unless repo

# Update a repository via git
# If the repository doesn't exist, clone it.
def pull(repo)
repo_path = File.join(@ogm_path, repo)
clone(repo) unless File.directory? repo_path

Git.open(repo_path).pull && 1
end

# Clone a repository via git, or all repositories if none specified.
# If the repository already exists, skip it. Return the count of repositories cloned.
def clone(repo = nil)
return repositories.map(&method(:clone)).reduce(:+) unless repo
# Update all repositories
# Return the count of repositories updated
def pull_all
repositories.map(&method(:pull)).reduce(:+)
end

# Clone a repository via git
# If the repository already exists, skip it.
def clone(repo)
repo_path = File.join(@ogm_path, repo)
return 0 if File.directory? repo_path

repo_url = "https://github.com/OpenGeoMetadata/#{repo}.git"
Git.clone(repo_url, repo, path: repo_path, depth: 1) && 1
Git.clone(repo_url, nil, path: ogm_path, depth: 1) && 1
end

# Clone all repositories via git
# Return the count of repositories cloned.
def clone_all
repositories.map(&method(:clone)).reduce(:+)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/geo_combine.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace :geocombine do
desc 'Clone OpenGeoMetadata repositories'
task :clone, [:repo] do |_t, args|
harvester = GeoCombine::Harvester.new
total = harvester.clone(args.repo)
total = args[:repo] ? harvester.clone(args.repo) : harvester.clone_all
puts "Cloned #{total} repositories"
end

desc '"git pull" OpenGeoMetadata repositories'
task :pull, [:repo] do |_t, args|
harvester = GeoCombine::Harvester.new
total = harvester.pull(args.repo)
total = args[:repo] ? harvester.pull(args.repo) : harvester.pull_all
puts "Updated #{total} repositories"
end

Expand Down
48 changes: 26 additions & 22 deletions spec/lib/geo_combine/harvester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@
expect(stub_repo).to have_received(:pull)
end

it 'clones a repo before pulling if it does not exist' do
harvester.pull(repo_name)
expect(Git).to have_received(:clone)
end
end

describe '#pull_all' do
it 'can pull all repositories' do
harvester.pull
harvester.pull_all
expect(Git).to have_received(:open).exactly(2).times
expect(stub_repo).to have_received(:pull).exactly(2).times
end

it 'skips repositories in the denylist' do
harvester.pull
expect(Git).not_to have_received(:open).with('https://github.com/OpenGeoMetadata/aardvark.git')
end

it 'clones a repo before pulling if it does not exist' do
harvester.pull(repo_name)
expect(Git).to have_received(:clone)
it 'returns the count of repositories pulled' do
expect(harvester.pull_all).to eq(2)
end

it 'returns the count of repositories pulled' do
expect(harvester.pull).to eq(2)
it 'skips repositories in the denylist' do
harvester.pull_all
expect(Git).not_to have_received(:open).with('https://github.com/OpenGeoMetadata/aardvark.git')
end
end

Expand All @@ -75,31 +77,33 @@
harvester.clone(repo_name)
expect(Git).to have_received(:clone).with(
repo_url,
repo_name, {
nil, {
depth: 1, # shallow clone
path: repo_path
path: harvester.ogm_path
}
)
end

it 'skips repositories that already exist' do
allow(File).to receive(:directory?).with(repo_path).and_return(true)
harvester.clone(repo_name)
expect(Git).not_to have_received(:clone)
end
end

describe '#clone_all' do
it 'can clone all repositories' do
harvester.clone
harvester.clone_all
expect(Git).to have_received(:clone).exactly(2).times
end

it 'skips repositories in the denylist' do
harvester.pull
harvester.clone_all
expect(Git).not_to have_received(:clone).with('https://github.com/OpenGeoMetadata/aardvark.git')
end

it 'skips repositories that already exist' do
allow(File).to receive(:directory?).with(repo_path).and_return(true)
harvester.clone(repo_name)
expect(Git).not_to have_received(:clone)
end

it 'returns the count of repositories cloned' do
expect(harvester.clone).to eq(2)
expect(harvester.clone_all).to eq(2)
end
end
end