class Octokit::ManageGHESClient

ManageGHESClient is only meant to be used by GitHub Enterprise Server (GHES) operators and provides access to the Manage GHES API endpoints.

@see Octokit::Client Use Octokit::Client for regular API use for GitHub

and GitHub Enterprise.

@see developer.github.com/v3/enterprise-admin/manage-ghes/

Client for the Manage GitHub Enterprise Server API

Public Class Methods

new(options = {}) click to toggle source
# File lib/octokit/manage_ghes_client.rb, line 21
def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  # rubocop:disable Style/HashEachMethods
  #
  # This may look like a `.keys.each` which should be replaced with `#each_key`, but
  # this doesn't actually work, since `#keys` is just a method we've defined ourselves.
  # The class doesn't fulfill the whole `Enumerable` contract.
  Octokit::Configurable.keys.each do |key|
    # rubocop:enable Style/HashEachMethods
    instance_variable_set(:"@#{key}", options[key] || Octokit.instance_variable_get(:"@#{key}"))
  end
end

Public Instance Methods

add_authorized_key(key) click to toggle source

Add an authorized SSH keys on the Enterprise install

@param key Either the file path to a key, a File handler to the key, or the contents of the key itself @return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 99
def add_authorized_key(key)
  conn = authenticated_client
  case key
  when String
    if File.exist?(key)
      key = File.open(key, 'r')
      content = key.read.strip
      key.close
    else
      content = key
    end
  when File
    content = key.read.strip
    key.close
  end

  queries = {}
  queries[:key] = content
  @last_response = conn.post('/manage/v1/access/ssh', queries)
end
authorized_keys() click to toggle source
# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 89
def authorized_keys
  conn = authenticated_client
  @last_response = conn.get('/manage/v1/access/ssh')
end
Also aliased as: get_authorized_keys
config_check()
Alias for: config_status
config_status() click to toggle source

Get information about the Enterprise installation

@return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 64
def config_status
  conn = authenticated_client
  @last_response = conn.get('/manage/v1/config/apply')
end
Also aliased as: config_check
delete_authorized_key(key)
edit_settings(settings) click to toggle source

Modify the Enterprise settings

@param settings [Hash] A hash configuration of the new settings

@return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 84
def edit_settings(settings)
  conn = authenticated_client
  @last_response = conn.put('/manage/v1/config/settings', settings.to_json.to_s)
end
get_authorized_keys()
Alias for: authorized_keys
get_settings()
Alias for: settings
remove_authorized_key(key) click to toggle source

Removes an authorized SSH keys from the Enterprise install

@param key Either the file path to a key, a File handler to the key, or the contents of the key itself @return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 124
def remove_authorized_key(key)
  conn = authenticated_client
  case key
  when String
    if File.exist?(key)
      key = File.open(key, 'r')
      content = key.read.strip
      key.close
    else
      content = key
    end
  when File
    content = key.read.strip
    key.close
  end

  queries = {}
  queries[:key] = content
  @last_response = conn.run_request(:delete, '/manage/v1/access/ssh', queries, nil)
end
Also aliased as: delete_authorized_key
settings() click to toggle source

Get information about the Enterprise installation

@return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 73
def settings
  conn = authenticated_client
  @last_response = conn.get('/manage/v1/config/settings')
end
Also aliased as: get_settings
start_configuration() click to toggle source

Start a configuration process.

@return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 56
def start_configuration
  conn = authenticated_client
  @last_response = conn.post('/manage/v1/config/apply')
end
upload_license(license) click to toggle source

Uploads a license for the first time

@param license [String] The path to your .ghl license file.

@return [nil]

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 37
    def upload_license(license)
      conn = authenticated_client
      begin
        conn.request :multipart
      rescue Faraday::Error
        raise Faraday::Error, <<~ERROR
          The `faraday-multipart` gem is required to upload a license.
          Please add `gem "faraday-multipart"` to your Gemfile.
        ERROR
      end
      params = {}
      params[:license] = Faraday::FilePart.new(license, 'binary')
      params[:password] = @manage_ghes_password
      @last_response = conn.post('/manage/v1/config/init', params, { 'Content-Type' => 'multipart/form-data' })
    end

Protected Instance Methods

endpoint() click to toggle source
# File lib/octokit/manage_ghes_client.rb, line 36
def endpoint
  manage_ghes_endpoint
end
manage_ghes_endpoint=(value) click to toggle source

Set Manage GHES API endpoint

@param value [String] Manage GHES API endpoint

# File lib/octokit/manage_ghes_client.rb, line 43
def manage_ghes_endpoint=(value)
  reset_agent
  @manage_ghes_endpoint = value
end
manage_ghes_password=(value) click to toggle source

Set Manage GHES API password

@param value [String] Manage GHES API password

# File lib/octokit/manage_ghes_client.rb, line 59
def manage_ghes_password=(value)
  reset_agent
  @manage_ghes_password = value
end
manage_ghes_username=(value) click to toggle source

Set Manage GHES API username

@param value [String] Manage GHES API username

# File lib/octokit/manage_ghes_client.rb, line 51
def manage_ghes_username=(value)
  reset_agent
  @manage_ghes_username = value
end

Private Instance Methods

authenticated_client() click to toggle source
# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 157
def authenticated_client
  @authenticated_client ||= Faraday.new(url: @manage_ghes_endpoint) do |c|
    c.headers[:user_agent] = user_agent
    c.request  :json
    c.response :json
    c.adapter Faraday.default_adapter

    if root_site_admin_assumed?
      username = 'api_key'
    elsif basic_authenticated?
      username = @manage_ghes_username
    end
    c.request(*FARADAY_BASIC_AUTH_KEYS, username, @manage_ghes_password)

    # Disabling SSL is essential for certain self-hosted Enterprise instances
    c.ssl[:verify] = false if connection_options[:ssl] && !connection_options[:ssl][:verify]

    c.use Octokit::Response::RaiseError
  end
end
basic_authenticated?() click to toggle source
# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 148
def basic_authenticated?
  !!(@manage_ghes_username && @manage_ghes_password)
end
root_site_admin_assumed?() click to toggle source

If no username is provided, we assume root site admin should be used

# File lib/octokit/manage_ghes_client/manage_ghes.rb, line 153
def root_site_admin_assumed?
  !@manage_ghes_username
end