-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathgithub.rb
59 lines (50 loc) · 1.87 KB
/
github.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'gh'
require 'core_ext/hash/compact'
GH::Remote.class_eval do
def http(verb, url, headers = {}, &block)
body = headers.delete :body
connection.run_request(verb, url, body, headers, &block)
rescue Exception => error
raise Error.new(error, nil, :verb => verb, :url => url, :headers => headers)
end
end
GH::TokenCheck.class_eval do
def check_token
return unless @check_token and client_id and client_secret and token
@check_token = false
auth_header = "Basic %s" % Base64.encode64("#{client_id}:#{client_secret}").gsub("\n", "")
if is_legacy?
http :head, path_for("/applications/#{client_id}/tokens/#{token}?client_id=#{client_id}&client_secret=#{client_secret}"), "Authorization" => auth_header
else
http :post, path_for("/applications/#{client_id}/token"), :body => "{\"access_token\": \"#{token}\"}", "Authorization" => auth_header
end
rescue GH::Error(:response_status => 404) => error
raise GH::TokenInvalid, error
end
def is_legacy?
Travis.config.github.enterprise_legacy_oauth
end
end
module Travis
module Github
class << self
def setup
GH.set(
client_id: Travis.config.oauth2.client_id,
client_secret: Travis.config.oauth2.client_secret,
user_agent: "GH/#{GH::VERSION}",
origin: Travis.config.host,
api_url: Travis.config.github.api_url,
ssl: Travis.config.ssl.to_h.merge(Travis.config.github.ssl.to_h || {}).to_h.compact
)
end
def authenticated(user, &block)
fail "we don't have a github token for #{user.inspect}" if user.github_oauth_token.blank?
GH.with(:token => user.github_oauth_token, &block)
end
end
require 'travis/github/education'
require 'travis/github/oauth'
require 'travis/github/services'
end
end