From 476d146e26072c356f3e50a76232bb8724690ad6 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Fri, 17 Feb 2017 08:38:25 +0000 Subject: [PATCH 1/3] Added weight slash command Closes #852 --- .../slash_commands/interpret_service.rb | 24 +++++++++++++++++++ .../unreleased-ee/weight-slash-command.yml | 4 ++++ 2 files changed, 28 insertions(+) create mode 100644 changelogs/unreleased-ee/weight-slash-command.yml diff --git a/app/services/slash_commands/interpret_service.rb b/app/services/slash_commands/interpret_service.rb index 3e0a85cf059..f6adbf65f46 100644 --- a/app/services/slash_commands/interpret_service.rb +++ b/app/services/slash_commands/interpret_service.rb @@ -316,6 +316,30 @@ module SlashCommands @updates[:target_branch] = branch_name if project.repository.branch_names.include?(branch_name) end + desc 'Set weight' + params '1-9' + condition do + issuable.persisted? && + issuable.is_a?(Issue) && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) + end + command :weight do |weight| + if Issue.weight_filter_options.include?(weight.to_i) + @updates[:weight] = weight + end + end + + desc 'Clear weight' + condition do + issuable.persisted? && + issuable.is_a?(Issue) && + issuable.weight? && + current_user.can?(:"update_#{issuable.to_ability_name}", issuable) + end + command :clear_weight do + @updates[:weight] = nil + end + def find_label_ids(labels_param) label_ids_by_reference = extract_references(labels_param, :label).map(&:id) labels_ids_by_name = LabelsFinder.new(current_user, project_id: project.id, name: labels_param.split).execute.select(:id) diff --git a/changelogs/unreleased-ee/weight-slash-command.yml b/changelogs/unreleased-ee/weight-slash-command.yml new file mode 100644 index 00000000000..ec95a30d19c --- /dev/null +++ b/changelogs/unreleased-ee/weight-slash-command.yml @@ -0,0 +1,4 @@ +--- +title: Added weight slash command +merge_request: +author: -- GitLab From db7ee8aec8883d81e757888f2b860f427ef8d5e5 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Fri, 17 Feb 2017 11:10:49 +0000 Subject: [PATCH 2/3] Added tests Updated docs --- .../slash_commands/interpret_service.rb | 5 +- doc/user/project/slash_commands.md | 2 + .../issues/user_uses_slash_commands_spec.rb | 76 +++++++++++++++++++ .../user_uses_slash_commands_spec.rb | 8 ++ .../slash_commands/interpret_service_spec.rb | 27 +++++++ 5 files changed, 115 insertions(+), 3 deletions(-) diff --git a/app/services/slash_commands/interpret_service.rb b/app/services/slash_commands/interpret_service.rb index f6adbf65f46..f524cebedac 100644 --- a/app/services/slash_commands/interpret_service.rb +++ b/app/services/slash_commands/interpret_service.rb @@ -319,13 +319,12 @@ module SlashCommands desc 'Set weight' params '1-9' condition do - issuable.persisted? && - issuable.is_a?(Issue) && + issuable.is_a?(Issue) && current_user.can?(:"update_#{issuable.to_ability_name}", issuable) end command :weight do |weight| if Issue.weight_filter_options.include?(weight.to_i) - @updates[:weight] = weight + @updates[:weight] = weight.to_i end end diff --git a/doc/user/project/slash_commands.md b/doc/user/project/slash_commands.md index ad5d51d34f2..aa5fc16284c 100644 --- a/doc/user/project/slash_commands.md +++ b/doc/user/project/slash_commands.md @@ -35,3 +35,5 @@ do. | /spend <1h 30m | -1h 5m> | Add or subtract spent time | | `/remove_time_spent` | Remove time spent | | `/target_branch ` | Set target branch for current merge request | +| `/weight <1-9>` | Set the weight of the issue | +| `/clear_weight` | Clears the issue weight | diff --git a/spec/features/issues/user_uses_slash_commands_spec.rb b/spec/features/issues/user_uses_slash_commands_spec.rb index 0a9cd11ad6e..05088f7221e 100644 --- a/spec/features/issues/user_uses_slash_commands_spec.rb +++ b/spec/features/issues/user_uses_slash_commands_spec.rb @@ -161,5 +161,81 @@ feature 'Issues > User uses slash commands', feature: true, js: true do expect(page).not_to have_content '/wip' end end + + describe 'adding a weight from a note' do + let(:issue) { create(:issue, project: project) } + + context 'when the user can update the weight' do + it 'does not create a note, and sets the weight accordingly' do + write_note("/weight 5") + + expect(page).not_to have_content '/weight 5' + expect(page).to have_content 'Commands applied' + + issue.reload + + expect(issue.weight).to eq(5) + end + end + + context 'when the current user cannot update the weight' do + let(:guest) { create(:user) } + before do + project.team << [guest, :guest] + logout + login_with(guest) + visit namespace_project_issue_path(project.namespace, project, issue) + end + + it 'creates a note, and does not set the weight' do + write_note("/weight 5") + + expect(page).to have_content '/weight 5' + expect(page).not_to have_content 'Commands applied' + + issue.reload + + expect(issue.weight).not_to eq(5) + end + end + end + + describe 'removing weight from a note' do + let(:issue) { create(:issue, project: project, weight: 1) } + + context 'when the user can update the weight' do + it 'does not create a note, and removes the weight accordingly' do + write_note("/clear_weight") + + expect(page).not_to have_content '/clear_weight' + expect(page).to have_content 'Commands applied' + + issue.reload + + expect(issue.weight).to eq(nil) + end + end + + context 'when the current user cannot update the weight' do + let(:guest) { create(:user) } + before do + project.team << [guest, :guest] + logout + login_with(guest) + visit namespace_project_issue_path(project.namespace, project, issue) + end + + it 'creates a note, and does not set the weight' do + write_note("/clear_weight") + + expect(page).to have_content '/clear_weight' + expect(page).not_to have_content 'Commands applied' + + issue.reload + + expect(issue.weight).to eq(1) + end + end + end end end diff --git a/spec/features/merge_requests/user_uses_slash_commands_spec.rb b/spec/features/merge_requests/user_uses_slash_commands_spec.rb index 2f3c3e45ae6..0e337e5ee0b 100644 --- a/spec/features/merge_requests/user_uses_slash_commands_spec.rb +++ b/spec/features/merge_requests/user_uses_slash_commands_spec.rb @@ -196,5 +196,13 @@ feature 'Merge Requests > User uses slash commands', feature: true, js: true do end end end + + describe 'adding a weight from a note' do + it 'does not recognize the command nor create a note' do + write_note("/weight 5") + + expect(page).not_to have_content '/weight 5' + end + end end end diff --git a/spec/services/slash_commands/interpret_service_spec.rb b/spec/services/slash_commands/interpret_service_spec.rb index 0b0925983eb..d8c14c26808 100644 --- a/spec/services/slash_commands/interpret_service_spec.rb +++ b/spec/services/slash_commands/interpret_service_spec.rb @@ -267,6 +267,23 @@ describe SlashCommands::InterpretService, services: true do end end + shared_examples 'weight command' do + it 'populates weight: 5 if content contains /weight 5' do + _, updates = service.execute(content, issuable) + + expect(updates).to eq(weight: 5) + end + end + + shared_examples 'clear weight command' do + it 'populates weight: nil if content contains /clear_weight' do + issuable.update(weight: 5) + _, updates = service.execute(content, issuable) + + expect(updates).to eq(weight: nil) + end + end + it_behaves_like 'reopen command' do let(:content) { '/reopen' } let(:issuable) { issue } @@ -603,6 +620,16 @@ describe SlashCommands::InterpretService, services: true do let(:issuable) { issue } end + it_behaves_like 'weight command' do + let(:content) { '/weight 5'} + let(:issuable) { issue } + end + + it_behaves_like 'clear weight command' do + let(:content) { '/clear_weight' } + let(:issuable) { issue } + end + context 'when current_user cannot :admin_issue' do let(:visitor) { create(:user) } let(:issue) { create(:issue, project: project, author: visitor) } -- GitLab From 7c831be0eb42ac0240e2af7cc21e0d94236b4213 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Mon, 20 Feb 2017 10:32:31 +0000 Subject: [PATCH 3/3] Updated permission check Changed how it checks if issuable has weight --- app/services/slash_commands/interpret_service.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/services/slash_commands/interpret_service.rb b/app/services/slash_commands/interpret_service.rb index f524cebedac..d943f130255 100644 --- a/app/services/slash_commands/interpret_service.rb +++ b/app/services/slash_commands/interpret_service.rb @@ -317,10 +317,10 @@ module SlashCommands end desc 'Set weight' - params '1-9' + params Issue::WEIGHT_RANGE.to_s.squeeze('.').tr('.', '-') condition do - issuable.is_a?(Issue) && - current_user.can?(:"update_#{issuable.to_ability_name}", issuable) + issuable.respond_to?(:weight) && + current_user.can?(:"admin_#{issuable.to_ability_name}", issuable) end command :weight do |weight| if Issue.weight_filter_options.include?(weight.to_i) @@ -331,9 +331,9 @@ module SlashCommands desc 'Clear weight' condition do issuable.persisted? && - issuable.is_a?(Issue) && + issuable.respond_to?(:weight) && issuable.weight? && - current_user.can?(:"update_#{issuable.to_ability_name}", issuable) + current_user.can?(:"admin_#{issuable.to_ability_name}", issuable) end command :clear_weight do @updates[:weight] = nil -- GitLab