Skip to content

Commit

Permalink
Improved text message creation process #17
Browse files Browse the repository at this point in the history
  • Loading branch information
onozaty committed Jan 27, 2022
1 parent a5bb6dd commit 010b701
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 172 deletions.
100 changes: 59 additions & 41 deletions lib/redmine_issue_assign_notice/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,109 @@
module RedmineIssueAssignNotice
module Formatter

def create(url)
if url.include? 'slack.com/'
return Slack.new
class Slack
def escape(msg)
msg.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
end
if url.include? 'office.com/'
return Teams.new

def change_line
"\n"
end

if url.include? 'googleapis.com/'
return GoogleChat.new
def link(title, url)
"<#{url}|#{escape(title)}>"
end

Default.new
end
def user_name(user)
if user.nil?
'_[none]_'
else
"_#{escape(user).gsub("_", " ")}_"
end
end

module_function :create
def mention(id)
"<@#{id}>"
end
end

class Default
class Teams
def escape(msg)
msg.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
msg.to_s
end

def change_line
"\n"
" \n"
end

def link(title, url)
"<#{url}|#{escape title}>"
end

def mention(id)
"@#{id}"
"[#{escape(title).gsub("[", " ").gsub("]", " ")}](#{url})"
end

def user_name(user)
if user.nil?
'_[none]_'
else
"_#{escape user}_"
"_#{escape(user).gsub("_", " ")}_"
end
end

def trimming(note)
if note.nil?
return ''
end

flat = note.gsub(/\r\n|\n|\r/, ' ')
if flat.length > 200
flat[0, 200] + '...'
def mention(id)
"@#{id}"
end
end

class GoogleChat
def escape(msg)
msg.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
end

def change_line
"\n"
end

def link(title, url)
"<#{url}|#{escape(title)}>"
end

def user_name(user)
if user.nil?
'_[none]_'
else
flat
"_#{escape(user).gsub("_", " ")}_"
end
end
end

class Slack < Default
def mention(id)
"<@#{id}>"
"<users/#{id}>"
end
end

class Teams < Default
class Other
def escape(msg)
msg.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("[", "\\[").gsub("]", "\\]")
msg.to_s
end

def change_line
" \n"
"\n"
end

def link(title, url)
"[#{escape title}](#{url})"
"[#{escape(title).gsub("[", " ").gsub("]", " ")}](#{url})"
end

def mention(id)
"@#{id}"
def user_name(user)
if user.nil?
'_[none]_'
else
"_#{escape(user).gsub("_", " ")}_"
end
end
end

class GoogleChat < Default
def mention(id)
"<users/#{id}>"
"@#{id}"
end
end

end
end
6 changes: 2 additions & 4 deletions lib/redmine_issue_assign_notice/issue_hook_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class IssueHookListener < Redmine::Hook::Listener

def initialize
@client = NoticeClient.new
@message_helper = MessageHelper.new
Rails.logger.debug "[RedmineIssueAssignNotice] IssueHookListener#initialize"
end

Expand Down Expand Up @@ -52,10 +51,9 @@ def notice(issue, old_assgined_to, new_assgined_to, note, author)
return
end

formatter = RedmineIssueAssignNotice::Formatter.create notice_url
message_creater = TextMessage.new(@message_helper, formatter)
message_creator = MessageCreator.from(notice_url)

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

Rails.logger.debug "[RedmineIssueAssignNotice] IssueHookListener#notice message:#{message}"

Expand Down
49 changes: 49 additions & 0 deletions lib/redmine_issue_assign_notice/message_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module RedmineIssueAssignNotice
module MessageCreator

def from(url)
if url.include? 'slack.com/'
return TextMessageCreator.new(Formatter::Slack.new)
end

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

if url.include? 'googleapis.com/'
return TextMessageCreator.new(Formatter::GoogleChat.new)
end

return TextMessageCreator.new(Formatter::Other.new)
end

module_function :from

class TextMessageCreator
def initialize(formatter)
@formatter = formatter
end

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)
text << " "
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))

return {:text => text}
end
end
end
end
18 changes: 16 additions & 2 deletions lib/redmine_issue_assign_notice/message_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RedmineIssueAssignNotice
class MessageHelper
def mention_target(assgined_to, author)
def self.mention_target(assgined_to, author)

if assgined_to.nil? ||
Setting.plugin_redmine_issue_assign_notice['mention_to_assignee'] != '1' ||
Expand All @@ -17,8 +17,22 @@ def mention_target(assgined_to, author)
noteice_field.value
end

def issue_url(issue)
def self.issue_url(issue)
"#{Setting.protocol}://#{Setting.host_name}/issues/#{issue.id}"
end

def self.trimming(note)
if note.nil?
return ''
end

flat = note.gsub(/\r\n|\n|\r/, ' ')
if flat.length > 200
flat[0, 200] + '...'
else
flat
end
end

