Create a pending task when an issue is assigned to someone

parent 1e0053f2
......@@ -23,6 +23,10 @@ class BaseService
EventCreateService.new
end
def task_service
TaskService.new
end
def log_info(message)
Gitlab::AppLogger.info message
end
......
......@@ -9,6 +9,7 @@ module Issues
if issue.save
issue.update_attributes(label_ids: label_params)
notification_service.new_issue(issue, current_user)
task_service.new_issue(issue, current_user)
event_service.open_issue(issue, current_user)
issue.create_cross_references!(current_user)
execute_hooks(issue, 'open')
......
......@@ -12,6 +12,7 @@ module Issues
if issue.previous_changes.include?('assignee_id')
create_assignee_note(issue)
notification_service.reassigned_issue(issue, current_user)
task_service.reassigned_issue(issue, current_user)
end
end
......
# TaskService class
#
# Used for creating tasks on task queue after certain user action
#
# Ex.
# TaskService.new.new_issue(issue, current_user)
#
class TaskService
# When create an issue we should:
#
# * creates a pending task for assignee if issue is assigned
#
def new_issue(issue, current_user)
if issue.is_assigned?
create_task(issue.project, issue, current_user, issue.assignee, Task::ASSIGNED)
end
end
# When we reassign an issue we should:
#
# * creates a pending task for new assignee if issue is assigned
#
def reassigned_issue(issue, current_user)
if issue.is_assigned?
create_task(issue.project, issue, current_user, issue.assignee, Task::ASSIGNED)
end
end
private
def create_task(project, target, author, user, action)
attributes = {
project: project,
user_id: user.id,
author_id: author.id,
target_id: target.id,
target_type: target.class.name,
action: action
}
Task.create(attributes)
end
end
......@@ -3,14 +3,18 @@ require 'spec_helper'
describe Issues::CreateService, services: true do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
let(:assignee) { create(:user) }
describe :execute do
context "valid params" do
context 'valid params' do
before do
project.team << [user, :master]
project.team << [assignee, :master]
opts = {
title: 'Awesome issue',
description: 'please fix'
description: 'please fix',
assignee: assignee
}
@issue = Issues::CreateService.new(project, user, opts).execute
......@@ -18,6 +22,20 @@ describe Issues::CreateService, services: true do
it { expect(@issue).to be_valid }
it { expect(@issue.title).to eq('Awesome issue') }
it { expect(@issue.assignee).to eq assignee }
it 'creates a pending task for new assignee' do
attributes = {
project: project,
author: user,
user: assignee,
target: @issue,
action: Task::ASSIGNED,
state: :pending
}
expect(Task.where(attributes).count).to eq 1
end
end
end
end
......@@ -78,6 +78,19 @@ describe Issues::UpdateService, services: true do
expect(note).not_to be_nil
expect(note.note).to eq 'Title changed from **Old title** to **New title**'
end
it 'creates a pending task if being reassigned' do
attributes = {
project: project,
author: user,
user: user2,
target: issue,
action: Task::ASSIGNED,
state: :pending
}
expect(Task.where(attributes).count).to eq 1
end
end
context 'when Issue has tasks' do
......
require 'spec_helper'
describe TaskService, services: true do
let(:service) { described_class.new }
describe 'Issues' do
let(:author) { create(:user) }
let(:john_doe) { create(:user) }
let(:project) { create(:empty_project, :public) }
let(:assigned_issue) { create(:issue, project: project, assignee: john_doe) }
let(:unassigned_issue) { create(:issue, project: project, assignee: nil) }
before do
project.team << [author, :developer]
project.team << [john_doe, :developer]
end
describe '#new_issue' do
it 'creates a pending task if assigned' do
service.new_issue(assigned_issue, author)
is_expected_to_create_pending_task(user: john_doe, target: assigned_issue, action: Task::ASSIGNED)
end
it 'does not create a task if unassigned' do
is_expected_to_not_create_task { service.new_issue(unassigned_issue, author) }
end
end
describe '#reassigned_issue' do
it 'creates a pending task for new assignee' do
unassigned_issue.update_attribute(:assignee, john_doe)
service.reassigned_issue(unassigned_issue, author)
is_expected_to_create_pending_task(user: john_doe, target: unassigned_issue, action: Task::ASSIGNED)
end
it 'does not create a task if unassigned' do
assigned_issue.update_attribute(:assignee, nil)
is_expected_to_not_create_task { service.reassigned_issue(assigned_issue, author) }
end
end
end
def is_expected_to_create_pending_task(attributes = {})
attributes.reverse_merge!(
project: project,
author: author,
state: :pending
)
expect(Task.where(attributes).count).to eq 1
end
def is_expected_to_not_create_task
expect { yield }.not_to change(Task, :count)
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