Skip to content

Commit

Permalink
Change sent messages in Teams to Adaptive Card format #17
Browse files Browse the repository at this point in the history
  • Loading branch information
onozaty committed Jan 28, 2022
1 parent 9bdd5d6 commit 3fec245
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
72 changes: 68 additions & 4 deletions lib/redmine_issue_assign_notice/message_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def from(url)
end

if url.include? 'office.com/'
return TextMessageCreator.new(Formatter::Teams.new)
return AdaptiveCardCreator.new
end

if url.include? 'googleapis.com/'
Expand All @@ -28,9 +28,9 @@ def create(issue, old_assgined_to, new_assgined_to, note, author)

text = ""

mention_to = MessageHelper.mention_target(new_assgined_to, author)
if mention_to.present?
text << @formatter.mention(mention_to)
mention_id = MessageHelper.mention_target(new_assgined_to, author)
if mention_id.present?
text << @formatter.mention(mention_id)
text << " "
end

Expand All @@ -45,5 +45,69 @@ def create(issue, old_assgined_to, new_assgined_to, note, author)
return {:text => text}
end
end

class AdaptiveCardCreator
def initialize()
@formatter = Formatter::Teams.new
end

def create(issue, old_assgined_to, new_assgined_to, note, author)

text = ""

mention_id = MessageHelper.mention_target(new_assgined_to, author)
if mention_id.present?
mention_part = "<at>#{new_assgined_to}</at>"
text << mention_part + " "
end

text << "Assign changed from #{@formatter.user_name old_assgined_to} to #{@formatter.user_name new_assgined_to}"
text << @formatter.change_line
text << "[#{@formatter.escape issue.project}] "
text << @formatter.link("#{issue.tracker} ##{issue.id}", MessageHelper.issue_url(issue))
text << " #{@formatter.escape issue.subject} (#{@formatter.escape issue.status})"
text << @formatter.change_line
text << @formatter.escape(MessageHelper.trimming(note))

mention_entities = []
if mention_id.present?
mention_entities.push(
{
:type => "mention",
:text => mention_part,
:mentioned => {
:id => mention_id,
:name => new_assgined_to.to_s
}
}
)
end

return {
:type => "message",
:attachments => [
{
:contentType => "application/vnd.microsoft.card.adaptive",
:content => {
:type => "AdaptiveCard",
:body => [
{
:type => "TextBlock",
:text => text,
:wrap => true
}
],
:$schema => "http://adaptivecards.io/schemas/adaptive-card.json",
:version => "1.0",
:msteams => {
:width => "Full",
:entities => mention_entities
}
}
}
]
}
end
end
end
end
40 changes: 35 additions & 5 deletions test/unit/message_creator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,48 @@ def test_create_mention_teams
note = "a"
author = User.find(1)

RedmineIssueAssignNotice::MessageHelper.stubs(:mention_target).returns("user")
RedmineIssueAssignNotice::MessageHelper.stubs(:mention_target).returns("mentionId")

# ACTION
message = message_creator.create(issue, old_assgined_to, new_assgined_to, note, author)

# ASSERT
assert_equal(
{
:text =>
"@user Assign changed from _[none]_ to _John Smith_ \n" +
"[eCookbook] [Bug #1](http://localhost:3000/issues/1) Cannot print recipes (New) \n" +
"a"
:type => "message",
:attachments => [
{
:contentType => "application/vnd.microsoft.card.adaptive",
:content => {
:type => "AdaptiveCard",
:body => [
{
:type => "TextBlock",
:text =>
"<at>John Smith</at> Assign changed from _[none]_ to _John Smith_ \n" +
"[eCookbook] [Bug #1](http://localhost:3000/issues/1) Cannot print recipes (New) \n" +
"a",
:wrap => true
}
],
:$schema => "http://adaptivecards.io/schemas/adaptive-card.json",
:version => "1.0",
:msteams => {
:width => "Full",
:entities => [
{
:type => "mention",
:text => "<at>John Smith</at>",
:mentioned => {
:id => "mentionId",
:name => "John Smith"
}
}
]
}
}
}
]
},
message)
end
Expand Down

0 comments on commit 3fec245

Please sign in to comment.