2013-06-06 101 views
1

我们正在开发一个从小问题跟踪软件到Redmine的混合模式。我们直接使用Ruby类来迁移数据。问题类定义如下:通过ruby代码创建和填充Redmine自定义字段

class BuggyIssue < ActiveRecord::Base 
    self.table_name = :issues 
    belongs_to :last_issue_change, :class_name => 'BuggyIssueChange', :foreign_key => 'last_issue_change_id' 
    has_many :issue_changes, :class_name => 'BuggyIssueChange', :foreign_key => 'issue_id', :order => 'issue_changes.date DESC' 
    set_inheritance_column :none 

    # Issue changes: only migrate status changes and comments 
    has_many :issue_changes, :class_name => "BuggyIssueChange", :foreign_key => :issue_id 

    def attachments 
     #BuggyMigrate::BuggyAttachment.all(:conditions => ["type = 'issue' AND id = ?", self.id.to_s]) 
    end 

    def issue_type 
     read_attribute(:type) 
    end 

    def summary 
     read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) 
    end 

    def description 
     read_attribute(:description).blank? ? summary : read_attribute(:description) 
    end 

    def time; Time.at(read_attribute(:time)) end 
    def changetime; Time.at(read_attribute(:changetime)) end 
    end 

创建问题并为问题定义自定义字段。但是,填充自定义字段似乎不起作用。有4个自定义字段(联系人,测试状态,来源和解决方案)。

自定义字段创建这样的:

repf = IssueCustomField.find_by_name("Contact") 
    repf ||= IssueCustomField.create(:name => "Contact", :field_format => 'string') if repf.nil? 
    repf.trackers = Tracker.find(:all) 
    repf.projects << product_map.values 
    repf.save! 

这些字段的值传递这样的:

i = Issue.new :project => product_map[first_change.product_id], 
... 
:custom_field_values => {:Contact => issue.contact, 'Test status' => '', :Source => '', :Resolution => ''} 

我也试图与索引哈希键版本:

:custom_field_values => {'1' => issue.contact, 'Test status' => '', :Source => '', :Resolution => ''} 

该问题可以保存没有问题,但是,没有任何价值传递给Redmine。 A

mysql> select count(*) from custom_values where value is not null; 
+----------+ 
| count(*) | 
+----------+ 
|  0 | 
+----------+ 
1 row in set (0.01 sec) 

显示迁移后自定义字段的所有值都为NULL。我似乎无法找到这是如何正确完成的,Redmine类的文档非常稀少。

回答

1

我花了很多时间来解决同样的问题。看看我编写的代码,通过Redmine REST API将数据从旧系统传输到新的。原因我使用ActiveResource代码将可用于您。

def update_custom_fields(issue, fields) 
    f_id = Hash.new { |hash, key| hash[key] = nil } 
    issue.available_custom_fields.each_with_index.map { |f,indx| f_id[f.name] = f.id } 
    field_list = [] 
    fields.each do |name, value| 
     field_id = f_id[name].to_s 
     field_list << Hash[field_id, value] 
    end 
    issue.custom_field_values = field_list.reduce({},:merge) 

    raise issue.errors.full_messages.join(', ') unless issue.save 
end 

现在你可以叫update_custom_fields(Issue.last, "MyField" => "MyValue" .. and so on)