Commit 0f277628 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use Slack::Notifier::LinkFormatter to convert markdown links to slack compat

parent f9f1a508
...@@ -37,7 +37,7 @@ class ChatSlashCommandsService < Service ...@@ -37,7 +37,7 @@ class ChatSlashCommandsService < Service
end end
Gitlab::ChatCommands::Command.new(project, user, Gitlab::ChatCommands::Command.new(project, user,
params.merge(presenter_format: presenter_format)).execute params).execute
end end
private private
...@@ -51,10 +51,6 @@ class ChatSlashCommandsService < Service ...@@ -51,10 +51,6 @@ class ChatSlashCommandsService < Service
end end
def presenter def presenter
Gitlab::ChatCommands::Presenter.new(presenter_format) Gitlab::ChatCommands::Presenter.new
end
def presenter_format
throw NotImplementedError
end end
end end
...@@ -18,8 +18,4 @@ class MattermostSlashCommandsService < ChatSlashCommandsService ...@@ -18,8 +18,4 @@ class MattermostSlashCommandsService < ChatSlashCommandsService
def to_param def to_param
'mattermost_slash_commands' 'mattermost_slash_commands'
end end
def presenter_format
'mattermost'
end
end end
...@@ -13,7 +13,14 @@ class SlackSlashCommandsService < ChatSlashCommandsService ...@@ -13,7 +13,14 @@ class SlackSlashCommandsService < ChatSlashCommandsService
'slack_slash_commands' 'slack_slash_commands'
end end
def presenter_format def trigger(params)
'slack' result = super
# Format messages to be Slack-compatible
if result && result[:text]
result[:text] = Slack::Notifier::LinkFormatter.format(result[:text])
end
result
end end
end end
...@@ -44,7 +44,7 @@ module Gitlab ...@@ -44,7 +44,7 @@ module Gitlab
end end
def presenter def presenter
Gitlab::ChatCommands::Presenter.new(params[:presenter_format]) Gitlab::ChatCommands::Presenter.new
end end
end end
end end
......
...@@ -3,15 +3,9 @@ module Gitlab ...@@ -3,15 +3,9 @@ module Gitlab
class Presenter class Presenter
include Gitlab::Routing.url_helpers include Gitlab::Routing.url_helpers
attr_reader :format
def initialize(format)
@format = format
end
def authorize_chat_name(url) def authorize_chat_name(url)
message = if 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 else
":sweat_smile: Couldn't identify you, nor can I autorize you!" ":sweat_smile: Couldn't identify you, nor can I autorize you!"
end end
...@@ -49,7 +43,7 @@ module Gitlab ...@@ -49,7 +43,7 @@ module Gitlab
end end
def access_denied 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 end
private private
...@@ -94,7 +88,7 @@ module Gitlab ...@@ -94,7 +88,7 @@ module Gitlab
reference = resource.try(:to_reference) || resource.try(:id) reference = resource.try(:to_reference) || resource.try(:id)
title = resource.try(:title) || resource.try(:name) title = resource.try(:title) || resource.try(:name)
link(url(resource), "#{reference} #{title}") "[#{reference} #{title}](#{url(resource)})"
end end
def header_with_list(header, items) def header_with_list(header, items)
...@@ -132,19 +126,6 @@ module Gitlab ...@@ -132,19 +126,6 @@ module Gitlab
status: 200 status: 200
} }
end end
def link(url, title)
case format
when 'slack'
"<#{url}|#{title}>"
when 'mattermost'
"[#{title}](#{url})"
else
title
end
end
end end
end end
end end
...@@ -3,12 +3,10 @@ require 'spec_helper' ...@@ -3,12 +3,10 @@ require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do describe Gitlab::ChatCommands::Command, service: true do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:format) { nil }
describe '#execute' do describe '#execute' do
subject do subject do
described_class.new(project, user, described_class.new(project, user, params).execute
params.merge(presenter_format: format)).execute
end end
context 'when no command is available' do context 'when no command is available' do
...@@ -51,14 +49,8 @@ describe Gitlab::ChatCommands::Command, service: true do ...@@ -51,14 +49,8 @@ describe Gitlab::ChatCommands::Command, service: true do
expect(subject[:text]).to match("my new issue") expect(subject[:text]).to match("my new issue")
end end
%w(slack mattermost).each do |format| it 'shows a link to the new issue' do
context "for #{format}" do expect(subject[:text]).to match(/\/issues\/\d+/)
let(:format) { format }
it 'shows a link to the new issue' do
expect(subject[:text]).to match(/\/issues\/\d+/)
end
end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe MattermostNotificationService, models: true do describe MattermostNotificationService, models: true do
it_behaves_like "slack or mattermost" it_behaves_like "slack or mattermost notifications"
end end
require 'spec_helper' require 'spec_helper'
describe MattermostSlashCommandsService, models: true do describe MattermostSlashCommandsService, models: true do
it { is_expected.to respond_to :presenter_format } it_behaves_like "chat slash commands"
end end
require 'spec_helper' require 'spec_helper'
describe SlackNotificationService, models: true do describe SlackNotificationService, models: true do
it_behaves_like "slack or mattermost" it_behaves_like "slack or mattermost notifications"
end end
require 'spec_helper' require 'spec_helper'
describe SlackSlashCommandsService, models: true do 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 end
require 'spec_helper' RSpec.shared_examples 'chat slash commands' do
describe ChatSlashCommandsService, models: true do
describe "Associations" do describe "Associations" do
it { is_expected.to respond_to :token } it { is_expected.to respond_to :token }
it { is_expected.to have_many :chat_names } it { is_expected.to have_many :chat_names }
...@@ -29,10 +27,6 @@ describe ChatSlashCommandsService, models: true do ...@@ -29,10 +27,6 @@ describe ChatSlashCommandsService, models: true do
describe '#trigger' do describe '#trigger' do
subject { described_class.new } subject { described_class.new }
before do
allow(subject).to receive(:presenter_format).and_return('unknown')
end
context 'no token is passed' do context 'no token is passed' do
let(:params) { Hash.new } let(:params) { Hash.new }
......
Dir[Rails.root.join("app/models/project_services/chat_message/*.rb")].each { |f| require f } 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(:chat_service) { described_class.new }
let(:webhook_url) { 'https://example.gitlab.com/' } let(:webhook_url) { 'https://example.gitlab.com/' }
......
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