|
| 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