Commit e8dd299e authored by Yorick Peterse's avatar Yorick Peterse Committed by Yorick Peterse

Improve chatops help output

This improves the output produced when running an unknown command,
running the "help" command, and when trying to run a command you are not
allowed to run. The new help output includes links to the project of the
chatops integration, and a link to the chatops documentation.
parent e8bcabb4
...@@ -35,7 +35,9 @@ class SlashCommandsService < Service ...@@ -35,7 +35,9 @@ class SlashCommandsService < Service
chat_user = find_chat_user(params) chat_user = find_chat_user(params)
if chat_user&.user if chat_user&.user
return Gitlab::SlashCommands::Presenters::Access.new.access_denied unless chat_user.user.can?(:use_slash_commands) unless chat_user.user.can?(:use_slash_commands)
return Gitlab::SlashCommands::Presenters::Access.new.access_denied(project)
end
Gitlab::SlashCommands::Command.new(project, chat_user, params).execute Gitlab::SlashCommands::Command.new(project, chat_user, params).execute
else else
......
---
title: Improve chatops help output
merge_request: 32208
author:
type: changed
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
module Gitlab module Gitlab
module SlashCommands module SlashCommands
class ApplicationHelp < BaseCommand class ApplicationHelp < BaseCommand
def initialize(params) def initialize(project, params)
@project = project
@params = params @params = params
end end
def execute def execute
Gitlab::SlashCommands::Presenters::Help.new(commands).present(trigger, params[:text]) Gitlab::SlashCommands::Presenters::Help
.new(project, commands)
.present(trigger, params[:text])
end end
private private
......
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
if command.allowed?(project, current_user) if command.allowed?(project, current_user)
command.new(project, chat_name, params).execute(match) command.new(project, chat_name, params).execute(match)
else else
Gitlab::SlashCommands::Presenters::Access.new.access_denied Gitlab::SlashCommands::Presenters::Access.new.access_denied(project)
end end
else else
Gitlab::SlashCommands::Help.new(project, chat_name, params) Gitlab::SlashCommands::Help.new(project, chat_name, params)
......
...@@ -19,7 +19,9 @@ module Gitlab ...@@ -19,7 +19,9 @@ module Gitlab
end end
def execute(commands, text) def execute(commands, text)
Gitlab::SlashCommands::Presenters::Help.new(commands).present(trigger, text) Gitlab::SlashCommands::Presenters::Help
.new(project, commands)
.present(trigger, text)
end end
def trigger def trigger
......
...@@ -4,8 +4,15 @@ module Gitlab ...@@ -4,8 +4,15 @@ module Gitlab
module SlashCommands module SlashCommands
module Presenters module Presenters
class Access < Presenters::Base class Access < Presenters::Base
def access_denied def access_denied(project)
ephemeral_response(text: "Whoops! This action is not allowed. This incident will be [reported](https://xkcd.com/838/).") ephemeral_response(text: <<~MESSAGE)
You are not allowed to perform the given chatops command. Most
likely you do not have access to the GitLab project for this chatops
integration.
The GitLab project for this chatops integration can be found at
#{url_for(project)}.
MESSAGE
end end
def not_found def not_found
...@@ -22,20 +29,6 @@ module Gitlab ...@@ -22,20 +29,6 @@ module Gitlab
ephemeral_response(text: message) ephemeral_response(text: message)
end end
def unknown_command(commands)
ephemeral_response(text: help_message(trigger))
end
private
def help_message(trigger)
header_with_list("Command not found, these are the commands you can use", full_commands(trigger))
end
def full_commands(trigger)
@resource.map { |command| "#{trigger} #{command.help_message}" }
end
end end
end end
end end
......
...@@ -4,6 +4,11 @@ module Gitlab ...@@ -4,6 +4,11 @@ module Gitlab
module SlashCommands module SlashCommands
module Presenters module Presenters
class Help < Presenters::Base class Help < Presenters::Base
def initialize(project, commands)
@project = project
@commands = commands
end
def present(trigger, text) def present(trigger, text)
ephemeral_response(text: help_message(trigger, text)) ephemeral_response(text: help_message(trigger, text))
end end
...@@ -11,17 +16,64 @@ module Gitlab ...@@ -11,17 +16,64 @@ module Gitlab
private private
def help_message(trigger, text) def help_message(trigger, text)
return "No commands available :thinking_face:" unless @resource.present? unless @commands.present?
return <<~MESSAGE
This chatops integration does not have any commands that can be
executed.
#{footer}
MESSAGE
end
if text.start_with?('help') if text.start_with?('help')
header_with_list("Available commands", full_commands(trigger)) <<~MESSAGE
#{full_commands_message(trigger)}
#{help_footer}
MESSAGE
else else
header_with_list("Unknown command, these commands are available", full_commands(trigger)) <<~MESSAGE
The specified command is not valid.
#{full_commands_message(trigger)}
#{help_footer}
MESSAGE
end end
end end
def full_commands(trigger) def help_footer
@resource.map { |command| "#{trigger} #{command.help_message}" } <<~MESSAGE
*Project*
The GitLab project for this chatops integration can be found at
#{url_for(@project)}.
*Documentation*
For more information about GitLab chatops, refer to its
documentation: https://docs.gitlab.com/ce/ci/chatops/README.html.
MESSAGE
end
def full_commands_message(trigger)
list = @commands
.map { |command| "#{trigger} #{command.help_message}" }
.join("\n")
<<~MESSAGE
*Available commands*
The following commands are available for this chatops integration:
#{list}
If available, the `run` command is used for running GitLab CI jobs
defined in this project's `.gitlab-ci.yml` file. For example, if a
job called "help" is defined you can run it like so:
`#{trigger} run help`
MESSAGE
end end
end end
end end
......
...@@ -4,10 +4,11 @@ require 'spec_helper' ...@@ -4,10 +4,11 @@ require 'spec_helper'
describe Gitlab::SlashCommands::ApplicationHelp do describe Gitlab::SlashCommands::ApplicationHelp do
let(:params) { { command: '/gitlab', text: 'help' } } let(:params) { { command: '/gitlab', text: 'help' } }
let(:project) { build(:project) }
describe '#execute' do describe '#execute' do
subject do subject do
described_class.new(params).execute described_class.new(project, params).execute
end end
it 'displays the help section' do it 'displays the help section' do
......
...@@ -27,7 +27,7 @@ describe Gitlab::SlashCommands::Command do ...@@ -27,7 +27,7 @@ describe Gitlab::SlashCommands::Command do
it 'displays the help message' do it 'displays the help message' do
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to start_with('Unknown command') expect(subject[:text]).to start_with('The specified command is not valid')
expect(subject[:text]).to match('/gitlab issue show') expect(subject[:text]).to match('/gitlab issue show')
end end
end end
...@@ -37,7 +37,7 @@ describe Gitlab::SlashCommands::Command do ...@@ -37,7 +37,7 @@ describe Gitlab::SlashCommands::Command do
it 'rejects the actions' do it 'rejects the actions' do
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to start_with('Whoops! This action is not allowed') expect(subject[:text]).to start_with('You are not allowed')
end end
end end
...@@ -57,7 +57,7 @@ describe Gitlab::SlashCommands::Command do ...@@ -57,7 +57,7 @@ describe Gitlab::SlashCommands::Command do
context 'and user can not create deployment' do context 'and user can not create deployment' do
it 'returns action' do it 'returns action' do
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to start_with('Whoops! This action is not allowed') expect(subject[:text]).to start_with('You are not allowed')
end end
end end
......
...@@ -4,12 +4,14 @@ require 'spec_helper' ...@@ -4,12 +4,14 @@ require 'spec_helper'
describe Gitlab::SlashCommands::Presenters::Access do describe Gitlab::SlashCommands::Presenters::Access do
describe '#access_denied' do describe '#access_denied' do
subject { described_class.new.access_denied } let(:project) { build(:project) }
subject { described_class.new.access_denied(project) }
it { is_expected.to be_a(Hash) } it { is_expected.to be_a(Hash) }
it 'displays an error message' do it 'displays an error message' do
expect(subject[:text]).to match("is not allowed") expect(subject[:text]).to match('are not allowed')
expect(subject[:response_type]).to be(:ephemeral) expect(subject[:response_type]).to be(:ephemeral)
end end
end end
......
...@@ -103,7 +103,7 @@ RSpec.shared_examples 'chat slash commands service' do ...@@ -103,7 +103,7 @@ RSpec.shared_examples 'chat slash commands service' do
expect_any_instance_of(Gitlab::SlashCommands::Command).not_to receive(:execute) expect_any_instance_of(Gitlab::SlashCommands::Command).not_to receive(:execute)
result = subject.trigger(params) result = subject.trigger(params)
expect(result).to include(text: /^Whoops! This action is not allowed/) expect(result).to include(text: /^You are not allowed/)
end end
end end
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