1
1
require 'sinatra'
2
2
require 'jwt'
3
- require 'rest_client'
4
3
require 'json'
5
4
require 'active_support/all'
6
5
require 'octokit'
7
6
7
+ @client = nil
8
8
9
9
post '/payload' do
10
10
github_event = request . env [ 'HTTP_X_GITHUB_EVENT' ]
17
17
18
18
end
19
19
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'
22
22
private_pem = File . read ( path_to_pem )
23
23
private_key = OpenSSL ::PKey ::RSA . new ( private_pem )
24
24
@@ -27,79 +27,63 @@ def get_jwt
27
27
iat : Time . now . to_i ,
28
28
# JWT expiration time (10 minute maximum)
29
29
exp : 5 . minutes . from_now . to_i ,
30
- # Integration's GitHub identifier
30
+ # GitHub App's identifier
31
31
iss : 2583
32
32
}
33
33
34
34
JWT . encode ( payload , private_key , "RS256" )
35
35
end
36
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
- }
37
+ def get_app_repositories
43
38
44
- response = RestClient . get ( url , headers )
45
- json_response = JSON . parse ( response )
39
+ json_response = @client . list_installation_repos
46
40
47
41
repository_list = [ ]
48
- if json_response [ "total_count" ] > 0
42
+ if json_response . count > 0
49
43
json_response [ "repositories" ] . each do |repo |
50
44
repository_list . push ( repo [ "full_name" ] )
51
45
end
46
+ else
47
+ puts json_response
52
48
end
53
49
54
50
repository_list
55
51
end
56
52
57
53
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 )
62
55
repositories . each do |repo |
63
56
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" )
65
58
rescue
66
- puts "no issues in this repository"
59
+ puts "Issues is disabled for this repository"
67
60
end
68
61
end
69
62
end
70
63
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
64
def parse_installation_payload ( json_body )
87
65
webhook_data = JSON . parse ( json_body )
88
66
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 " ]
90
68
# 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
+
93
77
repository_list = [ ]
94
78
if webhook_data [ "installation" ] . key? ( "repositories_added" )
95
79
webhook_data [ "installation" ] [ "repositories_added" ] . each do |repo |
96
80
repository_list . push ( repo [ "full_name" ] )
97
81
end
98
82
else
99
83
# Get repositories by query
100
- repository_list = get_app_repositories ( app_token )
84
+ repository_list = get_app_repositories
101
85
end
102
86
103
- create_issues ( app_token , repository_list , webhook_data [ "sender" ] [ "login" ] )
87
+ create_issues ( repository_list , webhook_data [ "sender" ] [ "login" ] )
104
88
end
105
89
end
0 commit comments