Class: Gitlab::Spamcheck::Client

Inherits:
Object
  • Object
show all
Includes:
Spam::SpamConstants
Defined in:
lib/gitlab/spamcheck/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT_SECS =
2
VERDICT_MAPPING =
{
  ::Spamcheck::SpamVerdict::Verdict::ALLOW => ALLOW,
  ::Spamcheck::SpamVerdict::Verdict::CONDITIONAL_ALLOW => CONDITIONAL_ALLOW,
  ::Spamcheck::SpamVerdict::Verdict::DISALLOW => DISALLOW,
  ::Spamcheck::SpamVerdict::Verdict::BLOCK => BLOCK_USER,
  ::Spamcheck::SpamVerdict::Verdict::NOOP => NOOP
}.freeze
ACTION_MAPPING =
{
  create: ::Spamcheck::Action::CREATE,
  update: ::Spamcheck::Action::UPDATE
}.freeze
URL_SCHEME_REGEX =
%r{^grpc://|^tls://}.freeze

Constants included from Spam::SpamConstants

Spam::SpamConstants::ALLOW, Spam::SpamConstants::BLOCK_USER, Spam::SpamConstants::CONDITIONAL_ALLOW, Spam::SpamConstants::DISALLOW, Spam::SpamConstants::NOOP, Spam::SpamConstants::OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM, Spam::SpamConstants::SUPPORTED_VERDICTS

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.


26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/spamcheck/client.rb', line 26

def initialize
  @endpoint_url = Gitlab::CurrentSettings.current_application_settings.spam_check_endpoint_url

  @creds = client_creds(@endpoint_url)

  # remove the `grpc://` or 'tls://' as it's only useful to ensure we're expecting to
  # connect with Spamcheck
  @endpoint_url = @endpoint_url.sub(URL_SCHEME_REGEX, '')
end

Instance Method Details

#issue_spam?(spam_issue:, user:, context: {}) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/spamcheck/client.rb', line 36

def issue_spam?(spam_issue:, user:, context: {})
  issue = build_issue_protobuf(issue: spam_issue, user: user, context: context)

  response = grpc_client.check_for_spam_issue(issue,
                                        metadata: { 'authorization' =>
                                                     Gitlab::CurrentSettings.spam_check_api_key })
  verdict = convert_verdict_to_gitlab_constant(response.verdict)
  [verdict, response.extra_attributes.to_h, response.error]
end