Skip to content

Commit 116b415

Browse files
authored
Add PreventReindexFullESDocumentCop (#35)
* Add PreventReindexFullESDocumentCop * Specs * Linting fix * Build fix
1 parent a959359 commit 116b415

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

config/default.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

lib/rubocop/cop/bugcrowd_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@
3737
require_relative 'bugcrowd/no_include_run_in_transaction'
3838
require_relative 'bugcrowd/no_event_deprecated_publish'
3939
require_relative 'bugcrowd/sidekiq_testing_inline'
40+
require_relative 'bugcrowd/prevent_reindex_full_es_document_cop.rb'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

0 commit comments

Comments
 (0)