Marks pending tasks for an user as done when he edit an issue
This commit is contained in:
parent
48ddf9a407
commit
e81061a211
|
@ -54,7 +54,7 @@ class IssuableBaseService < BaseService
|
||||||
if params.present? && issuable.update_attributes(params.merge(updated_by: current_user))
|
if params.present? && issuable.update_attributes(params.merge(updated_by: current_user))
|
||||||
issuable.reset_events_cache
|
issuable.reset_events_cache
|
||||||
handle_common_system_notes(issuable, old_labels: old_labels)
|
handle_common_system_notes(issuable, old_labels: old_labels)
|
||||||
handle_changes(issuable)
|
handle_changes(issuable, old_labels: old_labels)
|
||||||
issuable.create_new_cross_references!(current_user)
|
issuable.create_new_cross_references!(current_user)
|
||||||
execute_hooks(issuable, 'update')
|
execute_hooks(issuable, 'update')
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,11 @@ module Issues
|
||||||
update(issue)
|
update(issue)
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_changes(issue)
|
def handle_changes(issue, options = {})
|
||||||
|
if have_changes?(issue, options)
|
||||||
|
task_service.mark_as_done(issue, current_user)
|
||||||
|
end
|
||||||
|
|
||||||
if issue.previous_changes.include?('milestone_id')
|
if issue.previous_changes.include?('milestone_id')
|
||||||
create_milestone_note(issue)
|
create_milestone_note(issue)
|
||||||
end
|
end
|
||||||
|
@ -23,5 +27,22 @@ module Issues
|
||||||
def close_service
|
def close_service
|
||||||
Issues::CloseService
|
Issues::CloseService
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def have_changes?(issue, options = {})
|
||||||
|
valid_attrs = [:title, :description, :assignee_id, :milestone_id]
|
||||||
|
|
||||||
|
attrs_changed = valid_attrs.any? do |attr|
|
||||||
|
issue.previous_changes.include?(attr.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
old_labels = options[:old_labels]
|
||||||
|
labels_changed = old_labels && issue.labels != old_labels
|
||||||
|
|
||||||
|
if attrs_changed || labels_changed
|
||||||
|
task_service.mark_as_done(issue, current_user)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ module MergeRequests
|
||||||
update(merge_request)
|
update(merge_request)
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_changes(merge_request)
|
def handle_changes(merge_request, options = {})
|
||||||
if merge_request.previous_changes.include?('target_branch')
|
if merge_request.previous_changes.include?('target_branch')
|
||||||
create_branch_change_note(merge_request, 'target',
|
create_branch_change_note(merge_request, 'target',
|
||||||
merge_request.previous_changes['target_branch'].first,
|
merge_request.previous_changes['target_branch'].first,
|
||||||
|
|
|
@ -26,6 +26,15 @@ class TaskService
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# When we mark a task as done we should:
|
||||||
|
#
|
||||||
|
# * mark all pending tasks related to the target for the user as done
|
||||||
|
#
|
||||||
|
def mark_as_done(target, user)
|
||||||
|
pending_tasks = pending_tasks_for(user, target.project, target)
|
||||||
|
pending_tasks.update_all(state: :done)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_task(project, target, author, user, action)
|
def create_task(project, target, author, user, action)
|
||||||
|
@ -40,4 +49,8 @@ class TaskService
|
||||||
|
|
||||||
Task.create(attributes)
|
Task.create(attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pending_tasks_for(user, project, target)
|
||||||
|
user.tasks.pending.where(project: project, target: target)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,5 +19,15 @@ FactoryGirl.define do
|
||||||
project
|
project
|
||||||
author
|
author
|
||||||
user
|
user
|
||||||
|
|
||||||
|
factory :pending_assigned_task, traits: [:assgined, :pending]
|
||||||
|
|
||||||
|
trait :assgined do
|
||||||
|
action { Task::ASSIGNED }
|
||||||
|
end
|
||||||
|
|
||||||
|
trait :pending do
|
||||||
|
state { :pending }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -78,18 +78,74 @@ describe Issues::UpdateService, services: true do
|
||||||
expect(note).not_to be_nil
|
expect(note).not_to be_nil
|
||||||
expect(note.note).to eq 'Title changed from **Old title** to **New title**'
|
expect(note.note).to eq 'Title changed from **Old title** to **New title**'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'creates a pending task if being reassigned' do
|
context 'task queue' do
|
||||||
attributes = {
|
let!(:pending_task) do
|
||||||
project: project,
|
create(:pending_assigned_task, user: user, project: project, target: issue, author: user2)
|
||||||
author: user,
|
end
|
||||||
user: user2,
|
|
||||||
target: issue,
|
|
||||||
action: Task::ASSIGNED,
|
|
||||||
state: :pending
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(Task.where(attributes).count).to eq 1
|
context 'when the title change' do
|
||||||
|
before do
|
||||||
|
update_issue({ title: 'New title' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks pending tasks as done' do
|
||||||
|
expect(pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the description change' do
|
||||||
|
before do
|
||||||
|
update_issue({ description: 'Also please fix' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks pending tasks as done' do
|
||||||
|
expect(pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when is reassigned' do
|
||||||
|
before do
|
||||||
|
update_issue({ assignee: user2 })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks previous assignee pending tasks as done' do
|
||||||
|
expect(pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a pending task for new assignee' 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 the milestone change' do
|
||||||
|
before do
|
||||||
|
update_issue({ milestone: create(:milestone) })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks pending tasks as done' do
|
||||||
|
expect(pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the labels change' do
|
||||||
|
before do
|
||||||
|
update_issue({ label_ids: [label.id] })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks pending tasks as done' do
|
||||||
|
expect(pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -157,6 +213,5 @@ describe Issues::UpdateService, services: true do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,6 +45,23 @@ describe TaskService, services: true do
|
||||||
is_expected_to_not_create_task { service.reassigned_issue(assigned_issue, author) }
|
is_expected_to_not_create_task { service.reassigned_issue(assigned_issue, author) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#mark_as_done' do
|
||||||
|
let!(:first_pending_task) do
|
||||||
|
create(:pending_assigned_task, user: john_doe, project: project, target: assigned_issue, author: author)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:second_pending_task) do
|
||||||
|
create(:pending_assigned_task, user: john_doe, project: project, target: assigned_issue, author: author)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks related pending tasks to the target for the user as done' do
|
||||||
|
service.mark_as_done(assigned_issue, john_doe)
|
||||||
|
|
||||||
|
expect(first_pending_task.reload.done?).to eq true
|
||||||
|
expect(second_pending_task.reload.done?).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_expected_to_create_pending_task(attributes = {})
|
def is_expected_to_create_pending_task(attributes = {})
|
||||||
|
|
Loading…
Reference in New Issue