forked from evilmartians/terraforming-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-token
executable file
·79 lines (65 loc) · 2.11 KB
/
github-token
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env ruby
# This script is used to generate GitHub app access token
# used by Danger.
#
# Required env vars:
#
# GITHUB_APP_PRIVATE_KEY_PATH or GITHUB_APP_PRIVATE_KEY - path to the app's private key or the key itself
# GITHUB_APP_ID – the app's ID (you can find it on the app's page)
# GITHUB_APP_INSTALLATION_ID – the installation's ID (the instance of the app for the repo/organization).
#
# You can find the installation ID on the Project -> Settings -> Integrations & Services page:
# under "Installed GitHub Apps" find the app and click "Configure".
# The resulting URL will contain the installation ID, e.g. /organizations/myorg/settings/installations/12345.
#
# See https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/
require "openssl"
require "net/http"
require "json"
# Make this script work both as a standalone executable
# and as a part of the bundle
begin
require "jwt"
rescue LoadError
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "jwt"
end
require "jwt"
end
begin
# Private key contents
private_pem =
if ENV["GITHUB_APP_PRIVATE_KEY_PATH"]
File.read(ENV["GITHUB_APP_PRIVATE_KEY_PATH"])
else
ENV.fetch("GITHUB_APP_PRIVATE_KEY").gsub(/\\n/, "\n")
end
private_key = OpenSSL::PKey::RSA.new(private_pem)
payload = {
iat: Time.now.to_i,
# JWT expiration time (10 minute maximum)
exp: Time.now.to_i + (10 * 60),
iss: ENV.fetch("GITHUB_APP_ID")
}
jwt = JWT.encode(payload, private_key, "RS256")
uri = URI("https://api.github.com/app/installations/#{ENV.fetch("GITHUB_APP_INSTALLATION_ID")}/access_tokens")
req = Net::HTTP::Post.new(uri)
req["Authorization"] = "Bearer #{jwt}"
req["Accept"] = "application/vnd.github.machine-man-preview+json"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
puts JSON.parse(res.body).fetch("token")
else
warn res.value
warn res.body
end
rescue => e
warn "Failed to generate GitHub token!"
warn e.message
exit(1)
end