class Escobar::GitHub::Client

Top-level class for interacting with GitHub API

Attributes

name_with_owner[R]
token[R]

Public Class Methods

new(token, name_with_owner) click to toggle source
# File lib/escobar/github/client.rb, line 9
def initialize(token, name_with_owner)
  @token           = token
  @name_with_owner = name_with_owner
end

Public Instance Methods

accept_headers() click to toggle source
# File lib/escobar/github/client.rb, line 85
def accept_headers
  "application/vnd.github.loki-preview+json"
end
create_deployment(options) click to toggle source
# File lib/escobar/github/client.rb, line 58
def create_deployment(options)
  body = {
    ref: options[:ref] || "master",
    task: options[:task] || "deploy",
    auto_merge: false,
    required_contexts: options[:required_contexts] || [],
    payload: options[:payload] || {},
    environment: options[:environment] || "staging",
    description: "Shipped from chat with slash-heroku"
  }
  post("/repos/#{name_with_owner}/deployments", body)
end
create_deployment_status(url, payload) click to toggle source
# File lib/escobar/github/client.rb, line 71
def create_deployment_status(url, payload)
  uri = URI.parse(url)
  post("#{uri.path}/statuses", payload)
end
default_branch() click to toggle source
# File lib/escobar/github/client.rb, line 46
def default_branch
  response = http_method(:get, "/repos/#{name_with_owner}")
  not_found unless response.status == 200
  JSON.parse(response.body)["default_branch"]
end
deployments() click to toggle source
# File lib/escobar/github/client.rb, line 52
def deployments
  response = http_method(:get, "/repos/#{name_with_owner}/deployments")
  not_found unless response.status == 200
  JSON.parse(response.body)
end
get(path) click to toggle source
# File lib/escobar/github/client.rb, line 80
def get(path)
  response = http_method(:get, path)
  JSON.parse(response.body)
end
head(path) click to toggle source
# File lib/escobar/github/client.rb, line 76
def head(path)
  http_method(:head, path)
end
http_method(verb, path) click to toggle source
# File lib/escobar/github/client.rb, line 89
def http_method(verb, path)
  with_error_handling do
    client.send(verb) do |request|
      request.url path
      request.headers["Accept"] = accept_headers
      request.headers["Content-Type"] = "application/json"
      request.headers["Authorization"] = "token #{token}"
      request.options.timeout = Escobar.http_timeout
      request.options.open_timeout = Escobar.http_open_timeout
    end
  end
end
inspect() click to toggle source

mask password

Calls superclass method
# File lib/escobar/github/client.rb, line 15
def inspect
  inspected = super
  inspected = inspected.gsub! @token, "*******" if @token
  inspected
end
not_found() click to toggle source
# File lib/escobar/github/client.rb, line 31
def not_found
  raise RepoNotFound, "Unable to access #{name_with_owner}"
end
post(path, body) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/escobar/github/client.rb, line 103
def post(path, body)
  with_error_handling do
    response = client.post do |request|
      request.url path
      request.headers["Accept"] = accept_headers
      request.headers["Content-Type"] = "application/json"
      request.headers["Authorization"] = "token #{token}"
      request.options.timeout = Escobar.http_timeout
      request.options.open_timeout = Escobar.http_open_timeout
      request.body = body.to_json
    end

    JSON.parse(response.body)
  end
end
required_contexts() click to toggle source
# File lib/escobar/github/client.rb, line 35
def required_contexts
  path = "/repos/#{name_with_owner}/branches/#{default_branch}"
  response = http_method(:get, path)

  not_found unless response.status == 200

  repo = JSON.parse(response.body)
  return [] unless repo["protection"] && repo["protection"]["enabled"]
  repo["protection"]["required_status_checks"]["contexts"]
end
whoami() click to toggle source
# File lib/escobar/github/client.rb, line 21
def whoami
  client.get("/user")
end

Private Instance Methods

client() click to toggle source
# File lib/escobar/github/client.rb, line 130
def client
  @client ||= Escobar.zipkin_enabled? ? zipkin_client : default_client
end
default_client() click to toggle source
# File lib/escobar/github/client.rb, line 143
def default_client
  Faraday.new(url: "https://api.github.com") do |c|
    c.use Escobar::GitHub::Response::RaiseError
    c.adapter Faraday.default_adapter
  end
end
with_error_handling() { || ... } click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/escobar/github/client.rb, line 122
def with_error_handling
  yield
rescue Net::OpenTimeout, Faraday::TimeoutError => e
  raise Escobar::Client::TimeoutError.wrap(e)
rescue Faraday::Error::ClientError => e
  raise Escobar::Client::HTTPError.from_error(e)
end
zipkin_client() click to toggle source
# File lib/escobar/github/client.rb, line 134
def zipkin_client
  Faraday.new(url: "https://api.github.com") do |c|
    c.use :instrumentation
    c.use ZipkinTracer::FaradayHandler, "api.github.com"
    c.use Escobar::GitHub::Response::RaiseError
    c.adapter Faraday.default_adapter
  end
end