Class: GithubOverlays::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/github_overlays/git.rb

Class Method Summary collapse

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

.configObject



89
90
91
# File 'lib/github_overlays/git.rb', line 89

def self.config
  @@overlay_config ||= Rails.application.config.github_overlays
end

.configureObject

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
  overlay_config = GithubOverlays.configuration

  # Validate required config
  raise 'Configuration github_overlays.basic_auth not set' if (!overlay_config.auth || overlay_config.auth.nil?)

  Github.configure do |github_config|
    github_config.endpoint    = overlay_config.endpoint if overlay_config.endpoint
    github_config.site        = overlay_config.site if overlay_config.site
    github_config.basic_auth  = overlay_config.auth
    github_config.adapter     = :net_http
    github_config.ssl         = {:verify => false}
  end
end

.github_repoObject



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.overlay_directory 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'
      overlay_directory(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.overlay_repo 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'
      overlay_directory(entry.path, user, repo, branch)
    end
  end
end

.process_overlaysObject

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.process_overlays
  # 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])

    overlay_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.options[: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