From f01fce7f4683e06e83d3f91d38ca5b749e27e7ec Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Thu, 21 Jul 2016 18:11:53 -0500 Subject: Refactor spam validation to a concern that can be easily reused and improve legibility in `SpamCheckService` --- lib/api/issues.rb | 12 +++++------- lib/gitlab/akismet_helper.rb | 11 ----------- 2 files changed, 5 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 21b9eb367e7..c4d3134da6c 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -158,15 +158,13 @@ module API project = user_project - issue = ::Issues::CreateService.new(project, current_user, attrs.merge({ request: request })).execute + issue = ::Issues::CreateService.new(project, current_user, attrs.merge(request: request, api: true)).execute + + if issue.spam? + render_api_error!({ error: 'Spam detected' }, 400) + end if issue.valid? - # Need to check if id is nil here, because if issue is spam, errors - # get added, but Rails still thinks it's valid, but it is never saved - # so id will be nil - if issue.id.nil? - render_api_error!({ error: 'Spam detected' }, 400) - end # Find or create labels and attach to issue. Labels are valid because # we already checked its name, so there can't be an error here if params[:labels].present? diff --git a/lib/gitlab/akismet_helper.rb b/lib/gitlab/akismet_helper.rb index fc1fbc5b600..207736b59db 100644 --- a/lib/gitlab/akismet_helper.rb +++ b/lib/gitlab/akismet_helper.rb @@ -43,16 +43,5 @@ module Gitlab false end end - - def create_spam_log(project, current_user, attrs, env, api: true) - params = attrs.merge({ - source_ip: client_ip(env), - user_agent: user_agent(env), - noteable_type: 'Issue', - via_api: api - }) - - ::CreateSpamLogService.new(project, current_user, params).execute - end end end -- cgit v1.2.1