Use Slack::Notifier::LinkFormatter to convert markdown links to slack compat
This commit is contained in:
parent
f9f1a508c6
commit
0f2776287a
12 changed files with 57 additions and 58 deletions
|
@ -37,7 +37,7 @@ class ChatSlashCommandsService < Service
|
|||
end
|
||||
|
||||
Gitlab::ChatCommands::Command.new(project, user,
|
||||
params.merge(presenter_format: presenter_format)).execute
|
||||
params).execute
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -51,10 +51,6 @@ class ChatSlashCommandsService < Service
|
|||
end
|
||||
|
||||
def presenter
|
||||
Gitlab::ChatCommands::Presenter.new(presenter_format)
|
||||
end
|
||||
|
||||
def presenter_format
|
||||
throw NotImplementedError
|
||||
Gitlab::ChatCommands::Presenter.new
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,8 +18,4 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
|
|||
def to_param
|
||||
'mattermost_slash_commands'
|
||||
end
|
||||
|
||||
def presenter_format
|
||||
'mattermost'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,7 +13,14 @@ class SlackSlashCommandsService < ChatSlashCommandsService
|
|||
'slack_slash_commands'
|
||||
end
|
||||
|
||||
def presenter_format
|
||||
'slack'
|
||||
def trigger(params)
|
||||
result = super
|
||||
|
||||
# Format messages to be Slack-compatible
|
||||
if result && result[:text]
|
||||
result[:text] = Slack::Notifier::LinkFormatter.format(result[:text])
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
|
|
|
@ -44,7 +44,7 @@ module Gitlab
|
|||
end
|
||||
|
||||
def presenter
|
||||
Gitlab::ChatCommands::Presenter.new(params[:presenter_format])
|
||||
Gitlab::ChatCommands::Presenter.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,15 +3,9 @@ module Gitlab
|
|||
class Presenter
|
||||
include Gitlab::Routing.url_helpers
|
||||
|
||||
attr_reader :format
|
||||
|
||||
def initialize(format)
|
||||
@format = format
|
||||
end
|
||||
|
||||
def authorize_chat_name(url)
|
||||
message = if url
|
||||
":wave: Hi there! Before I do anything for you, please #{link(url, 'connect your GitLab account')}."
|
||||
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
|
||||
else
|
||||
":sweat_smile: Couldn't identify you, nor can I autorize you!"
|
||||
end
|
||||
|
@ -49,7 +43,7 @@ module Gitlab
|
|||
end
|
||||
|
||||
def access_denied
|
||||
ephemeral_response("Whoops! That action is not allowed. This incident will be #{link('https://xkcd.com/838/', 'reported')}.")
|
||||
ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -94,7 +88,7 @@ module Gitlab
|
|||
reference = resource.try(:to_reference) || resource.try(:id)
|
||||
title = resource.try(:title) || resource.try(:name)
|
||||
|
||||
link(url(resource), "#{reference} #{title}")
|
||||
"[#{reference} #{title}](#{url(resource)})"
|
||||
end
|
||||
|
||||
def header_with_list(header, items)
|
||||
|
@ -132,19 +126,6 @@ module Gitlab
|
|||
status: 200
|
||||
}
|
||||
end
|
||||
|
||||
def link(url, title)
|
||||
case format
|
||||
when 'slack'
|
||||
"<#{url}|#{title}>"
|
||||
|
||||
when 'mattermost'
|
||||
"[#{title}](#{url})"
|
||||
|
||||
else
|
||||
title
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,12 +3,10 @@ require 'spec_helper'
|
|||
describe Gitlab::ChatCommands::Command, service: true do
|
||||
let(:project) { create(:empty_project) }
|
||||
let(:user) { create(:user) }
|
||||
let(:format) { nil }
|
||||
|
||||
describe '#execute' do
|
||||
subject do
|
||||
described_class.new(project, user,
|
||||
params.merge(presenter_format: format)).execute
|
||||
described_class.new(project, user, params).execute
|
||||
end
|
||||
|
||||
context 'when no command is available' do
|
||||
|
@ -51,14 +49,8 @@ describe Gitlab::ChatCommands::Command, service: true do
|
|||
expect(subject[:text]).to match("my new issue")
|
||||
end
|
||||
|
||||
%w(slack mattermost).each do |format|
|
||||
context "for #{format}" do
|
||||
let(:format) { format }
|
||||
|
||||
it 'shows a link to the new issue' do
|
||||
expect(subject[:text]).to match(/\/issues\/\d+/)
|
||||
end
|
||||
end
|
||||
it 'shows a link to the new issue' do
|
||||
expect(subject[:text]).to match(/\/issues\/\d+/)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe MattermostNotificationService, models: true do
|
||||
it_behaves_like "slack or mattermost"
|
||||
it_behaves_like "slack or mattermost notifications"
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe MattermostSlashCommandsService, models: true do
|
||||
it { is_expected.to respond_to :presenter_format }
|
||||
it_behaves_like "chat slash commands"
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe SlackNotificationService, models: true do
|
||||
it_behaves_like "slack or mattermost"
|
||||
it_behaves_like "slack or mattermost notifications"
|
||||
end
|
||||
|
|
|
@ -1,5 +1,38 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe SlackSlashCommandsService, models: true do
|
||||
it { is_expected.to respond_to :presenter_format }
|
||||
it_behaves_like "chat slash commands"
|
||||
|
||||
describe '#trigger' do
|
||||
context 'when an auth url is generated' do
|
||||
let(:project) { create(:empty_project) }
|
||||
let(:params) do
|
||||
{
|
||||
team_domain: 'http://domain.tld',
|
||||
team_id: 'T3423423',
|
||||
user_id: 'U234234',
|
||||
user_name: 'mepmep',
|
||||
token: 'token'
|
||||
}
|
||||
end
|
||||
let(:service) do
|
||||
project.create_slack_slash_commands_service(
|
||||
properties: { token: 'token' }
|
||||
)
|
||||
end
|
||||
let(:authorize_url) do
|
||||
'http://authorize.example.com/'
|
||||
end
|
||||
|
||||
before do
|
||||
allow(service).to receive(:authorize_chat_name_url).and_return(authorize_url)
|
||||
end
|
||||
|
||||
it 'uses slack compatible links' do
|
||||
response = service.trigger(params)
|
||||
|
||||
expect(response[:text]).to include("<#{authorize_url}|connect your GitLab account>")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe ChatSlashCommandsService, models: true do
|
||||
RSpec.shared_examples 'chat slash commands' do
|
||||
describe "Associations" do
|
||||
it { is_expected.to respond_to :token }
|
||||
it { is_expected.to have_many :chat_names }
|
||||
|
@ -29,10 +27,6 @@ describe ChatSlashCommandsService, models: true do
|
|||
describe '#trigger' do
|
||||
subject { described_class.new }
|
||||
|
||||
before do
|
||||
allow(subject).to receive(:presenter_format).and_return('unknown')
|
||||
end
|
||||
|
||||
context 'no token is passed' do
|
||||
let(:params) { Hash.new }
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
Dir[Rails.root.join("app/models/project_services/chat_message/*.rb")].each { |f| require f }
|
||||
|
||||
RSpec.shared_examples 'slack or mattermost' do
|
||||
RSpec.shared_examples 'slack or mattermost notifications' do
|
||||
let(:chat_service) { described_class.new }
|
||||
let(:webhook_url) { 'https://example.gitlab.com/' }
|
||||
|
Loading…
Reference in a new issue