Commit 34c083a1 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'rubocop/enable-access-modifiers-cops' into 'master'

Enable Rubocop cops that check access modifiers

## What does this MR do?

This MR enables Rubocop cops that detect methods that should be restricted but are the part of public API because of access modifiers used improperly.

This also fixes existing offenses.

## Why was this MR needed?

Some method in our codebase are public instead of being private because it is sometimes difficult to get it right without static analysis.

## What are the relevant issue numbers?

See #17478  
Closes #17372 

See merge request !5014
parents 4284724d d6f66977
...@@ -510,6 +510,15 @@ Metrics/PerceivedComplexity: ...@@ -510,6 +510,15 @@ Metrics/PerceivedComplexity:
#################### Lint ################################ #################### Lint ################################
# Checks for useless access modifiers.
Lint/UselessAccessModifier:
Enabled: true
# Checks for attempts to use `private` or `protected` to set the visibility
# of a class method, which does not work.
Lint/IneffectiveAccessModifier:
Enabled: false
# Checks for ambiguous operators in the first argument of a method invocation # Checks for ambiguous operators in the first argument of a method invocation
# without parentheses. # without parentheses.
Lint/AmbiguousOperator: Lint/AmbiguousOperator:
......
...@@ -19,10 +19,6 @@ Lint/AssignmentInCondition: ...@@ -19,10 +19,6 @@ Lint/AssignmentInCondition:
Lint/HandleExceptions: Lint/HandleExceptions:
Enabled: false Enabled: false
# Offense count: 21
Lint/IneffectiveAccessModifier:
Enabled: false
# Offense count: 2 # Offense count: 2
Lint/Loop: Lint/Loop:
Enabled: false Enabled: false
...@@ -48,10 +44,6 @@ Lint/UnusedBlockArgument: ...@@ -48,10 +44,6 @@ Lint/UnusedBlockArgument:
Lint/UnusedMethodArgument: Lint/UnusedMethodArgument:
Enabled: false Enabled: false
# Offense count: 11
Lint/UselessAccessModifier:
Enabled: false
# Offense count: 12 # Offense count: 12
# Cop supports --auto-correct. # Cop supports --auto-correct.
Performance/PushSplat: Performance/PushSplat:
......
...@@ -82,8 +82,6 @@ class Import::BitbucketController < Import::BaseController ...@@ -82,8 +82,6 @@ class Import::BitbucketController < Import::BaseController
go_to_bitbucket_for_permissions go_to_bitbucket_for_permissions
end end
private
def access_params def access_params
{ {
bitbucket_access_token: session[:bitbucket_access_token], bitbucket_access_token: session[:bitbucket_access_token],
......
...@@ -61,8 +61,6 @@ class Import::GitlabController < Import::BaseController ...@@ -61,8 +61,6 @@ class Import::GitlabController < Import::BaseController
go_to_gitlab_for_permissions go_to_gitlab_for_permissions
end end
private
def access_params def access_params
{ gitlab_access_token: session[:gitlab_access_token] } { gitlab_access_token: session[:gitlab_access_token] }
end end
......
...@@ -144,8 +144,6 @@ module DiffHelper ...@@ -144,8 +144,6 @@ module DiffHelper
toggle_whitespace_link(url, options) toggle_whitespace_link(url, options)
end end
private
def hide_whitespace? def hide_whitespace?
params[:w] == '1' params[:w] == '1'
end end
......
module TokenAuthenticatable module TokenAuthenticatable
extend ActiveSupport::Concern extend ActiveSupport::Concern
private
def write_new_token(token_field)
new_token = generate_token(token_field)
write_attribute(token_field, new_token)
end
def generate_token(token_field)
loop do
token = Devise.friendly_token
break token unless self.class.unscoped.find_by(token_field => token)
end
end
class_methods do class_methods do
def authentication_token_fields def authentication_token_fields
@token_fields || [] @token_fields || []
end end
private private # rubocop:disable Lint/UselessAccessModifier
def add_authentication_token_field(token_field) def add_authentication_token_field(token_field)
@token_fields = [] unless @token_fields @token_fields = [] unless @token_fields
...@@ -32,18 +46,4 @@ module TokenAuthenticatable ...@@ -32,18 +46,4 @@ module TokenAuthenticatable
end end
end end
end end
private
def write_new_token(token_field)
new_token = generate_token(token_field)
write_attribute(token_field, new_token)
end
def generate_token(token_field)
loop do
token = Devise.friendly_token
break token unless self.class.unscoped.find_by(token_field => token)
end
end
end end
...@@ -24,10 +24,14 @@ module Auth ...@@ -24,10 +24,14 @@ module Auth
token[:access] = names.map do |name| token[:access] = names.map do |name|
{ type: 'repository', name: name, actions: %w(*) } { type: 'repository', name: name, actions: %w(*) }
end end
token.encoded token.encoded
end end
def self.token_expire_at
Time.now + current_application_settings.container_registry_token_expire_delay.minutes
end
private private
def authorized_token(*accesses) def authorized_token(*accesses)
...@@ -35,7 +39,7 @@ module Auth ...@@ -35,7 +39,7 @@ module Auth
token.issuer = registry.issuer token.issuer = registry.issuer
token.audience = params[:service] token.audience = params[:service]
token.subject = current_user.try(:username) token.subject = current_user.try(:username)
token.expire_time = ContainerRegistryAuthenticationService.token_expire_at token.expire_time = self.class.token_expire_at
token[:access] = accesses.compact token[:access] = accesses.compact
token token
end end
...@@ -81,9 +85,5 @@ module Auth ...@@ -81,9 +85,5 @@ module Auth
def registry def registry
Gitlab.config.registry Gitlab.config.registry
end end
def self.token_expire_at
Time.now + current_application_settings.container_registry_token_expire_delay.minutes
end
end end
end end
This diff is collapsed.
...@@ -38,6 +38,11 @@ module Banzai ...@@ -38,6 +38,11 @@ module Banzai
end end
end end
# Build a regexp that matches all valid :emoji: names.
def self.emoji_pattern
@emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
end
private private
def emoji_url(name) def emoji_url(name)
...@@ -59,11 +64,6 @@ module Banzai ...@@ -59,11 +64,6 @@ module Banzai
ActionController::Base.helpers.url_to_image(image) ActionController::Base.helpers.url_to_image(image)
end end
# Build a regexp that matches all valid :emoji: names.
def self.emoji_pattern
@emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
end
def emoji_pattern def emoji_pattern
self.class.emoji_pattern self.class.emoji_pattern
end end
......
...@@ -12,7 +12,12 @@ module Banzai ...@@ -12,7 +12,12 @@ module Banzai
html html
end end
private def self.renderer
@renderer ||= begin
renderer = Redcarpet::Render::HTML.new
Redcarpet::Markdown.new(renderer, redcarpet_options)
end
end
def self.redcarpet_options def self.redcarpet_options
# https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use # https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
...@@ -28,12 +33,7 @@ module Banzai ...@@ -28,12 +33,7 @@ module Banzai
}.freeze }.freeze
end end
def self.renderer private_class_method :redcarpet_options
@renderer ||= begin
renderer = Redcarpet::Render::HTML.new
Redcarpet::Markdown.new(renderer, redcarpet_options)
end
end
end end
end end
end end
module Banzai module Banzai
module Renderer module Renderer
extend self
# Convert a Markdown String into an HTML-safe String of HTML # Convert a Markdown String into an HTML-safe String of HTML
# #
# Note that while the returned HTML will have been sanitized of dangerous # Note that while the returned HTML will have been sanitized of dangerous
...@@ -14,7 +16,7 @@ module Banzai ...@@ -14,7 +16,7 @@ module Banzai
# context - Hash of context options passed to our HTML Pipeline # context - Hash of context options passed to our HTML Pipeline
# #
# Returns an HTML-safe String # Returns an HTML-safe String
def self.render(text, context = {}) def render(text, context = {})
cache_key = context.delete(:cache_key) cache_key = context.delete(:cache_key)
cache_key = full_cache_key(cache_key, context[:pipeline]) cache_key = full_cache_key(cache_key, context[:pipeline])
...@@ -52,7 +54,7 @@ module Banzai ...@@ -52,7 +54,7 @@ module Banzai
# texts_and_contexts # texts_and_contexts
# => [{ text: '### Hello', # => [{ text: '### Hello',
# context: { cache_key: [note, :note] } }] # context: { cache_key: [note, :note] } }]
def self.cache_collection_render(texts_and_contexts) def cache_collection_render(texts_and_contexts)
items_collection = texts_and_contexts.each_with_index do |item, index| items_collection = texts_and_contexts.each_with_index do |item, index|
context = item[:context] context = item[:context]
cache_key = full_cache_multi_key(context.delete(:cache_key), context[:pipeline]) cache_key = full_cache_multi_key(context.delete(:cache_key), context[:pipeline])
...@@ -81,7 +83,7 @@ module Banzai ...@@ -81,7 +83,7 @@ module Banzai
items_collection.map { |item| item[:rendered] } items_collection.map { |item| item[:rendered] }
end end
def self.render_result(text, context = {}) def render_result(text, context = {})
text = Pipeline[:pre_process].to_html(text, context) if text text = Pipeline[:pre_process].to_html(text, context) if text
Pipeline[context[:pipeline]].call(text, context) Pipeline[context[:pipeline]].call(text, context)
...@@ -100,7 +102,7 @@ module Banzai ...@@ -100,7 +102,7 @@ module Banzai
# :user - User object # :user - User object
# #
# Returns an HTML-safe String # Returns an HTML-safe String
def self.post_process(html, context) def post_process(html, context)
context = Pipeline[context[:pipeline]].transform_context(context) context = Pipeline[context[:pipeline]].transform_context(context)
pipeline = Pipeline[:post_process] pipeline = Pipeline[:post_process]
...@@ -113,7 +115,7 @@ module Banzai ...@@ -113,7 +115,7 @@ module Banzai
private private
def self.cacheless_render(text, context = {}) def cacheless_render(text, context = {})
Gitlab::Metrics.measure(:banzai_cacheless_render) do Gitlab::Metrics.measure(:banzai_cacheless_render) do
result = render_result(text, context) result = render_result(text, context)
...@@ -126,7 +128,7 @@ module Banzai ...@@ -126,7 +128,7 @@ module Banzai
end end
end end
def self.full_cache_key(cache_key, pipeline_name) def full_cache_key(cache_key, pipeline_name)
return unless cache_key return unless cache_key
["banzai", *cache_key, pipeline_name || :full] ["banzai", *cache_key, pipeline_name || :full]
end end
...@@ -134,7 +136,7 @@ module Banzai ...@@ -134,7 +136,7 @@ module Banzai
# To map Rails.cache.read_multi results we need to know the Rails.cache.expanded_key. # To map Rails.cache.read_multi results we need to know the Rails.cache.expanded_key.
# Other option will be to generate stringified keys on our side and don't delegate to Rails.cache.expanded_key # Other option will be to generate stringified keys on our side and don't delegate to Rails.cache.expanded_key
# method. # method.
def self.full_cache_multi_key(cache_key, pipeline_name) def full_cache_multi_key(cache_key, pipeline_name)
return unless cache_key return unless cache_key
Rails.cache.send(:expanded_key, full_cache_key(cache_key, pipeline_name)) Rails.cache.send(:expanded_key, full_cache_key(cache_key, pipeline_name))
end end
......
...@@ -40,7 +40,7 @@ module Gitlab ...@@ -40,7 +40,7 @@ module Gitlab
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }] Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
end end
private private # rubocop:disable Lint/UselessAccessModifier
def node(key, node, metadata) def node(key, node, metadata)
factory = Node::Factory.new(node) factory = Node::Factory.new(node)
......
...@@ -55,12 +55,12 @@ module Gitlab ...@@ -55,12 +55,12 @@ module Gitlab
end end
end end
private
def self.connection def self.connection
ActiveRecord::Base.connection ActiveRecord::Base.connection
end end
private_class_method :connection
def self.database_version def self.database_version
row = connection.execute("SELECT VERSION()").first row = connection.execute("SELECT VERSION()").first
...@@ -70,5 +70,7 @@ module Gitlab ...@@ -70,5 +70,7 @@ module Gitlab
row.first row.first
end end
end end
private_class_method :database_version
end end
end end
...@@ -19,24 +19,6 @@ module Gitlab ...@@ -19,24 +19,6 @@ module Gitlab
attr_accessor :old_line, :new_line, :offset attr_accessor :old_line, :new_line, :offset
def self.for_lines(lines)
changed_line_pairs = self.find_changed_line_pairs(lines)
inline_diffs = []
changed_line_pairs.each do |old_index, new_index|
old_line = lines[old_index]
new_line = lines[new_index]
old_diffs, new_diffs = new(old_line, new_line, offset: 1).inline_diffs
inline_diffs[old_index] = old_diffs
inline_diffs[new_index] = new_diffs
end
inline_diffs
end
def initialize(old_line, new_line, offset: 0) def initialize(old_line, new_line, offset: 0)
@old_line = old_line[offset..-1] @old_line = old_line[offset..-1]
@new_line = new_line[offset..-1] @new_line = new_line[offset..-1]
...@@ -63,32 +45,54 @@ module Gitlab ...@@ -63,32 +45,54 @@ module Gitlab
[old_diffs, new_diffs] [old_diffs, new_diffs]
end end
private class << self
def for_lines(lines)
changed_line_pairs = find_changed_line_pairs(lines)
# Finds pairs of old/new line pairs that represent the same line that changed inline_diffs = []
def self.find_changed_line_pairs(lines)
# Prefixes of all diff lines, indicating their types
# For example: `" - + -+ ---+++ --+ -++"`
line_prefixes = lines.each_with_object("") { |line, s| s << line[0] }.gsub(/[^ +-]/, ' ')
changed_line_pairs = [] changed_line_pairs.each do |old_index, new_index|
line_prefixes.scan(LINE_PAIRS_PATTERN) do old_line = lines[old_index]
# For `"---+++"`, `begin_index == 0`, `end_index == 6` new_line = lines[new_index]
begin_index, end_index = Regexp.last_match.offset(:del_ins)
# For `"---+++"`, `changed_line_count == 3` old_diffs, new_diffs = new(old_line, new_line, offset: 1).inline_diffs
changed_line_count = (end_index - begin_index) / 2
halfway_index = begin_index + changed_line_count inline_diffs[old_index] = old_diffs
(begin_index...halfway_index).each do |i| inline_diffs[new_index] = new_diffs
# For `"---+++"`, index 1 maps to 1 + 3 = 4
changed_line_pairs << [i, i + changed_line_count]
end end
inline_diffs
end end
changed_line_pairs private
# Finds pairs of old/new line pairs that represent the same line that changed
def find_changed_line_pairs(lines)
# Prefixes of all diff lines, indicating their types
# For example: `" - + -+ ---+++ --+ -++"`
line_prefixes = lines.each_with_object("") { |line, s| s << line[0] }.gsub(/[^ +-]/, ' ')
changed_line_pairs = []
line_prefixes.scan(LINE_PAIRS_PATTERN) do
# For `"---+++"`, `begin_index == 0`, `end_index == 6`
begin_index, end_index = Regexp.last_match.offset(:del_ins)
# For `"---+++"`, `changed_line_count == 3`
changed_line_count = (end_index - begin_index) / 2
halfway_index = begin_index + changed_line_count
(begin_index...halfway_index).each do |i|
# For `"---+++"`, index 1 maps to 1 + 3 = 4
changed_line_pairs << [i, i + changed_line_count]
end
end
changed_line_pairs
end
end end
private
def longest_common_prefix(a, b) def longest_common_prefix(a, b)
max_length = [a.length, b.length].max max_length = [a.length, b.length].max
......
...@@ -141,10 +141,10 @@ module Gitlab ...@@ -141,10 +141,10 @@ module Gitlab
end end
end end
private
def self.current_transaction def self.current_transaction
Transaction.current Transaction.current
end end
private_class_method :current_transaction
end end
end end
...@@ -2,6 +2,8 @@ module Gitlab ...@@ -2,6 +2,8 @@ module Gitlab
# Module containing GitLab's application theme definitions and helper methods # Module containing GitLab's application theme definitions and helper methods
# for accessing them. # for accessing them.
module Themes module Themes
extend self
# Theme ID used when no `default_theme` configuration setting is provided. # Theme ID used when no `default_theme` configuration setting is provided.
APPLICATION_DEFAULT = 2 APPLICATION_DEFAULT = 2
...@@ -22,7 +24,7 @@ module Gitlab ...@@ -22,7 +24,7 @@ module Gitlab
# classes that might be applied to the `body` element # classes that might be applied to the `body` element
# #
# Returns a String # Returns a String
def self.body_classes def body_classes
THEMES.collect(&:css_class).uniq.join(' ') THEMES.collect(&:css_class).uniq.join(' ')
end end
...@@ -33,26 +35,26 @@ module Gitlab ...@@ -33,26 +35,26 @@ module Gitlab
# id - Integer ID # id - Integer ID
# #
# Returns a Theme # Returns a Theme
def self.by_id(id) def by_id(id)
THEMES.detect { |t| t.id == id } || default THEMES.detect { |t| t.id == id } || default
end end
# Returns the number of defined Themes # Returns the number of defined Themes
def self.count def count
THEMES.size THEMES.size
end end
# Get the default Theme # Get the default Theme
# #
# Returns a Theme # Returns a Theme
def self.default def default
by_id(default_id) by_id(default_id)
end end
# Iterate through each Theme # Iterate through each Theme
# #
# Yields the Theme object # Yields the Theme object
def self.each(&block) def each(&block)
THEMES.each(&block) THEMES.each(&block)
end end
...@@ -61,7 +63,7 @@ module Gitlab ...@@ -61,7 +63,7 @@ module Gitlab
# user - User record # user - User record
# #
# Returns a Theme # Returns a Theme
def self.for_user(user) def for_user(user)
if user if user
by_id(user.theme_id) by_id(user.theme_id)
else else
...@@ -71,7 +73,7 @@ module Gitlab ...@@ -71,7 +73,7 @@ module Gitlab
private private
def self.default_id def default_id
id = Gitlab.config.gitlab.default_theme.to_i id = Gitlab.config.gitlab.default_theme.to_i
# Prevent an invalid configuration setting from causing an infinite loop # Prevent an invalid configuration setting from causing an infinite loop
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment