Class: GithubClient
- Inherits:
-
Object
- Object
- GithubClient
- Defined in:
- app/clients/github_client.rb
Instance Method Summary collapse
- #branches(repo) ⇒ Object
- #conn ⇒ Object
- #file(repo, path) ⇒ Object
-
#initialize(username, token) ⇒ GithubClient
constructor
A new instance of GithubClient.
- #latest_commit(repo) ⇒ Object
- #post_json(url, payload) ⇒ Object
- #register_webhook(repo) ⇒ Object
- #repo(repo) ⇒ Object
- #req(method, url, params = {}) ⇒ Object
- #req_cached(method, url, params = {}) ⇒ Object
- #teams ⇒ Object
Constructor Details
#initialize(username, token) ⇒ GithubClient
Returns a new instance of GithubClient.
3 4 5 6 |
# File 'app/clients/github_client.rb', line 3 def initialize(username, token) @token = token @username = username end |
Instance Method Details
#branches(repo) ⇒ Object
50 51 52 53 54 |
# File 'app/clients/github_client.rb', line 50 def branches(repo) req_cached(:get, "/repos/#{@username}/#{repo}/branches").map do |branch| {name: branch['name'], commit: branch['commit']['sha']} end end |
#conn ⇒ Object
92 93 94 95 96 97 98 |
# File 'app/clients/github_client.rb', line 92 def conn @github ||= Faraday.new(url: 'https://api.github.com/') do |faraday| faraday.request :url_encoded # form-encode POST params faraday.adapter Faraday.default_adapter # make requests with Net::HTTP faraday.headers['Authorization'] = "token #{@token}" end end |
#file(repo, path) ⇒ Object
56 57 58 59 |
# File 'app/clients/github_client.rb', line 56 def file(repo, path) res = req(:get, "/repos/#{@username}/#{repo}/contents/#{path}") Base64.decode64(res['content']) if res['content'] end |
#latest_commit(repo) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/clients/github_client.rb', line 34 def latest_commit(repo) res = req(:get, "/repos/#{@username}/#{repo}/git/refs") return nil if res.length == 0 res.each do |ref| if ref['ref'].split('/')[1] == 'heads' return { sha: ref['object']['sha'], branch: ref['ref'].split('refs/heads/')[-1], } end end nil end |
#post_json(url, payload) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'app/clients/github_client.rb', line 79 def post_json(url, payload) res = conn.post do |req| req.url url req.headers['Content-Type'] = 'application/json' req.body = JSON.generate(payload) end JSON.parse(res.body) end |
#register_webhook(repo) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/clients/github_client.rb', line 61 def register_webhook(repo) post_json("/repos/#{@username}/#{repo}/hooks", { name: 'web', active: true, events: %w(push pull_request), config: { url: "#{Config.api_root_url}/hooks/github", content_type: 'json' } }) end |
#repo(repo) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/clients/github_client.rb', line 14 def repo(repo) res = req(:get, "/repos/#{@username}/#{repo}") if res['source'] { id: res['source']['id'], name: res['source']['name'], owner: res['source']['owner']['login'], owner_id: res['source']['owner']['id'], } else { id: res['id'], name: res['name'], owner: res['owner']['login'], owner_id: res['owner']['id'], } end end |
#req(method, url, params = {}) ⇒ Object
73 74 75 76 77 |
# File 'app/clients/github_client.rb', line 73 def req(method, url, params={}) puts "#{method} https://api.github.com/#{url}" res = conn.send(method, url, params) JSON.parse(res.body) end |
#req_cached(method, url, params = {}) ⇒ Object
88 89 90 |
# File 'app/clients/github_client.rb', line 88 def req_cached(method, url, params={}) Rails.cache.fetch("github-api:#{method}.#{url}", expires_in: 1.day) { req(method, url, params) } end |
#teams ⇒ Object
8 9 10 11 12 |
# File 'app/clients/github_client.rb', line 8 def teams req(:get, 'user/orgs').map do |org| {id: org['id'], name: org['login']} end end |