Skip to content

Commit

Permalink
fix: add rubocop and fix offenses (#441)
Browse files Browse the repository at this point in the history
* Add rubocop dependency

* fix: add rubocop and fix offenses

* Remove rails cops

* Fix weird alignment
  • Loading branch information
bangn authored May 31, 2021
1 parent 404e14c commit 7c1c304
Show file tree
Hide file tree
Showing 159 changed files with 491 additions and 1,327 deletions.
905 changes: 26 additions & 879 deletions .rubocop.yml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ gem 'bump', '~> 0.5'

group :development do
gem 'pry-byebug'
gem 'rubocop', '~>1.1'
gem 'rubocop-performance'
gem 'sequel-annotate', '~>1.3'
end

Expand Down Expand Up @@ -39,4 +41,4 @@ end

if ENV['X_PACT_DEVELOPMENT'] == 'true'
gem 'pact-support', path: '../pact-support'
end
end
2 changes: 1 addition & 1 deletion config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
if File.exists?(ENV['BUNDLE_GEMFILE'])
if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'bundler/setup'
Bundler.require
end
Expand Down
2 changes: 1 addition & 1 deletion db/ddl_statements/latest_pact_consumer_version_orders.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The consumer id, provider id, and consumer version order
# for the latest consumer version that has a pact with that provider.

def latest_pact_consumer_version_orders_v1(connection = nil)
def latest_pact_consumer_version_orders_v1(_connection = nil)
"select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order
from all_pact_publications
group by provider_id, consumer_id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def latest_pact_publications_by_consumer_versions_v2(connection = nil)
def latest_pact_publications_by_consumer_versions_v2(_connection = nil)
"select app.*
from latest_pact_publication_ids_for_consumer_versions lpp
inner join all_pact_publications app
Expand All @@ -8,7 +8,7 @@ def latest_pact_publications_by_consumer_versions_v2(connection = nil)
end

# Don't need all the join keys, just pact_publication_id
def latest_pact_publications_by_consumer_versions_v3(connection)
def latest_pact_publications_by_consumer_versions_v3(_connection)
"select app.*
from latest_pact_publication_ids_for_consumer_versions lpp
inner join all_pact_publications app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
version = from(:versions)
.where(number: line[:provider_version], pacticipant_id: line[:provider_id]).single_record
version_id = if version
version[:id]
else
from(:versions).insert(
number: line[:provider_version],
pacticipant_id: line[:provider_id],
created_at: line[:created_at],
updated_at: line[:created_at]
)
end
version[:id]
else
from(:versions).insert(
number: line[:provider_version],
pacticipant_id: line[:provider_id],
created_at: line[:created_at],
updated_at: line[:created_at]
)
end
from(:verifications).where(id: line[:id]).update(provider_version_id: version_id)
end
end
Expand Down
1 change: 0 additions & 1 deletion db/migrations/20180311_optimise_head_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

Sequel.migration do
up do
pp = :pact_publications
# For each consumer_id/provider_id/tag_name, the version order of the latest version that has a pact
create_or_replace_view(:latest_tagged_pact_consumer_version_orders,
latest_tagged_pact_consumer_version_orders_v2(self))
Expand Down
2 changes: 1 addition & 1 deletion db/test/backwards_compatibility/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FileUtils.touch "pids/#{Process.pid}"
DATABASE_CREDENTIALS = {adapter: "sqlite", database: "pact_broker_database.sqlite3", :encoding => 'utf8'}

app = PactBroker::App.new do | config |
config.logger.formatter = proc do |severity, datetime, progname, msg|
config.logger.formatter = proc do |severity, _datetime, _progname, msg|
"v#{PactBroker::VERSION} #{severity} -- : #{msg}\n"
end
config.database_connection = Sequel.connect(DATABASE_CREDENTIALS.merge(:logger => config.logger))
Expand Down
4 changes: 2 additions & 2 deletions db/test/backwards_compatibility/spec/publish_pact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
subject { put path, nil, {'CONTENT_TYPE' => 'application/json' }; last_response }

it "returns a success status" do
expect(subject.status.to_s).to match /20\d/
expect(subject.status.to_s).to match(/20\d/)
end
end

describe "publishing a pact" do
subject { put path, pact_content, {'CONTENT_TYPE' => 'application/json' }; last_response }

it "returns a success status" do
expect(subject.status.to_s).to match /20\d/
expect(subject.status.to_s).to match(/20\d/)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def self.connect db_credentials
con.extension(:pagination)
con.extension(:statement_timeout)
con.extend_datasets do
# rubocop: disable Lint/NestedMethodDefinition
def any?
!empty?
end
# rubocop: enable Lint/NestedMethodDefinition
end
con.pool.connection_validation_timeout = -1 #Check the connection on every request
con.timezone = :utc
Expand Down Expand Up @@ -72,7 +74,7 @@ def self.postgres?
PACT_BROKER_DB ||= connection_for_env ENV.fetch('RACK_ENV')

def self.health_check
PACT_BROKER_DB.synchronize do |c| c
PACT_BROKER_DB.synchronize do |c|
PACT_BROKER_DB.valid_connection? c
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/doc/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize pact_dir, doc_dir, options
@file_extension = options[:file_extension]
@index_renderer = options[:index_renderer]
@index_name = options[:index_name]
@after = options.fetch(:after, lambda{|pact_dir, target_dir, consumer_contracts| })
@after = options.fetch(:after, lambda{|pact_dir_arg, target_dir_arg, consumer_contracts_arg| })
end

def call
Expand Down
8 changes: 4 additions & 4 deletions lib/pact/doc/interaction_view_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def initialize interaction, consumer_contract
def id
@id ||= begin
full_desc = if has_provider_state?
"#{description} given #{interaction.provider_state}"
else
description
end
"#{description} given #{interaction.provider_state}"
else
description
end
CGI.escapeHTML(full_desc.gsub(/\s+/,'_'))
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/pact/doc/sort_interactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def self.call interactions
interactions.sort{|a, b| sortable_id(a) <=> sortable_id(b)}
end

private

def self.sortable_id interaction
"#{(interaction.description || '').downcase} #{interaction.response.status} #{(interaction.provider_state || '').downcase}"
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def really_put?
end

module PactBroker
# rubocop: disable Metrics/MethodLength
def self.build_api(application_context = PactBroker::ApplicationContext.default_application_context)
pact_api = Webmachine::Application.new do |app|
app.routes do
Expand Down Expand Up @@ -149,4 +150,5 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_
API ||= begin
build_api
end
# rubocop: enable Metrics/MethodLength
end
8 changes: 2 additions & 6 deletions lib/pact_broker/api/contracts/dry_validation_workarounds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ def select_first_message(messages)
end

def flatten_array_of_hashes(array_of_hashes)
new_messages = array_of_hashes.collect do | index, hash_or_array |
array = if hash_or_array.is_a?(Hash)
hash_or_array.values.flatten
else
hash_or_array
end
array_of_hashes.collect do | index, hash_or_array |
array = hash_or_array.is_a?(Hash) ? hash_or_array.values.flatten : hash_or_array
array.collect { | text | "#{text} at index #{index}"}
end.flatten
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def self.not_provided?(value)
# when setting a tag, latest=true and latest=false are both valid
# when setting the branch, it doesn't make sense to verify all pacts for a branch,
# so latest is not required, but cannot be set to false
# rubocop: disable Metrics/CyclomaticComplexity, Metrics/MethodLength
def self.validate_consumer_version_selector(selector, index)
errors = []

Expand Down Expand Up @@ -118,6 +119,7 @@ def self.validate_consumer_version_selector(selector, index)

errors
end
# rubocop: enable Metrics/CyclomaticComplexity, Metrics/MethodLength
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/api/contracts/webhook_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def validate(*)
@first_errors.messages[key] = @first_errors.messages[key][0...1]
end

# rubocop: disable Lint/NestedMethodDefinition
def self.errors; @first_errors end
# rubocop: enable Lint/NestedMethodDefinition

result
end

Expand Down
8 changes: 4 additions & 4 deletions lib/pact_broker/api/decorators/dashboard_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def to_hash(options)

attr_reader :index_items

def items(index_items, base_url)
def items(_index_items, base_url)
sorted_index_items.collect do | index_item |
index_item_hash(index_item.consumer, index_item.provider, index_item.consumer_version, index_item, base_url)
end
Expand All @@ -53,7 +53,7 @@ def index_item_hash(consumer, provider, consumer_version, index_item, base_url)
}
end

