Skip to content

Commit

Permalink
Add PreventReindexFullESDocumentCop (#35)
Browse files Browse the repository at this point in the history
* Add PreventReindexFullESDocumentCop

* Specs

* Linting fix

* Build fix
  • Loading branch information
adarshaks91 authored Jun 26, 2024
1 parent a959359 commit 116b415
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ Bugcrowd/AvoidSampleInSpecs:
Enabled: true
Include:
- 'spec/**/*.rb'
BugcrowdCops/PreventReindexFullESDocumentCop:
Enabled: true
Include:
- 'app/**/*.rb'
41 changes: 41 additions & 0 deletions lib/rubocop/cop/bugcrowd/prevent_reindex_full_es_document_cop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Bugcrowd
class PreventReindexFullESDocumentCop < Cop
#
# @example
#
# # bad
# ```
# Reindexing a full ES document would reindex all the resource ids
# ```
# ValisCommands::ReindexDocument.call(document_type: 'SubmissionDocument')
#
# # good
# Reindex only a specific resource ID instead
# ```
# ValisReindexWorker.new.perform([submission.id], Submission.to_s)
# ```
#

MSG = 'Avoid reindexing the full Elasticsearch document. ' \
'Consider reindexing only specific resource ids.'

def_node_matcher :valis_reindex_document?, <<-PATTERN
(send
(const
(const nil? :ValisCommands) :ReindexDocument) :call
(hash $...))
PATTERN

def on_send(node)
if valis_reindex_document?(node)
add_offense(node, message: MSG)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/bugcrowd_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
require_relative 'bugcrowd/no_include_run_in_transaction'
require_relative 'bugcrowd/no_event_deprecated_publish'
require_relative 'bugcrowd/sidekiq_testing_inline'
require_relative 'bugcrowd/prevent_reindex_full_es_document_cop.rb'
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Bugcrowd::PreventReindexFullESDocumentCop do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

it 'registers an offence when reindexing a full elasticsearch document' do
expect_offense(<<~RUBY)
ValisCommands::ReindexDocument.call(document_type: 'SubmissionDocument')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid reindexing the full Elasticsearch document. Consider reindexing only specific resource ids.
RUBY
end

it 'does not register an offense when reindexing only a specific resource ID' do
expect_no_offenses(<<~RUBY)
ValisReindexWorker.new.perform([submission.id], Submission.to_s)
RUBY
end
end

0 comments on commit 116b415

Please sign in to comment.