class GithubSnapshot::Repository

Attributes

canonical_name[R]
folder[R]
gh_has_wiki[R]
name[R]
organization[R]
pushed_at[R]
ssh_url[R]
wiki_folder[R]
wiki_ssh_url[R]

Public Class Methods

new(repo, organization) click to toggle source
# File lib/github_snapshot/repository.rb, line 14
def initialize(repo, organization)
  @name           = repo['name']
  @folder         = "#{@name}-GST-#{GithubSnapshot.time_now}.git"
  @wiki_folder    = @folder.gsub('.git', '.wiki.git')
  @ssh_url        = repo['ssh_url']
  @wiki_ssh_url   = @ssh_url.gsub('.git', '.wiki.git')
  @canonical_name = "#{organization.name}/#{@name}"
  @pushed_at      = repo['pushed_at']
  @gh_has_wiki    = repo['has_wiki']
  @organization   = organization
end

Public Instance Methods

backup() click to toggle source
# File lib/github_snapshot/repository.rb, line 30
def backup
  GithubSnapshot.logger.info "#{@canonical_name} - backing up"

  # Go to next repo if the repository is empty
  unless @pushed_at
    GithubSnapshot.logger.info "#{@canonical_name} is empty"
    return nil
  end

  Dir.chdir "#{@organization.name}"
  begin
    clone
    clone_wiki if has_wiki?
    prune_old_backups
  rescue GithubSnapshot::Error
    GithubSnapshot.logger.error "#{@canonical_name} - skipping due to error"
    return nil
  end
  Dir.chdir ".."

  GithubSnapshot.logger.info "#{@canonical_name} - success"
end
has_wiki?() click to toggle source
# File lib/github_snapshot/repository.rb, line 26
def has_wiki?
  Open3.capture3("git ls-remote #{@wiki_ssh_url}")[1].empty? && @gh_has_wiki
end

Private Instance Methods

clone() click to toggle source
# File lib/github_snapshot/repository.rb, line 55
def clone
  GithubSnapshot.logger.info "#{canonical_name} - cloning"

  # Cloning large repos is a very sensitive operation; here we rely on
  # Timeout to make sure the script doesn't hang
  begin
    Timeout::timeout (GithubSnapshot.git_clone_timeout) {
      GithubSnapshot.exec "#{GithubSnapshot.git_clone_cmd} #{@ssh_url} #{@folder}"
    }
  rescue Timeout::Error, Utilities::ExecError => e
    message = e.class == Timeout::Error ? 'timedout' : 'exec error'
    GithubSnapshot.logger.error "Could not clone #{@canonical_name}, #{message}"
    raise GithubSnapshot::Error
  end

  Utilities.tar "#{@folder}", GithubSnapshot.logger
  GithubSnapshot.exec "rm -rf #{@folder}"
end
clone_wiki() click to toggle source
# File lib/github_snapshot/repository.rb, line 74
def clone_wiki
  GithubSnapshot.logger.info "#{@canonical_name} - cloning wiki"
  GithubSnapshot.exec "#{GithubSnapshot.git_clone_cmd} #{@wiki_ssh_url} #{@wiki_folder}"
  Utilities.tar "#{@wiki_folder}", GithubSnapshot.logger
  GithubSnapshot.exec "rm -rf #{@wiki_folder}"
end
prune_old_backups() click to toggle source
# File lib/github_snapshot/repository.rb, line 81
def prune_old_backups
  GithubSnapshot.logger.info "#{@canonical_name} - pruning old backups"
  file_regex  = "#{@name}-GST-*tar.gz"
  zipped_bkps = FileList[file_regex].exclude(/wiki/)
  zipped_bkps.sort[0..-(GithubSnapshot.releases_to_keep + 1)].each do |file|
    File.delete file
  end if zipped_bkps.length > GithubSnapshot.releases_to_keep

  wiki_regex   = "#{@name}-GST-*wiki*"
  zipped_wikis = FileList[wiki_regex]
  zipped_wikis.sort[0..-(GithubSnapshot.releases_to_keep + 1)].each do |file|
    File.delete file
  end if zipped_wikis.length > GithubSnapshot.releases_to_keep
end