-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PreventReindexFullESDocumentCop (#35)
* Add PreventReindexFullESDocumentCop * Specs * Linting fix * Build fix
- Loading branch information
1 parent
a959359
commit 116b415
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/rubocop/cop/bugcrowd/prevent_reindex_full_es_document_cop.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
spec/rubocop/cop/bugcrowd/prevent_reindex_full_es_document_cop_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |