Commit ce963d21 authored by Eugenia Grieff's avatar Eugenia Grieff

Move apend reply logic to ReplyProcessing

- Update specs
parent 109255cb
...@@ -40,15 +40,14 @@ module Gitlab ...@@ -40,15 +40,14 @@ module Gitlab
end end
def process_message(**kwargs) def process_message(**kwargs)
message = ReplyParser.new(mail, **kwargs).execute.strip message, stripped_text = ReplyParser.new(mail, **kwargs).execute
message_with_attachments = add_attachments(message) message = message.strip
# Support bot is specifically forbidden from using slash commands. # Support bot is specifically forbidden from using slash commands.
message = strip_quick_actions(message_with_attachments) message = strip_quick_actions(message)
return message unless kwargs[:append_reply].present? message = append_reply(message, stripped_text) if kwargs[:append_reply]
# Appended details section should be removed if the message only contains slash commands. add_attachments(message)
strip_details_section(message)
end end
def add_attachments(reply) def add_attachments(reply)
...@@ -102,18 +101,20 @@ module Gitlab ...@@ -102,18 +101,20 @@ module Gitlab
quick_actions_extractor.redact_commands(content) quick_actions_extractor.redact_commands(content)
end end
def strip_details_section(content)
body, commands = quick_actions_extractor.extract_commands(content)
return content if commands.empty?
return content unless body =~ /\A(<details>)/
content.sub(body, '').chomp
end
def quick_actions_extractor def quick_actions_extractor
command_definitions = ::QuickActions::InterpretService.command_definitions command_definitions = ::QuickActions::InterpretService.command_definitions
::Gitlab::QuickActions::Extractor.new(command_definitions) ::Gitlab::QuickActions::Extractor.new(command_definitions)
end end
def append_reply(message, reply)
return message if message.blank? || reply.blank?
# Do not append if message only contains slash commands
body, _commands = quick_actions_extractor.extract_commands(message)
return message if body.empty?
message + "\n\n<details><summary>...</summary>\n\n#{reply}\n\n</details>"
end
end end
end end
end end
......
...@@ -16,7 +16,7 @@ module Gitlab ...@@ -16,7 +16,7 @@ module Gitlab
body = select_body(message) body = select_body(message)
encoding = body.encoding encoding = body.encoding
body = trim_reply(body, append_trimmed_reply: @append_reply) if @trim_reply body, stripped_text = EmailReplyTrimmer.trim(body, @append_reply) if @trim_reply
return '' unless body return '' unless body
# not using /\s+$/ here because that deletes empty lines # not using /\s+$/ here because that deletes empty lines
...@@ -27,7 +27,9 @@ module Gitlab ...@@ -27,7 +27,9 @@ module Gitlab
# so we detect it manually here. # so we detect it manually here.
return "" if body.lines.all? { |l| l.strip.empty? || l.start_with?('>') } return "" if body.lines.all? { |l| l.strip.empty? || l.start_with?('>') }
body.force_encoding(encoding).encode("UTF-8") encoded_body = body.force_encoding(encoding).encode("UTF-8")
@append_reply ? [encoded_body, stripped_text] : encoded_body
end end
private private
...@@ -69,14 +71,12 @@ module Gitlab ...@@ -69,14 +71,12 @@ module Gitlab
nil nil
end end
def trim_reply(text, append_trimmed_reply: false) # def trim_reply(text, append_trimmed_reply: false)
trimmed_body, stripped_text = EmailReplyTrimmer.trim(text, true) # trimmed_body, stripped_text = EmailReplyTrimmer.trim(text, append_trimmed_reply)
# return trimmed_body if trimmed_body.blank? || stripped_text.blank?
return '' unless trimmed_body.present?
return trimmed_body unless stripped_text.present? && append_trimmed_reply
trimmed_body + "\n\n<details><summary>...</summary>\n\n#{stripped_text}\n\n</details>" # trimmed_body + "\n\n<details><summary>...</summary>\n\n#{stripped_text}\n\n</details>"
end # end
end end
end end
end end
...@@ -230,21 +230,19 @@ RSpec.describe Gitlab::Email::ReplyParser do ...@@ -230,21 +230,19 @@ RSpec.describe Gitlab::Email::ReplyParser do
end end
it "appends trimmed reply when when append_reply option is true" do it "appends trimmed reply when when append_reply option is true" do
expect(test_parse_body(fixture_file("emails/valid_new_issue_with_quote.eml"), { append_reply: true })) body = <<-BODY.strip_heredoc.chomp
.to eq( The reply by email functionality should be extended to allow creating a new issue by email.
<<-BODY.strip_heredoc.chomp even when the email is forwarded to the project which may include lines that begin with ">"
The reply by email functionality should be extended to allow creating a new issue by email.
even when the email is forwarded to the project which may include lines that begin with ">"
there should be a quote below this line: there should be a quote below this line:
BODY
<details><summary>...</summary> reply = <<-BODY.strip_heredoc.chomp
> this is a quote
BODY
> this is a quote expect(test_parse_body(fixture_file("emails/valid_new_issue_with_quote.eml"), { append_reply: true }))
.to contain_exactly(body, reply)
</details>
BODY
)
end end
end 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