def consumer_hash(index_item, consumer, consumer_version, base_url)
def consumer_hash(index_item, _consumer, _consumer_version, base_url)
{
name: index_item.consumer_name,
version: {
Expand All @@ -73,7 +73,7 @@ def consumer_hash(index_item, consumer, consumer_version, base_url)
}
end

def provider_hash(index_item, provider, base_url)
def provider_hash(index_item, _provider, base_url)
hash = {
name: index_item.provider_name,
version: nil,
Expand Down Expand Up @@ -153,7 +153,7 @@ def verification_tags(index_item, base_url)
end
end

def latest_webhook_execution(index_item, base_url)
def latest_webhook_execution(index_item, _base_url)
if index_item.last_webhook_execution_date
{
triggeredAt: format_date_time(index_item.last_webhook_execution_date)
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/dashboard_text_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def to_text(options)

attr_reader :index_items

def items(index_items, base_url)
def items(index_items, _base_url)
index_items.collect do | index_item |
index_item_object(index_item)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def hash_for_row(line, base_url)
line_hash(consumer, provider, consumer_version, provider_version, line, base_url)
end

# rubocop: disable Metrics/ParameterLists
def line_hash(consumer, provider, consumer_version, provider_version, line, base_url)
{
consumer: consumer_hash(line, consumer, consumer_version, base_url),
Expand All @@ -75,6 +76,7 @@ def line_hash(consumer, provider, consumer_version, provider_version, line, base
verificationResult: verification_hash(line, base_url)
}
end
# rubocop: enable Metrics/ParameterLists

def consumer_hash(line, consumer, consumer_version, base_url)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/matrix_text_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(lines)
@lines = lines
end

def to_text(options)
def to_text(_options)
json_decorator = PactBroker::Api::Decorators::MatrixDecorator.new(lines)
data = lines.collect do | line |
Line.new(line.consumer_name, line.consumer_version_number, line.pact_revision_number, line.provider_name, line.provider_version_number, line.verification_number, line.success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class PactWebhooksStatusDecorator < BaseDecorator
end

def summary
counts = represented.group_by(&:status).each_with_object({}) do | (status, triggered_webhooks), counts |
counts[status] = triggered_webhooks.count
counts = represented.group_by(&:status).each_with_object({}) do | (status, triggered_webhooks), count |
count[status] = triggered_webhooks.count
end
OpenStruct.new(counts)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PacticipantCollectionDecorator < BaseDecorator
end

class DeprecatedPacticipantDecorator < PactBroker::Api::Decorators::PacticipantDecorator
property :title, getter: ->(something) { "DEPRECATED - Please use the embedded pacticipants collection" }
property :title, getter: ->(_something) { "DEPRECATED - Please use the embedded pacticipants collection" }
end

class NonEmbeddedPacticipantCollectionDecorator < BaseDecorator
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/api/decorators/provider_pacts_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Decorators
class ProviderPactsDecorator < BaseDecorator

link :self do | context |
suffix = context[:tag] ? " with tag '#{context[:tag]}'" : ""
{
href: context[:resource_url],
title: context[:title]
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/api/decorators/reason_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def to_s

attr_reader :reason, :ignored

# rubocop: disable Metrics/CyclomaticComplexity
def reason_text
case reason
when PactBroker::Matrix::PactNotEverVerifiedByProvider
Expand Down Expand Up @@ -74,6 +75,7 @@ def interaction_description(interaction)
end
end
end
# rubocop: enable Metrics/CyclomaticComplexity
end
end
end
2 changes: 2 additions & 0 deletions lib/pact_broker/api/decorators/relationships_csv_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize pacts
@index_items = pacts.collect{|pact| PactBroker::Domain::IndexItem.new(pact.consumer, pact.provider)}
end

# rubocop: disable Metrics/CyclomaticComplexity
def to_csv
hash = {}

Expand All @@ -32,6 +33,7 @@ def to_csv
end
end
end
# rubocop: enable Metrics/CyclomaticComplexity

def pacticipant_array pacticipant, order
[pacticipant.id, pacticipant.name, 1, 1, 0, order]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def headers
def body
begin
::JSON.parse(represented.body)
rescue StandardError => e
rescue StandardError => _ex
represented.body
end
end
Expand All @@ -50,7 +50,7 @@ def headers
def body
begin
::JSON.parse(represented.body)
rescue StandardError => e
rescue StandardError => _ex
represented.body
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def badge_url_for_latest_pact pact, base_url = ''
"#{latest_pact_url(base_url, pact)}/badge.svg"
end

def matrix_url consumer_name, provider_name, base_url = ''
def matrix_url consumer_name, provider_name, _base_url = ''
"/matrix/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
end

Expand Down Expand Up @@ -322,11 +322,11 @@ def released_versions_for_version_and_environment_url(version, environment, base
"#{version_url(base_url, version)}/released-versions/environment/#{environment.uuid}"
end

def deployed_version_url(deployed_version, base_url = '')
def deployed_version_url(deployed_version, _base_url = '')
"/deployed-versions/#{deployed_version.uuid}"
end

def released_version_url(released_version, base_url = '')
def released_version_url(released_version, _base_url = '')
"/released-versions/#{released_version.uuid}"
end

Expand Down
Loading

0 comments on commit 7c1c304

Please sign in to comment.