class Underway::Api
Public Class Methods
client_for(installation_id: nil, access_token: nil)
click to toggle source
# File lib/underway/api.rb, line 31 def self.client_for(installation_id: nil, access_token: nil) token = access_token if !installation_id.nil? token = installation_token(id: installation_id) end return if token.nil? client = Octokit::Client.new( api_endpoint: Underway::Settings.configuration.github_api_host, access_token: token ) end
debug_octokit!()
click to toggle source
# File lib/underway/api.rb, line 81 def self.debug_octokit! stack = Faraday::RackBuilder.new do |builder| builder.use Octokit::Middleware::FollowRedirects builder.use Octokit::Response::RaiseError builder.use Octokit::Response::FeedParser builder.response :logger builder.adapter Faraday.default_adapter end Octokit.middleware = stack end
generate_jwt()
click to toggle source
# File lib/underway/api.rb, line 45 def self.generate_jwt payload = { # Issued at time: iat: Time.now.to_i, # JWT expiration time (10 minute maximum) exp: Time.now.to_i + (10 * 60), # GitHub Apps identifier iss: Underway::Settings.configuration.app_id } JWT.encode(payload, Underway::Settings.configuration.private_key, "RS256") end
installation_token(id:)
click to toggle source
Returns a valid auth token for the installation
# File lib/underway/api.rb, line 59 def self.installation_token(id:) if token = Underway::Settings.configuration.token_cache.lookup_installation_auth_token(id: id) log("token cache: hit") return token else log("token cache: miss") res = invoke( "app/installations/#{id}/access_tokens", method: :post ) if error = res[:error] raise ArgumentError.new(error) end token = res.token expires_at = res.expires_at.to_s Underway::Settings.configuration.token_cache.store_installation_auth_token(id: id, token: token, expires_at: expires_at) token end end
invoke(route, headers: {}, data: {}, method: :get)
click to toggle source
Returns a Sawyer::Resource or PORO from the GitHub REST API
# File lib/underway/api.rb, line 7 def self.invoke(route, headers: {}, data: {}, method: :get) debug_octokit! if verbose_logging? Octokit.api_endpoint = Underway::Settings.configuration.github_api_host if !headers[:authorization] && !headers["Authorization"] Octokit.bearer_token = generate_jwt end options = { accept: "application/vnd.github.machine-man-preview+json", headers: headers } begin case method when :post then Octokit.post(route, options.merge(data)) else Octokit.get(route, options) end rescue Octokit::Error => e { error: e.to_s } end end
log(message)
click to toggle source
# File lib/underway/api.rb, line 96 def self.log(message) if verbose_logging? ::Underway::Settings.configuration.logger.info(message) end end
verbose_logging?()
click to toggle source
# File lib/underway/api.rb, line 92 def self.verbose_logging? !!Underway::Settings.configuration.verbose_logging end