Project

General

Profile

Feature #28213 » 0001-Support-external-ID-when-importing-issues.patch

Gregor Schmidt, 2018-02-19 10:28

View differences:

app/models/import.rb
141 141
  # Adds a callback that will be called after the item at given position is imported
142 142
  def add_callback(position, name, *args)
143 143
    settings['callbacks'] ||= {}
144
    settings['callbacks'][position.to_i] ||= []
145
    settings['callbacks'][position.to_i] << [name, args]
144
    settings['callbacks'][position] ||= []
145
    settings['callbacks'][position] << [name, args]
146 146
    save!
147 147
  end
148 148

  
......
174 174
      if position > resume_after
175 175
        item = items.build
176 176
        item.position = position
177
        item.unique_id = row_value(row, 'unique_id') if use_unique_id?
177 178

  
178 179
        if object = build_object(row, item)
179 180
          if object.save
......
186 187
        item.save!
187 188
        imported += 1
188 189

  
189
        do_callbacks(item.position, object)
190
        do_callbacks(use_unique_id? ? item.unique_id : item.position, object)
190 191
      end
191 192
      current = position
192 193
    end
......
266 267
  def yes?(value)
267 268
    value == lu(user, :general_text_yes) || value == '1'
268 269
  end
270

  
271
  def use_unique_id?
272
    mapping['unique_id'].present?
273
  end
269 274
end
app/models/issue_import.rb
138 138
      end
139 139
    end
140 140
    if parent_issue_id = row_value(row, 'parent_issue_id')
141
      if parent_issue_id =~ /\A(#)?(\d+)\z/
142
        parent_issue_id = $2.to_i
143
        if $1
144
          attributes['parent_issue_id'] = parent_issue_id
141
      if parent_issue_id.start_with? '#'
142
        # refers to existing issue
143
        attributes['parent_issue_id'] = parent_issue_id[1..-1]
144
      elsif use_unique_id?
145
        # refers to other row with unique id
146
        issue_id = items.where(:unique_id => parent_issue_id).first.try(:obj_id)
147

  
148
        if issue_id
149
          attributes['parent_issue_id'] = issue_id
145 150
        else
146
          if parent_issue_id > item.position
147
            add_callback(parent_issue_id, 'set_as_parent', item.position)
148
          elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
149
            attributes['parent_issue_id'] = issue_id
150
          end
151
          add_callback(parent_issue_id, 'set_as_parent', item.position)
151 152
        end
153
      elsif parent_issue_id =~ /\A\d+\z/
154
        # refers to other row by position
155
        parent_issue_id = parent_issue_id.to_i
156

  
157
        if parent_issue_id > item.position
158
          add_callback(parent_issue_id, 'set_as_parent', item.position)
159
        elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
160
          attributes['parent_issue_id'] = issue_id
161
        end
162

  
152 163
      else
164
        # Something is odd. Assign parent_issue_id to trigger validation error
153 165
        attributes['parent_issue_id'] = parent_issue_id
154 166
      end
155 167
    end
app/views/imports/_fields_mapping.html.erb
1
<div class="splitcontent">
2
<div class="splitcontentleft">
1 3
<p>
2 4
  <label for="import_mapping_project_id"><%= l(:label_project) %></label>
3 5
  <%= select_tag 'import_settings[mapping][project_id]',
......
13 15
  <label for="import_mapping_status"><%= l(:field_status) %></label>
14 16
  <%= mapping_select_tag @import, 'status' %>
15 17
</p>
18
</div>
19

  
20
<div class="splitcontentright">
21
<p></p>
22
<p>
23
  <label for="import_mapping_unique_id"><%= l(:field_unique_id) %></label>
24
  <%= mapping_select_tag @import, 'unique_id' %>
25
</p>
26
</div>
27
</div>
16 28

  
17 29
<div class="splitcontent">
18 30
<div class="splitcontentleft">
config/locales/de.yml
390 390
  field_warn_on_leaving_unsaved: Vor dem Verlassen einer Seite mit ungesichertem Text im Editor warnen
391 391
  field_watcher: Beobachter
392 392
  field_default_assigned_to: Standardbearbeiter
393
  field_unique_id: Eindeutige ID
393 394

  
394 395
  general_csv_decimal_separator: ','
395 396
  general_csv_encoding: ISO-8859-1
config/locales/en.yml
376 376
  field_full_width_layout: Full width layout
377 377
  field_digest: Checksum
378 378
  field_default_assigned_to: Default assignee
379
  field_unique_id: Unique ID
379 380

  
380 381
  setting_app_title: Application title
381 382
  setting_app_subtitle: Application subtitle
db/migrate/20180216132815_add_unique_id_to_import_items.rb
1
class AddUniqueIdToImportItems < ActiveRecord::Migration[5.1]
2
  def change
3
    change_table :import_items do |t|
4
      t.string "unique_id"
5

  
6
      t.index ["import_id", "unique_id"]
7
    end
8
  end
9
end
test/fixtures/files/import_subtasks_with_unique_id.csv
1
id;tracker;subject;parent
2
RED-I;bug;Root;
3
RED-II;bug;Child 1;RED-I
4
RED-III;bug;Grand-child;RED-IV
5
RED-IV;bug;Child 2;RED-I
test/unit/issue_import_test.rb
128 128
    assert_equal child2, grandchild.parent
129 129
  end
130 130

  
131
  def test_backward_and_forward_reference_with_unique_id
132
    import = generate_import_with_mapping('import_subtasks_with_unique_id.csv')
133
    import.settings['mapping'] = {'project_id' => '1', 'unique_id' => '0', 'tracker' => '1', 'subject' => '2', 'parent_issue_id' => '3'}
134
    import.save!
135

  
136
    root, child1, grandchild, child2 = new_records(Issue, 4) { import.run }
137
    assert_equal root, child1.parent
138
    assert_equal child2, grandchild.parent
139
  end
140

  
131 141
  def test_assignee_should_be_set
132 142
    import = generate_import_with_mapping
133 143
    import.mapping.merge!('assigned_to' => '11')
(1-1/2)