Commit d64196dd authored by Kukovskii Vladimir's avatar Kukovskii Vladimir

Fix couple of moments in HangoutsChatService and its spec

parent 6cfb0a9e
...@@ -44,20 +44,22 @@ class HangoutsChatService < ChatNotificationService ...@@ -44,20 +44,22 @@ class HangoutsChatService < ChatNotificationService
private private
def notify(message, opts) def notify(message, opts)
simple_text = compose_simple_message(message) simple_text = parse_simple_text_message(message)
HangoutsChat::Sender.new(webhook).simple(simple_text) HangoutsChat::Sender.new(webhook).simple(simple_text)
end end
def compose_simple_message(message) def parse_simple_text_message(message)
header = message.pretext header = message.pretext
return header if message.attachments.empty? return header if message.attachments.empty?
title = fetch_attachment_title(message.attachments.first) attachment = message.attachments.first
body = message.attachments.first[:text] title = format_attachment_title(attachment)
body = attachment[:text]
[header, title, body].compact.join("\n") [header, title, body].compact.join("\n")
end end
def fetch_attachment_title(attachment) def format_attachment_title(attachment)
return attachment[:title] unless attachment[:title_link] return attachment[:title] unless attachment[:title_link]
"<#{attachment[:title_link]}|#{attachment[:title]}>" "<#{attachment[:title_link]}|#{attachment[:title]}>"
......
--- ---
title: Add Hangouts Chat integration title: Add Hangouts Chat integration
merge_request: merge_request: 20290
author: Kukovskii Vladimir author: Kukovskii Vladimir
type: added type: added
require 'spec_helper' require 'spec_helper'
describe HangoutsChatService do describe HangoutsChatService do
let(:chat_service) { described_class.new }
let(:webhook_url) { 'https://example.gitlab.com/' }
describe 'Associations' do describe 'Associations' do
it { is_expected.to belong_to :project } it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook } it { is_expected.to have_one :service_hook }
...@@ -31,9 +28,10 @@ describe HangoutsChatService do ...@@ -31,9 +28,10 @@ describe HangoutsChatService do
describe '#execute' do describe '#execute' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:webhook_url) { 'https://example.gitlab.com/' }
before do before do
allow(chat_service).to receive_messages( allow(subject).to receive_messages(
project: project, project: project,
project_id: project.id, project_id: project.id,
service_hook: true, service_hook: true,
...@@ -45,7 +43,7 @@ describe HangoutsChatService do ...@@ -45,7 +43,7 @@ describe HangoutsChatService do
shared_examples 'Hangouts Chat service' do shared_examples 'Hangouts Chat service' do
it 'calls Hangouts Chat API' do it 'calls Hangouts Chat API' do
chat_service.execute(sample_data) subject.execute(sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once expect(WebMock).to have_requested(:post, webhook_url).once
end end
...@@ -61,22 +59,28 @@ describe HangoutsChatService do ...@@ -61,22 +59,28 @@ describe HangoutsChatService do
it 'specifies the webhook when it is configured' do it 'specifies the webhook when it is configured' do
expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object) expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object)
chat_service.execute(sample_data) subject.execute(sample_data)
end
it 'sends the simple text message to the Hangouts Chat API' do
subject.execute(sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
.with { |req| req.body =~ /\A{"text":.+}\Z/ }
end end
context 'with not default branch' do context 'with not default branch' do
let(:sample_data) do let(:sample_data) do
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}test" Gitlab::DataBuilder::Push.build(project, user, nil, nil, 'not-the-default-branch')
Gitlab::DataBuilder::Push.build(project, user, nil, nil, ref, [])
end end
context 'when notify_only_default_branch enabled' do context 'when notify_only_default_branch enabled' do
before do before do
chat_service.notify_only_default_branch = true subject.notify_only_default_branch = true
end end
it 'does not call the Hangouts Chat API' do it 'does not call the Hangouts Chat API' do
result = chat_service.execute(sample_data) result = subject.execute(sample_data)
expect(result).to be_falsy expect(result).to be_falsy
end end
...@@ -84,7 +88,7 @@ describe HangoutsChatService do ...@@ -84,7 +88,7 @@ describe HangoutsChatService do
context 'when notify_only_default_branch disabled' do context 'when notify_only_default_branch disabled' do
before do before do
chat_service.notify_only_default_branch = false subject.notify_only_default_branch = false
end end
it_behaves_like 'Hangouts Chat service' it_behaves_like 'Hangouts Chat service'
...@@ -172,7 +176,7 @@ describe HangoutsChatService do ...@@ -172,7 +176,7 @@ describe HangoutsChatService do
it_behaves_like 'Hangouts Chat service' it_behaves_like 'Hangouts Chat service'
end end
context 'wiht snippet comment' do context 'with snippet comment' do
let(:note) do let(:note) do
create(:note_on_project_snippet, project: project, create(:note_on_project_snippet, project: project,
note: 'snippet note') note: 'snippet note')
...@@ -201,7 +205,7 @@ describe HangoutsChatService do ...@@ -201,7 +205,7 @@ describe HangoutsChatService do
context 'with default notify_only_broken_pipelines' do context 'with default notify_only_broken_pipelines' do
it 'does not call Hangouts Chat API' do it 'does not call Hangouts Chat API' do
result = chat_service.execute(sample_data) result = subject.execute(sample_data)
expect(result).to be_falsy expect(result).to be_falsy
end end
...@@ -209,7 +213,7 @@ describe HangoutsChatService do ...@@ -209,7 +213,7 @@ describe HangoutsChatService do
context 'when notify_only_broken_pipelines is false' do context 'when notify_only_broken_pipelines is false' do
before do before do
chat_service.notify_only_broken_pipelines = false subject.notify_only_broken_pipelines = false
end end
it_behaves_like 'Hangouts Chat service' it_behaves_like 'Hangouts Chat service'
...@@ -223,11 +227,11 @@ describe HangoutsChatService do ...@@ -223,11 +227,11 @@ describe HangoutsChatService do
context 'when notify_only_default_branch enabled' do context 'when notify_only_default_branch enabled' do
before do before do
chat_service.notify_only_default_branch = true subject.notify_only_default_branch = true
end end
it 'does not call the Hangouts Chat API' do it 'does not call the Hangouts Chat API' do
result = chat_service.execute(sample_data) result = subject.execute(sample_data)
expect(result).to be_falsy expect(result).to be_falsy
end end
...@@ -235,7 +239,7 @@ describe HangoutsChatService do ...@@ -235,7 +239,7 @@ describe HangoutsChatService do
context 'when notify_only_default_branch disabled' do context 'when notify_only_default_branch disabled' do
before do before do
chat_service.notify_only_default_branch = false subject.notify_only_default_branch = false
end end
it_behaves_like 'Hangouts Chat service' it_behaves_like 'Hangouts Chat service'
......
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