1

我们正在开发从小问题跟踪软件到 Redmine 的迁移。我们直接使用 Ruby 类来迁移数据。问题的类定义如下:

  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。一个

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

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

4

1 回答 1

2

我花了很多时间来解决几乎相同的问题。看看我编写的通过 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)

于 2013-06-20T12:55:07.523 回答