Commits

Yuya Nishihara committed 0952165

ability to convert character encoding

Kagemai 0.8.8 uses EUC-JP by default

Comments (0)

Files changed (1)

lib/tasks/import_kagemai_reports.rake

 incorporated herein by reference.
 =end
 
+require 'iconv'
+
 namespace :redmine do
   
 desc 'Import issues from Kagemai BTS'
                                   Kagemai::Config[:message_bundle_name])
 
       @bts = Kagemai::BTS.new(Kagemai::Config[:project_dir])
+
+      @iconv = Iconv.new($KCODE, Kagemai::Config[:charset])
     end
 
     def self.import_project(redmine_proj_id, kagemai_proj_id)
       redmine_proj = Project.find_by_identifier(redmine_proj_id) or raise "unknown redmine project: #{redmine_proj_id}"
       kagemai_proj = @bts.open_project(kagemai_proj_id)
       kagemai_proj.each do |rep|
-        issue = Issue.new(:subject => rep.attr('title'), :description => rep.first['body'],
+        issue = Issue.new(:subject => encode(rep.attr('title')), :description => encode(rep.first['body']),
                           :created_on => rep.create_time)
         issue.project = redmine_proj
-        issue.author = User.find_by_mail(rep.first['email']) or raise "unknown user: #{rep.first['email']}"
+        issue.author = User.find_by_mail(encode(rep.first['email'])) or raise "unknown user: #{encode(rep.first['email'])}"
         issue.tracker = TRACKER_BUG  # FIXME
         # TODO: priority, status, attachment
 
 
         rep.each do |msg|
           next if msg == rep.first  # first message is treated as issue
-          note = Journal.new(:notes => msg['body'])
+          note = Journal.new(:notes => encode(msg['body']))
           note.journalized = issue
-          note.user = User.find_by_mail(msg['email']) or raise "unknown user: #{rep['email']}"
+          note.user = User.find_by_mail(encode(msg['email'])) or raise "unknown user: #{encode(rep['email'])}"
           # TODO: attachment
           Time.fake(msg.create_time) { note.save }
           print '.'
         end
       end
     end
+
+    def self.encode(text)
+      @iconv.iconv text
+    end
   end
 
   KagemaiImport.initialize()