Skip to content

Commit

Permalink
Merge pull request #4096 from envato/rubocop
Browse files Browse the repository at this point in the history
Bump rubocop from 0.93.1 to 1.42.0
  • Loading branch information
grosser authored Feb 12, 2024
2 parents 6e48b68 + 6321822 commit 58f4be0
Show file tree
Hide file tree
Showing 52 changed files with 283 additions and 218 deletions.
5 changes: 1 addition & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ require:

AllCops:
DisplayCopNames: true
NewCops: enable
NewCops: disable
Exclude:
- db/schema.rb
- vendor/**/*
- tmp/**/*
- node_modules/**/*

Layout/LineLength:
Max: 120

Metrics/ClassLength:
Enabled: false

Expand Down
22 changes: 12 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,9 @@ GEM
parallel (1.24.0)
parallel_tests (2.32.0)
parallel
parser (2.7.2.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
path_expander (1.1.1)
pg (1.5.4)
pry (0.12.2)
Expand Down Expand Up @@ -576,7 +577,7 @@ GEM
rake (13.1.0)
rbtree3 (0.7.1)
recursive-open-struct (1.1.3)
regexp_parser (1.8.2)
regexp_parser (2.9.0)
request_store (1.5.1)
rack (>= 1.4)
rest-client (2.1.0)
Expand All @@ -588,17 +589,18 @@ GEM
rollbar (2.27.1)
rollbar-user_informer (0.1.0)
rollbar (~> 2.15)
rubocop (0.93.1)
rubocop (1.42.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 2.7.1.5)
parser (>= 3.1.2.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 0.6.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.24.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.8.0)
parser (>= 2.7.1.5)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-rails (2.3.2)
rack (>= 1.1)
rubocop (>= 0.72.0)
Expand Down
14 changes: 8 additions & 6 deletions app/controllers/integrations/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ def create_docker_images
options = {git_sha: commit, dockerfile: dockerfile}
next if scope.where(options).first

build = project.builds.create!(options.merge(
git_ref: branch,
description: message,
creator: user,
name: "Release #{branch}"
))
build = project.builds.create!(
options.merge(
git_ref: branch,
description: message,
creator: user,
name: "Release #{branch}"
)
)
DockerBuilderService.new(build).run(tag_as_latest: true)
end
end
Expand Down
12 changes: 7 additions & 5 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ def login(options = {})
uid = auth_hash.uid
end

user = find_or_create_user_from_hash(options.merge(
external_id: "#{strategy.name}-#{uid}",
name: auth_hash.info.name,
email: auth_hash.info.email
))
user = find_or_create_user_from_hash(
options.merge(
external_id: "#{strategy.name}-#{uid}",
name: auth_hash.info.name,
email: auth_hash.info.email
)
)

if user.persisted?
self.current_user = user
Expand Down
6 changes: 3 additions & 3 deletions app/models/build.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true
class Build < ActiveRecord::Base
SHA1_REGEX = /\A[0-9a-f]{40}\Z/i.freeze
SHA256_REGEX = /\A(sha256:)?[0-9a-f]{64}\Z/i.freeze
DIGEST_REGEX = /\A[\w.-]+[\w.-]*(:\d+)?[\w.\/-]*@sha256:[0-9a-f]{64}\Z/i.freeze
SHA1_REGEX = /\A[0-9a-f]{40}\Z/i
SHA256_REGEX = /\A(sha256:)?[0-9a-f]{64}\Z/i
DIGEST_REGEX = /\A[\w.-]+[\w.-]*(:\d+)?[\w.\/-]*@sha256:[0-9a-f]{64}\Z/i

belongs_to :project, inverse_of: :builds
belongs_to :docker_build_job, class_name: 'Job', optional: true, inverse_of: false
Expand Down
4 changes: 2 additions & 2 deletions app/models/changeset/commit.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Changeset::Commit
PULL_REQUEST_MERGE_MESSAGE = /\AMerge pull request #(\d+)/.freeze
PULL_REQUEST_SQUASH_MESSAGE = /\A.*\(#(\d+)\)$/.freeze
PULL_REQUEST_MERGE_MESSAGE = /\AMerge pull request #(\d+)/
PULL_REQUEST_SQUASH_MESSAGE = /\A.*\(#(\d+)\)$/

attr_reader :project

Expand Down
8 changes: 4 additions & 4 deletions app/models/changeset/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ class Changeset::PullRequest
CODE_ONLY = "[A-Z][A-Z\\d]+-\\d+" # e.g., S4MS0N-123, SAM-456
PUNCT = "\\s|\\p{Punct}|~|="

WEBHOOK_FILTER = /(^|\s)\[samson review\]($|\s)/i.freeze
WEBHOOK_FILTER = /(^|\s)\[samson review\]($|\s)/i

# Matches URLs to JIRA issues.
JIRA_ISSUE_URL = %r[https?://[\da-z.\-]+\.[a-z.]{2,6}/browse/#{CODE_ONLY}(?=#{PUNCT}|$)].freeze
JIRA_ISSUE_URL = %r[https?://[\da-z.\-]+\.[a-z.]{2,6}/browse/#{CODE_ONLY}(?=#{PUNCT}|$)]

# Matches "VOICE-1234" or "[VOICE-1234]"
JIRA_CODE_TITLE = /(\[)*(#{CODE_ONLY})(\])*/.freeze
JIRA_CODE_TITLE = /(\[)*(#{CODE_ONLY})(\])*/

# Matches "VOICE-1234" only
JIRA_CODE = /(?<=#{PUNCT}|^)(#{CODE_ONLY})(?=#{PUNCT}|$)/.freeze
JIRA_CODE = /(?<=#{PUNCT}|^)(#{CODE_ONLY})(?=#{PUNCT}|$)/

# Github pull request events can be triggered by a number of actions such as 'labeled', 'assigned'
# Actions which aren't related to a code push should not trigger a samson deploy.
Expand Down
2 changes: 1 addition & 1 deletion app/models/image_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ImageBuilder
class << self
extend ::Samson::PerformanceTracer::Tracers

DIGEST_SHA_REGEX = /Digest:.*(sha256:[0-9a-f]{64})/i.freeze
DIGEST_SHA_REGEX = /Digest:.*(sha256:[0-9a-f]{64})/i

def build_image(dir, build, executor, tag_as_latest:, **args)
if DockerRegistry.all.empty?
Expand Down
4 changes: 2 additions & 2 deletions app/models/outbound_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def connection
connection.adapter Faraday.default_adapter

case auth_type
when "None" # rubocop:disable Lint/EmptyWhen noop
when "None" # noop
when "Basic" then connection.request :authorization, :basic, username, password
when "Bearer", "Token" then connection.request :authorization, auth_type, password
else raise ArgumentError, "Unsupported auth_type #{auth_type.inspect}"
Expand All @@ -109,7 +109,7 @@ def connection

def validate_auth
case auth_type
when "None" # rubocop:disable Lint/EmptyWhen noop
when "None" # noop
when "Basic" then errors.add :username, "and password must be set" if !username? || !password?
when "Bearer", "Token" then errors.add :password, "must be set" unless password?
else errors.add(:auth_type, "unknown, supported types are #{AUTH_TYPES.to_sentence}")
Expand Down
4 changes: 2 additions & 2 deletions app/models/release.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Release < ActiveRecord::Base
NUMBER_REGEX = /\A#{Samson::RELEASE_NUMBER}\z/.freeze
VERSION_REGEX = /\Av(#{Samson::RELEASE_NUMBER})\z/.freeze
NUMBER_REGEX = /\A#{Samson::RELEASE_NUMBER}\z/
VERSION_REGEX = /\Av(#{Samson::RELEASE_NUMBER})\z/

belongs_to :project, touch: true, inverse_of: :releases
belongs_to :author, class_name: "User", inverse_of: false
Expand Down
2 changes: 1 addition & 1 deletion app/models/terminal_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TerminalExecutor
HIDDEN_TXT = "HIDDEN"
KILL_TIMEOUT = Integer(ENV['DEPLOY_KILL_TIMEOUT'] || '1')

CURSOR = /\e\[\d*[ABCDK]/.freeze
CURSOR = /\e\[\d*[ABCDK]/

attr_reader :pid, :pgid, :output, :timeout

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class User < ActiveRecord::Base
include HasRole

TIME_FORMATS = ['local', 'utc', 'relative'].freeze
GITHUB_USERNAME_REGEX = /\A[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}\Z/i.freeze
GITHUB_USERNAME_REGEX = /\A[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}\Z/i

has_soft_deletion default_scope: true
include SoftDeleteWithDestroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/webhook_recorder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class WebhookRecorder
KEY = 'WebhookRecorder-v2'
NATIVE_HEADER = /^[_A-Z]+$/.freeze
NATIVE_HEADER = /^[_A-Z]+$/
IGNORED_HEADERS = ["QUERY_STRING", "RAW_POST_DATA"].freeze

class << self
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def self.deprecated_url(var)

# Token used to request badges
config.samson.badge_token = \
Digest::MD5.hexdigest("badge_token#{(ENV['BADGE_TOKEN_BASE'] || Samson::Application.config.secret_key_base)}")
Digest::MD5.hexdigest("badge_token#{ENV['BADGE_TOKEN_BASE'] || Samson::Application.config.secret_key_base}")
end
end

Expand Down
10 changes: 6 additions & 4 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
# make our tests fast by avoiding asset compilation
# but do not raise when assets are not compiled either
Rails.application.config.assets.compile = false
Sprockets::Rails::Helper.prepend(Module.new do
def resolve_asset_path(path, *)
super || path
Sprockets::Rails::Helper.prepend(
Module.new do
def resolve_asset_path(path, *)
super || path
end
end
end)
)
14 changes: 8 additions & 6 deletions config/initializers/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# and any other backend that uses faraday
require 'faraday'

Faraday::Connection.prepend(Module.new do
def run_request(method, url, *args)
raise "Missing url for logging, do not use `do |request| request.url = ` pattern" unless url
log_url = url.gsub(/key=\w+/, "key=redacted") # ignore datadog credentials and maybe others
ActiveSupport::Notifications.instrument("request.faraday.samson", method: method, url: log_url) { super }
Faraday::Connection.prepend(
Module.new do
def run_request(method, url, *args)
raise "Missing url for logging, do not use `do |request| request.url = ` pattern" unless url
log_url = url.gsub(/key=\w+/, "key=redacted") # ignore datadog credentials and maybe others
ActiveSupport::Notifications.instrument("request.faraday.samson", method: method, url: log_url) { super }
end
end
end)
)
12 changes: 7 additions & 5 deletions config/initializers/migration_resetter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#
# to reproduce, add 2 migrations one that removes a column and one that puts Stage.column_hash to see if it removed
# an even safer (but slower/hackier) option would be to reset after each column change
ActiveRecord::Migration.prepend(Module.new do
def exec_migration(*)
ActiveRecord::Base.connection.schema_cache.clear!
super
ActiveRecord::Migration.prepend(
Module.new do
def exec_migration(*)
ActiveRecord::Base.connection.schema_cache.clear!
super
end
end
end)
)
12 changes: 7 additions & 5 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
require 'pagy/extras/overflow'

# pagination after last page renders an empty page
Pagy::Backend.prepend(Module.new do
private
Pagy::Backend.prepend(
Module.new do
private

def pagy(collection, options)
super collection, options.merge(overflow: :empty_page)
def pagy(collection, options)
super collection, options.merge(overflow: :empty_page)
end
end
end)
)
10 changes: 6 additions & 4 deletions config/initializers/vault.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# instrument all vault calls
require 'vault'

Vault::Client.prepend(Module.new do
def request(method, path, *)
ActiveSupport::Notifications.instrument("request.vault.samson", method: method, path: path) { super }
Vault::Client.prepend(
Module.new do
def request(method, path, *)
ActiveSupport::Notifications.instrument("request.vault.samson", method: method, path: path) { super }
end
end
end)
)
12 changes: 7 additions & 5 deletions config/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
# We saw initializers hanging boot so we are logging before every initializer is called to easily debug
# Test: boot up rails and look at the logs
if ENV['SERVER_MODE'] && !Rails.env.development?
Rails::Engine.prepend(Module.new do
def load(file, *)
Rails.logger.info "Loading initializer #{file.sub("#{Bundler.root}/", "")}"
super
Rails::Engine.prepend(
Module.new do
def load(file, *)
Rails.logger.info "Loading initializer #{file.sub("#{Bundler.root}/", "")}"
super
end
end
end)
)
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
class AddDockerReleaseBranchToProjects < ActiveRecord::Migration[5.0]
class Project < ActiveRecord::Base; end

def change
add_column :projects, :docker_release_branch, :string
Project.all.each do |project|
Expand Down
2 changes: 1 addition & 1 deletion lib/samson/secrets/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Secrets
module Manager
ID_PARTS = [:environment_permalink, :project_permalink, :deploy_group_permalink, :key].freeze
ID_PART_SEPARATOR = "/"
SECRET_ID_REGEX = %r{[\w/-]+}.freeze
SECRET_ID_REGEX = %r{[\w/-]+}
SECRET_LOOKUP_CACHE = 'secret_lookup_cache_v3'
SECRET_LOOKUP_CACHE_MUTEX = Mutex.new
VALUE_HASHED_BASE = Digest::SHA2.hexdigest("#{Samson::Application.config.secret_key_base}usedforhashing")
Expand Down
2 changes: 1 addition & 1 deletion lib/samson/secrets/vault_client_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def kv
if @versioned_kv
@kv ||= VaultKvWrapper.new(self, Samson::Secrets::VaultClientManager::MOUNT)
else
@logical ||= VaultLogicalWrapper.new(self)
@logical ||= VaultLogicalWrapper.new(self) # rubocop:disable Naming/MemoizedInstanceVariableName
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/samson/secrets/vault_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class VaultServer < ActiveRecord::Base
audited
include AttrEncryptedSupport
self.table_name = :vault_servers
ADDRESS_PATTERN = /\Ahttps?:\/\/.*\z/.freeze
ADDRESS_PATTERN = /\Ahttps?:\/\/.*\z/

DEFAULT_CLIENT_OPTIONS = {
use_ssl: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SamsonPlugin < Rails::Engine

class Notification
class << self
VALID_RAILS_ENV = /^[a-z]+$/.freeze
VALID_RAILS_ENV = /^[a-z]+$/
SECRET_KEY = 'airbrake_api_key'

def deliver_for(deploy)
Expand Down
2 changes: 1 addition & 1 deletion plugins/aws_ecr/lib/samson_aws_ecr/samson_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module SamsonAwsEcr
class SamsonPlugin < Rails::Engine
AMAZON_REGISTRY = /\A.*\.dkr.ecr.([\w\-]+).amazonaws.com\z/.freeze
AMAZON_REGISTRY = /\A.*\.dkr.ecr.([\w-]+).amazonaws.com\z/

class << self
# we make sure the repo exists so pushes do not fail
Expand Down
2 changes: 1 addition & 1 deletion plugins/datadog/app/models/datadog_deploy_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'faraday'
require 'digest/md5'

# Note: might be able to replace this with Samson.statsd.event
# NOTE: might be able to replace this with Samson.statsd.event
class DatadogDeployEvent
def self.deliver(deploy, tags:, time:)
status =
Expand Down
4 changes: 2 additions & 2 deletions plugins/env/app/models/external_environment_variable_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require 'aws-sdk-s3'

class ExternalEnvironmentVariableGroup < ActiveRecord::Base
S3_URL_REGEX = /\Ahttps:\/\/([^.]+)\.s3\.amazonaws\.com\/([\w\W]+)\Z/i.freeze
S3_URL_FORMAT = "https://#{ENV['EXTERNAL_ENV_GROUP_S3_BUCKET']}.s3.amazonaws.com/[key]?versionId=[version_id]"
S3_URL_REGEX = /\Ahttps:\/\/([^.]+)\.s3\.amazonaws\.com\/([\w\W]+)\Z/i
S3_URL_FORMAT = "https://#{ENV['EXTERNAL_ENV_GROUP_S3_BUCKET']}.s3.amazonaws.com/[key]?versionId=[version_id]".freeze
HELP_TEXT = ENV.fetch(
"EXTERNAL_ENV_GROUP_HELP_TEXT",
"Use external service to manage environment variable groups"
Expand Down
Loading

0 comments on commit 58f4be0

Please sign in to comment.