diff options
author | Gabriel Mazetto <gabriel@gitlab.com> | 2018-03-08 12:56:54 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2018-03-08 12:56:54 +0000 |
commit | 5c7a738105b88c52d7765791419ee398d9e27ae0 (patch) | |
tree | 3c317bc276420ae774b9fd0edea1788fdebd347b /lib | |
parent | a284400099af145fe63c6a00609ea59f768d383c (diff) | |
download | gitlab-ce-5c7a738105b88c52d7765791419ee398d9e27ae0.tar.gz |
[CE] Add Naming/FileName rule checking expected class/module per filename
Diffstat (limited to 'lib')
-rw-r--r-- | lib/constraints/group_url_constrainer.rb | 12 | ||||
-rw-r--r-- | lib/constraints/project_url_constrainer.rb | 20 | ||||
-rw-r--r-- | lib/constraints/user_url_constrainer.rb | 12 | ||||
-rw-r--r-- | lib/declarative_policy.rb | 4 | ||||
-rw-r--r-- | lib/declarative_policy/delegate_dsl.rb | 16 | ||||
-rw-r--r-- | lib/declarative_policy/dsl.rb | 103 | ||||
-rw-r--r-- | lib/declarative_policy/policy_dsl.rb | 44 | ||||
-rw-r--r-- | lib/declarative_policy/preferred_scope.rb | 2 | ||||
-rw-r--r-- | lib/declarative_policy/rule_dsl.rb | 45 | ||||
-rw-r--r-- | lib/gitlab/auth/result.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/ci/pipeline/chain/command.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/health_checks/metric.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/health_checks/result.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/middleware/release_env.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/slash_commands/result.rb | 2 | ||||
-rw-r--r-- | lib/haml_lint/inline_javascript.rb | 2 |
16 files changed, 141 insertions, 131 deletions
diff --git a/lib/constraints/group_url_constrainer.rb b/lib/constraints/group_url_constrainer.rb index fd2ac2db0a9..87649c50424 100644 --- a/lib/constraints/group_url_constrainer.rb +++ b/lib/constraints/group_url_constrainer.rb @@ -1,9 +1,11 @@ -class GroupUrlConstrainer - def matches?(request) - full_path = request.params[:group_id] || request.params[:id] +module Constraints + class GroupUrlConstrainer + def matches?(request) + full_path = request.params[:group_id] || request.params[:id] - return false unless NamespacePathValidator.valid_path?(full_path) + return false unless NamespacePathValidator.valid_path?(full_path) - Group.find_by_full_path(full_path, follow_redirects: request.get?).present? + Group.find_by_full_path(full_path, follow_redirects: request.get?).present? + end end end diff --git a/lib/constraints/project_url_constrainer.rb b/lib/constraints/project_url_constrainer.rb index e90ecb5ec69..32aea98f0f7 100644 --- a/lib/constraints/project_url_constrainer.rb +++ b/lib/constraints/project_url_constrainer.rb @@ -1,13 +1,15 @@ -class ProjectUrlConstrainer - def matches?(request) - namespace_path = request.params[:namespace_id] - project_path = request.params[:project_id] || request.params[:id] - full_path = [namespace_path, project_path].join('/') +module Constraints + class ProjectUrlConstrainer + def matches?(request) + namespace_path = request.params[:namespace_id] + project_path = request.params[:project_id] || request.params[:id] + full_path = [namespace_path, project_path].join('/') - return false unless ProjectPathValidator.valid_path?(full_path) + return false unless ProjectPathValidator.valid_path?(full_path) - # We intentionally allow SELECT(*) here so result of this query can be used - # as cache for further Project.find_by_full_path calls within request - Project.find_by_full_path(full_path, follow_redirects: request.get?).present? + # We intentionally allow SELECT(*) here so result of this query can be used + # as cache for further Project.find_by_full_path calls within request + Project.find_by_full_path(full_path, follow_redirects: request.get?).present? + end end end diff --git a/lib/constraints/user_url_constrainer.rb b/lib/constraints/user_url_constrainer.rb index 3b3ed1c6ddb..8afa04d29a4 100644 --- a/lib/constraints/user_url_constrainer.rb +++ b/lib/constraints/user_url_constrainer.rb @@ -1,9 +1,11 @@ -class UserUrlConstrainer - def matches?(request) - full_path = request.params[:username] +module Constraints + class UserUrlConstrainer + def matches?(request) + full_path = request.params[:username] - return false unless NamespacePathValidator.valid_path?(full_path) + return false unless NamespacePathValidator.valid_path?(full_path) - User.find_by_full_path(full_path, follow_redirects: request.get?).present? + User.find_by_full_path(full_path, follow_redirects: request.get?).present? + end end end diff --git a/lib/declarative_policy.rb b/lib/declarative_policy.rb index b1949d693ad..1dd2855063d 100644 --- a/lib/declarative_policy.rb +++ b/lib/declarative_policy.rb @@ -1,6 +1,8 @@ require_dependency 'declarative_policy/cache' require_dependency 'declarative_policy/condition' -require_dependency 'declarative_policy/dsl' +require_dependency 'declarative_policy/delegate_dsl' +require_dependency 'declarative_policy/policy_dsl' +require_dependency 'declarative_policy/rule_dsl' require_dependency 'declarative_policy/preferred_scope' require_dependency 'declarative_policy/rule' require_dependency 'declarative_policy/runner' diff --git a/lib/declarative_policy/delegate_dsl.rb b/lib/declarative_policy/delegate_dsl.rb new file mode 100644 index 00000000000..f544dffe888 --- /dev/null +++ b/lib/declarative_policy/delegate_dsl.rb @@ -0,0 +1,16 @@ +module DeclarativePolicy + # Used when the name of a delegate is mentioned in + # the rule DSL. + class DelegateDsl + def initialize(rule_dsl, delegate_name) + @rule_dsl = rule_dsl + @delegate_name = delegate_name + end + + def method_missing(m, *a, &b) + return super unless a.empty? && !block_given? + + @rule_dsl.delegate(@delegate_name, m) + end + end +end diff --git a/lib/declarative_policy/dsl.rb b/lib/declarative_policy/dsl.rb deleted file mode 100644 index 6ba1e7a3c5c..00000000000 --- a/lib/declarative_policy/dsl.rb +++ /dev/null @@ -1,103 +0,0 @@ -module DeclarativePolicy - # The DSL evaluation context inside rule { ... } blocks. - # Responsible for creating and combining Rule objects. - # - # See Base.rule - class RuleDsl - def initialize(context_class) - @context_class = context_class - end - - def can?(ability) - Rule::Ability.new(ability) - end - - def all?(*rules) - Rule::And.make(rules) - end - - def any?(*rules) - Rule::Or.make(rules) - end - - def none?(*rules) - ~Rule::Or.new(rules) - end - - def cond(condition) - Rule::Condition.new(condition) - end - - def delegate(delegate_name, condition) - Rule::DelegatedCondition.new(delegate_name, condition) - end - - def method_missing(m, *a, &b) - return super unless a.size == 0 && !block_given? - - if @context_class.delegations.key?(m) - DelegateDsl.new(self, m) - else - cond(m.to_sym) - end - end - end - - # Used when the name of a delegate is mentioned in - # the rule DSL. - class DelegateDsl - def initialize(rule_dsl, delegate_name) - @rule_dsl = rule_dsl - @delegate_name = delegate_name - end - - def method_missing(m, *a, &b) - return super unless a.size == 0 && !block_given? - - @rule_dsl.delegate(@delegate_name, m) - end - end - - # The return value of a rule { ... } declaration. - # Can call back to register rules with the containing - # Policy class (context_class here). See Base.rule - # - # Note that the #policy method just performs an #instance_eval, - # which is useful for multiple #enable or #prevent callse. - # - # Also provides a #method_missing proxy to the context - # class's class methods, so that helper methods can be - # defined and used in a #policy { ... } block. - class PolicyDsl - def initialize(context_class, rule) - @context_class = context_class - @rule = rule - end - - def policy(&b) - instance_eval(&b) - end - - def enable(*abilities) - @context_class.enable_when(abilities, @rule) - end - - def prevent(*abilities) - @context_class.prevent_when(abilities, @rule) - end - - def prevent_all - @context_class.prevent_all_when(@rule) - end - - def method_missing(m, *a, &b) - return super unless @context_class.respond_to?(m) - - @context_class.__send__(m, *a, &b) # rubocop:disable GitlabSecurity/PublicSend - end - - def respond_to_missing?(m) - @context_class.respond_to?(m) || super - end - end -end diff --git a/lib/declarative_policy/policy_dsl.rb b/lib/declarative_policy/policy_dsl.rb new file mode 100644 index 00000000000..f11b6e9f730 --- /dev/null +++ b/lib/declarative_policy/policy_dsl.rb @@ -0,0 +1,44 @@ +module DeclarativePolicy + # The return value of a rule { ... } declaration. + # Can call back to register rules with the containing + # Policy class (context_class here). See Base.rule + # + # Note that the #policy method just performs an #instance_eval, + # which is useful for multiple #enable or #prevent callse. + # + # Also provides a #method_missing proxy to the context + # class's class methods, so that helper methods can be + # defined and used in a #policy { ... } block. + class PolicyDsl + def initialize(context_class, rule) + @context_class = context_class + @rule = rule + end + + def policy(&b) + instance_eval(&b) + end + + def enable(*abilities) + @context_class.enable_when(abilities, @rule) + end + + def prevent(*abilities) + @context_class.prevent_when(abilities, @rule) + end + + def prevent_all + @context_class.prevent_all_when(@rule) + end + + def method_missing(m, *a, &b) + return super unless @context_class.respond_to?(m) + + @context_class.__send__(m, *a, &b) # rubocop:disable GitlabSecurity/PublicSend + end + + def respond_to_missing?(m) + @context_class.respond_to?(m) || super + end + end +end diff --git a/lib/declarative_policy/preferred_scope.rb b/lib/declarative_policy/preferred_scope.rb index b0754098149..5c214408dd0 100644 --- a/lib/declarative_policy/preferred_scope.rb +++ b/lib/declarative_policy/preferred_scope.rb @@ -1,4 +1,4 @@ -module DeclarativePolicy +module DeclarativePolicy # rubocop:disable Naming/FileName PREFERRED_SCOPE_KEY = :"DeclarativePolicy.preferred_scope" class << self diff --git a/lib/declarative_policy/rule_dsl.rb b/lib/declarative_policy/rule_dsl.rb new file mode 100644 index 00000000000..e948b7f2de1 --- /dev/null +++ b/lib/declarative_policy/rule_dsl.rb @@ -0,0 +1,45 @@ +module DeclarativePolicy + # The DSL evaluation context inside rule { ... } blocks. + # Responsible for creating and combining Rule objects. + # + # See Base.rule + class RuleDsl + def initialize(context_class) + @context_class = context_class + end + + def can?(ability) + Rule::Ability.new(ability) + end + + def all?(*rules) + Rule::And.make(rules) + end + + def any?(*rules) + Rule::Or.make(rules) + end + + def none?(*rules) + ~Rule::Or.new(rules) + end + + def cond(condition) + Rule::Condition.new(condition) + end + + def delegate(delegate_name, condition) + Rule::DelegatedCondition.new(delegate_name, condition) + end + + def method_missing(m, *a, &b) + return super unless a.empty? && !block_given? + + if @context_class.delegations.key?(m) + DelegateDsl.new(self, m) + else + cond(m.to_sym) + end + end + end +end diff --git a/lib/gitlab/auth/result.rb b/lib/gitlab/auth/result.rb index 75451cf8aa9..00cdc94a9ef 100644 --- a/lib/gitlab/auth/result.rb +++ b/lib/gitlab/auth/result.rb @@ -1,4 +1,4 @@ -module Gitlab +module Gitlab # rubocop:disable Naming/FileName module Auth Result = Struct.new(:actor, :project, :type, :authentication_abilities) do def ci?(for_project) diff --git a/lib/gitlab/ci/pipeline/chain/command.rb b/lib/gitlab/ci/pipeline/chain/command.rb index 7b19b10e05b..a1849b01c5d 100644 --- a/lib/gitlab/ci/pipeline/chain/command.rb +++ b/lib/gitlab/ci/pipeline/chain/command.rb @@ -1,4 +1,4 @@ -module Gitlab +module Gitlab # rubocop:disable Naming/FileName module Ci module Pipeline module Chain diff --git a/lib/gitlab/health_checks/metric.rb b/lib/gitlab/health_checks/metric.rb index 1a2eab0b005..d62d9136886 100644 --- a/lib/gitlab/health_checks/metric.rb +++ b/lib/gitlab/health_checks/metric.rb @@ -1,3 +1,3 @@ -module Gitlab::HealthChecks +module Gitlab::HealthChecks # rubocop:disable Naming/FileName Metric = Struct.new(:name, :value, :labels) end diff --git a/lib/gitlab/health_checks/result.rb b/lib/gitlab/health_checks/result.rb index 8086760023e..e323e2c9723 100644 --- a/lib/gitlab/health_checks/result.rb +++ b/lib/gitlab/health_checks/result.rb @@ -1,3 +1,3 @@ -module Gitlab::HealthChecks +module Gitlab::HealthChecks # rubocop:disable Naming/FileName Result = Struct.new(:success, :message, :labels) end diff --git a/lib/gitlab/middleware/release_env.rb b/lib/gitlab/middleware/release_env.rb index f8d0a135965..bfe8e113b5e 100644 --- a/lib/gitlab/middleware/release_env.rb +++ b/lib/gitlab/middleware/release_env.rb @@ -1,4 +1,4 @@ -module Gitlab +module Gitlab # rubocop:disable Naming/FileName module Middleware # Some of middleware would hold env for no good reason even after the # request had already been processed, and we could not garbage collect diff --git a/lib/gitlab/slash_commands/result.rb b/lib/gitlab/slash_commands/result.rb index 7021b4b01b2..3669dedf0fe 100644 --- a/lib/gitlab/slash_commands/result.rb +++ b/lib/gitlab/slash_commands/result.rb @@ -1,4 +1,4 @@ -module Gitlab +module Gitlab # rubocop:disable Naming/FileName module SlashCommands Result = Struct.new(:type, :message) end diff --git a/lib/haml_lint/inline_javascript.rb b/lib/haml_lint/inline_javascript.rb index 4f776330e80..adbed20f152 100644 --- a/lib/haml_lint/inline_javascript.rb +++ b/lib/haml_lint/inline_javascript.rb @@ -1,4 +1,4 @@ -unless Rails.env.production? +unless Rails.env.production? # rubocop:disable Naming/FileName require 'haml_lint/haml_visitor' require 'haml_lint/linter' require 'haml_lint/linter_registry' |