Commit 31af7e87 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'weight-slash-command' into 'master'

Added weight slash command

Closes #852

See merge request !1255
parents becf6e4c 7c831be0
...@@ -316,6 +316,29 @@ module SlashCommands ...@@ -316,6 +316,29 @@ module SlashCommands
@updates[:target_branch] = branch_name if project.repository.branch_names.include?(branch_name) @updates[:target_branch] = branch_name if project.repository.branch_names.include?(branch_name)
end end
desc 'Set weight'
params Issue::WEIGHT_RANGE.to_s.squeeze('.').tr('.', '-')
condition do
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)
@updates[:weight] = weight.to_i
end
end
desc 'Clear weight'
condition do
issuable.persisted? &&
issuable.respond_to?(:weight) &&
issuable.weight? &&
current_user.can?(:"admin_#{issuable.to_ability_name}", issuable)
end
command :clear_weight do
@updates[:weight] = nil
end
def find_label_ids(labels_param) def find_label_ids(labels_param)
label_ids_by_reference = extract_references(labels_param, :label).map(&:id) 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) labels_ids_by_name = LabelsFinder.new(current_user, project_id: project.id, name: labels_param.split).execute.select(:id)
......
---
title: Added weight slash command
merge_request:
author:
...@@ -35,3 +35,5 @@ do. ...@@ -35,3 +35,5 @@ do.
| <code>/spend &lt;1h 30m &#124; -1h 5m&gt;</code> | Add or subtract spent time | | <code>/spend &lt;1h 30m &#124; -1h 5m&gt;</code> | Add or subtract spent time |
| `/remove_time_spent` | Remove time spent | | `/remove_time_spent` | Remove time spent |
| `/target_branch <Branch Name>` | Set target branch for current merge request | | `/target_branch <Branch Name>` | Set target branch for current merge request |
| `/weight <1-9>` | Set the weight of the issue |
| `/clear_weight` | Clears the issue weight |
...@@ -161,5 +161,81 @@ feature 'Issues > User uses slash commands', feature: true, js: true do ...@@ -161,5 +161,81 @@ feature 'Issues > User uses slash commands', feature: true, js: true do
expect(page).not_to have_content '/wip' expect(page).not_to have_content '/wip'
end end
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
end end
...@@ -196,5 +196,13 @@ feature 'Merge Requests > User uses slash commands', feature: true, js: true do ...@@ -196,5 +196,13 @@ feature 'Merge Requests > User uses slash commands', feature: true, js: true do
end end
end 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
end end
...@@ -267,6 +267,23 @@ describe SlashCommands::InterpretService, services: true do ...@@ -267,6 +267,23 @@ describe SlashCommands::InterpretService, services: true do
end end
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 it_behaves_like 'reopen command' do
let(:content) { '/reopen' } let(:content) { '/reopen' }
let(:issuable) { issue } let(:issuable) { issue }
...@@ -603,6 +620,16 @@ describe SlashCommands::InterpretService, services: true do ...@@ -603,6 +620,16 @@ describe SlashCommands::InterpretService, services: true do
let(:issuable) { issue } let(:issuable) { issue }
end 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 context 'when current_user cannot :admin_issue' do
let(:visitor) { create(:user) } let(:visitor) { create(:user) }
let(:issue) { create(:issue, project: project, author: visitor) } let(:issue) { create(:issue, project: project, author: visitor) }
......
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