Commit cfd9fd30 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Move code for issue creation to service.

The goal of suych refactoring is to get rid of observers.
Its much easier to test and code when object creation and all other
related actions done in one class instead of splited across observers,
callbacks etc.
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 3b0510a7
...@@ -59,9 +59,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -59,9 +59,7 @@ class Projects::IssuesController < Projects::ApplicationController
end end
def create def create
@issue = @project.issues.new(params[:issue]) @issue = Issues::CreateService.new(project, current_user, params[:issue]).execute
@issue.author = current_user
@issue.save
respond_to do |format| respond_to do |format|
format.html do format.html do
......
class IssueObserver < BaseObserver class IssueObserver < BaseObserver
def after_create(issue)
notification.new_issue(issue, current_user)
event_service.open_issue(issue, current_user)
issue.create_cross_references!(issue.project, current_user)
execute_hooks(issue)
end
def after_close(issue, transition) def after_close(issue, transition)
notification.close_issue(issue, current_user) notification.close_issue(issue, current_user)
event_service.close_issue(issue, current_user) event_service.close_issue(issue, current_user)
......
...@@ -16,4 +16,16 @@ class BaseService ...@@ -16,4 +16,16 @@ class BaseService
def can?(object, action, subject) def can?(object, action, subject)
abilities.allowed?(object, action, subject) abilities.allowed?(object, action, subject)
end end
def notification_service
NotificationService.new
end
def event_service
EventCreateService.new
end
def log_info message
Gitlab::AppLogger.info message
end
end end
module Issues
class CreateService < BaseService
def execute
issue = project.issues.new(params)
issue.author = current_user
if issue.save
notification_service.new_issue(issue, current_user)
event_service.open_issue(issue, current_user)
issue.create_cross_references!(issue.project, current_user)
execute_hooks(issue)
end
issue
end
private
def execute_hooks(issue)
issue.project.execute_hooks(issue.to_hook_data, :issue_hooks)
end
end
end
...@@ -48,19 +48,17 @@ module API ...@@ -48,19 +48,17 @@ module API
# Example Request: # Example Request:
# POST /projects/:id/issues # POST /projects/:id/issues
post ":id/issues" do post ":id/issues" do
set_current_user_for_thread do
required_attributes! [:title] required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id]
attrs[:label_list] = params[:labels] if params[:labels].present? attrs[:label_list] = params[:labels] if params[:labels].present?
@issue = user_project.issues.new attrs issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute
@issue.author = current_user
if @issue.save if issue.valid?
present @issue, with: Entities::Issue present issue, with: Entities::Issue
else else
not_found! not_found!
end end
end end
end
# Update an existing issue # Update an existing issue
# #
......
require 'spec_helper'
describe Issues::CreateService do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
describe :execute do
context "valid params" do
before do
project.team << [user, :master]
opts = {
title: 'Awesome issue',
description: 'please fix'
}
@issue = Issues::CreateService.new(project, user, opts).execute
end
it { @issue.should be_valid }
end
end
end
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