Skip to content
Snippets Groups Projects
Commit 93ac32a5 authored by Stan Hu's avatar Stan Hu
Browse files

Merge branch 'generic-spammable-support' into 'master'

Add support for generic spammables

See merge request !119102



Merged-by: default avatarStan Hu <stanhu@gmail.com>
Approved-by: default avatarStan Hu <stanhu@gmail.com>
Reviewed-by: default avatarSam Kim <skim@gitlab.com>
Co-authored-by: default avatarimand3r <ianderson@gitlab.com>
parents bf79370d fbc772a7
No related branches found
No related tags found
2 merge requests!122597doc/gitaly: Remove references to removed metrics,!119102Add support for generic spammables
Pipeline #853808620 passed
......@@ -48,15 +48,16 @@ def get_spammable_mappings(spammable)
when Snippet
[::Spamcheck::Snippet, grpc_client.method(:check_for_spam_snippet)]
else
raise ArgumentError, "Not a spammable type: #{spammable.class.name}"
[::Spamcheck::Generic, grpc_client.method(:check_for_spam_generic)]
end
end
def build_protobuf(spammable:, user:, context:, extra_features:)
protobuf_class, grpc_method = get_spammable_mappings(spammable)
pb = protobuf_class.new(**extra_features)
pb.title = spammable.spam_title || ''
pb.description = spammable.spam_description || ''
pb.title = spammable.spam_title || '' if pb.respond_to?(:title)
pb.description = spammable.spam_description || '' if pb.respond_to?(:description)
pb.text = spammable.spammable_text || '' if pb.respond_to?(:text)
pb.created_at = convert_to_pb_timestamp(spammable.created_at) if spammable.created_at
pb.updated_at = convert_to_pb_timestamp(spammable.updated_at) if spammable.updated_at
pb.action = ACTION_MAPPING.fetch(context.fetch(:action)) if context.has_key?(:action)
......
......@@ -101,6 +101,19 @@
end
describe "#build_protobuf", :aggregate_failures do
let_it_be(:generic_spammable) { Object }
let_it_be(:generic_created_at) { issue.created_at }
let_it_be(:generic_updated_at) { issue.updated_at }
before do
allow(generic_spammable).to receive_messages(
spammable_text: 'generic spam',
created_at: generic_created_at,
updated_at: generic_updated_at,
project: nil
)
end
it 'builds the expected issue protobuf object' do
cxt = { action: :create }
issue_pb, _ = described_class.new.send(:build_protobuf,
......@@ -127,10 +140,20 @@
expect(snippet_pb.updated_at).to eq timestamp_to_protobuf_timestamp(snippet.updated_at)
expect(snippet_pb.action).to be ::Spamcheck::Action.lookup(::Spamcheck::Action::CREATE)
expect(snippet_pb.user.username).to eq user.username
expect(snippet_pb.user.username).to eq user.username
expect(snippet_pb.files.first.path).to eq 'first.rb'
expect(snippet_pb.files.last.path).to eq 'second.rb'
end
it 'builds the expected generic protobuf object' do
cxt = { action: :create }
generic_pb, _ = described_class.new.send(:build_protobuf, spammable: generic_spammable, user: user, context: cxt, extra_features: {})
expect(generic_pb.text).to eq 'generic spam'
expect(generic_pb.created_at).to eq timestamp_to_protobuf_timestamp(generic_created_at)
expect(generic_pb.updated_at).to eq timestamp_to_protobuf_timestamp(generic_updated_at)
expect(generic_pb.action).to be ::Spamcheck::Action.lookup(::Spamcheck::Action::CREATE)
expect(generic_pb.user.username).to eq user.username
end
end
describe '#build_user_protobuf', :aggregate_failures do
......@@ -171,15 +194,14 @@
end
describe "#get_spammable_mappings", :aggregate_failures do
it 'is an expected spammable' do
it 'is a defined spammable' do
protobuf_class, _ = described_class.new.send(:get_spammable_mappings, issue)
expect(protobuf_class).to eq ::Spamcheck::Issue
end
it 'is an unexpected spammable' do
expect { described_class.new.send(:get_spammable_mappings, 'spam') }.to raise_error(
ArgumentError, 'Not a spammable type: String'
)
it 'is a generic spammable' do
protobuf_class, _ = described_class.new.send(:get_spammable_mappings, Object)
expect(protobuf_class).to eq ::Spamcheck::Generic
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment