Project

General

Profile

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

Marius BĂLTEANU, 2019-05-10 09:19

View differences:

app/models/import.rb
156 156
  # Adds a callback that will be called after the item at given position is imported
157 157
  def add_callback(position, name, *args)
158 158
    settings['callbacks'] ||= {}
159
    settings['callbacks'][position.to_i] ||= []
160
    settings['callbacks'][position.to_i] << [name, args]
159
    settings['callbacks'][position] ||= []
160
    settings['callbacks'][position] << [name, args]
161 161
    save!
162 162
  end
163 163

  
......
189 189
      if position > resume_after
190 190
        item = items.build
191 191
        item.position = position
192
        item.unique_id = row_value(row, 'unique_id') if use_unique_id?
192 193

  
193 194
        if object = build_object(row, item)
194 195
          if object.save
......
201 202
        item.save!
202 203
        imported += 1
203 204

  
204
        do_callbacks(item.position, object)
205
        do_callbacks(use_unique_id? ? item.unique_id : item.position, object)
205 206
      end
206 207
      current = position
207 208
    end
......
282 283
  def yes?(value)
283 284
    value == lu(user, :general_text_yes) || value == '1'
284 285
  end
286

  
287
  def use_unique_id?
288
    mapping['unique_id'].present?
289
  end
285 290
end
app/models/issue_import.rb
150 150
      end
151 151
    end
152 152
    if parent_issue_id = row_value(row, 'parent_issue_id')
153
      if parent_issue_id =~ /\A(#)?(\d+)\z/
154
        parent_issue_id = $2.to_i
155
        if $1
156
          attributes['parent_issue_id'] = parent_issue_id
153
      if parent_issue_id.start_with? '#'
154
        # refers to existing issue
155
        attributes['parent_issue_id'] = parent_issue_id[1..-1]
156
      elsif use_unique_id?
157
        # refers to other row with unique id
158
        issue_id = items.where(:unique_id => parent_issue_id).first.try(:obj_id)
159

  
160
        if issue_id
161
          attributes['parent_issue_id'] = issue_id
157 162
        else
158
          if parent_issue_id > item.position
159
            add_callback(parent_issue_id, 'set_as_parent', item.position)
160
          elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
161
            attributes['parent_issue_id'] = issue_id
162
          end
163
          add_callback(parent_issue_id, 'set_as_parent', item.position)
163 164
        end
165
      elsif parent_issue_id =~ /\A\d+\z/
166
        # refers to other row by position
167
        parent_issue_id = parent_issue_id.to_i
168

  
169
        if parent_issue_id > item.position
170
          add_callback(parent_issue_id, 'set_as_parent', item.position)
171
        elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
172
          attributes['parent_issue_id'] = issue_id
173
        end
174

  
164 175
      else
176
        # Something is odd. Assign parent_issue_id to trigger validation error
165 177
        attributes['parent_issue_id'] = parent_issue_id
166 178
      end
167 179
    end
app/views/imports/_issues_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
392 392
  field_warn_on_leaving_unsaved: Vor dem Verlassen einer Seite mit ungesichertem Text im Editor warnen
393 393
  field_watcher: Beobachter
394 394
  field_default_assigned_to: Standardbearbeiter
395
  field_unique_id: Eindeutige ID
395 396

  
396 397
  general_csv_decimal_separator: ','
397 398
  general_csv_encoding: ISO-8859-1
config/locales/en.yml
382 382
  field_full_width_layout: Full width layout
383 383
  field_digest: Checksum
384 384
  field_default_assigned_to: Default assignee
385
  field_unique_id: Unique ID
385 386

  
386 387
  setting_app_title: Application title
387 388
  setting_welcome_text: Welcome text
db/migrate/20190510070108_add_unique_id_to_import_items.rb
1
class AddUniqueIdToImportItems < ActiveRecord::Migration[5.2]
2
  def change
3
    change_table :import_items do |t|
4
      t.string "unique_id"
5
      t.index ["import_id", "unique_id"]
6
    end
7
  end
8
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
146 146
    assert_equal child2, grandchild.parent
147 147
  end
148 148

  
149
  def test_backward_and_forward_reference_with_unique_id
150
    import = generate_import_with_mapping('import_subtasks_with_unique_id.csv')
151
    import.settings['mapping'] = {'project_id' => '1', 'unique_id' => '0', 'tracker' => '1', 'subject' => '2', 'parent_issue_id' => '3'}
152
    import.save!
153

  
154
    root, child1, grandchild, child2 = new_records(Issue, 4) { import.run }
155
    assert_equal root, child1.parent
156
    assert_equal child2, grandchild.parent
157
  end
158

  
149 159
  def test_assignee_should_be_set
150 160
    import = generate_import_with_mapping
151 161
    import.mapping.merge!('assigned_to' => '11')
(2-2/2)