Class: GitAuth::GitHubMirror

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gitauth/gh_mirror.rb

Defined Under Namespace

Classes: Project

Constant Summary collapse

VERSION =
[0, 0, 3, 2]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, token) ⇒ GitHubMirror

Returns a new instance of GitHubMirror.



17
18
19
# File 'lib/gitauth/gh_mirror.rb', line 17

def initialize(username, token)
  @api = GitHubApi.new(username, token)
end

Class Method Details

.run(options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gitauth/gh_mirror.rb', line 96

def run(options = {})
  options = GitAuth::Nash.new(options)
  options.user  = `git config --global github.user`.strip  unless options.user?
  options.token = `git config --global github.token`.strip unless options.token?
  logger.info "Preparing to run GitHub mirror for #{options.user}"
  mirror = self.new(options.user, options.token)
  logger.info "Mirroring all repositories"
  mirror.mirror_all
  logger.info "Mirroring user keys"
  mirror.mirror_user_keys
  logger.info "Updating key access to public repositories"
  mirror.update_public_project_authentication
rescue Exception => e
  logger.fatal "Got Exception: #{e.class.name} - #{e.message}"
  e.backtrace.each { |l| logger.fatal "--> #{l}" }
end

.version(include_path = false) ⇒ Object



113
114
115
# File 'lib/gitauth/gh_mirror.rb', line 113

def version(include_path = false)
  VERSION[0, (include_path ? 4 : 3)].join(".")
end

Instance Method Details

#clone!(p) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitauth/gh_mirror.rb', line 40

def clone!(p)
  if repository = GitAuth::Repo.create(p.name, "#{p.name}.git")
    FileUtils.rm_rf(repository.real_path)
    path = repository.real_path
    Dir.chdir(File.dirname(path)) do
      GitAuth.run "git clone --mirror #{p.github_clone_url} #{File.basename(path)}"
    end
    p.repository = repository
  else
    raise "Error creating local mirror of repository '#{p.name}'"
  end
end

#mirror!(p) ⇒ Object



28
29
30
31
# File 'lib/gitauth/gh_mirror.rb', line 28

def mirror!(p)
  mirrored?(p) ? update!(p) : clone!(p) 
  mirror_deploy_keys(p)
end

#mirror_allObject



53
54
55
56
57
58
59
# File 'lib/gitauth/gh_mirror.rb', line 53

def mirror_all
  logger.info "Mirroring all projects"
  projects.each do |project|
    logger.info "Mirroring for #{project.name} (#{project.github_clone_url})"
    mirror!(project)
  end
end

#mirror_deploy_keys(p) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/gitauth/gh_mirror.rb', line 61

def mirror_deploy_keys(p)
  if p.github.private?
    p.github.keys.each do |key|
      u = user_for_key(key)
      p.repository.readable_by(u) 
    end
  end
  GitAuth::Repo.save!
end

#mirror_user_keysObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitauth/gh_mirror.rb', line 71

def mirror_user_keys
  users = []
  @api.keys.each { |key| users << user_for_key(key) }
  users.compact!
  projects.each do |project|
    users.each do |user|
      project.repository.readable_by(user)
    end
  end
  GitAuth::Repo.save!
end

#projectsObject



21
22
23
24
25
26
# File 'lib/gitauth/gh_mirror.rb', line 21

def projects
  @projects ||= @api.repositories.map do |repository|
    local_repo = GitAuth::Repo.get(repository.name)
    Project.new(repository.name, github_url_for_repo(repository), local_repo, repository)
  end
end

#update!(p) ⇒ Object



33
34
35
36
37
38
# File 'lib/gitauth/gh_mirror.rb', line 33

def update!(p)
  return unless p.repository
  Dir.chdir(p.repository.real_path) do
    GitAuth.run "git fetch origin --force"
  end
end

#update_public_project_authenticationObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/gitauth/gh_mirror.rb', line 83

def update_public_project_authentication
  users = GitAuth::User.all.select { |u| u.name =~ /^github-/i }
  projects.each do |project|
    next unless project.github.public?
    users.each do |user|
      project.repository.readable_by(user)
    end
  end
  GitAuth::Repo.save!
end