From 58c0fb6db69e841fda4cc70534280955d859e995 Mon Sep 17 00:00:00 2001
From: Wael Nasreddine <wael.nasreddine@gmail.com>
Date: Sat, 5 Jun 2010 12:37:47 +0200
Subject: [PATCH] Add a "Version" keyword to receiving email. #5622

---
 app/models/mail_handler.rb                         |    6 +++++-
 .../mail_handler/ticket_on_given_project.eml       |    2 +-
 .../mail_handler/ticket_reply_with_status.eml      |    1 +
 test/unit/mail_handler_test.rb                     |    2 ++
 4 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index ae204b4..f121b13 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -123,13 +123,14 @@ class MailHandler < ActionMailer::Base
     assigned_to = (get_keyword(:assigned_to, :override => true) && find_user_from_keyword(get_keyword(:assigned_to, :override => true)))
     due_date = get_keyword(:due_date, :override => true)
     start_date = get_keyword(:start_date, :override => true)
+		version = (get_keyword(:version, :override => true) && project.versions.find_by_name(get_keyword(:version)))
 
     # check permission
     unless @@handler_options[:no_permission_check]
       raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
     end
 
-    issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority, :due_date => due_date, :start_date => start_date, :assigned_to => assigned_to)
+    issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority, :due_date => due_date, :start_date => start_date, :assigned_to => assigned_to, :fixed_version => version)
     # check workflow
     if status && issue.new_statuses_allowed_to(user).include?(status)
       issue.status = status
@@ -165,10 +166,12 @@ class MailHandler < ActionMailer::Base
   
   # Adds a note to an existing issue
   def receive_issue_reply(issue_id)
+		project = target_project
     status =  (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
     due_date = get_keyword(:due_date, :override => true)
     start_date = get_keyword(:start_date, :override => true)
     assigned_to = (get_keyword(:assigned_to, :override => true) && find_user_from_keyword(get_keyword(:assigned_to, :override => true)))
+		version = (get_keyword(:version, :override => true) && project.versions.find_by_name(get_keyword(:version)))
     
     issue = Issue.find_by_id(issue_id)
     return unless issue
@@ -188,6 +191,7 @@ class MailHandler < ActionMailer::Base
     issue.start_date = start_date if start_date
     issue.due_date = due_date if due_date
     issue.assigned_to = assigned_to if assigned_to
+		issue.fixed_version = version if version
     
     issue.save!
     logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
diff --git a/test/fixtures/mail_handler/ticket_on_given_project.eml b/test/fixtures/mail_handler/ticket_on_given_project.eml
index cfc90b5..d5aaad2 100644
--- a/test/fixtures/mail_handler/ticket_on_given_project.eml
+++ b/test/fixtures/mail_handler/ticket_on_given_project.eml
@@ -54,4 +54,4 @@ Status: Resolved
 due date: 2010-12-31
 Start Date:2010-01-01
 Assigned to: John Smith
-
+Version: Alpha
diff --git a/test/fixtures/mail_handler/ticket_reply_with_status.eml b/test/fixtures/mail_handler/ticket_reply_with_status.eml
index e7d68c3..4059339 100644
--- a/test/fixtures/mail_handler/ticket_reply_with_status.eml
+++ b/test/fixtures/mail_handler/ticket_reply_with_status.eml
@@ -29,6 +29,7 @@ Status: Resolved
 due date: 2010-12-31
 Start Date:2010-01-01
 Assigned to: jsmith@somenet.foo
+Version: 0.5
 
 ------=_NextPart_000_0067_01C8D3CE.711F9CC0
 Content-Type: text/html;
diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb
index 29659da..e54118d 100644
--- a/test/unit/mail_handler_test.rb
+++ b/test/unit/mail_handler_test.rb
@@ -58,6 +58,7 @@ class MailHandlerTest < ActiveSupport::TestCase
     assert_equal '2010-01-01', issue.start_date.to_s
     assert_equal '2010-12-31', issue.due_date.to_s
     assert_equal User.find_by_login('jsmith'), issue.assigned_to
+    assert_equal 'Alpha', issue.fixed_version.name
     # keywords should be removed from the email body
     assert !issue.description.match(/^Project:/i)
     assert !issue.description.match(/^Status:/i)
@@ -258,6 +259,7 @@ class MailHandlerTest < ActiveSupport::TestCase
     assert_equal '2010-01-01', issue.start_date.to_s
     assert_equal '2010-12-31', issue.due_date.to_s
     assert_equal User.find_by_login('jsmith'), issue.assigned_to
+    assert_equal '0.5', issue.fixed_version.name
   end
 
   def test_add_issue_note_should_send_email_notification
-- 
1.7.0.3

