Skip to content

Commit ad1146e

Browse files
author
Thomas Osowski
committed
Simplify to use octokit.rb methods
1 parent d3670a3 commit ad1146e

File tree

3 files changed

+24
-45
lines changed

3 files changed

+24
-45
lines changed

app/ruby/app-issue-creator/Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ gem "json", "~> 1.8"
44
gem 'sinatra', '~> 1.3.5'
55
gem 'octokit'
66
gem 'jwt'
7-
gem 'rest_client'

app/ruby/app-issue-creator/Gemfile.lock

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ GEM
88
json (1.8.6)
99
jwt (1.5.6)
1010
multipart-post (2.0.0)
11-
netrc (0.7.9)
1211
octokit (4.7.0)
1312
sawyer (~> 0.8.0, >= 0.5.3)
1413
public_suffix (2.0.5)
1514
rack (1.6.8)
1615
rack-protection (1.5.3)
1716
rack
18-
rest_client (1.8.3)
19-
netrc (~> 0.7.7)
2017
sawyer (0.8.1)
2118
addressable (>= 2.3.5, < 2.6)
2219
faraday (~> 0.8, < 1.0)
@@ -33,8 +30,7 @@ DEPENDENCIES
3330
json (~> 1.8)
3431
jwt
3532
octokit
36-
rest_client
3733
sinatra (~> 1.3.5)
3834

3935
BUNDLED WITH
40-
1.14.6
36+
1.15.1

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

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'sinatra'
22
require 'jwt'
3-
require 'rest_client'
43
require 'json'
54
require 'active_support/all'
65
require 'octokit'
76

7+
@client = nil
88

99
post '/payload' do
1010
github_event = request.env['HTTP_X_GITHUB_EVENT']
@@ -17,8 +17,8 @@
1717

1818
end
1919

20-
def get_jwt
21-
path_to_pem = './platform-samples.pem'
20+
def get_jwt_token
21+
path_to_pem = './platform-samples-app-bot.2017-06-24.private-key.pem'
2222
private_pem = File.read(path_to_pem)
2323
private_key = OpenSSL::PKey::RSA.new(private_pem)
2424

@@ -27,79 +27,63 @@ def get_jwt
2727
iat: Time.now.to_i,
2828
# JWT expiration time (10 minute maximum)
2929
exp: 5.minutes.from_now.to_i,
30-
# Integration's GitHub identifier
30+
# GitHub App's identifier
3131
iss: 2583
3232
}
3333

3434
JWT.encode(payload, private_key, "RS256")
3535
end
3636

37-
def get_app_repositories(token)
38-
url = "https://api.github.com/installation/repositories"
39-
headers = {
40-
authorization: "token #{token}",
41-
accept: "application/vnd.github.machine-man-preview+json"
42-
}
37+
def get_app_repositories
4338

44-
response = RestClient.get(url,headers)
45-
json_response = JSON.parse(response)
39+
json_response = @client.list_installation_repos
4640

4741
repository_list = []
48-
if json_response["total_count"] > 0
42+
if json_response.count > 0
4943
json_response["repositories"].each do |repo|
5044
repository_list.push(repo["full_name"])
5145
end
46+
else
47+
puts json_response
5248
end
5349

5450
repository_list
5551
end
5652

5753

58-
def create_issues(access_token, repositories, sender_username)
59-
client = Octokit::Client.new(access_token: access_token )
60-
client.default_media_type = "application/vnd.github.machine-man-preview+json"
61-
54+
def create_issues(repositories, sender_username)
6255
repositories.each do |repo|
6356
begin
64-
client.create_issue(repo, "#{sender_username} created new app!", "Added GitHub App")
57+
@client.create_issue(repo, "#{sender_username} created new app!", "Added GitHub App")
6558
rescue
66-
puts "no issues in this repository"
59+
puts "Issues is disabled for this repository"
6760
end
6861
end
6962
end
7063

71-
72-
def get_app_token(access_tokens_url)
73-
jwt = get_jwt
74-
75-
headers = {
76-
authorization: "Bearer #{jwt}",
77-
accept: "application/vnd.github.machine-man-preview+json"
78-
}
79-
response = RestClient.post(access_tokens_url,{},headers)
80-
81-
app_token = JSON.parse(response)
82-
app_token["token"]
83-
end
84-
85-
8664
def parse_installation_payload(json_body)
8765
webhook_data = JSON.parse(json_body)
8866
if webhook_data["action"] == "created" || webhook_data["action"] == "added"
89-
access_tokens_url = webhook_data["installation"]["access_tokens_url"]
67+
installation_id = webhook_data["installation"]["id"]
9068
# Get token for app
91-
app_token = get_app_token(access_tokens_url)
92-
69+
puts get_jwt_token
70+
jwt_client = Octokit::Client.new(:bearer_token => get_jwt_token)
71+
jwt_client.default_media_type = "application/vnd.github.machine-man-preview+json"
72+
app_token = jwt_client.create_installation_access_token(installation_id)
73+
74+
@client = Octokit::Client.new(access_token: app_token[:token] )
75+
@client.default_media_type = "application/vnd.github.machine-man-preview+json"
76+
9377
repository_list = []
9478
if webhook_data["installation"].key?("repositories_added")
9579
webhook_data["installation"]["repositories_added"].each do |repo|
9680
repository_list.push(repo["full_name"])
9781
end
9882
else
9983
# Get repositories by query
100-
repository_list = get_app_repositories(app_token)
84+
repository_list = get_app_repositories
10185
end
10286

103-
create_issues(app_token, repository_list, webhook_data["sender"]["login"])
87+
create_issues(repository_list, webhook_data["sender"]["login"])
10488
end
10589
end

0 commit comments

Comments
 (0)