Skip to content

Commit

Permalink
ruby upgrade (#21)
Browse files Browse the repository at this point in the history
* ruby up

* rubocop

* Update version.rb

---------

Co-authored-by: Dominik <[email protected]>
  • Loading branch information
gbarc-dt and DominikAlberski authored Feb 27, 2024
1 parent c96dc93 commit 146c5c4
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 125 deletions.
61 changes: 61 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require: rubocop-performance
require: rubocop-rspec

Documentation:
Enabled: false
Metrics/ClassLength:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Metrics/LineLength:
Enabled: false
Metrics/MethodLength:
Max: 40
Style/AsciiComments:
Enabled: false
Metrics/AbcSize:
Enabled: false
Style/GuardClause:
Enabled: false
Style/FormatStringToken:
Enabled: false
Lint/AssignmentInCondition:
Enabled: false
Style/IfUnlessModifier:
Enabled: false
Naming/MemoizedInstanceVariableName:
EnforcedStyleForLeadingUnderscores: required
Style/MultilineBlockChain:
Enabled: false
Lint/ConstantDefinitionInBlock:
Enabled: false
Naming/VariableNumber:
Enabled: false
Metrics/BlockLength:
Enabled: false
Lint/ImplicitStringConcatenation:
Enabled: false
Metrics/MethodLength:
Enabled: false
Style/StructInheritance:
Enabled: false
Lint/RescueException:
Enabled: false
Lint/SuppressedException:
Enabled: false
Naming/MemoizedInstanceVariableName:
Enabled: false
Style/SymbolProc:
Enabled: false
RSpec/MultipleMemoizedHelpers:
Enabled: false
RSpec/NamedSubject:
Enabled: false
RSpec/AnyInstance:
Enabled: false
RSpec/ContextWording:
Enabled: false
RSpec/ExampleLength:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: ruby
rvm:
- 2.5.5
- 3.2.2
before_install:
- gem install bundler -v 1.16.1
- gem install bundler -v 2.4.14
env:
matrix:
- ACTIVESUPPORT_VERSION=3.2.22.5
- ACTIVESUPPORT_VERSION=4.2.10
- ACTIVESUPPORT_VERSION=5.2.0
- ACTIVESUPPORT_VERSION=7.0.0
- ACTIVESUPPORT_VERSION=6.0.2.2
14 changes: 12 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
source "https://rubygems.org"
# frozen_string_literal: true

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in travis-github_apps.gemspec
gemspec
gem 'activesupport', ENV['ACTIVESUPPORT_VERSION'] if ENV['ACTIVESUPPORT_VERSION']

group :development, :test do
gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rspec'
gem 'simplecov'
gem 'simplecov-console'
end
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
7 changes: 4 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "travis/github_apps"
require 'bundler/setup'
require 'travis/github_apps'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +11,5 @@ require "travis/github_apps"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
47 changes: 25 additions & 22 deletions lib/travis/github_apps.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "travis/github_apps/version"
# frozen_string_literal: true

require 'travis/github_apps/version'

require 'active_support'
require 'json'
require 'jwt'
require 'redis'
require 'faraday'
require 'faraday_middleware'
require 'faraday/follow_redirects'

module Travis
# Object for working with GitHub Apps installations
Expand All @@ -20,29 +22,29 @@ class GithubApps
# GitHub allows JWT token to be valid up to 10 minutes.
# We set this to 9 minutes, to be sure.
#
JWT_TOKEN_TTL = 9*60
JWT_TOKEN_TTL = 9 * 60

# GitHub Apps tokens have a maximum life of 60 minutes, but we'll use 40 to
# allow a healthy buffer for timing issues, lag, etc.
#
APP_TOKEN_TTL = 40*60 # 40 minutes in seconds
APP_TOKEN_TTL = 40 * 60 # 40 minutes in seconds

attr_reader :accept_header, :cache, :installation_id, :debug, :repositories

def initialize(installation_id, config = {}, repositories = nil)
def initialize(installation_id, config = {}, repositories = nil) # rubocop:disable Metrics/CyclomaticComplexity
# ID of the GitHub App. This value can be found on the "General Information"
# page of the App.
#
# TODO: this value is set to those of the "travis-ci-staging" app for
# development.
#
@github_apps_id = ENV['GITHUB_APPS_ID'] || config[:apps_id] || 10131
@github_apps_id = ENV['GITHUB_APPS_ID'] || config[:apps_id] || 10_131

@github_api_endpoint = ENV['GITHUB_API_ENDPOINT'] || config[:api_endpoint] || "https://api.github.com"
@github_api_endpoint = ENV['GITHUB_API_ENDPOINT'] || config[:api_endpoint] || 'https://api.github.com'
@github_private_pem = ENV['GITHUB_PRIVATE_PEM'] || config[:private_pem] || read_private_key_from_file
@github_private_key = OpenSSL::PKey::RSA.new(@github_private_pem)

@accept_header = config.fetch(:accept_header, "application/vnd.github.machine-man-preview+json")
@accept_header = config.fetch(:accept_header, 'application/vnd.github.machine-man-preview+json')
@installation_id = installation_id
@repositories = repositories
@cache = Redis.new(config[:redis]) if config[:redis]
Expand Down Expand Up @@ -108,8 +110,8 @@ def fetch_new_access_token
response = github_api_conn.post do |req|
req.url "app/installations/#{installation_id}/access_tokens"
req.headers['Authorization'] = "Bearer #{authorization_jwt}"
req.headers['Accept'] = "application/vnd.github.machine-man-preview+json"
req.body = JSON.dump({:repository_ids => repositories_list, :permissions => { :contents => "read" } }) if repositories
req.headers['Accept'] = 'application/vnd.github.machine-man-preview+json'
req.body = JSON.dump({ repository_ids: repositories_list, permissions: { contents: 'read' } }) if repositories
end

# We probably want to do something different than `raise` here but I don't
Expand All @@ -121,8 +123,8 @@ def fetch_new_access_token

# Parse the response for the token and expiration
#
response_body = JSON.load(response.body)
github_access_token = response_body.fetch('token')
response_body = JSON.parse(response.body)
github_access_token = response_body.fetch('token')

# Store the access_token in redis, with a computed expiration. We need to
# recalculate this here instead of using APP_TOKEN_TTL because we don't
Expand All @@ -139,7 +141,7 @@ def repositories_list
end

def authorization_jwt
JWT.encode(jwt_payload, @github_private_key, "RS256")
JWT.encode(jwt_payload, @github_private_key, 'RS256')
end

def jwt_payload(jwt_token_ttl: JWT_TOKEN_TTL)
Expand All @@ -161,22 +163,23 @@ def jwt_payload(jwt_token_ttl: JWT_TOKEN_TTL)
def github_api_conn
@_github_api_conn ||= Faraday.new(url: @github_api_endpoint) do |f|
f.response :logger if debug
f.use FaradayMiddleware::FollowRedirects, limit: 5
f.response :follow_redirects, limit: 5
f.request :retry
f.adapter Faraday.default_adapter
end
end
end

def read_cache
cache.get(cache_key) if cache
cache&.get(cache_key)
end

def write_cache(token)
cache.set(cache_key, token, ex: APP_TOKEN_TTL) if cache
cache&.set(cache_key, token, ex: APP_TOKEN_TTL)
end

def cache_key
return "github_access_token_repo:#{installation_id}_#{repositories_list.join('-')}" if repositories

"github_access_token:#{installation_id}"
end

Expand All @@ -186,13 +189,13 @@ def cache_key
# source control. If you need a copy of this for development, ask Kerri.
#
def read_private_key_from_file
pem_files_in_root = Dir["*.pem"]
pem_files_in_root = Dir['*.pem']

@_github_private_key ||= if pem_files_in_root.any?
File.read(pem_files_in_root.first)
else
raise "Sorry, can't find local pem file"
end
File.read(pem_files_in_root.first)
else
raise "Sorry, can't find local pem file"
end
end
end
end
4 changes: 3 additions & 1 deletion lib/travis/github_apps/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Travis
class GithubApps
VERSION = "0.2.1"
VERSION = '0.3.0'
end
end
10 changes: 7 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require "bundler/setup"
require "travis/github_apps"
# frozen_string_literal: true

require 'bundler/setup'
require 'travis/github_apps'
require 'simplecov'
SimpleCov.start

RSpec.configure do |config|
config.mock_with :mocha

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
Expand Down
Loading

0 comments on commit 146c5c4

Please sign in to comment.