end
end
29 changes: 0 additions & 29 deletions lib/redmine_issue_assign_notice/text_message.rb

This file was deleted.

54 changes: 21 additions & 33 deletions test/unit/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,55 @@ def setup
@user = User.find(1)
end

def test_default

formatter = RedmineIssueAssignNotice::Formatter.create ""

assert_equal "&lt;&gt;&amp;[]abc", formatter.escape("<>&[]abc")
assert_equal "\n", formatter.change_line
assert_equal "<http://example.com|[&lt;title&gt;]>", formatter.link("[<title>]", "http://example.com")
assert_equal "@xxx", formatter.mention("xxx")
assert_equal "_Redmine Admin_", formatter.user_name(@user)
assert_equal "_[none]_", formatter.user_name(nil)
assert_equal "a b c d", formatter.trimming("a\nb\rc\r\nd")
assert_equal "a" * 200, formatter.trimming("a" * 200)
assert_equal ("a" * 200) + "...", formatter.trimming("a" * 201)
assert_equal "", formatter.trimming(nil)
end

def test_slack

formatter = RedmineIssueAssignNotice::Formatter.create "https://hooks.slack.com/services/xxxx/yyyy"
formatter = RedmineIssueAssignNotice::Formatter::Slack.new

assert_equal "&lt;&gt;&amp;[]abc", formatter.escape("<>&[]abc")
assert_equal "\n", formatter.change_line
assert_equal "<http://example.com|[&lt;title&gt;]>", formatter.link("[<title>]", "http://example.com")
assert_equal "<@xxx>", formatter.mention("xxx")
assert_equal "_Redmine Admin_", formatter.user_name(@user)
assert_equal "_[none]_", formatter.user_name(nil)
assert_equal "a b c d", formatter.trimming("a\nb\rc\r\nd")
assert_equal "a" * 200, formatter.trimming("a" * 200)
assert_equal ("a" * 200) + "...", formatter.trimming("a" * 201)
assert_equal "", formatter.trimming(nil)
assert_equal "_first last_", formatter.user_name("first_last")
end

def test_teams

formatter = RedmineIssueAssignNotice::Formatter.create "https://examplecom.webhook.office.com/webhookb2/xxx/yyyy"
formatter = RedmineIssueAssignNotice::Formatter::Teams.new

assert_equal "&lt;&gt;&amp;\\[\\]abc", formatter.escape("<>&[]abc")
assert_equal "<>&[]abc", formatter.escape("<>&[]abc")
assert_equal " \n", formatter.change_line
assert_equal "[\\[&lt;title&gt;\\]](http://example.com)", formatter.link("[<title>]", "http://example.com")
assert_equal "[ <title> ](http://example.com)", formatter.link("[<title>]", "http://example.com")
assert_equal "@xxx", formatter.mention("xxx")
assert_equal "_Redmine Admin_", formatter.user_name(@user)
assert_equal "_[none]_", formatter.user_name(nil)
assert_equal "a b c d", formatter.trimming("a\nb\rc\r\nd")
assert_equal "a" * 200, formatter.trimming("a" * 200)
assert_equal ("a" * 200) + "...", formatter.trimming("a" * 201)
assert_equal "", formatter.trimming(nil)
assert_equal "_first last_", formatter.user_name("first_last")
end

def test_google_chat

formatter = RedmineIssueAssignNotice::Formatter.create "https://chat.googleapis.com/v1/xxxx/yyyy"
formatter = RedmineIssueAssignNotice::Formatter::GoogleChat.new

assert_equal "&lt;&gt;&amp;[]abc", formatter.escape("<>&[]abc")
assert_equal "\n", formatter.change_line
assert_equal "<http://example.com|[&lt;title&gt;]>", formatter.link("[<title>]", "http://example.com")
assert_equal "<users/xxx>", formatter.mention("xxx")
assert_equal "_Redmine Admin_", formatter.user_name(@user)
assert_equal "_[none]_", formatter.user_name(nil)
assert_equal "a b c d", formatter.trimming("a\nb\rc\r\nd")
assert_equal "a" * 200, formatter.trimming("a" * 200)
assert_equal ("a" * 200) + "...", formatter.trimming("a" * 201)
assert_equal "", formatter.trimming(nil)
assert_equal "_first last_", formatter.user_name("first_last")
end

def test_other

formatter = RedmineIssueAssignNotice::Formatter::Other.new

assert_equal "<>&[]abc", formatter.escape("<>&[]abc")
assert_equal "\n", formatter.change_line
assert_equal "[ <title> ](http://example.com)", formatter.link("[<title>]", "http://example.com")
assert_equal "@xxx", formatter.mention("xxx")
assert_equal "_Redmine Admin_", formatter.user_name(@user)
assert_equal "_[none]_", formatter.user_name(nil)
assert_equal "_first last_", formatter.user_name("first_last")
end
end

0 comments on commit 010b701

Please sign in to comment.