Skip to content

Commit d3670a3

Browse files
author
Thomas Osowski
committed
Add issue creator app
1 parent bfedfa6 commit d3670a3

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

app/ruby/app-issue-creator/Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source "http://rubygems.org"
2+
3+
gem "json", "~> 1.8"
4+
gem 'sinatra', '~> 1.3.5'
5+
gem 'octokit'
6+
gem 'jwt'
7+
gem 'rest_client'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
addressable (2.5.1)
5+
public_suffix (~> 2.0, >= 2.0.2)
6+
faraday (0.12.1)
7+
multipart-post (>= 1.2, < 3)
8+
json (1.8.6)
9+
jwt (1.5.6)
10+
multipart-post (2.0.0)
11+
netrc (0.7.9)
12+
octokit (4.7.0)
13+
sawyer (~> 0.8.0, >= 0.5.3)
14+
public_suffix (2.0.5)
15+
rack (1.6.8)
16+
rack-protection (1.5.3)
17+
rack
18+
rest_client (1.8.3)
19+
netrc (~> 0.7.7)
20+
sawyer (0.8.1)
21+
addressable (>= 2.3.5, < 2.6)
22+
faraday (~> 0.8, < 1.0)
23+
sinatra (1.3.6)
24+
rack (~> 1.4)
25+
rack-protection (~> 1.3)
26+
tilt (~> 1.3, >= 1.3.3)
27+
tilt (1.4.1)
28+
29+
PLATFORMS
30+
ruby
31+
32+
DEPENDENCIES
33+
json (~> 1.8)
34+
jwt
35+
octokit
36+
rest_client
37+
sinatra (~> 1.3.5)
38+
39+
BUNDLED WITH
40+
1.14.6

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require 'sinatra'
2+
require 'jwt'
3+
require 'rest_client'
4+
require 'json'
5+
require 'active_support/all'
6+
require 'octokit'
7+
8+
9+
post '/payload' do
10+
github_event = request.env['HTTP_X_GITHUB_EVENT']
11+
if github_event == "integration_installation"
12+
#|| github_event == "installation_repositories"
13+
parse_installation_payload(request.body.read)
14+
else
15+
puts "New event #{github_event}"
16+
end
17+
18+
end
19+
20+
def get_jwt
21+
path_to_pem = './platform-samples.pem'
22+
private_pem = File.read(path_to_pem)
23+
private_key = OpenSSL::PKey::RSA.new(private_pem)
24+
25+
payload = {
26+
# issued at time
27+
iat: Time.now.to_i,
28+
# JWT expiration time (10 minute maximum)
29+
exp: 5.minutes.from_now.to_i,
30+
# Integration's GitHub identifier
31+
iss: 2583
32+
}
33+
34+
JWT.encode(payload, private_key, "RS256")
35+
end
36+
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+
}
43+
44+
response = RestClient.get(url,headers)
45+
json_response = JSON.parse(response)
46+
47+
repository_list = []
48+
if json_response["total_count"] > 0
49+
json_response["repositories"].each do |repo|
50+
repository_list.push(repo["full_name"])
51+
end
52+
end
53+
54+
repository_list
55+
end
56+
57+
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+
62+
repositories.each do |repo|
63+
begin
64+
client.create_issue(repo, "#{sender_username} created new app!", "Added GitHub App")
65+
rescue
66+
puts "no issues in this repository"
67+
end
68+
end
69+
end
70+
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+
86+
def parse_installation_payload(json_body)
87+
webhook_data = JSON.parse(json_body)
88+
if webhook_data["action"] == "created" || webhook_data["action"] == "added"
89+
access_tokens_url = webhook_data["installation"]["access_tokens_url"]
90+
# Get token for app
91+
app_token = get_app_token(access_tokens_url)
92+
93+
repository_list = []
94+
if webhook_data["installation"].key?("repositories_added")
95+
webhook_data["installation"]["repositories_added"].each do |repo|
96+
repository_list.push(repo["full_name"])
97+
end
98+
else
99+
# Get repositories by query
100+
repository_list = get_app_repositories(app_token)
101+
end
102+
103+
create_issues(app_token, repository_list, webhook_data["sender"]["login"])
104+
end
105+
end

0 commit comments

Comments
 (0)