Sutto-gitauth-gh 0.0.2.0 → 0.0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/git_hub_api.rb CHANGED
@@ -8,6 +8,8 @@ class GitHubApi
8
8
 
9
9
  class Error < StandardError; end
10
10
 
11
+ class SSHPublicKey < Struct.new(:name, :id, :key); end
12
+
11
13
  base_uri 'https://github.com'
12
14
 
13
15
  attr_accessor :user, :api_key
@@ -36,6 +38,12 @@ class GitHubApi
36
38
  hash_get(results, "repository")
37
39
  end
38
40
 
41
+ def keys
42
+ keys = hash_get(get("/user/keys"), "public_keys")
43
+ return false if keys.nil?
44
+ keys.map { |k| SSHPublicKey.new(k["title"], k["id"], k["key"]) }
45
+ end
46
+
39
47
  def repository(name, user = @user)
40
48
  results = get("repos/show/#{user}/#{name}")
41
49
  repo = hash_get(results, "repository")
@@ -49,8 +57,6 @@ class GitHubApi
49
57
 
50
58
  class Repository
51
59
 
52
- class SSHPublicKey < Struct.new(:name, :id); end
53
-
54
60
  attr_accessor :name, :api, :attributes, :owner
55
61
 
56
62
  def initialize(attributes, api)
@@ -121,7 +127,7 @@ class GitHubApi
121
127
  def keys
122
128
  keys = api.hash_get(api.get("repos/keys/#{@name}"), "public_keys")
123
129
  return false if keys.nil?
124
- keys.map { |k| SSHPublicKey.new(k["title"], k["id"]) }
130
+ keys.map { |k| SSHPublicKey.new(k["title"], k["id"], k["key"]) }
125
131
  end
126
132
 
127
133
  def add_key(title, key)
@@ -131,7 +137,7 @@ class GitHubApi
131
137
  })
132
138
  keys = api.hash_get(result, "public_keys")
133
139
  return false if keys.nil?
134
- keys.map { |k| SSHPublicKey.new(k["title"], k["id"]) }
140
+ keys.map { |k| SSHPublicKey.new(k["title"], k["id"], k["key"]) }
135
141
  end
136
142
 
137
143
  def remove_key(key_id)
@@ -145,7 +151,7 @@ class GitHubApi
145
151
 
146
152
  end
147
153
 
148
- # Methods
154
+ # Helper Methods
149
155
 
150
156
  def get(path, opts = {})
151
157
  self.class.get(full_path_for(path), :query => with_auth(opts))
@@ -1,4 +1,5 @@
1
1
  require 'gitauth'
2
+ require 'digest/sha2'
2
3
 
3
4
  dir = Pathname.new(__FILE__).dirname.dirname
4
5
  $:.unshift(dir) unless $:.include?(dir)
@@ -9,9 +10,9 @@ module GitAuth
9
10
  class GitHubMirror
10
11
  include GitAuth::Loggable
11
12
 
12
- Project = Struct.new(:name, :github_clone_url, :repository)
13
+ Project = Struct.new(:name, :github_clone_url, :repository, :github)
13
14
 
14
- VERSION = [0, 0, 2, 0]
15
+ VERSION = [0, 0, 3, 0]
15
16
 
16
17
  def initialize(username, token)
17
18
  @api = GitHubApi.new(username, token)
@@ -20,12 +21,13 @@ module GitAuth
20
21
  def projects
21
22
  @projects ||= @api.repositories.map do |repository|
22
23
  local_repo = GitAuth::Repo.get(repository.name)
23
- Project.new(repository.name, github_url_for_repo(repository), local_repo)
24
+ Project.new(repository.name, github_url_for_repo(repository), local_repo, repository)
24
25
  end
25
26
  end
26
27
 
27
28
  def mirror!(p)
28
29
  mirrored?(p) ? update!(p) : clone!(p)
30
+ mirror_deploy_keys(p)
29
31
  end
30
32
 
31
33
  def update!(p)
@@ -56,6 +58,39 @@ module GitAuth
56
58
  end
57
59
  end
58
60
 
61
+ def mirror_deploy_keys(p)
62
+ if p.github.private?
63
+ p.github.keys.each do |key|
64
+ u = user_for_key(key)
65
+ p.repository.readable_by(u)
66
+ end
67
+ end
68
+ GitAuth::Repo.save!
69
+ end
70
+
71
+ def mirror_user_keys
72
+ users = []
73
+ @api.keys.each { |key| users << user_for_key(key) }
74
+ users.compact!
75
+ projects.each do |project|
76
+ users.each do |user|
77
+ project.repository.readable_by(user)
78
+ end
79
+ end
80
+ GitAuth::Repo.save!
81
+ end
82
+
83
+ def update_public_project_authentication
84
+ users = GitAuth::User.all.select { |u| u.name =~ /^github-/i }
85
+ projects.each do |project|
86
+ next unless project.repository.public?
87
+ users.each do |user|
88
+ project.repository.readable_by(user)
89
+ end
90
+ end
91
+ GitAuth::Repo.save
92
+ end
93
+
59
94
  class << self
60
95
 
61
96
  def run(options = {})
@@ -64,7 +99,12 @@ module GitAuth
64
99
  options.token = `git config --global github.token`.strip unless options.token?
65
100
  logger.info "Preparing to run GitHub mirror for #{options.user}"
66
101
  mirror = self.new(options.user, options.token)
102
+ logger.info "Mirroring all repositories"
67
103
  mirror.mirror_all
104
+ logger.info "Mirroring user keys"
105
+ mirror.mirror_user_keys
106
+ logger.info "Updating key access to public repositories"
107
+ mirror.update_public_project_authentication
68
108
  rescue Exception => e
69
109
  logger.fatal "Got Exception: #{e.class.name} - #{e.message}"
70
110
  e.backtrace.each { |l| logger.fatal "--> #{l}" }
@@ -87,6 +127,19 @@ module GitAuth
87
127
  repo.repository.present?
88
128
  end
89
129
 
130
+ def user_for_key(k)
131
+ name = "github-#{Digest::SHA256.hexdigest(k.key)[0, 8]}"
132
+ if u = GitAuth::User.get(name)
133
+ u
134
+ else
135
+ if GitAuth::User.create(name, false, k.key)
136
+ GitAuth::User.get(name)
137
+ else
138
+ raise "Unable to create user for key '#{k.key}'"
139
+ end
140
+ end
141
+ end
142
+
90
143
  end
91
144
  end
92
145
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Sutto-gitauth-gh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.0
4
+ version: 0.0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darcy Laycock
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-17 00:00:00 -07:00
12
+ date: 2009-09-18 00:00:00 -07:00
13
13
  default_executable: gitauth-gh
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency