Skip to content

Commit fc80aed

Browse files
committed
Merge enterprise-admin-client
2 parents 0703691 + 4b5661f commit fc80aed

80 files changed

Lines changed: 1559 additions & 207 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,64 @@ custom pattern for traversing large lists.
257257

258258
[paginated]: http://developer.github.com/v3/#pagination
259259

260+
## Working with GitHub Enterprise
261+
262+
With a bit of setup, you can also use Octokit with your Github Enterprise instance.
263+
264+
### Interacting with the GitHub.com APIs in GitHub Enterprise
265+
266+
To interact with the "regular" GitHub.com APIs in GitHub Enterprise, simply configure the `api_endpoint` to match your hostname. For example:
267+
268+
``` ruby
269+
Octokit.configure do |c|
270+
c.api_endpoint = "<hostname>/api/v3/"
271+
end
272+
client = Octokit::Client.new(:access_token => "<your 40 char token>")
273+
```
274+
275+
### Interacting with the GitHub Enterprise Admin APIs
276+
277+
The GitHub Enterprise Admin APIs are under a different client: `EnterpriseAdminClient`. You'll need to have an administrator account in order to use these APIs.
278+
279+
``` ruby
280+
admin_client = Octokit::EnterpriseAdminClient.new \
281+
:access_token => "<your 40 char token>",
282+
:api_endpoint = "https://<hostname>/api/v3/"
283+
284+
# or
285+
Octokit.configure do |c|
286+
c.api_endpoint = "https://hostname/api/v3/"
287+
c.access_token = "<your 40 char token>"
288+
end
289+
admin_client = Octokit.enterprise_admin_client
290+
```
291+
292+
### Interacting with the GitHub Enterprise Management Console APIs
293+
294+
The GitHub Enterprise Management Console APIs are also under a separate client: `EnterpriseManagementConsoleClient`. In order to use it, you'll need to provide both your management console password as well as the endpoint to your management console. This is different than the API endpoint provided above.
295+
296+
``` ruby
297+
management_console_client = Octokit::EnterpriseManagementConsoleClient.new \
298+
:management_console_password => "secret",
299+
:management_console_endpoint = "https://hostname:8633"
300+
# or
301+
Octokit.configure do |c|
302+
c.management_console_endpoint = "https://hostname:8633"
303+
c.management_console_password = "secret"
304+
end
305+
management_console_client = Octokit.enterprise_management_console_client
306+
```
307+
308+
### SSL Connection Errors
309+
310+
You *may* need to disable SSL temporarily while first setting up your GitHub Enterprise install. You can do that with the following configuration:
311+
312+
``` ruby
313+
client.connection_options[:ssl] = { :verify => false }
314+
```
315+
316+
Do remember to turn `:verify` back to `true`, as it's important for secure communication.
317+
260318
## Configuration and defaults
261319

262320
While `Octokit::Client` accepts a range of options when creating a new client
@@ -510,6 +568,11 @@ ENV Variable | Description |
510568
`OCTOKIT_TEST_GITHUB_CLIENT_SECRET` | Test OAuth application client secret.
511569
`OCTOKIT_TEST_GITHUB_REPOSITORY` | Test repository to perform destructive actions against, this should not be set to any repository of importance. **Automatically created by the test suite if nonexistent** Default: `api-sandbox`
512570
`OCTOKIT_TEST_GITHUB_ORGANIZATION` | Test organization.
571+
`OCTOKIT_TEST_GITHUB_ENTERPRISE_LOGIN` | GitHub Enterprise login name
572+
`OCTOKIT_TEST_GITHUB_ENTERPRISE_TOKEN` | GitHub Enterprise token
573+
`OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_PASSWORD` | GitHub Enterprise management console password
574+
`OCTOKIT_TEST_GITHUB_ENTERPRISE_ENDPOINT` | GitHub Enterprise hostname
575+
`OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_ENDPOINT` | GitHub Enterprise Management Console endpoint
513576

514577
Since we periodically refresh our cassettes, please keep some points in mind
515578
when writing new specs.
@@ -590,4 +653,3 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
590653
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
591654
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
592655
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
593-

lib/octokit.rb

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'octokit/client'
2+
require 'octokit/enterprise_admin_client'
3+
require 'octokit/enterprise_management_console_client'
24
require 'octokit/default'
35

46
# Ruby toolkit for the GitHub API
@@ -11,20 +13,44 @@ class << self
1113
#
1214
# @return [Octokit::Client] API wrapper
1315
def client
14-
@client = Octokit::Client.new(options) unless defined?(@client) && @client.same_options?(options)
15-
@client
16+
return @client if defined?(@client) && @client.same_options?(options)
17+
@client = Octokit::Client.new(options)
1618
end
1719

