Class: Escobar::GitHub::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/escobar/github/client.rb

Overview

Top-level class for interacting with GitHub API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, name_with_owner) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/escobar/github/client.rb', line 7

def initialize(token, name_with_owner)
  @token           = token
  @name_with_owner = name_with_owner
end

Instance Attribute Details

#name_with_ownerObject (readonly)

Returns the value of attribute name_with_owner.



6
7
8
# File 'lib/escobar/github/client.rb', line 6

def name_with_owner
  @name_with_owner
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/escobar/github/client.rb', line 6

def token
  @token
end

Instance Method Details

#accept_headersObject



83
84
85
# File 'lib/escobar/github/client.rb', line 83

def accept_headers
  "application/vnd.github.loki-preview+json"
end


23
24
25
26
27
# File 'lib/escobar/github/client.rb', line 23

def archive_link(ref)
  path = "/repos/#{name_with_owner}/tarball/#{ref}"
  response = http_method(:head, path)
  response && response.headers && response.headers["Location"]
end

#create_deployment(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/escobar/github/client.rb', line 56

def create_deployment(options)
  body = {
    ref: options[:ref] || "master",
    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) ⇒ Object



69
70
71
72
# File 'lib/escobar/github/client.rb', line 69

def create_deployment_status(url, payload)
  uri = URI.parse(url)
  post("#{uri.path}/statuses", payload)
end

#default_branchObject



44
45
46
47
48
# File 'lib/escobar/github/client.rb', line 44

def default_branch
  response = http_method(:get, "/repos/#{name_with_owner}")
  not_found unless response.status == 200
  JSON.parse(response.body)["default_branch"]
end

#deploymentsObject



50
51
52
53
54
# File 'lib/escobar/github/client.rb', line 50

def deployments
  response = http_method(:get, "/repos/#{name_with_owner}/deployments")
  not_found unless response.status == 200
  JSON.parse(response.body)
end

#get(path) ⇒ Object



78
79
80
81
# File 'lib/escobar/github/client.rb', line 78

def get(path)
  response = http_method(:get, path)
  JSON.parse(response.body)
end

#head(path) ⇒ Object



74
75
76
# File 'lib/escobar/github/client.rb', line 74

def head(path)
  http_method(:head, path)
end

#http_method(verb, path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/escobar/github/client.rb', line 87

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

#inspectObject

mask password



13
14
15
16
17
# File 'lib/escobar/github/client.rb', line 13

def inspect
  inspected = super
  inspected = inspected.gsub! @token, "*******" if @token
  inspected
end

#not_foundObject

Raises:



29
30
31
# File 'lib/escobar/github/client.rb', line 29

def not_found
  raise RepoNotFound, "Unable to access #{name_with_owner}"
end

#post(path, body) ⇒ Object

rubocop:disable Metrics/AbcSize



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/escobar/github/client.rb', line 101

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_contextsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/escobar/github/client.rb', line 33

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

#whoamiObject



19
20
21
# File 'lib/escobar/github/client.rb', line 19

def whoami
  client.get("/user")
end