adds microsoft team activity object and refactors code accordingly
This commit is contained in:
parent
9bfa0a5ffa
commit
05f69ef212
11 changed files with 107 additions and 98 deletions
|
@ -2,8 +2,10 @@ require 'slack-notifier'
|
||||||
|
|
||||||
module ChatMessage
|
module ChatMessage
|
||||||
class BaseMessage
|
class BaseMessage
|
||||||
|
attr_reader :markdown_format
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
raise NotImplementedError
|
@markdown_format = params[:markdown_format] || false
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretext
|
def pretext
|
||||||
|
|
|
@ -10,9 +10,10 @@ module ChatMessage
|
||||||
attr_reader :action
|
attr_reader :action
|
||||||
attr_reader :state
|
attr_reader :state
|
||||||
attr_reader :description
|
attr_reader :description
|
||||||
attr_reader :markdown_format
|
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
|
super(params)
|
||||||
|
|
||||||
@user_name = params[:user][:username]
|
@user_name = params[:user][:username]
|
||||||
@user_avatar = params[:user][:avatar_url]
|
@user_avatar = params[:user][:avatar_url]
|
||||||
@project_name = params[:project_name]
|
@project_name = params[:project_name]
|
||||||
|
@ -26,7 +27,6 @@ module ChatMessage
|
||||||
@action = obj_attr[:action]
|
@action = obj_attr[:action]
|
||||||
@state = obj_attr[:state]
|
@state = obj_attr[:state]
|
||||||
@description = obj_attr[:description] || ''
|
@description = obj_attr[:description] || ''
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
@ -36,12 +36,12 @@ module ChatMessage
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
MicrosoftTeams::Activity.new(
|
||||||
title: "Issue #{state} by #{user_name}",
|
"Issue #{state} by #{user_name}",
|
||||||
subtitle: "to: #{project_link}",
|
"to: #{project_link}",
|
||||||
text: issue_link,
|
issue_link,
|
||||||
image: user_avatar
|
user_avatar
|
||||||
}
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -9,9 +9,10 @@ module ChatMessage
|
||||||
attr_reader :target_branch
|
attr_reader :target_branch
|
||||||
attr_reader :state
|
attr_reader :state
|
||||||
attr_reader :title
|
attr_reader :title
|
||||||
attr_reader :markdown_format
|
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
|
super(params)
|
||||||
|
|
||||||
@user_name = params[:user][:username]
|
@user_name = params[:user][:username]
|
||||||
@user_avatar = params[:user][:avatar_url]
|
@user_avatar = params[:user][:avatar_url]
|
||||||
@project_name = params[:project_name]
|
@project_name = params[:project_name]
|
||||||
|
@ -24,16 +25,15 @@ module ChatMessage
|
||||||
@target_branch = obj_attr[:target_branch]
|
@target_branch = obj_attr[:target_branch]
|
||||||
@state = obj_attr[:state]
|
@state = obj_attr[:state]
|
||||||
@title = format_title(obj_attr[:title])
|
@title = format_title(obj_attr[:title])
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
MicrosoftTeams::Activity.new(
|
||||||
title: "Merge Request #{state} by #{user_name}",
|
"Merge Request #{state} by #{user_name}",
|
||||||
subtitle: "to: #{project_link}",
|
"to: #{project_link}",
|
||||||
text: merge_request_link,
|
merge_request_link,
|
||||||
image: user_avatar
|
user_avatar
|
||||||
}
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
|
|
@ -7,30 +7,30 @@ module ChatMessage
|
||||||
attr_reader :note
|
attr_reader :note
|
||||||
attr_reader :note_url
|
attr_reader :note_url
|
||||||
attr_reader :comment_attrs
|
attr_reader :comment_attrs
|
||||||
attr_reader :markdown_format
|
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
params = HashWithIndifferentAccess.new(params)
|
params = HashWithIndifferentAccess.new(params)
|
||||||
|
|
||||||
|
super(params)
|
||||||
|
|
||||||
@user_name = params[:user][:name]
|
@user_name = params[:user][:name]
|
||||||
@user_avatar = params[:user][:avatar_url]
|
@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]
|
||||||
|
|
||||||
obj_attr = params[:object_attributes]
|
obj_attr = HashWithIndifferentAccess.new(params[:object_attributes])
|
||||||
obj_attr = HashWithIndifferentAccess.new(obj_attr)
|
|
||||||
@note = obj_attr[:note]
|
@note = obj_attr[:note]
|
||||||
@note_url = obj_attr[:url]
|
@note_url = obj_attr[:url]
|
||||||
@comment_attrs = comment_params(obj_attr[:noteable_type], params)
|
@comment_attrs = comment_params(obj_attr[:noteable_type], params)
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
MicrosoftTeams::Activity.new(
|
||||||
title: "#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)}",
|
"#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)}",
|
||||||
subtitle: "to: #{project_link}",
|
"to: #{project_link}",
|
||||||
text: "*#{comment_attrs[:title]}*",
|
comment_attrs[:title],
|
||||||
image: user_avatar
|
user_avatar
|
||||||
}
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
@ -40,19 +40,21 @@ module ChatMessage
|
||||||
private
|
private
|
||||||
|
|
||||||
def message
|
def message
|
||||||
commented_on_message(comment_attrs)
|
"#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)} in #{project_link}: *#{comment_attrs[:title]}*"
|
||||||
end
|
end
|
||||||
|
|
||||||
def comment_params(noteable_type, params)
|
def comment_params(noteable_type, params)
|
||||||
|
params = HashWithIndifferentAccess.new(params)
|
||||||
|
|
||||||
case noteable_type
|
case noteable_type
|
||||||
when "Commit"
|
when "Commit"
|
||||||
create_commit_note(HashWithIndifferentAccess.new(params[:commit]))
|
create_commit_note(params[:commit])
|
||||||
when "Issue"
|
when "Issue"
|
||||||
create_issue_note(HashWithIndifferentAccess.new(params[:issue]))
|
create_issue_note(params[:issue])
|
||||||
when "MergeRequest"
|
when "MergeRequest"
|
||||||
create_merge_note(HashWithIndifferentAccess.new(params[:merge_request]))
|
create_merge_note(params[:merge_request])
|
||||||
when "Snippet"
|
when "Snippet"
|
||||||
create_snippet_note(HashWithIndifferentAccess.new(params[:snippet]))
|
create_snippet_note(params[:snippet])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,8 +67,7 @@ module ChatMessage
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commit_note(commit)
|
def create_commit_note(commit)
|
||||||
commit_sha = commit[:id]
|
commit_sha = Commit.truncate_sha(commit[:id])
|
||||||
commit_sha = Commit.truncate_sha(commit_sha)
|
|
||||||
|
|
||||||
{ target: "commit #{commit_sha}", title: format_title(commit[:message]) }
|
{ target: "commit #{commit_sha}", title: format_title(commit[:message]) }
|
||||||
end
|
end
|
||||||
|
@ -86,9 +87,5 @@ module ChatMessage
|
||||||
def project_link
|
def project_link
|
||||||
link(project_name, project_url)
|
link(project_name, project_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def commented_on_message(target:, title:)
|
|
||||||
"#{user_name} #{link('commented on ' + target, note_url)} in #{project_link}: *#{title}*"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,6 @@ module ChatMessage
|
||||||
attr_reader :duration
|
attr_reader :duration
|
||||||
attr_reader :pipeline_id
|
attr_reader :pipeline_id
|
||||||
attr_reader :user_avatar
|
attr_reader :user_avatar
|
||||||
attr_reader :markdown_format
|
|
||||||
|
|
||||||
def initialize(data)
|
def initialize(data)
|
||||||
pipeline_attributes = data[:object_attributes]
|
pipeline_attributes = data[:object_attributes]
|
||||||
|
@ -19,12 +18,12 @@ module ChatMessage
|
||||||
@duration = pipeline_attributes[:duration]
|
@duration = pipeline_attributes[:duration]
|
||||||
@pipeline_id = pipeline_attributes[:id]
|
@pipeline_id = pipeline_attributes[:id]
|
||||||
|
|
||||||
|
super(data)
|
||||||
|
|
||||||
@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]) || 'API'
|
@user_name = (data[:user] && data[:user][:name]) || 'API'
|
||||||
@user_avatar = data[:user][:avatar_url] || ''
|
@user_avatar = data[:user][:avatar_url] || ''
|
||||||
|
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretext
|
def pretext
|
||||||
|
@ -36,12 +35,12 @@ module ChatMessage
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
MicrosoftTeams::Activity.new(
|
||||||
title: "Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
|
"Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
|
||||||
subtitle: "to: #{project_link}",
|
"to: #{project_link}",
|
||||||
text: "in #{duration} #{'second'.pluralize(duration)}",
|
"in #{duration} #{time_measure}",
|
||||||
image: user_avatar
|
user_avatar
|
||||||
}
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
@ -51,7 +50,7 @@ module ChatMessage
|
||||||
private
|
private
|
||||||
|
|
||||||
def message
|
def message
|
||||||
"#{project_link}: Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} #{'second'.pluralize(duration)}"
|
"#{project_link}: Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} #{time_measure}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def humanized_status
|
def humanized_status
|
||||||
|
@ -90,5 +89,9 @@ module ChatMessage
|
||||||
def pipeline_link
|
def pipeline_link
|
||||||
"[##{pipeline_id}](#{pipeline_url})"
|
"[##{pipeline_id}](#{pipeline_url})"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def time_measure
|
||||||
|
'second'.pluralize(duration)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,9 +9,10 @@ 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)
|
||||||
|
super(params)
|
||||||
|
|
||||||
@after = params[:after]
|
@after = params[:after]
|
||||||
@before = params[:before]
|
@before = params[:before]
|
||||||
@commits = params.fetch(:commits, [])
|
@commits = params.fetch(:commits, [])
|
||||||
|
@ -21,16 +22,24 @@ 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]
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
action =
|
||||||
title: activity_title,
|
if new_branch?
|
||||||
subtitle: "to: #{project_link}",
|
"created"
|
||||||
text: compare_link,
|
elsif removed_branch?
|
||||||
image: user_avatar
|
"removed"
|
||||||
}
|
else
|
||||||
|
"pushed to"
|
||||||
|
end
|
||||||
|
|
||||||
|
MicrosoftTeams::Activity.new(
|
||||||
|
"#{user_name} #{action} #{ref_type}",
|
||||||
|
"to: #{project_link}",
|
||||||
|
compare_link,
|
||||||
|
user_avatar
|
||||||
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
@ -112,19 +121,6 @@ module ChatMessage
|
||||||
"[Compare changes](#{compare_url})"
|
"[Compare changes](#{compare_url})"
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity_title
|
|
||||||
action =
|
|
||||||
if new_branch?
|
|
||||||
"created"
|
|
||||||
elsif removed_branch?
|
|
||||||
"removed"
|
|
||||||
else
|
|
||||||
"pushed to"
|
|
||||||
end
|
|
||||||
|
|
||||||
"#{user_name} #{action} #{ref_type}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def attachment_color
|
def attachment_color
|
||||||
'#345'
|
'#345'
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,9 +7,10 @@ module ChatMessage
|
||||||
attr_reader :wiki_page_url
|
attr_reader :wiki_page_url
|
||||||
attr_reader :action
|
attr_reader :action
|
||||||
attr_reader :description
|
attr_reader :description
|
||||||
attr_reader :markdown_format
|
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
|
super(params)
|
||||||
|
|
||||||
@user_name = params[:user][:username]
|
@user_name = params[:user][:username]
|
||||||
@user_avatar = params[:user][:avatar_url]
|
@user_avatar = params[:user][:avatar_url]
|
||||||
@project_name = params[:project_name]
|
@project_name = params[:project_name]
|
||||||
|
@ -28,17 +29,15 @@ module ChatMessage
|
||||||
when "update"
|
when "update"
|
||||||
"edited"
|
"edited"
|
||||||
end
|
end
|
||||||
|
|
||||||
@markdown_format = params[:format]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
{
|
MicrosoftTeams::Activity.new(
|
||||||
title: "#{user_name} #{action} #{wiki_page_link}",
|
"#{user_name} #{action} #{wiki_page_link}",
|
||||||
subtitle: "in: #{project_link}",
|
"in: #{project_link}",
|
||||||
text: title,
|
title,
|
||||||
image: user_avatar
|
user_avatar
|
||||||
}
|
).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments
|
def attachments
|
||||||
|
|
|
@ -51,8 +51,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: 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
|
||||||
|
|
|
@ -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: true
|
markdown_format: true
|
||||||
)
|
)
|
||||||
|
|
||||||
message = get_message(object_kind, data)
|
message = get_message(object_kind, data)
|
||||||
|
@ -59,8 +59,9 @@ class MicrosoftTeamsService < ChatNotificationService
|
||||||
|
|
||||||
MicrosoftTeams::Notifier.new(webhook).ping({
|
MicrosoftTeams::Notifier.new(webhook).ping({
|
||||||
title: message.project_name,
|
title: message.project_name,
|
||||||
|
pretext: message.pretext,
|
||||||
activity: message.activity,
|
activity: message.activity,
|
||||||
attachments: message.attachments,
|
attachments: message.attachments
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
19
lib/microsoft_teams/activity.rb
Normal file
19
lib/microsoft_teams/activity.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
module MicrosoftTeams
|
||||||
|
class Activity
|
||||||
|
def initialize(title, subtitle, text, image)
|
||||||
|
@title = title
|
||||||
|
@subtitle = subtitle
|
||||||
|
@text = text
|
||||||
|
@image = image
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json
|
||||||
|
{
|
||||||
|
'activityTitle' => @title,
|
||||||
|
'activitySubtitle' => @subtitle,
|
||||||
|
'activityText' => @text,
|
||||||
|
'activityImage' => @image
|
||||||
|
}.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -15,26 +15,19 @@ module MicrosoftTeams
|
||||||
private
|
private
|
||||||
|
|
||||||
def body(options = {})
|
def body(options = {})
|
||||||
|
attachments = options[:attachments]
|
||||||
result = { 'sections' => [] }
|
result = { 'sections' => [] }
|
||||||
|
|
||||||
result['title'] = options[:title] if options[:title]
|
result['title'] = options[:title]
|
||||||
result['summary'] = options[:activity][:title]
|
result['summary'] = options[:pretext]
|
||||||
result['sections'] << {
|
result['sections'] << options[:activity]
|
||||||
'activityTitle' => options[:activity][:title],
|
|
||||||
'activitySubtitle' => options[:activity][:subtitle],
|
|
||||||
'activityText' => options[:activity][:text],
|
|
||||||
'activityImage' => options[:activity][:image]
|
|
||||||
}
|
|
||||||
|
|
||||||
if options[:attachments].present? && !options[:attachments].empty?
|
result['sections'] << {
|
||||||
result['sections'] << { 'title' => 'Details', 'facts' => attachments(options[:attachments]) }
|
'title' => 'Details',
|
||||||
end
|
'facts' => [{ 'name' => 'Attachments', 'value' => attachments }]
|
||||||
|
} if attachments.present? && attachments.empty?
|
||||||
|
|
||||||
result.to_json
|
result.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments(content)
|
|
||||||
[{ 'name' => 'Attachments', 'value' => content }]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue