This repository was archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgithub.rb
65 lines (53 loc) · 1.69 KB
/
github.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Interface for the Github API (V3)
module Github
ROOT_ENDPOINT = 'https://api.github.com'
APP_NAME = APP_CONFIG['github_app_name']
OWNER = APP_CONFIG['github_owner']
REPO = APP_CONFIG['github_repo']
REQUEST_PARAMS = {
'access_token' => ENV['GITHUB_APPLICATION_ACCESS_TOKEN'],
'client_id' => ENV['GITHUB_APPLICATION_CLIENT_ID'],
'client_secret' => ENV['GITHUB_APPLICATION_CLIENT_SECRET']
}
VALID_VERBS = {
get: Net::HTTP::Get,
post: Net::HTTP::Post,
patch: Net::HTTP::Patch
}
def self.enabled?
APP_NAME.present? && OWNER.present? && REPO.present?
end
def self.pusher
@pusher ||= Pusher.new
end
def self.send_request(verb, url, params={})
raise "invalid HTTP verb #{verb}" unless VALID_VERBS.include?(verb)
query_params = REQUEST_PARAMS
query_params.merge!(params) if verb == :get
url += "?#{query_params.to_query}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = VALID_VERBS[verb].new(uri.request_uri)
request['Accept'] = 'application/vnd.github.v3+json'
request['User-Agent'] = APP_NAME
if request.request_body_permitted?
request.body = params.to_json
request.content_type = 'application/json'
end
Retriable.retriable { http.request(request) }
end
def self.get(url, params={})
send_request(:get, url, params)
end
def self.post(url, params={})
send_request(:post, url, params)
end
def self.patch(url, params={})
send_request(:patch, url, params)
end
def self.valid_sha?(sha)
sha.present? && sha.length == 40 && sha =~ /^[0-9A-F]+$/i
end
end