Files
analyzer-pipeline/lib/pipeline/analyzer_repo.rb

71 lines
1.1 KiB
Ruby
Raw Normal View History

2019-08-12 16:46:05 +01:00
require 'pp'
2019-08-08 10:40:29 +01:00
class Pipeline::AnalyzerRepo
BASE_DIR = ENV.fetch("ANALYZER_REPO_BASE_DIR", "./tmp/repos")
attr_reader :repo_url
def initialize(repo_url)
@repo_url = repo_url
puts repo_dir
end
def fetch!
repo.fetch('origin')
end
def checkout(ref)
2019-08-12 16:46:05 +01:00
ref_pointer = repo.checkout(ref)
return ref_pointer.target.target.oid
2019-08-08 10:40:29 +01:00
end
def workdir
repo.workdir
end
def tags
return @tags if @tags
@tags = {}
repo.tags.each do |tag|
@tags[tag.name] = tag.target.oid
end
@tags
end
2019-08-08 10:40:29 +01:00
private
def repo
@repo ||= if repo_dir_exists?
Rugged::Repository.new(repo_dir)
else
Rugged::Repository.clone_at(repo_url, repo_dir)
end
rescue => e
puts "Failed to clone repo #{repo_url}"
puts e.message
raise
end
def main_branch_ref
"origin/master"
end
def repo_dir_exists?
File.directory?(repo_dir)
end
def repo_dir
"#{BASE_DIR}/#{url_hash}-#{local_name}"
end
def url_hash
Digest::SHA1.hexdigest(repo_url)
end
def local_name
repo_url.split("/").last
end
end