diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-06-13 18:01:25 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-06-13 18:01:25 +0800 |
commit | edc46ce3e44147d5fe1b3071ba1e020846b60b13 (patch) | |
tree | f31f887566921b6ad432dd65eec86da713cab66a /rubocop | |
parent | c81db2442029417cf2836c62093b6772e35d2164 (diff) | |
parent | e69299b7090f9790ce158a99b5ab63093223d75a (diff) | |
download | gitlab-ce-edc46ce3e44147d5fe1b3071ba1e020846b60b13.tar.gz |
Merge remote-tracking branch 'upstream/master' into 33149-rename-more-builds33149-rename-more-builds
* upstream/master: (460 commits)
Center dropdown for pipeline's mini graph
Documentation bugfix of invalid JSON payload example of Create a commit with multiple files and actions
Fix filename method of GitlabUploader to return always real filename
Ignore CVE-2017-5029 in Nokogiri
Refactor atom builder by using xml.atom layout
Let PhantomJS load local images
Add a changelog entry
Only add a description change note when no tasks are updated
Doc: Add the need to upgrade to Go 1.8.3 in the 9.1->9.2 documentation as the upgrade fails with Go 1.5 (installed with Gitlab 8.1)
Use gitaly 0.11.2
Add the ability to perform background migrations
Always render warnings icon in orange
Fix a few translation for zh_TW
Improve Job detail view to make it refreshed in real-time instead of reloading
Attempts to run RSpec tests twice (1 retry)
ignore name validation on importing
Only show hover state on links and buttons
Use vue files for navigation tabs and buttons
doc: add example of scheduler when
Add test for u2f helper and changelog entry
...
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/activerecord_serialize.rb | 16 | ||||
-rw-r--r-- | rubocop/cop/polymorphic_associations.rb | 23 | ||||
-rw-r--r-- | rubocop/cop/redirect_with_status.rb | 44 | ||||
-rw-r--r-- | rubocop/model_helpers.rb | 11 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 2 |
5 files changed, 85 insertions, 11 deletions
diff --git a/rubocop/cop/activerecord_serialize.rb b/rubocop/cop/activerecord_serialize.rb index bfa0cff9a67..9bdcc3b4c34 100644 --- a/rubocop/cop/activerecord_serialize.rb +++ b/rubocop/cop/activerecord_serialize.rb @@ -1,24 +1,18 @@ +require_relative '../model_helpers' + module RuboCop module Cop # Cop that prevents the use of `serialize` in ActiveRecord models. class ActiverecordSerialize < RuboCop::Cop::Cop + include ModelHelpers + MSG = 'Do not store serialized data in the database, use separate columns and/or tables instead'.freeze def on_send(node) - return unless in_models?(node) + return unless in_model?(node) add_offense(node, :selector) if node.children[1] == :serialize end - - def models_path - File.join(Dir.pwd, 'app', 'models') - end - - def in_models?(node) - path = node.location.expression.source_buffer.name - - path.start_with?(models_path) - end end end end diff --git a/rubocop/cop/polymorphic_associations.rb b/rubocop/cop/polymorphic_associations.rb new file mode 100644 index 00000000000..7d554704550 --- /dev/null +++ b/rubocop/cop/polymorphic_associations.rb @@ -0,0 +1,23 @@ +require_relative '../model_helpers' + +module RuboCop + module Cop + # Cop that prevents the use of polymorphic associations + class PolymorphicAssociations < RuboCop::Cop::Cop + include ModelHelpers + + MSG = 'Do not use polymorphic associations, use separate tables instead'.freeze + + def on_send(node) + return unless in_model?(node) + return unless node.children[1] == :belongs_to + + node.children.last.each_node(:pair) do |pair| + key_name = pair.children[0].children[0] + + add_offense(pair, :expression) if key_name == :polymorphic + end + end + end + end +end diff --git a/rubocop/cop/redirect_with_status.rb b/rubocop/cop/redirect_with_status.rb new file mode 100644 index 00000000000..36810642c88 --- /dev/null +++ b/rubocop/cop/redirect_with_status.rb @@ -0,0 +1,44 @@ +module RuboCop + module Cop + # This cop prevents usage of 'redirect_to' in actions 'destroy' without specifying 'status'. + # See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840 + class RedirectWithStatus < RuboCop::Cop::Cop + MSG = 'Do not use "redirect_to" without "status" in "destroy" action'.freeze + + def on_def(node) + return unless in_controller?(node) + return unless destroy?(node) || destroy_all?(node) + + node.each_descendant(:send) do |def_node| + next unless redirect_to?(def_node) + + methods = [] + + def_node.children.last.each_node(:pair) do |pair| + methods << pair.children.first.children.first + end + + add_offense(def_node, :selector) unless methods.include?(:status) + end + end + + private + + def in_controller?(node) + node.location.expression.source_buffer.name.end_with?('_controller.rb') + end + + def destroy?(node) + node.children.first == :destroy + end + + def destroy_all?(node) + node.children.first == :destroy_all + end + + def redirect_to?(node) + node.children[1] == :redirect_to + end + end + end +end diff --git a/rubocop/model_helpers.rb b/rubocop/model_helpers.rb new file mode 100644 index 00000000000..309723dc34c --- /dev/null +++ b/rubocop/model_helpers.rb @@ -0,0 +1,11 @@ +module RuboCop + module ModelHelpers + # Returns true if the given node originated from the models directory. + def in_model?(node) + path = node.location.expression.source_buffer.name + models_path = File.join(Dir.pwd, 'app', 'models') + + path.start_with?(models_path) + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 17d2bf6aa1c..dae30969abf 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -1,6 +1,8 @@ require_relative 'cop/custom_error_class' require_relative 'cop/gem_fetcher' require_relative 'cop/activerecord_serialize' +require_relative 'cop/redirect_with_status' +require_relative 'cop/polymorphic_associations' require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_column_with_default_to_large_table' require_relative 'cop/migration/add_concurrent_foreign_key' |