Class: GithubOverlays::Git
- Inherits:
-
Object
- Object
- GithubOverlays::Git
- Defined in:
- lib/github_overlays/git.rb
Class Method Summary collapse
- .clone_file(path, user, repo, branch) ⇒ Object
- .config ⇒ Object
-
.configure ⇒ Object
Configure the github api.
- .github_repo ⇒ Object
- .overlay_directory(path, user, repo, branch) ⇒ Object
- .overlay_repo(user, repo, branch) ⇒ Object
-
.process_overlays ⇒ Object
Cycle through all configured repositories and overlay.
-
.register_web_hook(user, repo) ⇒ Object
Register our listener on the repo.
Class Method Details
.clone_file(path, user, repo, branch) ⇒ Object
78 79 80 81 |
# File 'lib/github_overlays/git.rb', line 78 def self.clone_file path, user, repo, branch file = github_repo.contents.get(user, repo, path, ref: branch).response.body.content File.open("#{Rails.application.root}/#{path}", "wb") { |f| f.write(Base64.decode64(file)) } end |
.config ⇒ Object
89 90 91 |
# File 'lib/github_overlays/git.rb', line 89 def self.config ||= Rails.application.config. end |
.configure ⇒ Object
Configure the github api
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/github_overlays/git.rb', line 94 def self.configure = GithubOverlays.configuration # Validate required config raise 'Configuration github_overlays.basic_auth not set' if (!.auth || .auth.nil?) Github.configure do |github_config| github_config.endpoint = .endpoint if .endpoint github_config.site = .site if .site github_config.basic_auth = .auth github_config.adapter = :net_http github_config.ssl = {:verify => false} end end |
.github_repo ⇒ Object
85 86 87 |
# File 'lib/github_overlays/git.rb', line 85 def self.github_repo @@github ||= Github::Repos.new end |
.overlay_directory(path, user, repo, branch) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/github_overlays/git.rb', line 65 def self. path, user, repo, branch FileUtils.mkdir_p "#{Rails.application.root}/#{path}" directory_entries = github_repo.contents.get(user, repo, path, ref: branch).response.body directory_entries.each do |entry| if entry.type == 'dir' (entry.path, user, repo, branch) elsif entry.type == 'file' clone_file(entry.path, user, repo, branch) end end end |
.overlay_repo(user, repo, branch) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/github_overlays/git.rb', line 51 def self. user, repo, branch # Get our root entries # root_entries = github_repo.contents.get(user, repo, '/', ref: branch).response.body # We aren't pulling anything out of root. Cycle through directories and overlay # root_entries.each do |entry| if entry.type == 'dir' (entry.path, user, repo, branch) end end end |
.process_overlays ⇒ Object
Cycle through all configured repositories and overlay
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/github_overlays/git.rb', line 10 def self. # If we aren't running in Rails, bail out. This may be some # other request such as rake routes loading the environment # return unless defined?(Rails::Server) # Configure github api configure GithubOverlays.configuration.repositories.each do |repo_config| # Validate repository config raise 'Respository config missing user' if (!repo_config[:user] || repo_config[:user].nil?) raise 'Respository config missing repo' if (!repo_config[:repo] || repo_config[:repo].nil?) branch = repo_config[:branch] || 'master' register_web_hook(repo_config[:user], repo_config[:repo]) (repo_config[:user], repo_config[:repo], branch) end end |
.register_web_hook(user, repo) ⇒ Object
Register our listener on the repo
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/github_overlays/git.rb', line 34 def self.register_web_hook user, repo # Make sure our routes are loaded Rails.application.reload_routes! # Build hook url host = GithubOverlays.configuration.hostname || Socket.gethostname port = GithubOverlays.configuration.host_port || Rails::Server.new.[:Port] uri = GithubOverlays::Engine.routes.url_for({:controller=>"github_overlays/webhooks", :action=>"update", :host => host, :port => port}) # Retrieve current web hooks current_hooks = github_repo.hooks.list(user, repo).response.body if current_hooks.find {|hook| hook.config.url == uri}.nil? #register hook github_repo.hooks.create(user, repo, name: 'web', active: true, config: {:url => uri, :content_type => 'json'}) end end |