Class: Overlay::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/overlay/github.rb

Class Method Summary collapse

Class Method Details

.clone_file(path, repo_config) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/overlay/github.rb', line 95

def self.clone_file path, repo_config
  root_path = repo_config[:root_dest_path].empty? ? "#{Rails.application.root}" : "#{Rails.application.root}/#{repo_config[:root_dest_path]}"
  dynamic_path = path.partition(repo_config[:root_source_path]).last

  file = github_repo.contents.get(repo_config[:user], repo_config[:repo], path, ref: repo_config[:branch]).response.body.content
  File.open("#{root_path}/#{dynamic_path}", "wb") { |f| f.write(Base64.decode64(file)) }
end

.overlay_directory(path, repo_config) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/overlay/github.rb', line 79

def self.overlay_directory path, repo_config
  root_path = repo_config[:root_dest_path].empty? ? "#{Rails.application.root}" : "#{Rails.application.root}/#{repo_config[:root_dest_path]}"
  dynamic_path = path.partition(repo_config[:root_source_path]).last

  FileUtils.mkdir_p "#{root_path}/#{dynamic_path}"
  directory_entries = github_repo.contents.get(repo_config[:user], repo_config[:repo], path, ref: repo_config[:branch]).response.body

  directory_entries.each do |entry|
    if entry.type == 'dir'
      overlay_directory(entry.path, repo_config)
    elsif entry.type == 'file'
      clone_file(entry.path, repo_config)
    end
  end
end

.overlay_repo(repo_config) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/overlay/github.rb', line 56

def self.overlay_repo repo_config
  # Get our root entries
  #
  root = repo_config[:root_source_path] || '/'

  # If we have a root defined, jump right into it
  #
  if (root != '/')
    overlay_directory(root, repo_config)
    return
  end

  root_entries = github_repo.contents.get(repo_config[:user], repo_config[:repo], root, ref: repo_config[: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, repo_config)
    end
  end
end

.process_overlaysObject

Cycle through all configured repositories and overlay



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/overlay/github.rb', line 11

def self.process_overlays
  # This can be called in an application initializer which will
  # load anytime the environment is loaded.  Make sure we are prepared to run
  # this.
  #
  return unless (config.host_port || ENV['SERVER_HOST_PORT'] || defined? Rails::Server)

  # Configure github api
  configure

  Overlay.configuration.repositories.each do |repo_config|
    next unless repo_config.class == GithubRepo

    # 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?)

    repo_config[:branch] ||= 'master'

    register_web_hook(repo_config)

    GithubJob.new.async.perform repo_config
  end
end

.register_web_hook(repo_config) ⇒ Object

Register our listener on the repo



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/overlay/github.rb', line 38

def self.register_web_hook repo_config
  # Make sure our routes are loaded
  Rails.application.reload_routes!

  # Build hook url
  host = config.host_name || ENV['SERVER_HOST_NAME'] || Socket.gethostname
  port = config.host_port || ENV['SERVER_HOST_PORT'] || Rails::Server.new.options[:Port]
  path = Overlay::Engine.routes.url_for({:controller=>"overlay/github", :action=>"update", :only_path => true})
  uri  = ActionDispatch::Http::URL::url_for({:host => host, :port => port, :path => "#{config.relative_root_url}#{path}"})

  # Retrieve current web hooks
  current_hooks = github_repo.hooks.list(repo_config[:user], repo_config[:repo]).response.body
  if current_hooks.find {|hook| hook.config.url == uri}.nil?
    # register hook
    github_repo.hooks.create(repo_config[:user], repo_config[:repo], name: 'web', active: true, config: {:url => uri, :content_type => 'json'})
  end
end