main
 1#!/usr/bin/env ruby
 2
 3require 'bundler/inline'
 4require 'openssl'
 5
 6gemfile do
 7  source 'https://rubygems.org'
 8  gem 'jwt'
 9  gem 'net-hippie'
10end
11
12private_pem = IO.read('config/gh-app.pem')
13private_key = OpenSSL::PKey::RSA.new(private_pem)
14
15jwt = JWT.encode(
16  {
17    iat: Time.now.to_i - 60,
18    exp: Time.now.to_i + (10 * 60),
19    iss: 125988
20  },
21  private_key,
22  "RS256"
23)
24
25client = Net::Hippie::Client.new(logger: Logger.new('/dev/null'), headers: {
26  'Accept' => 'application/vnd.github.v3+json',
27  'Authorization' => "Bearer #{jwt}",
28})
29
30response = client.get("https://api.github.com/app")
31puts JSON.pretty_generate(JSON.parse(response.body))
32
33response = client.get("https://api.github.com/app/installations")
34json = JSON.parse(response.body)
35json.each do |installation|
36  installation_id = installation['id']
37  response = client.post("https://api.github.com/app/installations/#{installation_id}/access_tokens")
38  json = JSON.parse(response.body)
39  token = json['token']
40  response = client.get("https://api.github.com/installation/repositories", headers:{
41    'Authorization': "token #{token}"
42  })
43  puts JSON.pretty_generate(JSON.parse(response.body))
44end