forked from ukautz/mantis2redmine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mantis2redmine.pl
1366 lines (1064 loc) · 43.6 KB
/
mantis2redmine.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
=head1 NAME
mantis2redmine.pl - Import Mantis database into Redmine
=head1 DESCRIPTION
This script imports provided Mantis database into existing Redmine database without destroying existing content in the redmine database. The idea is a non-destructive migration. Via user interaction re-mappings of mantis users, priojects and so on to Redmine equivalents can be performed.
Tested with:
Redmine 0.9.3-1 stable
Mantis 1.2.4 stable
Inspired by "migrate_from_mantis.rake" from the Redmine project.
=head1 DEPENDENCIES
Getopt::Long
DBIx::Simple
YAML
This would require the following packages on a debian system
perl-modules
libdbix-simple-perl
libyaml-perl
=head1 SYNOPSIS
mantis2redmine.pl -c mantis2redmine.config --load_maps --dry-run
=head1 FAQ
=over
=item * It says "Load from file" ..
You already ran this script once (eg in dry-run-mode). It will create store-<name>.map files within the current directory to save your answers. If you want to answer one (or all) of the mappings again simply remove the corresponding store-file (name should be self-explanatory.
=back
=head1 AUTHOR
=over
=item * Ulrich Kautz <[email protected]>
=item * Philipp Schüle <[email protected]>
=back
=head1 COPYRIGHT
Copyright (c) 2010. See L</AUTHOR>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
=head1 WARRANTIES
None. Make a backup!
=head1 Changelog
modified by Philipp Schüle <[email protected]>
did the following:
- import categories / do not use the trackers as categories
- use severity column to determine the tracker
- skip attachments because they might be in the DB
TODO
- make it possible to switch between categories -> categories / categories -> tracker import (via config/cli param)
=cut
# Load Modules
use strict;
use warnings;
use Getopt::Long;
use DBIx::Simple;
use Data::Dumper;
use YAML;
use File::Spec;
use File::Copy;
use version 0.74; our $VERSION = qv(v0.3.1);
# Unbuffered output
$| = 1;
# Read commandline options
my %opt;
GetOptions(
"dry_run|n" => \( my $DRY = 0 ),
"help|h" => \( $opt{ help } = 0 ),
"mantis_db_host=s" => \( $opt{ mantis_db_host } = "localhost" ),
"mantis_db_name=s" => \( $opt{ mantis_db_name } = "mantis" ),
"mantis_db_login=s" => \( $opt{ mantis_db_login } = "" ),
"mantis_db_pass=s" => \( $opt{ mantis_db_pass } = "" ),
"redmine_db_host=s" => \( $opt{ redmine_db_host } = "localhost" ),
"redmine_db_name=s" => \( $opt{ redmine_db_name } = "redmine" ),
"redmine_db_login=s" => \( $opt{ redmine_db_login } = "" ),
"redmine_db_pass=s" => \( $opt{ redmine_db_pass } = "" ),
"load_maps" => \( $opt{ load_maps } ),
"config|c=s" => \( $opt{ config } = "" ),
"category_source" => \( $opt{ category_source } = "categories" ),
"attachment_dir=s" => \( $opt{ attachment_dir } = "attachments" ),
"attachment_disk=s" => \( $opt{ attachment_disk } = "" )
);
# Print help
die <<HELP if $opt{ help };
mantis2redmine.pl v$VERSION
by Ulrich Kautz <uk\@fortrabbit.de>
Usage: $0 [options]
Options
--help | -h
Show this help
--dry_run | -n
Dont migrate anything.. just try to
--load_maps
Load map files which are created on generation, even on dry-run.
You can re-use them later on.. for lazy people.
--config | -c <path to config>
Each parameter can be provided via config file .. eg:
mantis_db_host = localhost
mantis_db_name = mantis
and so on ..
--attachment_dir <path>
Direcory for outputting any attachment file.
default: attachments (in current dir)
--attachment_disk <path>
Directory where files are stored in Mantis
if Mantis configured to store files on disk
Import flavor:
--category-source <source>
You can either use "categories" or "trackers" as source for your
newly created redmine categories.
Default: "categories"
Mantis Database:
--mantis_db_host <hostname>
default: localhost
--mantis_db_name <database>
default: mantis
--mantis_db_login <login>
--mantis_db_pass <password>
Redmine Database:
--redmine_db_host <hostname>
default: localhost
--redmine_db_name <database>
default: redmine
--redmine_db_login <login>
--redmine_db_pass <password>
HELP
# Init
# read config file for options ..
%opt = ( %opt, read_config( $opt{ config } ) ) if $opt{ config };
# check attribs
my @check_err = ();
foreach my $type( qw/ mantis redmine / ) {
foreach my $key( qw/ host name login pass / ) {
push @check_err, "--$type\_db_$key" unless $opt{ "$type\_db_$key" };
}
}
die "Missing: \n ". join( ", ", @check_err ). "\nUse --help for all options\n" if @check_err;
# check attachment table
die "No such directory '$opt{ attachment_dir }'.. please created!\n"
unless (-d $opt{ attachment_dir } || $opt{ attachments_in_db });
# check attachment source if on disk
die "No such directory '$opt{ attachment_disk }'.. please check path to Mantis filestore!\n"
if ($opt { attachment_disk } && !-d $opt { attachment_disk });
# check category-source
die "Not allowed --category-source '$opt{ category_source }', use one of 'categories' or 'trackers'\n"
unless $opt{ category_source } =~ /^(?:categories|trackers)$/;
# display warning
unless ( $DRY || read_in( "Are you sure you? Do you have a backup of your important data?\n eg: mysqldump --add-drop-table --lock-tables --complete-insert --create-options -u$opt{ redmine_db_login } -p$opt{ redmine_db_pass } -h$opt{ redmine_db_host } $opt{ redmine_db_name }\nType uppercase YES if you want to continue" ) eq "YES" ) {
die "Make a backup!\n";
}
# open dbi
my $dbix_mantis = DBIx::Simple->connect(
'DBI:mysql:database='. $opt{ mantis_db_name }. ';host='. $opt{ mantis_db_host },
$opt{ mantis_db_login }, $opt{ mantis_db_pass },
{ RaiseError => 1 }
);
my $dbix_redmine = DBIx::Simple->connect(
'DBI:mysql:database='. $opt{ redmine_db_name }. ';host='. $opt{ redmine_db_host },
$opt{ redmine_db_login }, $opt{ redmine_db_pass },
{ RaiseError => 1 }
);
# Prepare user-interactive import
# import mappings
my %map = ();
# build list of import modules
my @import_modules = (
qw/ stati priorities roles custom_fields relations projects versions /,
$opt{ category_source },
qw/ users /
);
# run import
foreach my $import( @import_modules ) {
my $meth = "import_$import";
print " *** ". ucfirst( $import ). " ***\n\n";
{
no strict 'refs';
$map{ $import } = $opt{ load_maps } && -f "store-$import.map"
? do {
print "-> Load from file\n";
load_map( $import )
}
: do {
my $ref = $meth->();
save_map( $import => $ref );
$ref;
}
;
}
print "\n\n";
}
# Run import
perform_import( \%map );
=head1 METHODS
=head2 import_stati
Rewrite the Mantis static stati to Redmine database stati.
User interactive.
=cut
sub import_stati {
my %redmine_stati = map {
( $_->{ position } => $_ )
} $dbix_redmine->query( 'SELECT id, name, position FROM issue_statuses' )->hashes;
my $default_ref = $redmine_stati{ 1 };
my %mantis_stati = (
10 => [ "new", $redmine_stati{ 1 } ], # new
20 => [ "feedback", $redmine_stati{ 4 } || $default_ref ], # feedback
30 => [ "acknowledged", $redmine_stati{ 1 } ], # acknowledged
40 => [ "confirmed", $redmine_stati{ 1 } ], # confirmed
50 => [ "assigned", $redmine_stati{ 2 } || $default_ref ], # assigned
80 => [ "resolved", $redmine_stati{ 3 } || $default_ref ], # resolved
90 => [ "closed", $redmine_stati{ 3 } || $default_ref ] # closed
);
return create_map( 'Status', \%mantis_stati, \%redmine_stati, $default_ref, 'position' );
}
=head2 import_priorities
Rewrite the Mantis static priorities to Redmine database priorities.
User interactive.
=cut
sub import_priorities {
my %redmine = map {
( $_->{ position } => $_ )
} $dbix_redmine->query( 'SELECT id, name, position FROM enumerations WHERE type = ?', 'IssuePriority' )->hashes;
my $default_ref = $redmine{ 1 };
my %mantis = (
10 => [ 'none', $redmine{ 1 } ], # none
20 => [ 'low', $redmine{ 1 } ], # low
30 => [ 'normal', $redmine{ 2 } || $default_ref ], # normal
40 => [ 'high', $redmine{ 3 } || $default_ref ], # high
50 => [ 'urgent', $redmine{ 4 } || $default_ref ], # urgent
60 => [ 'immediate', $redmine{ 5 } || $default_ref ] # immediate
);
return create_map( 'Priority', \%mantis, \%redmine, $default_ref, 'position' );
}
=head2 import_roles
Rewrite the Mantis static roles to Redmine database roles.
User interactive.
=cut
sub import_roles {
my %redmine = map {
( $_->{ position } => $_ )
} $dbix_redmine->query( 'SELECT id, name, position FROM roles' )->hashes;
my $default_ref = $redmine{ scalar keys %redmine };
my %mantis = (
10 => [ 'viewer', $default_ref ], # viewer
25 => [ 'reporter', $redmine{ 5 } || $default_ref ], # reporter
40 => [ 'updater', $default_ref ], # updater
55 => [ 'developer', $redmine{ 4 } || $default_ref ], # developer
70 => [ 'manager', $redmine{ 3 } || $default_ref ], # manager
90 => [ 'administrator', $redmine{ 3 } || $default_ref ] # administrator
);
return create_map( 'Role', \%mantis, \%redmine, $default_ref, 'position' );
}
=head2 import_custom_fields
Rewrite the Mantis static custom field types to Redmine static custom field types.
Non interactive.
=cut
sub import_custom_fields {
return {
0 => 'string', # String
1 => 'int', # Numeric
2 => 'int', # Float
3 => 'list', # Enumeration
4 => 'string', # Email
5 => 'bool', # Checkbox
6 => 'list', # List
7 => 'list', # Multiselection list
8 => 'date', # Date
};
}
=head2 import_relations
Rewrite the Mantis static relation types to Redmine static relation types.
Non interactive.
=cut
sub import_relations {
return {
1 => 'relates', # related to
2 => 'relates', # parent of
3 => 'relates', # child of
0 => 'duplicates', # duplicate of
4 => 'duplicates' # has duplicate
};
}
=head2 import_projects
Rewrite the Mantis projects to Redmine projects.
Non interactive.
=cut
sub import_projects {
my %mantis = map {
$_->{ name } = substr( $_->{ name }, 0, 30 );
( $_->{ id } => $_ );
} $dbix_mantis->query( 'SELECT id, name FROM mantis_project_table' )->hashes;
my ( $first_mantis_id ) = sort keys %mantis;
die "Did not find any mantis projects\n"
unless $first_mantis_id;
my %redmine = map {
( $_->{ id } => $_ );
} $dbix_redmine->query( 'SELECT id, name FROM projects' )->hashes;
my ( $first_id ) = sort keys %redmine;
my $mantis_ref = { map {
( $_ => [ $mantis{ $_ }->{ name }, { name => 'new', id => -1 } ] )
} keys %mantis };
my $redmine_ref = { map {
( $_ => $redmine{ $_ } )
} keys %redmine };
premap( $mantis_ref, $redmine_ref, 'name' );
my $default_ref = $first_id
? $redmine{ $first_id }
: { id => -1, name => '*no project found in redmine*' }
;
my $new_ref = create_map( 'Project', $mantis_ref, $redmine_ref, $default_ref, 'id', {
allow_new => 1,
print_mantis => 1,
print_redmine => 1
} );
my $ref = update_maps( $new_ref, \%mantis );
print Dumper $ref;
return $ref;
#return update_maps( $new_ref, \%mantis );
}
=head2 import_versions
Rewrite the Mantis projects to Redmine projects.
User interactive.
=cut
sub import_versions {
#my $sql_version_table = 'SELECT id, version, description, project_id, DATE_FORMAT( date, \'%Y-%m-%d\' ) as date, released FROM mantis_project_version_table';
my $sql_version_table = 'SELECT id, version, description, project_id, released FROM mantis_project_version_table';
my %mantis = map {
$_->{ name } = substr( delete $_->{ version }, 0, 30 );
( $_->{ id } => $_ );
} $dbix_mantis->query( $sql_version_table )->hashes;
my %redmine = map {
( $_->{ id } => $_ );
} $dbix_redmine->query( 'SELECT id, name FROM versions' )->hashes;
my ( $first_id ) = sort keys %redmine;
my $mantis_ref = { map {
( $_ => [ $mantis{ $_ }->{ name }, { name => 'new', id => -1 } ] )
} keys %mantis };
my $redmine_ref = { map {
( $_ => $redmine{ $_ } )
} keys %redmine };
premap( $mantis_ref, $redmine_ref, 'name' );
my $default_ref = $first_id
? $redmine{ $first_id }
: { id => -1, name => '*no version found in redmine*' }
;
my $new_ref = create_map( 'Version', $mantis_ref, $redmine_ref, $default_ref, 'id', {
allow_new => 1,
print_mantis => 1,
print_redmine => 1
} );
return update_maps( $new_ref, \%mantis );
}
=head2 import_trackers
Maps Mantis categories to Redmine trackers.
User interactive.
=cut
sub import_trackers {
my %mantis = map {
( $_->{ id } => $_ );
} $dbix_mantis->query( 'SELECT id, name, project_id FROM mantis_category_table' )->hashes;
my %redmine = map {
( $_->{ id } => $_ );
} $dbix_redmine->query( 'SELECT id, name FROM trackers' )->hashes;
my ( $first_id ) = sort keys %redmine;
my $mantis_ref = { map {
( $_ => [ $mantis{ $_ }->{ name }, { name => 'new', id => -1 } ] )
} keys %mantis };
my $redmine_ref = { map {
( $_ => $redmine{ $_ } )
} keys %redmine };
premap( $mantis_ref, $redmine_ref, 'name' );
my $new_ref = create_map( 'Tracker', $mantis_ref, $redmine_ref, $redmine{ $first_id }, 'id', {
allow_new => 1,
print_mantis => 1,
print_redmine => 1
} );
return update_maps( $new_ref, \%mantis );
}
=head2 import_categories
Maps Mantis categories to Redmine categories.
User interactive.
=cut
sub import_categories {
my %mantis = map {
( $_->{ id } => $_ );
} $dbix_mantis->query( 'SELECT id, name, project_id FROM mantis_category_table' )->hashes;
my %redmine = map {
( $_->{ id } => $_ );
} $dbix_redmine->query( 'SELECT id, name FROM issue_categories' )->hashes;
my ( $first_id ) = sort keys %redmine;
my $mantis_ref = { map {
( $_ => [ $mantis{ $_ }->{ name }, { name => 'new', id => -1 } ] )
} keys %mantis };
my $redmine_ref = { map {
( $_ => $redmine{ $_ } )
} keys %redmine };
premap( $mantis_ref, $redmine_ref, 'name' );
my $default_ref = $first_id
? $redmine{ $first_id }
: { id => -1, name => '*no category found in redmine*' }
;
my $new_ref = create_map( 'Category', $mantis_ref, $redmine_ref, $default_ref, 'id', {
allow_new => 1,
print_mantis => 1,
print_redmine => 1
} );
return update_maps( $new_ref, \%mantis );
}
=head2 import_users
Maps Mantis users to Redmine users.
User interactive.
=cut
sub import_users {
my $rx_user = qr/[^a-zA-Z0-9_\-@\.]/;
my $rx_name = qr/[^\w\s\'\-]/;
my %mantis = map {
$_->{ username } =~ s/$rx_user//;
my ( $firstname, $lastname ) = split( ' ', $_->{ realname } );
( $_->{ firstname } = substr( $firstname || $_->{ username }, 0, 30 ) ) =~ s/$rx_name//;
( $_->{ lastname } = substr( $lastname || '', 0, 30 ) ) =~ s/$rx_name//;
delete $_->{ realname };
$_->{ email } ||= $_->{ username } . '@dev.null';
$_->{ mail } = delete $_->{ email };
$_->{ login } = delete $_->{ username };
( $_->{ id } => $_ );
} $dbix_mantis->query( 'SELECT id, username, realname, email, access_level >= 90 as admin FROM mantis_user_table' )->hashes;
my %redmine = map {
( $_->{ id } => $_ );
} $dbix_redmine->query( 'SELECT id, login, firstname, lastname, mail FROM users' )->hashes;
my ( $first_id ) = sort keys %redmine;
my $mantis_ref = { map {
( $_ => [ $mantis{ $_ }->{ login }, { name => 'new', id => -1 } ] )
} keys %mantis };
my $redmine_ref = { map {
( $_ => { name => $redmine{ $_ }->{ login }, id => $redmine{ $_ }->{ id } } )
} keys %redmine };
premap( $mantis_ref, $redmine_ref, 'name' );
my $default_ref = $first_id
? $redmine{ $first_id }
: {
id => -1,
name => '*no user found in redmine*',
username => '*no user found in redmine*',
mail => '*',
login => '*no user found in redmine*',
firstname => '*no user found in redmine*',
lastname => '*no user found in redmine*',
}
;
my $new_ref = create_map( 'User', $mantis_ref, $redmine_ref, $default_ref, 'id', {
allow_new => 1,
print_mantis => 1,
print_redmine => 1
} );
return update_maps( $new_ref, \%mantis );
}
=head2 perform_import
Perform the whole import.
User interactive.
=cut
sub perform_import {
my ( $map_ref ) = @_;
my %report = ();
print "Import Users\n";
while( my ( $old_id, $new_ref ) = each %{ $map_ref->{ users } } ) {
print ".";
# create new user
if ( $new_ref->{ id } == -1 ) {
delete $new_ref->{ id };
unless ( $DRY ) {
$dbix_redmine->insert( users => {
%$new_ref, # firstname, lastname, login, mail, admin
status => 1,
type => 'User',
} );
( $map_ref->{ users }->{ $old_id } ) = $dbix_redmine->query( 'SELECT MAX(id) FROM users' )->list;
}
$report{ users_created } ++;
}
# use existing
else {
$map_ref->{ users }->{ $old_id } = $new_ref->{ id };
$report{ users_migrated } ++;
}
}
print "OK\n";
print "Import Projects\n";
my $count = 1;
while( my ( $old_id, $new_ref ) = each %{ $map_ref->{ projects } } ) {
print ".";
# create new project
if ( $new_ref->{ id } == -1 ) {
delete $new_ref->{ id };
# get max lft/rgt
my ( $lft, $rgt ) = $dbix_redmine->query( 'SELECT MAX( lft ), MAX(rgt) FROM projects' )->list;
$lft ||= 0;
$rgt ||= 0;
my $max = $lft > $rgt ? $lft : $rgt;
$max++;
unless ( $DRY ) {
$dbix_redmine->insert( projects => {
%$new_ref, # name
description => 'Imported from Mantis',
is_public => 0,
created_on => \'NOW()',
updated_on => \'NOW()',
identifier => 'mantis-'. substr( ''. time(), -5 ) . $count++,
status => 1,
lft => $max,
rgt => $max+1
} );
( $map_ref->{ projects }->{ $old_id } ) = $dbix_redmine->query( 'SELECT MAX(id) FROM projects' )->list;
if ( $opt{ category_source } ne 'trackers' ) {
# Enable default trackers for Bug, Feature, Support
$dbix_redmine->insert( projects_trackers => {
project_id => $map_ref->{ projects }->{ $old_id },
tracker_id => 1
} );
$dbix_redmine->insert( projects_trackers => {
project_id => $map_ref->{ projects }->{ $old_id },
tracker_id => 2
} );
$dbix_redmine->insert( projects_trackers => {
project_id => $map_ref->{ projects }->{ $old_id },
tracker_id => 3
} );
}
# Enable module Issue Tracking
$dbix_redmine->insert ( enabled_modules => {
project_id => $map_ref->{ projects }->{ $old_id },
name => 'issue_tracking'
} );
}
$report{ projects_created } ++;
}
# use existing
else {
$map_ref->{ projects }->{ $old_id } = $new_ref->{ id };
$report{ projects_migrated } ++;
}
}
print "OK\n";
print "Import Versions\n";
my %version_map = ();
while( my ( $old_id, $new_ref ) = each %{ $map_ref->{ versions } } ) {
print ".";
# create new version
if ( $new_ref->{ id } == -1 ) {
delete $new_ref->{ id };
my $project_id = $map_ref->{ projects }->{ delete $new_ref->{ project_id } };
my $released = $new_ref->{ released } ? 'closed' : 'open';
unless ( $DRY ) {
$dbix_redmine->insert( versions => {
name => $new_ref->{ name },
description => $new_ref->{ description },
project_id => $project_id,
status => $released,
effective_date => $new_ref->{ date }
} );
( $map_ref->{ versions }->{ $old_id } ) = $dbix_redmine->query( 'SELECT MAX(id) FROM versions' )->list;
$version_map{ $new_ref->{ name } } = $map_ref->{ versions }->{ $old_id };
}
else {
$version_map{ $new_ref->{ name } } =
$map_ref->{ versions }->{ $old_id } = 'NEW';
}
$report{ versions_created } ++;
}
# use existing
else {
$version_map{ $new_ref->{ name } } = $map_ref->{ versions }->{ $old_id } = $new_ref->{ id };
$report{ versions_migrated } ++;
}
}
print "OK\n";
# use Trackers -> Categories
if ( $opt{ category_source } eq 'trackers' ) {
print "Import Trackers\n";
my @project_ids = $dbix_redmine->query( 'SELECT id FROM projects' )->flat;
while( my ( $old_id, $new_ref ) = each %{ $map_ref->{ trackers } } ) {
print ".";
# create new tracker
if ( $new_ref->{ id } == -1 ) {
delete $new_ref->{ id };
# get tracker probs
my $name = delete $new_ref->{ name };
my $project_id = delete $new_ref->{ project_id };
my ( $position ) = $dbix_redmine->query( 'SELECT MAX(position)+1 FROM trackers' )->list;
unless ( $DRY ) {
# create tracker
$dbix_redmine->insert( trackers => {
name => $name,
position => $position,
is_in_roadmap => 0,
is_in_chlog => 0,
} );
( $map_ref->{ trackers }->{ $old_id } ) = $dbix_redmine->query( 'SELECT MAX(id) FROM trackers' )->list;
# link tracker to project(s)
my @insert = $project_id == 0 ? @project_ids : ( $map_ref->{ projects }->{ $project_id } );
foreach my $insert( @insert ) {
$dbix_redmine->insert( projects_trackers => {
project_id => $insert,
tracker_id => $map_ref->{ trackers }->{ $old_id }
} );
}
}
$report{ trackers_created } ++;
}
# use existing
else {
$map_ref->{ trackers }->{ $old_id } = $new_ref->{ id };
$report{ trackers_migrated } ++;
}
}
print "OK\n";
}
# use Categories -> Categories (default)
else {
print "Import Categories\n";
my @category_ids = $dbix_redmine->query( 'SELECT id FROM issue_categories' )->flat;
while( my ( $old_id, $new_ref ) = each %{ $map_ref->{ categories } } ) {
print ".";
# create new category
if ( $new_ref->{ id } == -1 ) {
delete $new_ref->{ id };
# get category probs
my $name = delete $new_ref->{ name };
my $project_id = delete $new_ref->{ project_id };
unless ( $DRY ) {
# create category
$dbix_redmine->insert( issue_categories => {
name => $name,
project_id => $project_id
} );
( $map_ref->{ categories }->{ $old_id } ) = $dbix_redmine->query( 'SELECT MAX(id) FROM issue_categories' )->list;
}
$report{ categories_created } ++;
}
# use existing
else {
$map_ref->{ categories }->{ $old_id } = $new_ref->{ id };
$report{ categories_migrated } ++;
}
}
print "OK\n";
}
# now the hard part .. import all issues!
print "Import Issues (. = Issue, - = Journal, + = Attachment)\n";
my $issues = $dbix_mantis->query( <<SQL );
SELECT
b.id,
b.project_id,
b.reporter_id,
b.handler_id,
b.priority,
b.status,
b.target_version,
b.severity,
b.category_id,
b.summary AS `subject`,
DATE_FORMAT( b.date_submitted , '%Y-%m-%d %T' ) AS `created_on`,
DATE_FORMAT( b.date_submitted , '%Y-%m-%d' ) AS `start_date`,
DATE_FORMAT( b.last_updated , '%Y-%m-%d %T' ) AS `updated_on`,
CONCAT_WS( "\n\n", tt.description, tt.steps_to_reproduce, tt.additional_information ) AS `description`
FROM mantis_bug_table b
LEFT JOIN mantis_bug_text_table tt ON ( tt.id = b.bug_text_id )
SQL
my $notes_sql = <<SQLNOTES;
SELECT
b.reporter_id,
DATE_FORMAT( b.date_submitted , '%Y-%m-%d %T' ) AS `created_on`,
tt.note
FROM mantis_bugnote_table b
LEFT JOIN mantis_bugnote_text_table tt ON ( tt.id = b.bugnote_text_id )
WHERE
b.bug_id = ?
SQLNOTES
my $attachments_sql = <<SQLNOTES;
SELECT
b.diskfile,
b.filename,
b.file_type,
b.folder,
REPLACE(b.diskfile, b.folder, '') as `diskfile_name`,
DATE_FORMAT( b.date_added , '%Y-%m-%d %T' ) AS `created_on`,
CONCAT_WS( "\n", b.title, b.description ) AS `description`,
b.content
FROM mantis_bug_file_table b
WHERE
b.bug_id = ?
SQLNOTES
# get admin id for file uploads
my ( $admin_id ) = $dbix_redmine->query( 'SELECT id FROM users WHERE login = "admin" LIMIT 1' )->list;
unless ( $admin_id ) {
( $admin_id ) = $dbix_redmine->query( 'SELECT id FROM users LIMIT 1' )->list;
}
my %issue_map = ();
while ( my $issue_ref = $issues->hash ) {
print ".";
my $issue_id;
#print "$issue_ref->{ id }: $issue_ref->{ target_version } -> $version_map{ $issue_ref->{ target_version } }\n" if $issue_ref->{ target_version };
#print "is feature: $issue_ref->{ subject } ( $opt{ tracker_id_feature } )\n" if ($issue_ref->{ severity } == 10);
my $trackerIdFeature = ($opt{ tracker_id_feature }) ? $opt{ tracker_id_feature } : 1;
my $trackerIdBug = ($opt{ tracker_id_bug }) ? $opt{ tracker_id_bug } : 2;
unless ( $DRY ) {
# insert
$dbix_redmine->insert( issues => my $ref = {
# severity 10 => feature / alle anderen -> bug
tracker_id => ($issue_ref->{ severity } == 10) ? $trackerIdFeature : $trackerIdBug,
project_id => $map_ref->{ projects }->{ $issue_ref->{ project_id } },
category_id => $map_ref->{ projects }->{ $issue_ref->{ category_id } },
subject => $issue_ref->{ subject },
description => $issue_ref->{ description },
status_id => $map_ref->{ stati }->{ $issue_ref->{ status } }->{ id },
assigned_to_id => $map_ref->{ users }->{ $issue_ref->{ handler_id } },
priority_id => $map_ref->{ priorities }->{ $issue_ref->{ priority } }->{ id },
author_id => $map_ref->{ users }->{ $issue_ref->{ reporter_id } },
created_on => $issue_ref->{ created_on },
updated_on => $issue_ref->{ updated_on },
start_date => $issue_ref->{ start_date },
fixed_version_id => $issue_ref->{ target_version } && defined $version_map{ $issue_ref->{ target_version } }
? $version_map{ $issue_ref->{ target_version } }
: 0
} );
# get id of the issue
( $issue_id ) = $dbix_redmine->query( 'SELECT MAX(id) FROM issues' )->list;
$issue_map{ $issue_ref->{ id } } = $issue_id;
}
$report{ issues_created } ++;
# insert notes
my $notes = $dbix_mantis->query( $notes_sql, $issue_ref->{ id } );
while ( my $note_ref = $notes->hash ) {
print "-";
unless ( $DRY ) {
# insert
$dbix_redmine->insert( journals => {
journalized_id => $issue_id,
journalized_type => 'Issue',
user_id => $map_ref->{ users }->{ $note_ref->{ reporter_id } },
notes => $note_ref->{ note },
created_on => $note_ref->{ created_on },
} );
}
$report{ journals_created } ++;
}
# insert attachments
if ( $opt{ attachment_disk } ) {
my $attachments = $dbix_mantis->query( $attachments_sql, $issue_ref->{ id } );
while ( my $attachment_ref = $attachments->hash ) {
print "+";
unless ( $DRY ) {
# copy file
my $mantis_path = File::Spec->catfile( $opt{ attachment_disk }, $attachment_ref->{ diskfile_name } );
my $output_path = File::Spec->catfile( $opt{ attachment_dir } , $attachment_ref->{ diskfile_name } );
if ( ! copy ($mantis_path, $output_path) ) {
print "Failed to copy attachment " . $attachment_ref->{ diskfile_name } . " for issue id " . $issue_ref->{ id } . "\n";
}
# insert
$dbix_redmine->insert( attachments => {
container_id => $issue_id,
container_type => 'Issue',
filename => $attachment_ref->{ filename },
disk_filename => $attachment_ref->{ diskfile_name },
filesize => -s $output_path,
content_type => $attachment_ref->{ file_type },
created_on => $attachment_ref->{ created_on },
author_id => $admin_id,
description => $attachment_ref->{ description }
} );
}
$report{ attachments_created } ++;
}
} else {
# we have the attachments in the db -> exit
# DON'T KNOW IF THIS WORKS
my $attachments = $dbix_mantis->query( $attachments_sql, $issue_ref->{ id } );
while ( my $attachment_ref = $attachments->hash ) {
print "+";
unless ( $DRY ) {