Skip to content

Commit 441d23d

Browse files
author
Thomas Osowski
committed
Better documentation
1 parent ad1146e commit 441d23d

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

app/ruby/app-issue-creator/server.rb

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,53 @@
44
require 'active_support/all'
55
require 'octokit'
66

7+
begin
8+
GITHUB_APP_ID = contents["app_id"]
9+
path_to_pem = './private-key.pem'
10+
GITHUB_PRIVATE_KEY = File.read(path_to_pem)
11+
rescue KeyError
12+
$stderr.puts "To run this script, please set the following environment variables:"
13+
$stderr.puts "- GITHUB_APP_ID: GitHub App ID"
14+
rescue Exception => e
15+
$stderr.puts "To run this script, please copy you App's private key to this directory"
16+
$stderr.puts " and rename it to `private_key.pem`"
17+
end
18+
719
@client = nil
820

21+
# Webhook listener
922
post '/payload' do
1023
github_event = request.env['HTTP_X_GITHUB_EVENT']
11-
if github_event == "integration_installation"
12-
#|| github_event == "installation_repositories"
24+
if github_event == "installation"
1325
parse_installation_payload(request.body.read)
1426
else
1527
puts "New event #{github_event}"
1628
end
17-
1829
end
1930

31+
# To authenticate as a GitHub App, generate a private key. Use this key to sign
32+
# a JSON Web Token (JWT), and encode using the RS256 algorithm. GitHub checks
33+
# that the request is authenticated by verifying the token with the
34+
# integration's stored public key. https://git.io/vQOLW
2035
def get_jwt_token
21-
path_to_pem = './platform-samples-app-bot.2017-06-24.private-key.pem'
22-
private_pem = File.read(path_to_pem)
23-
private_key = OpenSSL::PKey::RSA.new(private_pem)
36+
private_key = OpenSSL::PKey::RSA.new(GITHUB_PRIVATE_KEY)
2437

2538
payload = {
2639
# issued at time
2740
iat: Time.now.to_i,
2841
# JWT expiration time (10 minute maximum)
2942
exp: 5.minutes.from_now.to_i,
3043
# GitHub App's identifier
31-
iss: 2583
44+
iss: GITHUB_APP_ID
3245
}
3346

3447
JWT.encode(payload, private_key, "RS256")
3548
end
3649

50+
# A GitHub App is installed by a user on one or more repositories.
51+
# The installation ID is passed in the webhook event. This returns all
52+
# repositories this installation has access to.
3753
def get_app_repositories
38-
3954
json_response = @client.list_installation_repos
4055

4156
repository_list = []
@@ -50,7 +65,8 @@ def get_app_repositories
5065
repository_list
5166
end
5267

53-
68+
# For each repository that has Issues enabled, create an issue stating that a
69+
# GitHub App was installed
5470
def create_issues(repositories, sender_username)
5571
repositories.each do |repo|
5672
begin
@@ -61,19 +77,24 @@ def create_issues(repositories, sender_username)
6177
end
6278
end
6379

80+
# When an App is added by a user, it will generate a webhook event. Parse an
81+
# `installation` webhook event, list all repositories this App has access to,
82+
# and create an issue.
6483
def parse_installation_payload(json_body)
6584
webhook_data = JSON.parse(json_body)
6685
if webhook_data["action"] == "created" || webhook_data["action"] == "added"
6786
installation_id = webhook_data["installation"]["id"]
68-
# Get token for app
69-
puts get_jwt_token
87+
88+
# Get JWT for App and get access token for an installation
7089
jwt_client = Octokit::Client.new(:bearer_token => get_jwt_token)
7190
jwt_client.default_media_type = "application/vnd.github.machine-man-preview+json"
7291
app_token = jwt_client.create_installation_access_token(installation_id)
7392

93+
# Create octokit client that has access to installation resources
7494
@client = Octokit::Client.new(access_token: app_token[:token] )
7595
@client.default_media_type = "application/vnd.github.machine-man-preview+json"
76-
96+
97+
# List all repositories this installation has access to
7798
repository_list = []
7899
if webhook_data["installation"].key?("repositories_added")
79100
webhook_data["installation"]["repositories_added"].each do |repo|
@@ -84,6 +105,7 @@ def parse_installation_payload(json_body)
84105
repository_list = get_app_repositories
85106
end
86107

108+
# Create an issue in each repository stating an App has been given added
87109
create_issues(repository_list, webhook_data["sender"]["login"])
88110
end
89111
end

0 commit comments

Comments
 (0)