diff --git a/lib/travis/config/defaults.rb b/lib/travis/config/defaults.rb index d12dd2911f..981d2d9405 100644 --- a/lib/travis/config/defaults.rb +++ b/lib/travis/config/defaults.rb @@ -40,7 +40,7 @@ def logs_api_auth_token sidekiq: { namespace: 'sidekiq', pool_size: 1 }, smtp: {}, email: {}, - github: { api_url: 'https://api.github.com', token: 'travisbot-token' }, + github: { api_url: 'https://api.github.com', token: 'travisbot-token', enterprise_legacy_oauth: true }, async: {}, notifications: [], # TODO rename to event.handlers metrics: { reporter: 'librato' }, diff --git a/lib/travis/github.rb b/lib/travis/github.rb index a2fd9ad2eb..ae60812d20 100644 --- a/lib/travis/github.rb +++ b/lib/travis/github.rb @@ -1,6 +1,37 @@ 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