diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-08-13 11:58:51 -0500 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-08-16 17:42:34 -0500 |
commit | 029b7d2e9266246feff2f165a10b16be1d7fe88e (patch) | |
tree | 41581f4e08043530161c95aef9ccb533ec58a5e7 /app/services | |
parent | b2b1b4a4226267dbc442d62e19949909d9e58235 (diff) | |
download | gitlab-ce-029b7d2e9266246feff2f165a10b16be1d7fe88e.tar.gz |
Fixed specs and fixes based on failing specs
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/notes/create_service.rb | 23 | ||||
-rw-r--r-- | app/services/notes/slash_commands_service.rb | 19 | ||||
-rw-r--r-- | app/services/slash_commands/interpret_service.rb | 79 |
3 files changed, 73 insertions, 48 deletions
diff --git a/app/services/notes/create_service.rb b/app/services/notes/create_service.rb index e9f37e04993..a36008c3ef5 100644 --- a/app/services/notes/create_service.rb +++ b/app/services/notes/create_service.rb @@ -15,20 +15,29 @@ module Notes # **before** we save the note because if the note consists of commands # only, there is no need be create a note! slash_commands_service = SlashCommandsService.new(project, current_user) - content, command_params = slash_commands_service.extract_commands(note) - note.note = content + if slash_commands_service.supported?(note) + content, command_params = slash_commands_service.extract_commands(note) - if note.save + only_commands = content.empty? + + note.note = content + end + + if !only_commands && note.save # Finish the harder work in the background NewNoteWorker.perform_in(2.seconds, note.id, params) todo_service.new_note(note, current_user) end - # We must add the error after we call #save because errors are reset - # when #save is called - if slash_commands_service.execute(command_params, note) && note.note.blank? - note.errors.add(:commands_only, 'Your commands have been executed!') + if command_params && command_params.any? + slash_commands_service.execute(command_params, note) + + # We must add the error after we call #save because errors are reset + # when #save is called + if only_commands + note.errors.add(:commands_only, 'Your commands have been executed!') + end end note diff --git a/app/services/notes/slash_commands_service.rb b/app/services/notes/slash_commands_service.rb index f2c43775b72..4a9a8a64653 100644 --- a/app/services/notes/slash_commands_service.rb +++ b/app/services/notes/slash_commands_service.rb @@ -5,10 +5,13 @@ module Notes 'MergeRequest' => MergeRequests::UpdateService } + def supported?(note) + noteable_update_service(note) && + can?(current_user, :"update_#{note.noteable_type.underscore}", note.noteable) + end + def extract_commands(note) - @noteable_update_service = UPDATE_SERVICES[note.noteable_type] - return [] unless @noteable_update_service - return [] unless can?(current_user, :"update_#{note.noteable_type.underscore}", note.noteable) + return [note.note, {}] unless supported?(note) SlashCommands::InterpretService.new(project, current_user). execute(note.note, note.noteable) @@ -16,9 +19,15 @@ module Notes def execute(command_params, note) return if command_params.empty? + return unless supported?(note) + + noteable_update_service(note).new(project, current_user, command_params).execute(note.noteable) + end + + private - @noteable_update_service.new(project, current_user, command_params). - execute(note.noteable) + def noteable_update_service(note) + UPDATE_SERVICES[note.noteable_type] end end end diff --git a/app/services/slash_commands/interpret_service.rb b/app/services/slash_commands/interpret_service.rb index f7b9547bd2b..126f97b0f9b 100644 --- a/app/services/slash_commands/interpret_service.rb +++ b/app/services/slash_commands/interpret_service.rb @@ -2,16 +2,16 @@ module SlashCommands class InterpretService < BaseService include Gitlab::SlashCommands::Dsl - attr_reader :noteable + attr_reader :issuable # Takes a text and interpret the commands that are extracted from it. # Returns a hash of changes to be applied to a record. - def execute(content, noteable) - @noteable = noteable + def execute(content, issuable) + @issuable = issuable @updates = {} opts = { - noteable: noteable, + issuable: issuable, current_user: current_user, project: project } @@ -35,23 +35,24 @@ module SlashCommands end desc do - "Close this #{noteable.to_ability_name.humanize(capitalize: false)}" + "Close this #{issuable.to_ability_name.humanize(capitalize: false)}" end condition do - noteable.persisted? && - noteable.open? && - current_user.can?(:"update_#{noteable.to_ability_name}", noteable) + issuable.persisted? && + issuable.open? && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :close do @updates[:state_event] = 'close' end desc do - "Reopen this #{noteable.to_ability_name.humanize(capitalize: false)}" + "Reopen this #{issuable.to_ability_name.humanize(capitalize: false)}" end condition do - noteable.closed? && - current_user.can?(:"update_#{noteable.to_ability_name}", noteable) + issuable.persisted? && + issuable.closed? && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :reopen, :open do @updates[:state_event] = 'reopen' @@ -60,8 +61,8 @@ module SlashCommands desc 'Change title' params '<New title>' condition do - noteable.persisted? && - current_user.can?(:"update_#{noteable.to_ability_name}", noteable) + issuable.persisted? && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :title do |title_param| @updates[:title] = title_param @@ -70,7 +71,7 @@ module SlashCommands desc 'Assign' params '@user' condition do - current_user.can?(:"admin_#{noteable.to_ability_name}", project) + current_user.can?(:"admin_#{issuable.to_ability_name}", project) end command :assign do |assignee_param| user = extract_references(assignee_param, :user).first @@ -82,8 +83,9 @@ module SlashCommands desc 'Remove assignee' condition do - noteable.assignee_id? && - current_user.can?(:"admin_#{noteable.to_ability_name}", project) + issuable.persisted? && + issuable.assignee_id? && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) end command :unassign, :remove_assignee do @updates[:assignee_id] = nil @@ -92,7 +94,7 @@ module SlashCommands desc 'Set milestone' params '%"milestone"' condition do - current_user.can?(:"admin_#{noteable.to_ability_name}", project) && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) && project.milestones.active.any? end command :milestone do |milestone_param| @@ -104,8 +106,9 @@ module SlashCommands desc 'Remove milestone' condition do - noteable.milestone_id? && - current_user.can?(:"admin_#{noteable.to_ability_name}", project) + issuable.persisted? && + issuable.milestone_id? && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) end command :clear_milestone, :remove_milestone do @updates[:milestone_id] = nil @@ -114,7 +117,7 @@ module SlashCommands desc 'Add label(s)' params '~label1 ~"label 2"' condition do - current_user.can?(:"admin_#{noteable.to_ability_name}", project) && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) && project.labels.any? end command :label, :labels do |labels_param| @@ -126,8 +129,9 @@ module SlashCommands desc 'Remove label(s)' params '~label1 ~"label 2"' condition do - noteable.labels.any? && - current_user.can?(:"admin_#{noteable.to_ability_name}", project) + issuable.persisted? && + issuable.labels.any? && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) end command :unlabel, :remove_label, :remove_labels do |labels_param| label_ids = find_label_ids(labels_param) @@ -137,8 +141,9 @@ module SlashCommands desc 'Remove all labels' condition do - noteable.labels.any? && - current_user.can?(:"admin_#{noteable.to_ability_name}", project) + issuable.persisted? && + issuable.labels.any? && + current_user.can?(:"admin_#{issuable.to_ability_name}", project) end command :clear_labels, :clear_label do @updates[:label_ids] = [] @@ -146,8 +151,8 @@ module SlashCommands desc 'Add a todo' condition do - noteable.persisted? && - !TodoService.new.todo_exist?(noteable, current_user) + issuable.persisted? && + !TodoService.new.todo_exist?(issuable, current_user) end command :todo do @updates[:todo_event] = 'add' @@ -155,7 +160,8 @@ module SlashCommands desc 'Mark todo as done' condition do - TodoService.new.todo_exist?(noteable, current_user) + issuable.persisted? && + TodoService.new.todo_exist?(issuable, current_user) end command :done do @updates[:todo_event] = 'done' @@ -163,8 +169,8 @@ module SlashCommands desc 'Subscribe' condition do - noteable.persisted? && - !noteable.subscribed?(current_user) + issuable.persisted? && + !issuable.subscribed?(current_user) end command :subscribe do @updates[:subscription_event] = 'subscribe' @@ -172,8 +178,8 @@ module SlashCommands desc 'Unsubscribe' condition do - noteable.persisted? && - noteable.subscribed?(current_user) + issuable.persisted? && + issuable.subscribed?(current_user) end command :unsubscribe do @updates[:subscription_event] = 'unsubscribe' @@ -182,8 +188,8 @@ module SlashCommands desc 'Set due date' params '<in 2 days; this Friday; December 31st>' condition do - noteable.respond_to?(:due_date) && - current_user.can?(:"update_#{noteable.to_ability_name}", noteable) + issuable.respond_to?(:due_date) && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :due, :due_date do |due_date_param| due_date = Chronic.parse(due_date_param).try(:to_date) @@ -193,9 +199,10 @@ module SlashCommands desc 'Remove due date' condition do - noteable.respond_to?(:due_date) && - noteable.due_date? && - current_user.can?(:"update_#{noteable.to_ability_name}", noteable) + issuable.persisted? && + issuable.respond_to?(:due_date) && + issuable.due_date? && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :clear_due_date do @updates[:due_date] = nil |