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.



9
10
11
12
# File 'lib/escobar/github/client.rb', line 9

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.



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

def name_with_owner
  @name_with_owner
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#accept_headersObject



85
86
87
# File 'lib/escobar/github/client.rb', line 85

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


25
26
27
28
29
# File 'lib/escobar/github/client.rb', line 25

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



58
59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
# 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_branchObject



46
47
48
49
50
# 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

#deploymentsObject



52
53
54
55
56
# 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) ⇒ Object



80
81
82
83
# File 'lib/escobar/github/client.rb', line 80

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

#head(path) ⇒ Object



76
77
78
# File 'lib/escobar/github/client.rb', line 76

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

#http_method(verb, path) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# 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

#inspectObject

mask password



15
16
17
18
19
# File 'lib/escobar/github/client.rb', line 15

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

#not_foundObject

Raises:



31
32
33
# File 'lib/escobar/github/client.rb', line 31

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

#post(path, body) ⇒ Object

rubocop:disable Metrics/AbcSize



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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_contextsObject



35
36
37
38
39
40
41
42
43
44
# 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

#whoamiObject



21
22
23
# File 'lib/escobar/github/client.rb', line 21

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