Skip to content

Commit

Permalink
Merge pull request #153 from OpenGeoMetadata/repo-warnings
Browse files Browse the repository at this point in the history
Warn if cloning an empty or archived OGM repo
  • Loading branch information
thatbudakguy authored Apr 6, 2023
2 parents 9248ab5 + 0634bad commit 9ea7778
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/geo_combine/harvester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ def pull_all
# If the repository already exists, skip it.
def clone(repo)
repo_path = File.join(@ogm_path, repo)
repo_info = repository_info(repo)

# Skip if exists; warn if archived or empty
if File.directory? repo_path
puts "Skipping clone to #{repo_path}; directory exists"
return 0
end
puts "WARNING: repository '#{repo}' is archived" if repo_info['archived']
puts "WARNING: repository '#{repo}' is empty" if repo_info['size'].zero?

repo_url = "https://github.com/OpenGeoMetadata/#{repo}.git"
Git.clone(repo_url, nil, path: ogm_path, depth: 1)
Expand All @@ -102,5 +107,9 @@ def repositories
.map { |repo| repo['name'] }
.reject { |name| self.class.denylist.include? name }
end

def repository_info(repo_name)
JSON.parse(Net::HTTP.get(URI("https://api.github.com/repos/opengeometadata/#{repo_name}")))
end
end
end
29 changes: 27 additions & 2 deletions spec/lib/geo_combine/harvester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
{ name: 'outdated-institution', size: 100, archived: true }, # archived
{ name: 'aardvark', size: 300 }, # on denylist
{ name: 'empty', size: 0 } # no data
].to_json
]
end

before do
allow(Net::HTTP).to receive(:get).with(described_class.ogm_api_uri).and_return(stub_gh_api)
# stub github API requests
# use the whole org response, or just a portion for particular repos
allow(Net::HTTP).to receive(:get) do |uri|
if uri == described_class.ogm_api_uri
stub_gh_api.to_json
else
repo_name = uri.path.split('/').last.gsub('.git', '')
stub_gh_api.find { |repo| repo[:name] == repo_name }.to_json
end
end

# stub git commands
allow(Git).to receive(:open).and_return(stub_repo)
allow(Git).to receive(:clone).and_return(stub_repo)
allow(stub_repo).to receive(:pull).and_return(stub_repo)
Expand Down Expand Up @@ -96,6 +107,20 @@
harvester.clone(repo_name)
expect(Git).not_to have_received(:clone)
end

it 'warns if a repository is empty' do
allow(Net::HTTP).to receive(:get).with('https://api.github.com/repos/opengeometadata/empty').and_return('{"size": 0}')
expect do
harvester.clone('empty')
end.to output(/repository 'empty' is empty/).to_stdout
end

it 'warns if a repository is archived' do
allow(Net::HTTP).to receive(:get).with('https://api.github.com/repos/opengeometadata/empty').and_return('{"archived": true}')
expect do
harvester.clone('outdated-institution')
end.to output(/repository 'outdated-institution' is archived/).to_stdout
end
end

describe '#clone_all' do
Expand Down

0 comments on commit 9ea7778

Please sign in to comment.