BigW Consortium Gitlab

Commit 05f69ef2 by Tiago Botelho

adds microsoft team activity object and refactors code accordingly

parent 9bfa0a5f
......@@ -2,8 +2,10 @@ require 'slack-notifier'
module ChatMessage
class BaseMessage
attr_reader :markdown_format
def initialize(params)
raise NotImplementedError
@markdown_format = params[:markdown_format] || false
end
def pretext
......
......@@ -10,9 +10,10 @@ module ChatMessage
attr_reader :action
attr_reader :state
attr_reader :description
attr_reader :markdown_format
def initialize(params)
super(params)
@user_name = params[:user][:username]
@user_avatar = params[:user][:avatar_url]
@project_name = params[:project_name]
......@@ -26,7 +27,6 @@ module ChatMessage
@action = obj_attr[:action]
@state = obj_attr[:state]
@description = obj_attr[:description] || ''
@markdown_format = params[:format]
end
def attachments
......@@ -36,12 +36,12 @@ module ChatMessage
end
def activity
{
title: "Issue #{state} by #{user_name}",
subtitle: "to: #{project_link}",
text: issue_link,
image: user_avatar
}
MicrosoftTeams::Activity.new(
"Issue #{state} by #{user_name}",
"to: #{project_link}",
issue_link,
user_avatar
).to_json
end
private
......
......@@ -9,9 +9,10 @@ module ChatMessage
attr_reader :target_branch
attr_reader :state
attr_reader :title
attr_reader :markdown_format
def initialize(params)
super(params)
@user_name = params[:user][:username]
@user_avatar = params[:user][:avatar_url]
@project_name = params[:project_name]
......@@ -24,16 +25,15 @@ module ChatMessage
@target_branch = obj_attr[:target_branch]
@state = obj_attr[:state]
@title = format_title(obj_attr[:title])
@markdown_format = params[:format]
end
def activity
{
title: "Merge Request #{state} by #{user_name}",
subtitle: "to: #{project_link}",
text: merge_request_link,
image: user_avatar
}
MicrosoftTeams::Activity.new(
"Merge Request #{state} by #{user_name}",
"to: #{project_link}",
merge_request_link,
user_avatar
).to_json
end
def attachments
......
......@@ -7,30 +7,30 @@ module ChatMessage
attr_reader :note
attr_reader :note_url
attr_reader :comment_attrs
attr_reader :markdown_format
def initialize(params)
params = HashWithIndifferentAccess.new(params)
super(params)
@user_name = params[:user][:name]
@user_avatar = params[:user][:avatar_url]
@project_name = params[:project_name]
@project_url = params[:project_url]
obj_attr = params[:object_attributes]
obj_attr = HashWithIndifferentAccess.new(obj_attr)
obj_attr = HashWithIndifferentAccess.new(params[:object_attributes])
@note = obj_attr[:note]
@note_url = obj_attr[:url]
@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
}
MicrosoftTeams::Activity.new(
"#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)}",
"to: #{project_link}",
comment_attrs[:title],
user_avatar
).to_json
end
def attachments
......@@ -40,19 +40,21 @@ module ChatMessage
private
def message
commented_on_message(comment_attrs)
"#{user_name} #{link('commented on ' + comment_attrs[:target], note_url)} in #{project_link}: *#{comment_attrs[:title]}*"
end
def comment_params(noteable_type, params)
params = HashWithIndifferentAccess.new(params)
case noteable_type
when "Commit"
create_commit_note(HashWithIndifferentAccess.new(params[:commit]))
create_commit_note(params[:commit])
when "Issue"
create_issue_note(HashWithIndifferentAccess.new(params[:issue]))
create_issue_note(params[:issue])
when "MergeRequest"
create_merge_note(HashWithIndifferentAccess.new(params[:merge_request]))
create_merge_note(params[:merge_request])
when "Snippet"
create_snippet_note(HashWithIndifferentAccess.new(params[:snippet]))
create_snippet_note(params[:snippet])
end
end
......@@ -65,8 +67,7 @@ module ChatMessage
end
def create_commit_note(commit)
commit_sha = commit[:id]
commit_sha = Commit.truncate_sha(commit_sha)
commit_sha = Commit.truncate_sha(commit[:id])
{ target: "commit #{commit_sha}", title: format_title(commit[:message]) }
end
......@@ -86,9 +87,5 @@ module ChatMessage
def project_link
link(project_name, project_url)
end
def commented_on_message(target:, title:)
"#{user_name} #{link('commented on ' + target, note_url)} in #{project_link}: *#{title}*"
end
end
end
......@@ -9,7 +9,6 @@ module ChatMessage
attr_reader :duration
attr_reader :pipeline_id
attr_reader :user_avatar
attr_reader :markdown_format
def initialize(data)
pipeline_attributes = data[:object_attributes]
......@@ -19,12 +18,12 @@ module ChatMessage
@duration = pipeline_attributes[:duration]
@pipeline_id = pipeline_attributes[:id]
super(data)
@project_name = data[:project][:path_with_namespace]
@project_url = data[:project][:web_url]
@user_name = (data[:user] && data[:user][:name]) || 'API'
@user_avatar = data[:user][:avatar_url] || ''
@markdown_format = params[:format]
end
def pretext
......@@ -36,12 +35,12 @@ module ChatMessage
end
def activity
{
title: "Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
subtitle: "to: #{project_link}",
text: "in #{duration} #{'second'.pluralize(duration)}",
image: user_avatar
}
MicrosoftTeams::Activity.new(
"Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
"to: #{project_link}",
"in #{duration} #{time_measure}",
user_avatar
).to_json
end
def attachments
......@@ -51,7 +50,7 @@ module ChatMessage
private
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
def humanized_status
......@@ -90,5 +89,9 @@ module ChatMessage
def pipeline_link
"[##{pipeline_id}](#{pipeline_url})"
end
def time_measure
'second'.pluralize(duration)
end
end
end
......@@ -9,9 +9,10 @@ module ChatMessage
attr_reader :ref_type
attr_reader :user_name
attr_reader :user_avatar
attr_reader :markdown_format
def initialize(params)
super(params)
@after = params[:after]
@before = params[:before]
@commits = params.fetch(:commits, [])
......@@ -21,16 +22,24 @@ module ChatMessage
@ref = Gitlab::Git.ref_name(params[:ref])
@user_name = params[:user_name]
@user_avatar = params[:user_avatar]
@markdown_format = params[:format]
end
def activity
{
title: activity_title,
subtitle: "to: #{project_link}",
text: compare_link,
image: user_avatar
}
action =
if new_branch?
"created"
elsif removed_branch?
"removed"
else
"pushed to"
end
MicrosoftTeams::Activity.new(
"#{user_name} #{action} #{ref_type}",
"to: #{project_link}",
compare_link,
user_avatar
).to_json
end
def attachments
......@@ -112,19 +121,6 @@ module ChatMessage
"[Compare changes](#{compare_url})"
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
'#345'
end
......
......@@ -7,9 +7,10 @@ module ChatMessage
attr_reader :wiki_page_url
attr_reader :action
attr_reader :description
attr_reader :markdown_format
def initialize(params)
super(params)
@user_name = params[:user][:username]
@user_avatar = params[:user][:avatar_url]
@project_name = params[:project_name]
......@@ -28,17 +29,15 @@ module ChatMessage
when "update"
"edited"
end
@markdown_format = params[:format]
end
def activity
{
title: "#{user_name} #{action} #{wiki_page_link}",
subtitle: "in: #{project_link}",
text: title,
image: user_avatar
}
MicrosoftTeams::Activity.new(
"#{user_name} #{action} #{wiki_page_link}",
"in: #{project_link}",
title,
user_avatar
).to_json
end
def attachments
......
......@@ -51,8 +51,7 @@ class ChatNotificationService < Service
data = data.merge(
project_url: project_url,
project_name: project_name,
format: false
project_name: project_name
)
# WebHook events often have an 'update' event that follows a 'open' or
......
......@@ -50,7 +50,7 @@ class MicrosoftTeamsService < ChatNotificationService
data = data.merge(
project_url: project_url,
project_name: project_name,
format: true
markdown_format: true
)
message = get_message(object_kind, data)
......@@ -59,8 +59,9 @@ class MicrosoftTeamsService < ChatNotificationService
MicrosoftTeams::Notifier.new(webhook).ping({
title: message.project_name,
pretext: message.pretext,
activity: message.activity,
attachments: message.attachments,
attachments: message.attachments
})
end
end
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
def body(options = {})
attachments = options[:attachments]
result = { 'sections' => [] }
result['title'] = options[:title] if options[:title]
result['summary'] = options[:activity][:title]
result['sections'] << {
'activityTitle' => options[:activity][:title],
'activitySubtitle' => options[:activity][:subtitle],
'activityText' => options[:activity][:text],
'activityImage' => options[:activity][:image]
}
result['title'] = options[:title]
result['summary'] = options[:pretext]
result['sections'] << options[:activity]
if options[:attachments].present? && !options[:attachments].empty?
result['sections'] << { 'title' => 'Details', 'facts' => attachments(options[:attachments]) }
end
result['sections'] << {
'title' => 'Details',
'facts' => [{ 'name' => 'Attachments', 'value' => attachments }]
} if attachments.present? && attachments.empty?
result.to_json
end
def attachments(content)
[{ 'name' => 'Attachments', 'value' => content }]
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