Commit 29d5a7ab authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '26500-informative-slack-notifications' into 'master'

Adding links to user & build stage in a Build message

Closes #26500

See merge request !8641
parents f662bc5a d5fa77d5
...@@ -30,5 +30,9 @@ module ChatMessage ...@@ -30,5 +30,9 @@ module ChatMessage
def attachment_color def attachment_color
'#345' '#345'
end end
def link(text, url)
"[#{text}](#{url})"
end
end end
end end
...@@ -7,7 +7,11 @@ module ChatMessage ...@@ -7,7 +7,11 @@ module ChatMessage
attr_reader :project_name attr_reader :project_name
attr_reader :project_url attr_reader :project_url
attr_reader :user_name attr_reader :user_name
attr_reader :user_url
attr_reader :duration attr_reader :duration
attr_reader :stage
attr_reader :build_id
attr_reader :build_name
def initialize(params) def initialize(params)
@sha = params[:sha] @sha = params[:sha]
...@@ -17,7 +21,11 @@ module ChatMessage ...@@ -17,7 +21,11 @@ module ChatMessage
@project_url = params[:project_url] @project_url = params[:project_url]
@status = params[:commit][:status] @status = params[:commit][:status]
@user_name = params[:commit][:author_name] @user_name = params[:commit][:author_name]
@user_url = params[:commit][:author_url]
@duration = params[:commit][:duration] @duration = params[:commit][:duration]
@stage = params[:build_stage]
@build_name = params[:build_name]
@build_id = params[:build_id]
end end
def pretext def pretext
...@@ -35,7 +43,19 @@ module ChatMessage ...@@ -35,7 +43,19 @@ module ChatMessage
private private
def message def message
"#{project_link}: Commit #{commit_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} #{'second'.pluralize(duration)}" "#{project_link}: Commit #{commit_link} of #{branch_link} #{ref_type} by #{user_link} #{humanized_status} on build #{build_link} of stage #{stage} in #{duration} #{'second'.pluralize(duration)}"
end
def build_url
"#{project_url}/builds/#{build_id}"
end
def build_link
link(build_name, build_url)
end
def user_link
link(user_name, user_url)
end end
def format(string) def format(string)
...@@ -64,11 +84,11 @@ module ChatMessage ...@@ -64,11 +84,11 @@ module ChatMessage
end end
def branch_link def branch_link
"[#{ref}](#{branch_url})" link(ref, branch_url)
end end
def project_link def project_link
"[#{project_name}](#{project_url})" link(project_name, project_url)
end end
def commit_url def commit_url
...@@ -76,7 +96,7 @@ module ChatMessage ...@@ -76,7 +96,7 @@ module ChatMessage
end end
def commit_link def commit_link
"[#{Commit.truncate_sha(sha)}](#{commit_url})" link(Commit.truncate_sha(sha), commit_url)
end end
end end
end end
...@@ -55,11 +55,11 @@ module ChatMessage ...@@ -55,11 +55,11 @@ module ChatMessage
end end
def project_link def project_link
"[#{project_name}](#{project_url})" link(project_name, project_url)
end end
def issue_link def issue_link
"[#{issue_title}](#{issue_url})" link(issue_title, issue_url)
end end
def issue_title def issue_title
......
...@@ -42,7 +42,7 @@ module ChatMessage ...@@ -42,7 +42,7 @@ module ChatMessage
end end
def project_link def project_link
"[#{project_name}](#{project_url})" link(project_name, project_url)
end end
def merge_request_message def merge_request_message
...@@ -50,7 +50,7 @@ module ChatMessage ...@@ -50,7 +50,7 @@ module ChatMessage
end end
def merge_request_link def merge_request_link
"[merge request !#{merge_request_id}](#{merge_request_url})" link("merge request !#{merge_request_id}", merge_request_url)
end end
def merge_request_url def merge_request_url
......
...@@ -3,10 +3,9 @@ module ChatMessage ...@@ -3,10 +3,9 @@ module ChatMessage
attr_reader :message attr_reader :message
attr_reader :user_name attr_reader :user_name
attr_reader :project_name attr_reader :project_name
attr_reader :project_link attr_reader :project_url
attr_reader :note attr_reader :note
attr_reader :note_url attr_reader :note_url
attr_reader :title
def initialize(params) def initialize(params)
params = HashWithIndifferentAccess.new(params) params = HashWithIndifferentAccess.new(params)
...@@ -69,15 +68,15 @@ module ChatMessage ...@@ -69,15 +68,15 @@ module ChatMessage
end end
def description_message def description_message
[{ text: format(@note), color: attachment_color }] [{ text: format(note), color: attachment_color }]
end end
def project_link def project_link
"[#{@project_name}](#{@project_url})" link(project_name, project_url)
end end
def commented_on_message(target, title) def commented_on_message(target, title)
@message = "#{@user_name} [commented on #{target}](#{@note_url}) in #{project_link}: *#{title}*" @message = "#{user_name} #{link('commented on ' + target, note_url)} in #{project_link}: *#{title}*"
end end
end end
end end
---
title: Add user & build links in Slack Notifications
merge_request: 8641
author: Poornima M
...@@ -8,6 +8,8 @@ module Gitlab ...@@ -8,6 +8,8 @@ module Gitlab
commit = build.pipeline commit = build.pipeline
user = build.user user = build.user
author_url = build_author_url(build.commit, commit)
data = { data = {
object_kind: 'build', object_kind: 'build',
...@@ -43,6 +45,7 @@ module Gitlab ...@@ -43,6 +45,7 @@ module Gitlab
message: commit.git_commit_message, message: commit.git_commit_message,
author_name: commit.git_author_name, author_name: commit.git_author_name,
author_email: commit.git_author_email, author_email: commit.git_author_email,
author_url: author_url,
status: commit.status, status: commit.status,
duration: commit.duration, duration: commit.duration,
started_at: commit.started_at, started_at: commit.started_at,
...@@ -62,6 +65,13 @@ module Gitlab ...@@ -62,6 +65,13 @@ module Gitlab
data data
end end
private
def build_author_url(commit, pipeline)
author = commit.try(:author)
author ? Gitlab::Routing.url_helpers.user_url(author) : "mailto:#{pipeline.git_author_email}"
end
end end
end end
end end
...@@ -128,5 +128,17 @@ FactoryGirl.define do ...@@ -128,5 +128,17 @@ FactoryGirl.define do
build.save! build.save!
end end
end end
trait :with_commit do
after(:build) do |build|
allow(build).to receive(:commit).and_return build(:commit, :without_author)
end
end
trait :with_commit_and_author do
after(:build) do |build|
allow(build).to receive(:commit).and_return build(:commit)
end
end
end end
end end
...@@ -8,5 +8,15 @@ FactoryGirl.define do ...@@ -8,5 +8,15 @@ FactoryGirl.define do
initialize_with do initialize_with do
new(git_commit, project) new(git_commit, project)
end end
after(:build) do |commit|
allow(commit).to receive(:author).and_return build(:author)
end
trait :without_author do
after(:build) do |commit|
allow(commit).to receive(:author).and_return nil
end
end
end end
end end
...@@ -17,5 +17,31 @@ describe Gitlab::DataBuilder::Build do ...@@ -17,5 +17,31 @@ describe Gitlab::DataBuilder::Build do
it { expect(data[:build_allow_failure]).to eq(false) } it { expect(data[:build_allow_failure]).to eq(false) }
it { expect(data[:project_id]).to eq(build.project.id) } it { expect(data[:project_id]).to eq(build.project.id) }
it { expect(data[:project_name]).to eq(build.project.name_with_namespace) } it { expect(data[:project_name]).to eq(build.project.name_with_namespace) }
context 'commit author_url' do
context 'when no commit present' do
let(:build) { create(:ci_build) }
it 'sets to mailing address of git_author_email' do
expect(data[:commit][:author_url]).to eq("mailto:#{build.pipeline.git_author_email}")
end
end
context 'when commit present but has no author' do
let(:build) { create(:ci_build, :with_commit) }
it 'sets to mailing address of git_author_email' do
expect(data[:commit][:author_url]).to eq("mailto:#{build.pipeline.git_author_email}")
end
end
context 'when commit and author are present' do
let(:build) { create(:ci_build, :with_commit_and_author) }
it 'sets to GitLab user url' do
expect(data[:commit][:author_url]).to eq(Gitlab::Routing.url_helpers.user_url(username: build.commit.author.username))
end
end
end
end end
end end
...@@ -11,21 +11,28 @@ describe ChatMessage::BuildMessage do ...@@ -11,21 +11,28 @@ describe ChatMessage::BuildMessage do
project_name: 'project_name', project_name: 'project_name',
project_url: 'http://example.gitlab.com', project_url: 'http://example.gitlab.com',
build_id: 1,
build_name: build_name,
build_stage: stage,
commit: { commit: {
status: status, status: status,
author_name: 'hacker', author_name: 'hacker',
author_url: 'http://example.gitlab.com/hacker',
duration: duration, duration: duration,
}, },
} }
end end
let(:message) { build_message } let(:message) { build_message }
let(:stage) { 'test' }
let(:status) { 'success' }
let(:build_name) { 'rspec' }
let(:duration) { 10 }
context 'build succeeded' do context 'build succeeded' do
let(:status) { 'success' } let(:status) { 'success' }
let(:color) { 'good' } let(:color) { 'good' }
let(:duration) { 10 }
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
...@@ -38,7 +45,6 @@ describe ChatMessage::BuildMessage do ...@@ -38,7 +45,6 @@ describe ChatMessage::BuildMessage do
context 'build failed' do context 'build failed' do
let(:status) { 'failed' } let(:status) { 'failed' }
let(:color) { 'danger' } let(:color) { 'danger' }
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 expect(subject.pretext).to be_empty
...@@ -47,11 +53,25 @@ describe ChatMessage::BuildMessage do ...@@ -47,11 +53,25 @@ describe ChatMessage::BuildMessage do
end end
end end
def build_message(status_text = status) it 'returns a message with information on build' do
expect(subject.fallback).to include("on build <http://example.gitlab.com/builds/1|#{build_name}>")
end
it 'returns a message with stage name' do
expect(subject.fallback).to include("of stage #{stage}")
end
it 'returns a message with link to author' do
expect(subject.fallback).to include("by <http://example.gitlab.com/hacker|hacker>")
end
def build_message(status_text = status, stage_text = stage, build_text = build_name)
"<http://example.gitlab.com|project_name>:" \ "<http://example.gitlab.com|project_name>:" \
" Commit <http://example.gitlab.com/commit/" \ " Commit <http://example.gitlab.com/commit/" \
"97de212e80737a608d939f648d959671fb0a0142/builds|97de212e>" \ "97de212e80737a608d939f648d959671fb0a0142/builds|97de212e>" \
" of <http://example.gitlab.com/commits/develop|develop> branch" \ " of <http://example.gitlab.com/commits/develop|develop> branch" \
" by hacker #{status_text} in #{duration} #{'second'.pluralize(duration)}" " by <http://example.gitlab.com/hacker|hacker> #{status_text}" \
" on build <http://example.gitlab.com/builds/1|#{build_text}>" \
" of stage #{stage_text} in #{duration} #{'second'.pluralize(duration)}"
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