File tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed
spec/rubocop/cop/bugcrowd Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -72,3 +72,7 @@ Bugcrowd/AvoidSampleInSpecs:
7272 Enabled : true
7373 Include :
7474 - ' spec/**/*.rb'
75+ BugcrowdCops/PreventReindexFullESDocumentCop :
76+ Enabled : true
77+ Include :
78+ - ' app/**/*.rb'
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module RuboCop
4+ module Cop
5+ module Bugcrowd
6+ class PreventReindexFullESDocumentCop < Cop
7+ #
8+ # @example
9+ #
10+ # # bad
11+ # ```
12+ # Reindexing a full ES document would reindex all the resource ids
13+ # ```
14+ # ValisCommands::ReindexDocument.call(document_type: 'SubmissionDocument')
15+ #
16+ # # good
17+ # Reindex only a specific resource ID instead
18+ # ```
19+ # ValisReindexWorker.new.perform([submission.id], Submission.to_s)
20+ # ```
21+ #
22+
23+ MSG = 'Avoid reindexing the full Elasticsearch document. ' \
24+ 'Consider reindexing only specific resource ids.'
25+
26+ def_node_matcher :valis_reindex_document? , <<-PATTERN
27+ (send
28+ (const
29+ (const nil? :ValisCommands) :ReindexDocument) :call
30+ (hash $...))
31+ PATTERN
32+
33+ def on_send ( node )
34+ if valis_reindex_document? ( node )
35+ add_offense ( node , message : MSG )
36+ end
37+ end
38+ end
39+ end
40+ end
41+ end
Original file line number Diff line number Diff line change 3737require_relative 'bugcrowd/no_include_run_in_transaction'
3838require_relative 'bugcrowd/no_event_deprecated_publish'
3939require_relative 'bugcrowd/sidekiq_testing_inline'
40+ require_relative 'bugcrowd/prevent_reindex_full_es_document_cop.rb'
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ RSpec . describe RuboCop ::Cop ::Bugcrowd ::PreventReindexFullESDocumentCop do
4+ subject ( :cop ) { described_class . new ( config ) }
5+
6+ let ( :config ) { RuboCop ::Config . new }
7+
8+ it 'registers an offence when reindexing a full elasticsearch document' do
9+ expect_offense ( <<~RUBY )
10+ ValisCommands::ReindexDocument.call(document_type: 'SubmissionDocument')
11+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid reindexing the full Elasticsearch document. Consider reindexing only specific resource ids.
12+ RUBY
13+ end
14+
15+ it 'does not register an offense when reindexing only a specific resource ID' do
16+ expect_no_offenses ( <<~RUBY )
17+ ValisReindexWorker.new.perform([submission.id], Submission.to_s)
18+ RUBY
19+ end
20+ end
You can’t perform that action at this time.
0 commit comments