Merge branch 'fix-slack-pipeline-message-by-api' into 'master'

Fix Slack pipeline message by API

Pipelines triggered from API don't have a corresponding
user, so we show that it's from API, same as from the
web UI

Closes #25609

See merge request !8059
This commit is contained in:
Kamil Trzciński 2016-12-15 11:08:45 +00:00
commit 01ffcceb81
3 changed files with 26 additions and 10 deletions

View file

@ -13,7 +13,7 @@ class SlackService
@project_name = data[:project][:path_with_namespace] @project_name = data[:project][:path_with_namespace]
@project_url = data[:project][:web_url] @project_url = data[:project][:web_url]
@user_name = data[:user] && data[:user][:name] @user_name = (data[:user] && data[:user][:name]) || 'API'
end end
def pretext def pretext

View file

@ -0,0 +1,4 @@
---
title: Fix Slack pipeline message from pipelines made by API
merge_request: 8059
author:

View file

@ -2,6 +2,7 @@ require 'spec_helper'
describe SlackService::PipelineMessage do describe SlackService::PipelineMessage do
subject { SlackService::PipelineMessage.new(args) } subject { SlackService::PipelineMessage.new(args) }
let(:user) { { name: 'hacker' } }
let(:args) do let(:args) do
{ {
@ -15,7 +16,7 @@ describe SlackService::PipelineMessage do
}, },
project: { path_with_namespace: 'project_name', project: { path_with_namespace: 'project_name',
web_url: 'example.gitlab.com' }, web_url: 'example.gitlab.com' },
user: { name: 'hacker' } user: user
} }
end end
@ -28,9 +29,7 @@ describe SlackService::PipelineMessage do
let(:message) { build_message('passed') } let(:message) { build_message('passed') }
it 'returns a message with information about succeeded build' do it 'returns a message with information about succeeded build' do
expect(subject.pretext).to be_empty verify_message
expect(subject.fallback).to eq(message)
expect(subject.attachments).to eq([text: message, color: color])
end end
end end
@ -40,16 +39,29 @@ describe SlackService::PipelineMessage do
let(:duration) { 10 } let(:duration) { 10 }
it 'returns a message with information about failed build' do it 'returns a message with information about failed build' do
expect(subject.pretext).to be_empty verify_message
expect(subject.fallback).to eq(message) end
expect(subject.attachments).to eq([text: message, color: color])
context 'when triggered by API therefore lacking user' do
let(:user) { nil }
let(:message) { build_message(status, 'API') }
it 'returns a message stating it is by API' do
verify_message
end
end end
end end
def build_message(status_text = status) def verify_message
expect(subject.pretext).to be_empty
expect(subject.fallback).to eq(message)
expect(subject.attachments).to eq([text: message, color: color])
end
def build_message(status_text = status, name = user[:name])
"<example.gitlab.com|project_name>:" \ "<example.gitlab.com|project_name>:" \
" Pipeline <example.gitlab.com/pipelines/123|#123>" \ " Pipeline <example.gitlab.com/pipelines/123|#123>" \
" of <example.gitlab.com/commits/develop|develop> branch" \ " of <example.gitlab.com/commits/develop|develop> branch" \
" by hacker #{status_text} in #{duration} #{'second'.pluralize(duration)}" " by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}"
end end
end end