add note message integration with microsoft teams

This commit is contained in:
Tiago Botelho 2017-04-03 17:39:17 +01:00
parent efe2d96a90
commit 7dd645e6c9
5 changed files with 45 additions and 36 deletions

View file

@ -7,7 +7,7 @@ module ChatMessage
end end
def pretext def pretext
format(message) markdown_format ? message : format(message)
end end
def fallback def fallback

View file

@ -1,16 +1,18 @@
module ChatMessage module ChatMessage
class NoteMessage < BaseMessage class NoteMessage < BaseMessage
attr_reader :message
attr_reader :user_name attr_reader :user_name
attr_reader :user_avatar
attr_reader :project_name attr_reader :project_name
attr_reader :project_url attr_reader :project_url
attr_reader :note attr_reader :note
attr_reader :note_url attr_reader :note_url
attr_reader :comment_attrs
attr_reader :markdown_format
def initialize(params) def initialize(params)
params = HashWithIndifferentAccess.new(params) params = HashWithIndifferentAccess.new(params)
@user_name = params[:user][:username] @user_name = params[:user][:name]
@user_avatar = params[:user][:avatar] @user_avatar = params[:user][:avatar_url]
@project_name = params[:project_name] @project_name = params[:project_name]
@project_url = params[:project_url] @project_url = params[:project_url]
@ -18,7 +20,30 @@ module ChatMessage
obj_attr = HashWithIndifferentAccess.new(obj_attr) obj_attr = HashWithIndifferentAccess.new(obj_attr)
@note = obj_attr[:note] @note = obj_attr[:note]
@note_url = obj_attr[:url] @note_url = obj_attr[:url]
noteable_type = obj_attr[:noteable_type] @comment_attrs = comment_params(obj_attr[:noteable_type], params)
@markdown_format = params[:format]
end
def activity
{
title: "#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)}",
subtitle: "to: #{project_link}",
text: "*#{comment_attrs[:title]}*",
image: user_avatar
}
end
def attachments
markdown_format ? note : description_message
end
private
def message
commented_on_message(comment_attrs)
end
def comment_params(noteable_type, params)
case noteable_type case noteable_type
when "Commit" when "Commit"
create_commit_note(HashWithIndifferentAccess.new(params[:commit])) create_commit_note(HashWithIndifferentAccess.new(params[:commit]))
@ -31,40 +56,27 @@ module ChatMessage
end end
end end
def attachments
description_message
end
private
def format_title(title) def format_title(title)
title.lines.first.chomp title.lines.first.chomp
end end
def create_issue_note(issue)
{ target: "issue ##{issue[:iid]}", title: format_title(issue[:title]) }
end
def create_commit_note(commit) def create_commit_note(commit)
commit_sha = commit[:id] commit_sha = commit[:id]
commit_sha = Commit.truncate_sha(commit_sha) commit_sha = Commit.truncate_sha(commit_sha)
commented_on_message(
"commit #{commit_sha}",
format_title(commit[:message]))
end
def create_issue_note(issue) { target: "commit #{commit_sha}", title: format_title(commit[:message]) }
commented_on_message(
"issue ##{issue[:iid]}",
format_title(issue[:title]))
end end
def create_merge_note(merge_request) def create_merge_note(merge_request)
commented_on_message( { target: "merge request !#{merge_request[:iid]}", title: format_title(merge_request[:title]) }
"merge request !#{merge_request[:iid]}",
format_title(merge_request[:title]))
end end
def create_snippet_note(snippet) def create_snippet_note(snippet)
commented_on_message( { target: "snippet ##{snippet[:id]}", title: format_title(snippet[:title]) }
"snippet ##{snippet[:id]}",
format_title(snippet[:title]))
end end
def description_message def description_message
@ -75,8 +87,8 @@ module ChatMessage
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} #{link('commented on ' + target, note_url)} in #{project_link}: *#{title}*" "#{user_name} #{link('commented on ' + target, note_url)} in #{project_link}: *#{title}*"
end end
end end
end end

View file

@ -9,6 +9,7 @@ module ChatMessage
attr_reader :ref_type attr_reader :ref_type
attr_reader :user_name attr_reader :user_name
attr_reader :user_avatar attr_reader :user_avatar
attr_reader :markdown_format
def initialize(params) def initialize(params)
@after = params[:after] @after = params[:after]
@ -20,7 +21,7 @@ module ChatMessage
@ref = Gitlab::Git.ref_name(params[:ref]) @ref = Gitlab::Git.ref_name(params[:ref])
@user_name = params[:user_name] @user_name = params[:user_name]
@user_avatar = params[:user_avatar] @user_avatar = params[:user_avatar]
@format = params[:format] @markdown_format = params[:format]
end end
def activity def activity
@ -28,18 +29,14 @@ module ChatMessage
title: activity_title, title: activity_title,
subtitle: "to: #{project_link}", subtitle: "to: #{project_link}",
text: compare_link, text: compare_link,
image: params[:user_avatar] image: user_avatar
} }
end end
def pretext
@format ? format(message) : message
end
def attachments def attachments
return [] if new_branch? || removed_branch? return [] if new_branch? || removed_branch?
@format ? commit_message_attachments : commit_messages markdown_format ? commit_messages : commit_message_attachments
end end
private private

View file

@ -52,7 +52,7 @@ class ChatNotificationService < Service
data = data.merge( data = data.merge(
project_url: project_url, project_url: project_url,
project_name: project_name, project_name: project_name,
format: true format: false
) )
# WebHook events often have an 'update' event that follows a 'open' or # WebHook events often have an 'update' event that follows a 'open' or

View file

@ -50,7 +50,7 @@ class MicrosoftTeamsService < ChatNotificationService
data = data.merge( data = data.merge(
project_url: project_url, project_url: project_url,
project_name: project_name, project_name: project_name,
format: false format: true
) )
message = get_message(object_kind, data) message = get_message(object_kind, data)