From 8a27063e8e9e415242592068859457b9071daa08 Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Tue, 30 Apr 2019 21:23:17 -0500 Subject: [PATCH 1/6] octokit --- .env-example | 5 ----- server.rb | 31 ++++++++----------------------- 2 files changed, 8 insertions(+), 28 deletions(-) delete mode 100644 .env-example diff --git a/.env-example b/.env-example deleted file mode 100644 index 9a932c2..0000000 --- a/.env-example +++ /dev/null @@ -1,5 +0,0 @@ -GITHUB_PRIVATE_KEY="" -GITHUB_APP_IDENTIFIER= -GITHUB_WEBHOOK_SECRET= -GITHUB_APP_USER_NAME= -GITHUB_APP_USER_EMAIL= diff --git a/server.rb b/server.rb index 8b9e422..2d30260 100644 --- a/server.rb +++ b/server.rb @@ -83,20 +83,8 @@ class GHAapp < Sinatra::Application # Create a new check run with the status queued def create_check_run - # At the time of writing, Octokit does not support the Checks API, but - # it does provide generic HTTP methods you can use: - # https://developer.github.com/v3/checks/runs/#create-a-check-run - check_run = @installation_client.post( - "repos/#{@payload['repository']['full_name']}/check-runs", - { - # This header allows for beta access to Checks API - accept: 'application/vnd.github.antiope-preview+json', - # The name of your check run. - name: 'Octo RuboCop', - # The payload structure differs depending on whether a check run or a check suite event occurred. - head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] - } - ) + head_sha = @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] + Octokit.create_check_run(@payload['repository']['full_name'], 'Octo RuboCop', head_sha) # You requested the creation of a check run from GitHub. Now, you'll wait # to get confirmation from GitHub, in the form of a webhook, that it was @@ -110,13 +98,8 @@ def initiate_check_run # to 'in_progress' and run the CI process. When the CI finishes, you'll # update the check run status to 'completed' and add the CI results. - # At the time of writing, Octokit doesn't support the Checks API, but - # it does provide generic HTTP methods you can use: - # https://developer.github.com/v3/checks/runs/#update-a-check-run - updated_check_run = @installation_client.patch( - "repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", + Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], { { - accept: 'application/vnd.github.antiope-preview+json', name: 'Octo RuboCop', status: 'in_progress', started_at: Time.now.utc.iso8601 @@ -192,8 +175,7 @@ def initiate_check_run text = "Octo RuboCop version: #{@output['metadata']['rubocop_version']}" # Mark the check run as complete! And if there are warnings, share them. - updated_check_run = @installation_client.patch( - "repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", + Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], { accept: 'application/vnd.github.antiope-preview+json', name: 'Octo RuboCop', @@ -306,6 +288,9 @@ def authenticate_installation(payload) @installation_id = payload['installation']['id'] @installation_token = @app_client.create_app_installation_access_token(@installation_id)[:token] @installation_client = Octokit::Client.new(bearer_token: @installation_token) + Octokit.configure do |c| + c.bearer_token = @installation_token + end end # Check X-Hub-Signature to confirm that this webhook was generated by @@ -323,7 +308,7 @@ def verify_webhook_signature their_signature_header = request.env['HTTP_X_HUB_SIGNATURE'] || 'sha1=' method, their_digest = their_signature_header.split('=') our_digest = OpenSSL::HMAC.hexdigest(method, WEBHOOK_SECRET, @payload_raw) - halt 401 unless their_digest == our_digest + # halt 401 unless their_digest == our_digest # The X-GITHUB-EVENT header provides the name of the event. # The action value indicates the which action triggered the event. From b6df75aaf2652346a7642dbf09b42571667b5c5f Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Tue, 30 Apr 2019 21:26:51 -0500 Subject: [PATCH 2/6] octokit --- Gemfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2611e27..38b40a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,14 +5,14 @@ GEM public_suffix (>= 2.0.2, < 4.0) ast (2.4.0) dotenv (2.5.0) - faraday (0.15.3) + faraday (0.15.4) multipart-post (>= 1.2, < 3) git (1.5.0) jaro_winkler (1.5.1) jwt (2.1.0) multipart-post (2.0.0) mustermann (1.0.3) - octokit (4.13.0) + octokit (4.14.0) sawyer (~> 0.8.0, >= 0.5.3) parallel (1.12.1) parser (2.5.3.0) From 3df2e5e78ff2bb8e04f2ffdbf7182b20b6869d4e Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Sun, 12 May 2019 17:50:46 -0500 Subject: [PATCH 3/6] cleanup --- Gemfile | 2 +- server.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index c03ffaf..8c4d7c0 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'http://rubygems.org' gem 'sinatra', '~> 2.0' gem 'jwt', '~> 2.1' -gem 'octokit', '~> 4.0' +gem 'octokit', '~> 4.14' gem 'rubocop' gem 'dotenv' gem 'git' diff --git a/server.rb b/server.rb index 2d30260..2f16a96 100644 --- a/server.rb +++ b/server.rb @@ -98,7 +98,7 @@ def initiate_check_run # to 'in_progress' and run the CI process. When the CI finishes, you'll # update the check run status to 'completed' and add the CI results. - Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], { + Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], { name: 'Octo RuboCop', status: 'in_progress', @@ -269,7 +269,7 @@ def authenticate_app iat: Time.now.to_i, # JWT expiration time (10 minute maximum) - exp: Time.now.to_i + (10 * 60), + exp: Time.now.to_i + (9 * 60), # Your GitHub App's identifier number iss: APP_IDENTIFIER @@ -287,7 +287,6 @@ def authenticate_app def authenticate_installation(payload) @installation_id = payload['installation']['id'] @installation_token = @app_client.create_app_installation_access_token(@installation_id)[:token] - @installation_client = Octokit::Client.new(bearer_token: @installation_token) Octokit.configure do |c| c.bearer_token = @installation_token end From a4bb8a1bda4a6d51363ab87b476670e67f250e77 Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Sat, 25 May 2019 19:39:07 -0500 Subject: [PATCH 4/6] moved options --- Gemfile.lock | 2 +- server.rb | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 38b40a1..80446fc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,7 +50,7 @@ DEPENDENCIES dotenv git jwt (~> 2.1) - octokit (~> 4.0) + octokit (~> 4.14) rubocop sinatra (~> 2.0) diff --git a/server.rb b/server.rb index 2f16a96..35b4a03 100644 --- a/server.rb +++ b/server.rb @@ -83,7 +83,7 @@ class GHAapp < Sinatra::Application # Create a new check run with the status queued def create_check_run - head_sha = @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] + head_sha = @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] Octokit.create_check_run(@payload['repository']['full_name'], 'Octo RuboCop', head_sha) # You requested the creation of a check run from GitHub. Now, you'll wait @@ -97,14 +97,8 @@ def initiate_check_run # Once the check run is created, you'll update the status of the check run # to 'in_progress' and run the CI process. When the CI finishes, you'll # update the check run status to 'completed' and add the CI results. - - Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], - { - name: 'Octo RuboCop', - status: 'in_progress', - started_at: Time.now.utc.iso8601 - } - ) + Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], name: 'Octo RuboCop', + status: 'in_progress', started_at: Time.now.utc.iso8601) # ***** RUN A CI TEST ***** # Ideally this would be performed async, so you could return immediately. From 97d969f12ef8a12ed2686244bdd972bab8b03faa Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Sat, 25 May 2019 19:42:03 -0500 Subject: [PATCH 5/6] presonal review --- .env-example | 5 +++++ server.rb | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .env-example diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..9a932c2 --- /dev/null +++ b/.env-example @@ -0,0 +1,5 @@ +GITHUB_PRIVATE_KEY="" +GITHUB_APP_IDENTIFIER= +GITHUB_WEBHOOK_SECRET= +GITHUB_APP_USER_NAME= +GITHUB_APP_USER_EMAIL= diff --git a/server.rb b/server.rb index 35b4a03..866ea5b 100644 --- a/server.rb +++ b/server.rb @@ -263,7 +263,7 @@ def authenticate_app iat: Time.now.to_i, # JWT expiration time (10 minute maximum) - exp: Time.now.to_i + (9 * 60), + exp: Time.now.to_i + (10 * 60), # Your GitHub App's identifier number iss: APP_IDENTIFIER @@ -301,7 +301,7 @@ def verify_webhook_signature their_signature_header = request.env['HTTP_X_HUB_SIGNATURE'] || 'sha1=' method, their_digest = their_signature_header.split('=') our_digest = OpenSSL::HMAC.hexdigest(method, WEBHOOK_SECRET, @payload_raw) - # halt 401 unless their_digest == our_digest + halt 401 unless their_digest == our_digest # The X-GITHUB-EVENT header provides the name of the event. # The action value indicates the which action triggered the event. From 71513fcbcbecba2c2b18cd07a1d3c31a6595626a Mon Sep 17 00:00:00 2001 From: "Roush,Kyle" Date: Sat, 25 May 2019 20:07:54 -0500 Subject: [PATCH 6/6] add back removed comments --- server.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server.rb b/server.rb index 866ea5b..414f8df 100644 --- a/server.rb +++ b/server.rb @@ -83,8 +83,11 @@ class GHAapp < Sinatra::Application # Create a new check run with the status queued def create_check_run + # The name of your check run. + name = 'Octo RuboCop' + # The payload structure differs depending on whether a check run or a check suite event occurred. head_sha = @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] - Octokit.create_check_run(@payload['repository']['full_name'], 'Octo RuboCop', head_sha) + Octokit.create_check_run(@payload['repository']['full_name'], name, head_sha) # You requested the creation of a check run from GitHub. Now, you'll wait # to get confirmation from GitHub, in the form of a webhook, that it was