Commit 1b6793b1 authored by Eugenia Grieff's avatar Eugenia Grieff

Include reply in email egenrated notes

- The reply or any text formatted as a quote willl be
appended in a details section
- Add specs

Changelog: added
parent a96ba32b
......@@ -5,6 +5,7 @@ require 'gitlab/email/handler/reply_processing'
# handles note/reply creation emails with these formats:
# incoming+1234567890abcdef1234567890abcdef@incoming.gitlab.com
# Quoted material is _not_ stripped but appended as a `details` section
module Gitlab
module Email
module Handler
......@@ -24,7 +25,7 @@ module Gitlab
validate_permission!(:create_note)
raise NoteableNotFoundError unless noteable
raise EmptyEmailError if message.blank?
raise EmptyEmailError if message_including_truncated_reply.blank?
verify_record!(
record: create_note,
......@@ -47,7 +48,7 @@ module Gitlab
end
def create_note
sent_notification.create_reply(message)
sent_notification.create_reply(message_including_truncated_reply)
end
end
end
......
......@@ -35,6 +35,10 @@ module Gitlab
@message_with_reply ||= process_message(trim_reply: false)
end
def message_including_truncated_reply
@message_including_truncated_reply ||= process_message(trim_reply: false, append_reply: true)
end
def process_message(**kwargs)
message = ReplyParser.new(mail, **kwargs).execute.strip
message_with_attachments = add_attachments(message)
......
......@@ -6,9 +6,10 @@ module Gitlab
class ReplyParser
attr_accessor :message
def initialize(message, trim_reply: true)
def initialize(message, trim_reply: true, append_reply: false)
@message = message
@trim_reply = trim_reply
@append_reply = append_reply
end
def execute
......@@ -17,7 +18,9 @@ module Gitlab
encoding = body.encoding
if @trim_reply
body = EmailReplyTrimmer.trim(body)
body = trim_reply(body)
elsif @append_reply
body = trim_reply(body, append_trimmed_reply: true)
end
return '' unless body
......@@ -71,6 +74,18 @@ module Gitlab
rescue StandardError
nil
end
def trim_reply(text, append_trimmed_reply: false)
trimmed_body = EmailReplyTrimmer.trim(text)
return trimmed_body unless append_trimmed_reply
partitioned_body = text.partition(trimmed_body)
main_body = partitioned_body[1]
stripped_text = partitioned_body[2].chomp
return main_body if stripped_text.empty?
main_body + "\n<details><summary>...</summary>\n#{stripped_text}\n</details>"
end
end
end
end
Return-Path: <jake@adventuretime.ooo>
Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq-gitlabhq-project_id-auth_token-issue-issue_iid@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: Jake the Dog <jake@adventuretime.ooo>
To: incoming+gitlabhq-gitlabhq-project_id-auth_token-issue-issue_iid@appmail.adventuretime.ooo
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: New Issue comment by email
Mime-Version: 1.0
Content-Type: text/plain;
charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Sieve: CMU Sieve 2.2
X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
13 Jun 2013 14:03:48 -0700 (PDT)
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
> This is a quote
-- This is a line with 2 dashes
*This is bold text*
- This is a signature
> Final quote
\ No newline at end of file
......@@ -66,6 +66,8 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do
expect(new_note.author).to eq(sent_notification.recipient)
expect(new_note.position).to eq(note.position)
expect(new_note.note).to include('I could not disagree more.')
expect(new_note.note).to include('<details><summary>...</summary>')
expect(new_note.note).to include('> hey guys everyone knows adventure time sucks!')
expect(new_note.in_reply_to?(note)).to be_truthy
expect(new_note.discussion_id).to eq(note.discussion_id)
......
......@@ -228,5 +228,44 @@ RSpec.describe Gitlab::Email::ReplyParser do
BODY
)
end
context 'when append_reply option is true' do
it "appends trimmed reply when body includes a quote at the end" do
expect(test_parse_body(fixture_file("emails/valid_new_issue_with_quote.eml"), { trim_reply: false, append_reply: true }))
.to eq(
<<-BODY.strip_heredoc.chomp
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:
<details><summary>...</summary>
> this is a quote
</details>
BODY
)
end
it 'appends trimmed reply when body includes a quote at the beginning' do
expect(test_parse_body(fixture_file("emails/valid_note_with_quote.eml"), { trim_reply: false, append_reply: true }))
.to eq(
<<-BODY.strip_heredoc.chomp
> This is a quote
-- This is a line with 2 dashes
*This is bold text*
- This is a signature
<details><summary>...</summary>
> Final quote
</details>
BODY
)
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