18-
# @private
19-
def respond_to_missing?(method_name, include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
20-
# @private
21-
def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"
20+
# EnterpriseAdminClient client based on configured options {Configurable}
21+
#
22+
# @return [Octokit::EnterpriseAdminClient] API wrapper
23+
def enterprise_admin_client
24+
return @enterprise_admin_client if defined?(@enterprise_admin_client) && @enterprise_admin_client.same_options?(options)
25+
@enterprise_admin_client = Octokit::EnterpriseAdminClient.new(options)
26+
end
2227

23-
private
28+
# EnterpriseManagementConsoleClient client based on configured options {Configurable}
29+
#
30+
# @return [Octokit::EnterpriseManagementConsoleClient] API wrapper
31+
def enterprise_management_console_client
32+
return @enterprise_management_console_client if defined?(@enterprise_management_console_client) && @enterprise_management_console_client.same_options?(options)
33+
@enterprise_management_console_client = Octokit::EnterpriseManagementConsoleClient.new(options)
34+
end
35+
36+
private
37+
38+
def respond_to_missing?(method_name, include_private=false)
39+
client.respond_to?(method_name, include_private) ||
40+
enterprise_admin_client.respond_to?(method_name, include_private) ||
41+
enterprise_management_console_client.respond_to?(method_name, include_private)
42+
end
2443

2544
def method_missing(method_name, *args, &block)
26-
return super unless client.respond_to?(method_name)
27-
client.send(method_name, *args, &block)
45+
if client.respond_to?(method_name)
46+
return client.send(method_name, *args, &block)
47+
elsif enterprise_admin_client.respond_to?(method_name)
48+
return enterprise_admin_client.send(method_name, *args, &block)
49+
elsif enterprise_management_console_client.respond_to?(method_name)
50+
return enterprise_management_console_client.send(method_name, *args, &block)
51+
end
52+
53+
super
2854
end
2955

3056
end

lib/octokit/client.rb

Lines changed: 5 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'sawyer'
1+
require 'octokit/connection'
2+
require 'octokit/warnable'
23
require 'octokit/arguments'
34
require 'octokit/repo_arguments'
45
require 'octokit/configurable'
@@ -53,6 +54,8 @@ class Client
5354

5455
include Octokit::Authentication
5556
include Octokit::Configurable
57+
include Octokit::Connection
58+
include Octokit::Warnable
5659
include Octokit::Client::Authorizations
5760
include Octokit::Client::Commits
5861
include Octokit::Client::CommitComments
@@ -101,14 +104,6 @@ def initialize(options = {})
101104
login_from_netrc unless user_authenticated? || application_authenticated?
102105
end
103106

104-
# Compares client options to a Hash of requested options
105-
#
106-
# @param opts [Hash] Options to compare with current client options
107-
# @return [Boolean]
108-
def same_options?(opts)
109-
opts.hash == options.hash
110-
end
111-
112107
# Text representation of the client, masking tokens and passwords
113108
#
114109
# @return [String]
@@ -117,6 +112,7 @@ def inspect
117112

118113
# mask password
119114
inspected = inspected.gsub! @password, "*******" if @password
115+
inspected = inspected.gsub! @management_console_password, "*******" if @management_console_password
120116
# Only show last 4 of token, secret
121117
if @access_token
122118
inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
@@ -128,126 +124,6 @@ def inspect
128124
inspected
129125
end
130126

131-
# Make a HTTP GET request
132-
#
133-
# @param url [String] The path, relative to {#api_endpoint}
134-
# @param options [Hash] Query and header params for request
135-
# @return [Sawyer::Resource]
136-
def get(url, options = {})
137-
request :get, url, parse_query_and_convenience_headers(options)
138-
end
139-
140-
# Make a HTTP POST request
141-
#
142-
# @param url [String] The path, relative to {#api_endpoint}
143-
# @param options [Hash] Body and header params for request
144-
# @return [Sawyer::Resource]
145-
def post(url, options = {})
146-
request :post, url, options
147-
end
148-
149-
# Make a HTTP PUT request
150-
#
151-
# @param url [String] The path, relative to {#api_endpoint}
152-
# @param options [Hash] Body and header params for request
153-
# @return [Sawyer::Resource]
154-
def put(url, options = {})
155-
request :put, url, options
156-
end
157-
158-
# Make a HTTP PATCH request
159-
#
160-
# @param url [String] The path, relative to {#api_endpoint}
161-
# @param options [Hash] Body and header params for request
162-
# @return [Sawyer::Resource]
163-
def patch(url, options = {})
164-
request :patch, url, options
165-
end
166-
167-
# Make a HTTP DELETE request
168-
#
169-
# @param url [String] The path, relative to {#api_endpoint}
170-
# @param options [Hash] Query and header params for request
171-
# @return [Sawyer::Resource]
172-
def delete(url, options = {})
173-
request :delete, url, options
174-
end
175-
176-
# Make a HTTP HEAD request
177-
#
178-
# @param url [String] The path, relative to {#api_endpoint}
179-
# @param options [Hash] Query and header params for request
180-
# @return [Sawyer::Resource]
181-
def head(url, options = {})
182-
request :head, url, parse_query_and_convenience_headers(options)
183-
end
184-
185-
# Make one or more HTTP GET requests, optionally fetching
186-
# the next page of results from URL in Link response header based
187-
# on value in {#auto_paginate}.
188-
#
189-
# @param url [String] The path, relative to {#api_endpoint}
190-
# @param options [Hash] Query and header params for request
191-
# @param block [Block] Block to perform the data concatination of the
192-
# multiple requests. The block is called with two parameters, the first
193-
# contains the contents of the requests so far and the second parameter
194-
# contains the latest response.
195-
# @return [Sawyer::Resource]
196-
def paginate(url, options = {}, &block)
197-
opts = parse_query_and_convenience_headers(options.dup)
198-
if @auto_paginate || @per_page
199-
opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil)
200-
end
201-
202-
data = request(:get, url, opts.dup)
203-
204-
if @auto_paginate
205-
while @last_response.rels[:next] && rate_limit.remaining > 0
206-
@last_response = @last_response.rels[:next].get(:headers => opts[:headers])
207-
if block_given?
208-
yield(data, @last_response)
209-
else
210-
data.concat(@last_response.data) if @last_response.data.is_a?(Array)
211-
end
212-
end
213-
214-
end
215-
216-
data
217-
end
218-
219-
# Hypermedia agent for the GitHub API
220-
#
221-
# @return [Sawyer::Agent]
222-
def agent
223-
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
224-
http.headers[:accept] = default_media_type
225-
http.headers[:content_type] = "application/json"
226-
http.headers[:user_agent] = user_agent
227-
if basic_authenticated?
228-
http.basic_auth(@login, @password)
229-
elsif token_authenticated?
230-
http.authorization 'token', @access_token
231-
elsif application_authenticated?
232-
http.params = http.params.merge application_authentication
233-
end
234-
end
235-
end
236-
237-
# Fetch the root resource for the API
238-
#
239-
# @return [Sawyer::Resource]
240-
def root
241-
get "/"
242-
end
243-
244-
# Response for last HTTP request
245-
#
246-
# @return [Sawyer::Response]
247-
def last_response
248-
@last_response if defined? @last_response
249-
end
250-
251127
# Duplicate client using client_id and client_secret as
252128
# Basic Authentication credentials.
253129
# @example
@@ -312,72 +188,5 @@ def client_secret=(value)
312188
reset_agent
313189
@client_secret = value
314190
end
315-
316-
# Wrapper around Kernel#warn to print warnings unless
317-
# OCTOKIT_SILENT is set to true.
318-
#
319-
# @return [nil]
320-
def octokit_warn(*message)
321-
unless ENV['OCTOKIT_SILENT']
322-
warn message
323-
end
324-
end
325-
326-
private
327-
328-
def reset_agent
329-
@agent = nil
330-
end
331-
332-
def request(method, path, data, options = {})
333-
if data.is_a?(Hash)
334-
options[:query] = data.delete(:query) || {}
335-
options[:headers] = data.delete(:headers) || {}
336-
if accept = data.delete(:accept)
337-
options[:headers][:accept] = accept
338-
end
339-
end
340-
341-
@last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
342-
response.data
343-
end
344-
345-
# Executes the request, checking if it was successful
346-
#
347-
# @return [Boolean] True on success, false otherwise
348-
def boolean_from_response(method, path, options = {})
349-
request(method, path, options)
350-
@last_response.status == 204
351-
rescue Octokit::NotFound
352-
false
353-
end
354-
355-
356-
def sawyer_options
357-
opts = {
358-
:links_parser => Sawyer::LinkParsers::Simple.new
359-
}
360-
conn_opts = @connection_options
361-
conn_opts[:builder] = @middleware if @middleware
362-
conn_opts[:proxy] = @proxy if @proxy
363-
opts[:faraday] = Faraday.new(conn_opts)
364-
365-
opts
366-
end
367-
368-
def parse_query_and_convenience_headers(options)
369-
headers = options.delete(:headers) { Hash.new }
370-
CONVENIENCE_HEADERS.each do |h|
371-
if header = options.delete(h)
372-
headers[h] = header
373-
end
374-
end
375-
query = options.delete(:query)
376-
opts = {:query => options}
377-
opts[:query].merge!(query) if query && query.is_a?(Hash)
378-
opts[:headers] = headers unless headers.empty?
379-
380-
opts
381-
end
382191
end
383192
end

lib/octokit/client/pub_sub_hubbub.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def unsubscribe_service_hook(repo, service_name)
9393
private
9494

9595
def pub_sub_hubbub_request(options = {})
96-
# This method is janky, bypass normal stack so we don'tl
96+
# This method is janky, bypass normal stack so we don't
9797
# serialize request as JSON
9898
conn = Faraday.new(:url => @api_endpoint) do |http|
9999
http.headers[:user_agent] = user_agent

0 commit comments

Comments
 (0)