Commit 947c09c2 authored by Jarka Kadlecova's avatar Jarka Kadlecova

Simplify checking if objects exist code in new issaubles workers

parent 9e7ac48b
...@@ -16,6 +16,7 @@ module Issues ...@@ -16,6 +16,7 @@ module Issues
spam_check(issue, current_user) spam_check(issue, current_user)
issue.move_to_end issue.move_to_end
# current_user (defined in BaseService) is not available within run_after_commit block
user = current_user user = current_user
issue.run_after_commit do issue.run_after_commit do
NewIssueWorker.perform_async(issue.id, user.id) NewIssueWorker.perform_async(issue.id, user.id)
......
...@@ -17,6 +17,7 @@ module MergeRequests ...@@ -17,6 +17,7 @@ module MergeRequests
end end
def before_create(merge_request) def before_create(merge_request)
# current_user (defined in BaseService) is not available within run_after_commit block
user = current_user user = current_user
merge_request.run_after_commit do merge_request.run_after_commit do
NewMergeRequestWorker.perform_async(merge_request.id, user.id) NewMergeRequestWorker.perform_async(merge_request.id, user.id)
......
module NewIssuable module NewIssuable
attr_reader :issuable, :user attr_reader :issuable, :user
def ensure_objects_found(issuable_id, user_id) def objects_found?(issuable_id, user_id)
@issuable = issuable_class.find_by(id: issuable_id) set_user(user_id)
unless @issuable set_issuable(issuable_id)
log_error(issuable_class, issuable_id)
return false user && issuable
end end
def set_user(user_id)
@user = User.find_by(id: user_id) @user = User.find_by(id: user_id)
unless @user
log_error(User, user_id) log_error(User, user_id) unless @user
return false
end end
true def set_issuable(issuable_id)
@issuable = issuable_class.find_by(id: issuable_id)
log_error(issuable_class, issuable_id) unless @issuable
end end
def log_error(record_class, record_id) def log_error(record_class, record_id)
......
...@@ -4,7 +4,7 @@ class NewIssueWorker ...@@ -4,7 +4,7 @@ class NewIssueWorker
include NewIssuable include NewIssuable
def perform(issue_id, user_id) def perform(issue_id, user_id)
return unless ensure_objects_found(issue_id, user_id) return unless objects_found?(issue_id, user_id)
EventCreateService.new.open_issue(issuable, user) EventCreateService.new.open_issue(issuable, user)
NotificationService.new.new_issue(issuable, user) NotificationService.new.new_issue(issuable, user)
......
...@@ -4,7 +4,7 @@ class NewMergeRequestWorker ...@@ -4,7 +4,7 @@ class NewMergeRequestWorker
include NewIssuable include NewIssuable
def perform(merge_request_id, user_id) def perform(merge_request_id, user_id)
return unless ensure_objects_found(merge_request_id, user_id) return unless objects_found?(merge_request_id, user_id)
EventCreateService.new.open_mr(issuable, user) EventCreateService.new.open_mr(issuable, user)
NotificationService.new.new_merge_request(issuable, user) NotificationService.new.new_merge_request(issuable, user)
......
---
title: Simplify checking if objects exist code in new issaubles workers
merge_request:
author:
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment