Class: Escobar::GitHub::Client
- Inherits:
-
Object
- Object
- Escobar::GitHub::Client
- Defined in:
- lib/escobar/github/client.rb
Overview
Top-level class for interacting with GitHub API
Instance Attribute Summary collapse
-
#name_with_owner ⇒ Object
readonly
Returns the value of attribute name_with_owner.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #accept_headers ⇒ Object
- #archive_link(ref) ⇒ Object
- #create_deployment(options) ⇒ Object
- #create_deployment_status(url, payload) ⇒ Object
- #default_branch ⇒ Object
- #deployments ⇒ Object
- #get(path) ⇒ Object
- #head(path) ⇒ Object
- #http_method(verb, path) ⇒ Object
-
#initialize(token, name_with_owner) ⇒ Client
constructor
A new instance of Client.
-
#inspect ⇒ Object
mask password.
- #not_found ⇒ Object
-
#post(path, body) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #required_contexts ⇒ Object
- #whoami ⇒ Object
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_owner ⇒ Object (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 |
#token ⇒ Object (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_headers ⇒ Object
85 86 87 |
# File 'lib/escobar/github/client.rb', line 85 def accept_headers "application/vnd.github.loki-preview+json" end |
#archive_link(ref) ⇒ Object
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() body = { ref: [:ref] || "master", task: [:task] || "deploy", auto_merge: false, required_contexts: [:required_contexts] || [], payload: [:payload] || {}, environment: [: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_branch ⇒ Object
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 |
#deployments ⇒ Object
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..timeout = Escobar.http_timeout request..open_timeout = Escobar.http_open_timeout end end end |
#inspect ⇒ Object
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_found ⇒ Object
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..timeout = Escobar.http_timeout request..open_timeout = Escobar.http_open_timeout request.body = body.to_json end JSON.parse(response.body) end end |
#required_contexts ⇒ Object
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 |
#whoami ⇒ Object
21 22 23 |
# File 'lib/escobar/github/client.rb', line 21 def whoami client.get("/user") end |