Adding links to user & build in Chat Notifications

This commit is contained in:
Poornima M 2017-01-18 16:08:59 +05:30
parent 1fe80c2962
commit d5fa77d53d
11 changed files with 122 additions and 17 deletions

View file

@ -30,5 +30,9 @@ module ChatMessage
def attachment_color
'#345'
end
def link(text, url)
"[#{text}](#{url})"
end
end
end

View file

@ -7,7 +7,11 @@ module ChatMessage
attr_reader :project_name
attr_reader :project_url
attr_reader :user_name
attr_reader :user_url
attr_reader :duration
attr_reader :stage
attr_reader :build_id
attr_reader :build_name
def initialize(params)
@sha = params[:sha]
@ -17,7 +21,11 @@ module ChatMessage
@project_url = params[:project_url]
@status = params[:commit][:status]
@user_name = params[:commit][:author_name]
@user_url = params[:commit][:author_url]
@duration = params[:commit][:duration]
@stage = params[:build_stage]
@build_name = params[:build_name]
@build_id = params[:build_id]
end
def pretext
@ -35,7 +43,19 @@ module ChatMessage
private
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
def format(string)
@ -64,11 +84,11 @@ module ChatMessage
end
def branch_link
"[#{ref}](#{branch_url})"
link(ref, branch_url)
end
def project_link
"[#{project_name}](#{project_url})"
link(project_name, project_url)
end
def commit_url
@ -76,7 +96,7 @@ module ChatMessage
end
def commit_link
"[#{Commit.truncate_sha(sha)}](#{commit_url})"
link(Commit.truncate_sha(sha), commit_url)
end
end
end

View file

@ -55,11 +55,11 @@ module ChatMessage
end
def project_link
"[#{project_name}](#{project_url})"
link(project_name, project_url)
end
def issue_link
"[#{issue_title}](#{issue_url})"
link(issue_title, issue_url)
end
def issue_title

View file

@ -42,7 +42,7 @@ module ChatMessage
end
def project_link
"[#{project_name}](#{project_url})"
link(project_name, project_url)
end
def merge_request_message
@ -50,7 +50,7 @@ module ChatMessage
end
def merge_request_link
"[merge request !#{merge_request_id}](#{merge_request_url})"
link("merge request !#{merge_request_id}", merge_request_url)
end
def merge_request_url

View file

@ -3,10 +3,9 @@ module ChatMessage
attr_reader :message
attr_reader :user_name
attr_reader :project_name
attr_reader :project_link
attr_reader :project_url
attr_reader :note
attr_reader :note_url
attr_reader :title
def initialize(params)
params = HashWithIndifferentAccess.new(params)
@ -69,15 +68,15 @@ module ChatMessage
end
def description_message
[{ text: format(@note), color: attachment_color }]
[{ text: format(note), color: attachment_color }]
end
def project_link
"[#{@project_name}](#{@project_url})"
link(project_name, project_url)
end
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

View file

@ -0,0 +1,4 @@
---
title: Add user & build links in Slack Notifications
merge_request: 8641
author: Poornima M

View file

@ -8,6 +8,8 @@ module Gitlab
commit = build.pipeline
user = build.user
author_url = build_author_url(build.commit, commit)
data = {
object_kind: 'build',
@ -43,6 +45,7 @@ module Gitlab
message: commit.git_commit_message,
author_name: commit.git_author_name,
author_email: commit.git_author_email,
author_url: author_url,
status: commit.status,
duration: commit.duration,
started_at: commit.started_at,
@ -62,6 +65,13 @@ module Gitlab
data
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

View file

@ -128,5 +128,17 @@ FactoryGirl.define do
build.save!
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

View file

@ -8,5 +8,15 @@ FactoryGirl.define do
initialize_with do
new(git_commit, project)
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

View file

@ -17,5 +17,31 @@ describe Gitlab::DataBuilder::Build do
it { expect(data[:build_allow_failure]).to eq(false) }
it { expect(data[:project_id]).to eq(build.project.id) }
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

View file

@ -11,21 +11,28 @@ describe ChatMessage::BuildMessage do
project_name: 'project_name',
project_url: 'http://example.gitlab.com',
build_id: 1,
build_name: build_name,
build_stage: stage,
commit: {
status: status,
author_name: 'hacker',
author_url: 'http://example.gitlab.com/hacker',
duration: duration,
},
}
end
let(:message) { build_message }
let(:stage) { 'test' }
let(:status) { 'success' }
let(:build_name) { 'rspec' }
let(:duration) { 10 }
context 'build succeeded' do
let(:status) { 'success' }
let(:color) { 'good' }
let(:duration) { 10 }
let(:message) { build_message('passed') }
it 'returns a message with information about succeeded build' do
@ -38,7 +45,6 @@ describe ChatMessage::BuildMessage do
context 'build failed' do
let(:status) { 'failed' }
let(:color) { 'danger' }
let(:duration) { 10 }
it 'returns a message with information about failed build' do
expect(subject.pretext).to be_empty
@ -47,11 +53,25 @@ describe ChatMessage::BuildMessage do
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>:" \
" Commit <http://example.gitlab.com/commit/" \
"97de212e80737a608d939f648d959671fb0a0142/builds|97de212e>" \
" 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