From e97401db2d071d6c28fc9a06f37378559d94a58c Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 11:58:28 +0000 Subject: [PATCH 1/6] Generate access token so we dont need github action --- Gemfile | 2 +- Gemfile.lock | 4 +++- lib/config.rb | 24 +++++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 16e3163..c5cbd9f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,9 +2,9 @@ source 'https://rubygems.org' +gem 'jwt' gem 'octokit' gem 'semantic' - group :development, :test do gem 'pry' gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 0e90ede..18865e8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -22,6 +22,7 @@ GEM i18n (1.8.8) concurrent-ruby (~> 1.0) json (2.5.1) + jwt (2.2.2) method_source (1.0.0) minitest (5.14.3) multipart-post (2.1.1) @@ -94,6 +95,7 @@ PLATFORMS ruby DEPENDENCIES + jwt octokit pry rspec @@ -102,4 +104,4 @@ DEPENDENCIES simplycop BUNDLED WITH - 2.1.4 + 2.2.3 diff --git a/lib/config.rb b/lib/config.rb index 0a863ef..096720c 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -2,15 +2,37 @@ require 'octokit' require 'json' +require 'jwt' # configuration for octokit class Config attr_reader :client, :payload, :version_file_path, :event_name def initialize - @client = Octokit::Client.new(access_token: ENV['ACCESS_TOKEN']) + @client = Octokit::Client.new(access_token: access_token) @payload = JSON.parse(File.read(ENV['GITHUB_EVENT_PATH'])) @event_name = ENV['GITHUB_EVENT_NAME'] @version_file_path = ENV['VERSION_FILE_PATH'] end + + private + + def access_token + bearer_client = Octokit::Client.new(bearer_token: bearer_token) + installation = bearer_client.find_repository_installation(payload['repository']['full_name']) + response = bearer_client.create_app_installation_access_token(installation[:id]) + response[:token] + end + + def bearer_token + payload = { + iat: Time.now.to_i, + exp: Time.now.to_i + (10 * 60), + iss: ENV['DOBBY_APP_ID'] + } + private_key = OpenSSL::PKey::RSA.new(ENV['DOBBY_PRIVATE_KEY']) + + JWT.encode(payload, private_key, 'RS256') + end end + From 30d7297f23baaa3aba17691d5632605406849cbc Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 11:59:17 +0000 Subject: [PATCH 2/6] rubocop --- lib/config.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/config.rb b/lib/config.rb index 096720c..78a313a 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -35,4 +35,3 @@ def bearer_token JWT.encode(payload, private_key, 'RS256') end end - From a970999b7dd28d0e122286c907934a1e348a12c4 Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 12:10:00 +0000 Subject: [PATCH 3/6] use constant for time Co-authored-by: @sleepyfox --- lib/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config.rb b/lib/config.rb index 78a313a..6389118 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -27,7 +27,7 @@ def access_token def bearer_token payload = { iat: Time.now.to_i, - exp: Time.now.to_i + (10 * 60), + exp: Time.now.to_i + (TEN_MINUTES), iss: ENV['DOBBY_APP_ID'] } private_key = OpenSSL::PKey::RSA.new(ENV['DOBBY_PRIVATE_KEY']) From 3800c0c155ca957723006187fb9db498c40db437 Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 12:10:24 +0000 Subject: [PATCH 4/6] set time costant Co-authored-by: @sleepyfox --- lib/config.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/config.rb b/lib/config.rb index 6389118..e799da8 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -4,6 +4,8 @@ require 'json' require 'jwt' +TEN_MINUTES = 600 # seconds + # configuration for octokit class Config attr_reader :client, :payload, :version_file_path, :event_name From 8b7f228878d41c7f5b8ad6b08f16aa0157d5812a Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 12:13:02 +0000 Subject: [PATCH 5/6] rubocop --- lib/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config.rb b/lib/config.rb index e799da8..2900084 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -29,7 +29,7 @@ def access_token def bearer_token payload = { iat: Time.now.to_i, - exp: Time.now.to_i + (TEN_MINUTES), + exp: Time.now.to_i + TEN_MINUTES, iss: ENV['DOBBY_APP_ID'] } private_key = OpenSSL::PKey::RSA.new(ENV['DOBBY_PRIVATE_KEY']) From 4ce7df05d61a71feaf065f9a64cf2890244596d3 Mon Sep 17 00:00:00 2001 From: Punit Jain Date: Mon, 15 Feb 2021 12:32:10 +0000 Subject: [PATCH 6/6] need payload --- lib/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config.rb b/lib/config.rb index 2900084..914b854 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -11,10 +11,10 @@ class Config attr_reader :client, :payload, :version_file_path, :event_name def initialize - @client = Octokit::Client.new(access_token: access_token) @payload = JSON.parse(File.read(ENV['GITHUB_EVENT_PATH'])) @event_name = ENV['GITHUB_EVENT_NAME'] @version_file_path = ENV['VERSION_FILE_PATH'] + @client = Octokit::Client.new(access_token: access_token) end private