Skip to content

Commit

Permalink
Add GHES Manage API client
Browse files Browse the repository at this point in the history
  • Loading branch information
manue1 committed May 16, 2024
1 parent 0cb1a8e commit 87249f6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/octokit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,24 @@ def enterprise_management_console_client
@enterprise_management_console_client = Octokit::EnterpriseManagementConsoleClient.new(options)
end

# ManageGHESClient client based on configured options {Configurable}
#
# @return [Octokit::ManageGHESClient] API wrapper
def manage_ghes_client
if defined?(@manage_ghes_client) && @manage_ghes_client.same_options?(options)
return @manage_ghes_client
end

@manage_ghes_client = Octokit::ManageGHESClient.new(options)
end

private

def respond_to_missing?(method_name, include_private = false)
client.respond_to?(method_name, include_private) ||
enterprise_admin_client.respond_to?(method_name, include_private) ||
enterprise_management_console_client.respond_to?(method_name, include_private)
enterprise_management_console_client.respond_to?(method_name, include_private) ||
manage_ghes_client.respond_to?(method_name, include_private)
end

def method_missing(method_name, *args, &block)
Expand All @@ -56,6 +68,8 @@ def method_missing(method_name, *args, &block)
return enterprise_admin_client.send(method_name, *args, &block)
elsif enterprise_management_console_client.respond_to?(method_name)
return enterprise_management_console_client.send(method_name, *args, &block)
elsif manage_ghes_client.respond_to?(method_name)
return manage_ghes_client.send(method_name, *args, &block)
end

super
Expand Down
64 changes: 64 additions & 0 deletions lib/octokit/manage_ghes_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'octokit/configurable'
require 'octokit/connection'
require 'octokit/warnable'
require 'octokit/manage_ghes_client/manage_ghes'

module Octokit
# 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 https://developer.github.com/v3/enterprise-admin/manage-ghes/
class ManageGHESClient
include Octokit::Configurable
include Octokit::Connection
include Octokit::Warnable
include Octokit::ManageGHESClient::ManageAPI

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

protected

def endpoint
manage_ghes_endpoint
end

# Set Manage GHES API username
#
# @param value [String] Manage GHES API username
def manage_api_username=(value)
reset_agent
@manage_api_username = value
end

# Set Manage GHES API password
#
# @param value [String] Manage GHES API password
def manage_api_password=(value)
reset_agent
@manage_api_password = value
end

# Set Manage GHES API endpoint
#
# @param value [String] Manage GHES API endpoint
def manage_ghes_endpoint=(value)
reset_agent
@manage_ghes_endpoint = value
end
end
end
47 changes: 47 additions & 0 deletions lib/octokit/manage_ghes_client/manage_ghes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module Octokit
class ManageGHESClient
# Methods for the Manage GitHub Enterprise Server API
#
# @see https://developer.github.com/v3/enterprise-admin/manage-ghes
module ManageAPI
end

private

def password_hash
{ query: { api_key: @management_console_password } }
end

def basic_authenticated?
!!(@manage_api_username && @manage_api_password)
end

def root_site_admin_assumed?
!!(@manage_api_username)
end

def faraday_configuration
@faraday_configuration ||= Faraday.new(url: @manage_ghes_endpoint) do |http|
http.headers[:user_agent] = user_agent
http.headers[:content_type] = 'application/json'

if root_site_admin_assumed?
http.basic_auth('api_key', @manage_api_password)
elsif basic_authenticated?
http.basic_auth(@manage_api_username, @manage_api_password)
end

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

http.use Octokit::Response::RaiseError
http.adapter Faraday.default_adapter
end
end

end
end

0 comments on commit 87249f6

Please sign in to comment.