-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmigrate_jira.rake
1069 lines (907 loc) · 34 KB
/
migrate_jira.rake
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
require 'rexml/document'
require 'active_record'
require 'yaml'
require 'fileutils'
require File.expand_path('../../../config/environment', __FILE__) # Assumes that migrate_jira.rake is in lib/tasks/
# require 'byebug'
module JiraMigration
include Nokogiri
############## Configuration mapping file. Maps Jira Entities to Redmine Entities. Generated on the first run.
CONF_FILE = 'map_jira_to_redmine.yml'
############## Jira backup main xml file with all data
ENTITIES_FILE = 'entities.xml'
############## Location of jira attachements
JIRA_ATTACHMENTS_DIR = 'attachments'
#JIRA_ATTACHMENTS_DIR = '/home/ubuntu/JIRA-backup-20150303/data/attachments'
############## Jira URL
$JIRA_WEB_URL = 'https://glorium.jira.com'
#$JIRA_WEB_URL = 'https://leasepipeline.atlassian.net'
class BaseJira
MAP = {}
attr_reader :tag
attr_accessor :new_record, :is_new
def map
self.class::MAP
end
def initialize(node)
@tag = node
end
def method_missing(key, *args)
if key.to_s.start_with?('jira_')
attr = key.to_s.sub('jira_', '')
return @tag[attr]
end
puts "Method missing: #{key}"
raise NoMethodError key
end
def run_all_redmine_fields
ret = {}
self.methods.each do |method_name|
m = method_name.to_s
if m.start_with?('red_')
mm = m.to_s.sub('red_', '')
ret[mm] = self.send(m)
end
end
return ret
end
def migrate
all_fields = self.run_all_redmine_fields()
#pp('Saving:', all_fields)
record = self.retrieve
if record
record.update_attributes(all_fields)
else
record = self.class::DEST_MODEL.new all_fields
self.is_new = true
end
if self.respond_to?('before_save')
self.before_save(record)
end
record.save!
record.reload
self.map[self.jira_id] = record
self.new_record = record
if self.respond_to?('post_migrate')
self.post_migrate(record, self.is_new)
end
record.reload
return record
end
def retrieve
self.class::DEST_MODEL.find_by_name(self.jira_id)
end
end
class JiraUser < BaseJira
DEST_MODEL = User
MAP = {}
attr_accessor :jira_emailAddress, :jira_name
def initialize(node)
super
end
def retrieve
# Check mail address first, as it is more likely to match across systems
user = self.class::DEST_MODEL.find_by_mail(self.jira_emailAddress)
if !user
user = self.class::DEST_MODEL.find_by_login(self.jira_name)
end
return user
end
def migrate
super
$MIGRATED_USERS_BY_NAME[self.jira_name] = self.new_record
end
# First Name, Last Name, E-mail, Password
# here is the tranformation of Jira attributes in Redmine attribues
def red_firstname
self.jira_firstName
end
def red_lastname
self.jira_lastName
end
def red_mail
self.jira_emailAddress
end
def red_login
self.jira_name
end
def before_save(new_record)
new_record.login = red_login
if new_record.new_record?
new_record.salt_password('Pa$$w0rd')
end
end
def post_migrate(new_record, is_new)
if is_new
new_record.update_attribute(:must_change_passwd, true)
new_record.reload
end
end
end
class JiraGroup < BaseJira
DEST_MODEL = Group
MAP = {}
def initialize(node)
super
end
def retrieve
group = self.class::DEST_MODEL.find_by_lastname(self.jira_lowerGroupName)
end
def red_name
self.jira_lowerGroupName
end
end
class JiraProject < BaseJira
DEST_MODEL = Project
MAP = {}
def retrieve
self.class::DEST_MODEL.find_by_identifier(self.red_identifier)
end
def post_migrate(new_record, is_new)
if !new_record.module_enabled?('issue_tracking')
new_record.enabled_modules << EnabledModule.new(:name => 'issue_tracking')
end
$MIGRATED_ISSUE_TYPES.values.uniq.each do |issue_type|
if !new_record.trackers.include?(issue_type)
new_record.trackers << issue_type
end
end
new_record.update_column(:is_public, false)
new_record.reload
end
# here is the tranformation of Jira attributes in Redmine attribues
def red_name
self.jira_name
end
def red_description
self.jira_name
end
def red_identifier
ret = self.jira_key.downcase
return ret
end
end
class JiraVersion < BaseJira
DEST_MODEL = Version
MAP = {}
def jira_marker
return "FROM JIRA: \"#{$MAP_PROJECT_ID_TO_PROJECT_KEY[self.jira_project]}\":#{$JIRA_WEB_URL}/browse/#{$MAP_PROJECT_ID_TO_PROJECT_KEY[self.jira_project]}\n"
end
def retrieve
self.class::DEST_MODEL.find_by_name(self.jira_name)
end
def red_project
# needs to return the Rails Project object
JiraProject::MAP[self.jira_project]
end
def red_name
self.jira_name
end
def red_description
self.jira_description
end
def red_due_date
if self.jira_releasedate
Time.parse(self.jira_releasedate)
end
end
end
class JiraIssue < BaseJira
DEST_MODEL = Issue
MAP = {}
attr_reader :jira_description, :jira_reporter
def initialize(node_tag)
super
if @tag.at('description')
@jira_description = @tag.at('description').text
#elsif @tag['description']
else
@jira_description = @tag['description']
end
#@jira_description = node_tag['description'].to_s
@jira_reporter = node_tag['reporter'].to_s
end
def jira_marker
return "FROM JIRA: \"#{self.jira_key}\":#{$JIRA_WEB_URL}/browse/#{self.jira_key}"
end
def retrieve
Issue.where("description Like '#{self.jira_marker}%'").first()
end
def red_project
# needs to return the Rails Project object
JiraProject::MAP[self.jira_project]
end
=begin
def red_fixed_version
path = "/*/NodeAssociation[@sourceNodeId=\"#{self.jira_id}\" and @sourceNodeEntity=\"Issue\" and @sinkNodeEntity=\"Version\" and @associationType=\"IssueFixVersion\"]"
assocs = JiraMigration.get_list_from_tag(path)
versions = []
assocs.each do |assoc|
version = JiraVersion::MAP[assoc["sinkNodeId"]]
versions.push(version)
end
versions.last
end
=end
def red_subject
if self.jira_summary.nil?
return "jira blank summary"
else
return self.jira_summary
end
end
def red_description
"#{self.jira_marker}\n%s" % @jira_description
#dsc = self.jira_marker + "\n"
#if @jira_description
# dsc += @jira_description
#else
# dsc += self.red_subject
#end
#return dsc
end
def red_priority
name = $MIGRATED_ISSUE_PRIORITIES_BY_ID[self.jira_priority]
return $MIGRATED_ISSUE_PRIORITIES[name]
end
def red_created_on
Time.parse(self.jira_created)
end
def red_updated_on
Time.parse(self.jira_updated)
end
def red_estimated_hours
self.jira_timeestimate.to_s.empty? ? 0 : self.jira_timeestimate.to_f / 3600
end
# def red_start_date
# Time.parse(self.jira_created)
# end
def red_due_date
Time.parse(self.jira_resolutiondate) if self.jira_resolutiondate
end
def red_status
name = $MIGRATED_ISSUE_STATUS_BY_ID[self.jira_status]
return $MIGRATED_ISSUE_STATUS[name]
end
def red_tracker
type_name = $MIGRATED_ISSUE_TYPES_BY_ID[self.jira_type]
return $MIGRATED_ISSUE_TYPES[type_name]
end
def red_author
JiraMigration.find_user_by_jira_name(self.jira_reporter)
end
def red_assigned_to
if self.jira_assignee
JiraMigration.find_user_by_jira_name(self.jira_assignee)
else
nil
end
end
def post_migrate(new_record, is_new)
new_record.update_column :updated_on, Time.parse(self.jira_updated)
new_record.update_column :created_on, Time.parse(self.jira_created)
new_record.reload
end
end
class JiraComment < BaseJira
DEST_MODEL = Journal
MAP = {}
def initialize(node)
super
# get a body from a comment
# comment can have the comment body as a attribute or as a child tag
#@jira_body = @tag["body"] || @tag.at("body").text
@jira_body = node['body']
end
def jira_marker
return "FROM JIRA: #{self.jira_id}"
end
def retrieve
Journal.where(notes: "LIKE '#{self.jira_marker}%'").first()
end
# here is the tranformation of Jira attributes in Redmine attribues
def red_notes
#self.jira_marker + "\n" + @jira_body
"#{self.jira_marker}\n%s" % @jira_body
end
def red_created_on
DateTime.parse(self.jira_created)
end
def red_user
# retrieving the Rails object
JiraMigration.find_user_by_jira_name(self.jira_author)
end
def red_journalized
# retrieving the Rails object
JiraIssue::MAP[self.jira_issue]
end
def post_migrate(new_record, is_new)
new_record.update_column :created_on, Time.parse(self.jira_created)
new_record.reload
end
end
class JiraAttachment < BaseJira
DEST_MODEL = Attachment
MAP = {}
def retrieve
nil
end
def before_save(new_record)
new_record.container = self.red_container
pp(new_record)
# JIRA stores attachments as follows:
# <PROJECTKEY>/<ISSUE-KEY>/<ATTACHMENT_ID>_filename.ext
#
# We have to recreate this path in order to copy the file
issue_key = $MAP_ISSUE_TO_PROJECT_KEY[self.jira_issue][:issue_key]
project_key = $MAP_ISSUE_TO_PROJECT_KEY[self.jira_issue][:project_key]
jira_attachment_file = File.join(JIRA_ATTACHMENTS_DIR,
project_key,
issue_key,
"#{self.jira_id}")
puts "Jira Attachment File: #{jira_attachment_file}"
if File.exists? jira_attachment_file
new_record.file = File.open(jira_attachment_file)
puts "Setting attachment #{jira_attachment_file} for record"
# redmine_attachment_file = File.join(Attachment.storage_path, new_record.disk_filename)
# puts "Copying attachment [#{jira_attachment_file}] to [#{redmine_attachment_file}]"
# FileUtils.cp jira_attachment_file, redmine_attachment_file
else
puts "Attachment file [#{jira_attachment_file}] not found. Skipping copy."
end
end
# here is the tranformation of Jira attributes in Redmine attribues
#<FileAttachment id="10084" issue="10255" mimetype="image/jpeg" filename="Landing_Template.jpg"
# created="2011-05-05 15:54:59.411" filesize="236515" author="emiliano"/>
def red_filename
self.jira_filename.gsub(/[^\w\.\-]/,'_') # stole from Redmine: app/model/attachment (methods sanitize_filenanme)
end
# def red_disk_filename
# Attachment.disk_filename(self.jira_issue+self.jira_filename)
# end
def red_content_type
self.jira_mimetype.to_s.chomp
end
# def red_filesize
# self.jira_filesize
# end
def red_created_on
DateTime.parse(self.jira_created)
end
def red_author
JiraMigration.find_user_by_jira_name(self.jira_author)
end
def red_container
JiraIssue::MAP[self.jira_issue]
end
def post_migrate(new_record, is_new)
new_record.update_column :created_on, Time.parse(self.jira_created)
new_record.reload
end
end
ISSUELINK_TYPE_MARKER = IssueRelation::TYPE_RELATES
DEFAULT_ISSUELINK_TYPE_MAP = {
# Default map from Jira (key) to Redmine (value)
"Duplicate" => IssueRelation::TYPE_DUPLICATES, # inward="is duplicated by" outward="duplicates"
"Relates" => IssueRelation::TYPE_RELATES, # inward="relates to" outward="relates to"
"Blocked" => IssueRelation::TYPE_BLOCKS, # inward="blocked by" outward="blocks"
"Dependent" => IssueRelation::TYPE_FOLLOWS, # inward="is depended on by" outward="depends on"
"Epic-Story Link" => "Epic-Story",
"jira_subtask_link" => "Subtask"
}
ISSUE_TYPE_MARKER = "(choose a Redmine Tracker)"
DEFAULT_ISSUE_TYPE_MAP = {
# Default map from Jira (key) to Redmine (value)
# the comments on right side are Jira definitions - http://confluence.atlassian.com/display/JIRA/What+is+an+Issue#
"Bug" => "Bug", # A problem which impairs or prevents the functions of the product.
"Improvement" => "Feature", # An enhancement to an existing feature.
"New Feature" => "Feature", # A new feature of the product.
"Epic" => "Feature", # A task that needs to be done.
"Story" => "Feature", # A task that needs to be done.
"Task" => "Feature", # A task that needs to be done.
"Technical task" => "Feature", # A task that needs to be done.
"QA task" => "Feature", # A task that needs to be done.
"Custom Issue" => "Support" # A custom issue type, as defined by your organisation if required.
}
ISSUE_STATUS_MARKER = "(choose a Redmine Issue Status)"
DEFAULT_ISSUE_STATUS_MAP = {
# Default map from Jira (key) to Redmine (value)
# the comments on right side are Jira definitions - http://confluence.atlassian.com/display/JIRA/What+is+an+Issue#
"Open" => "New", # This issue is in the initial 'Open' state, ready for the assignee to start work on it.
"In Progress" => "In Progress", # This issue is being actively worked on at the moment by the assignee.
"Resolved" => "Resolved", # A Resolution has been identified or implemented, and this issue is awaiting verification by the reporter. From here, issues are either 'Reopened' or are 'Closed'.
"Reopened" => "New", # This issue was once 'Resolved' or 'Closed', but is now being re-examined. (For example, an issue with a Resolution of 'Cannot Reproduce' is Reopened when more information becomes available and the issue becomes reproducible). From here, issues are either marked In Progress, Resolved or Closed.
"Closed" => "Closed", # This issue is complete. ## Be careful to choose one which a "issue closed" attribute marked :-)
"In Test" => "In Test",
"Verified" => "Verified"
}
ISSUE_PRIORITY_MARKER = "(choose a Redmine Enumeration Issue Priority)"
DEFAULT_ISSUE_PRIORITY_MAP = {
# Default map from Jira (key) to Redmine (value)
# the comments on right side are Jira definitions - http://confluence.atlassian.com/display/JIRA/What+is+an+Issue#
"Blocker" => "Blocker", # Highest priority. Indicates that this issue takes precedence over all others.
"Critical" => "Urgent", # Indicates that this issue is causing a problem and requires urgent attention.
"Major" => "High", # Indicates that this issue has a significant impact.
"Minor" => "Normal", # Indicates that this issue has a relatively minor impact.
"Trivial" => "Low", # Lowest priority.
}
# Xml file holder
$doc = nil
# A dummy Redmine user to use in place of JIRA users who have been deleted.
# This user is lazily migrated only if needed.
$GHOST_USER = nil
# Jira projects to ignore during import
$IGNORED_PROJECTS = ['Demo', 'Test']
# Mapping between Jira Issue Type and Jira Issue Type Id - key = Id, value = Type
$MIGRATED_ISSUE_TYPES_BY_ID = {}
# Mapping between Jira Issue Status and Jira Issue Status Id - key = Id, value = Status
$MIGRATED_ISSUE_STATUS_BY_ID = {}
# Mapping between Jira Issue Priority and Jira Issue Priority Id - key = Id, value = Priority
$MIGRATED_ISSUE_PRIORITIES_BY_ID = {}
# Mapping between Jira Issue Type and Redmine Issue Type - key = Jira, value = Redmine
$MIGRATED_ISSUE_TYPES = {}
# Mapping between Jira Issue Status and Redmine Issue Status - key = Jira, value = Redmine
$MIGRATED_ISSUE_STATUS = {}
# Mapping between Jira Issue Priorities and Redmine Issue Priorities - key = Jira, value = Redmine
$MIGRATED_ISSUE_PRIORITIES = {}
# Migrated Users by Name.
$MIGRATED_USERS_BY_NAME = {}
# those maps are for parsing attachments optimisation. My jira xml was huge ~7MB, and parsing it for each attachment lasted for ever.
# Now needed data are parsed once and put into those maps, which makes all things much faster.
$MAP_ISSUE_TO_PROJECT_KEY = {}
$MAP_PROJECT_ID_TO_PROJECT_KEY = {}
# gets all mapping options
def self.get_all_options()
# return all options
# Issue Type, Issue Status, Issue Priority
ret = {}
ret["types"] = self.get_jira_issue_types()
ret["status"] = self.get_jira_statuses()
ret["priorities"] = self.get_jira_priorities()
return ret
end
# Get or create Ghost (Dummy) user which will be used for jira issues if no corresponding user found
def self.use_ghost_user
ghost = User.find_by_login('deleted-user')
if ghost.nil?
puts "Creating ghost user to represent deleted JIRA users. Login name = deleted-user"
ghost = User.new({ :firstname => 'Deleted',
:lastname => 'User',
:mail => '[email protected]',
:password => 'deleteduser123' })
ghost.login = 'deleted-user'
ghost.lock # disable the user
ghost.save!
ghost.reload
end
$GHOST_USER = ghost
ghost
end
def self.find_version_by_jira_id(jira_id)
end
def self.find_user_by_jira_name(jira_name)
user = $MIGRATED_USERS_BY_NAME[jira_name]
if user.nil?
# User has not been migrated. Probably a user who has been deleted from JIRA.
# Select or create the ghost user and use him instead.
user = use_ghost_user
end
user
end
def self.get_list_from_tag(xpath_query)
# Get a tag node and get all attributes as a hash
ret = []
# $doc.elements.each(xpath_query) {|node| ret.push(node.attributes.rehash)}
$doc.xpath(xpath_query).each {|node|
nm = node.attr("name")
ret.push(Hash[node.attributes.map { |k,v| [k,v.content]}])}
#ret.push(node.attributes.rehash)}
return ret
end
def self.migrate_fixed_versions()
path = "/*/NodeAssociation[@sourceNodeEntity=\"Issue\" and @sinkNodeEntity=\"Version\" and @associationType=\"IssueFixVersion\"]"
associations = JiraMigration.get_list_from_tag(path)
associations.each do |assoc|
version = JiraVersion::MAP[assoc['sinkNodeId']]
issue = JiraIssue::MAP[assoc['sourceNodeId']]
issue.update_column(:fixed_version_id, version.id)
issue.reload
end
end
def self.migrate_membership()
memberships = self.get_list_from_tag('/*/Membership[@membershipType="GROUP_USER"]')
memberships.each do |m|
user = User.find_by_login(m['lowerChildName'])
if user.nil? or user == $GHOST_USER
users = self.get_list_from_tag("/*/User[@lowerUserName=\"%s\"]" % m['lowerChildName'])
if !users.nil? and !users.empty?
user = User.find_by_mail(users[0]['emailAddress'])
end
end
group = Group.find_by_lastname(m['lowerParentName'])
if !user.nil? and !group.nil?
if !group.users.exists?(user.id)
group.users << user
end
end
end
end
def self.migrate_issue_links()
# Issue Link Types
issue_link_types = self.get_list_from_tag('/*/IssueLinkType')
# migrated_issue_link_types = {"jira issuelink type" => "redmine link type"}
migrated_issue_link_types = {}
issue_link_types.each do |linktype|
migrated_issue_link_types[linktype['id']] = DEFAULT_ISSUELINK_TYPE_MAP.fetch(linktype['linkname'], ISSUELINK_TYPE_MARKER)
end
# Set Issue Links
issue_links = self.get_list_from_tag('/*/IssueLink')
issue_links.each do |link|
linktype = migrated_issue_link_types[link['linktype']]
issue_from = JiraIssue::MAP[link['source']]
issue_to = JiraIssue::MAP[link['destination']]
if linktype.downcase == 'subtask' or linktype.downcase == 'epic-story'
pp "Set Parent #{issue_from.id} to:", issue_to
to_updated_on = issue_to.updated_on
from_updated_on = issue_from.updated_on
issue_to.update_attribute(:parent_issue_id, issue_from.id)
issue_to.reload
issue_to.update_column :updated_on, to_updated_on
issue_from.update_column :updated_on, from_updated_on
issue_to.reload
issue_from.reload
else
r = IssueRelation.new(:relation_type => linktype, :issue_from => issue_from, :issue_to => issue_to)
r.save!
r.reload
end
end
end
def self.migrate_worktime()
# Set Issue Links
worklogs = self.get_list_from_tag('/*/Worklog')
worklogs.each do |log|
issue = JiraIssue::MAP[log['issue']]
user = JiraMigration.find_user_by_jira_name(log['author'])
TimeEntry.create!(:user => user, :issue_id => issue.id, :project_id => issue.project.id,
:hours => (log['timeworked'].to_s.empty? ? 0 : log['timeworked'].to_f / 3600),
:comments => log['body'].to_s.truncate(250, separator: ' '),
:spent_on => Time.parse(log['startdate']),
:created_on => Time.parse(log['created']),
:activity_id => TimeEntryActivity.find_by_name('Development').id)
end
end
def self.get_jira_issue_types()
# Issue Type
issue_types = self.get_list_from_tag('/*/IssueType')
# migrated_issue_types = {"jira_type" => "redmine tracker"}
migrated_issue_types = {}
issue_types.each do |issue|
migrated_issue_types[issue["name"]] = DEFAULT_ISSUE_TYPE_MAP.fetch(issue["name"], ISSUE_TYPE_MARKER)
$MIGRATED_ISSUE_TYPES_BY_ID[issue["id"]] = issue["name"]
end
return migrated_issue_types
end
def self.get_jira_statuses()
# Issue Status
issue_status = self.get_list_from_tag('/*/Status')
# migrated_issue_status = {"jira_status" => "redmine status"}
migrated_issue_status = {}
issue_status.each do |issue|
migrated_issue_status[issue["name"]] = DEFAULT_ISSUE_STATUS_MAP.fetch(issue["name"], ISSUE_STATUS_MARKER)
$MIGRATED_ISSUE_STATUS_BY_ID[issue["id"]] = issue["name"]
end
return migrated_issue_status
end
def self.get_jira_priorities()
# Issue Priority
issue_priority = self.get_list_from_tag('/*/Priority')
# migrated_issue_priority = {"jira_priortiy" => "redmine priority"}
migrated_issue_priority = {}
issue_priority.each do |issue|
migrated_issue_priority[issue["name"]] = DEFAULT_ISSUE_PRIORITY_MAP.fetch(issue["name"], ISSUE_PRIORITY_MARKER)
$MIGRATED_ISSUE_PRIORITIES_BY_ID[issue["id"]] = issue["name"]
end
return migrated_issue_priority
end
# Parse jira xml for Users and attributes and return new User record
def self.parse_jira_users()
users = []
# For users in Redmine we need:
# First Name, Last Name, E-mail, Password
#<User id="110" directoryId="1" userName="userName" lowerUserName="username" active="1" createdDate="2013-08-14 13:07:57.734" updatedDate="2013-09-29 21:52:19.776" firstName="firstName" lowerFirstName="firstname" lastName="lastName" lowerLastName="lastname" displayName="User Name" lowerDisplayName="user name" emailAddress="[email protected]" lowerEmailAddress="[email protected]" credential="" externalId=""/>
# $doc.elements.each('/*/User') do |node|
$doc.xpath('/*/User').each do |node|
if(node['emailAddress'] =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
if !node['firstName'].to_s.empty? and !node['lastName'].to_s.empty? and node['active'].to_s == '1'
user = JiraUser.new(node)
# Set user names (first name, last name)
#user.jira_firstName = node["firstName"]
#user.jira_lastName = node["lastName"]
# Set email address
user.jira_emailAddress = node["lowerEmailAddress"]
user.jira_name = node["lowerUserName"]
users.push(user)
puts "Found JIRA user: #{user.jira_name}"
end
end
end
return users
end
# Parse jira xml for Group and attributes and return new Group record
def self.parse_jira_groups()
groups = []
#<Group id="30" groupName="developers" lowerGroupName="developers" active="1" local="0" createdDate="2011-05-08 15:47:01.492" updatedDate="2011-05-08 15:47:01.492" type="GROUP" directoryId="1"/>
$doc.xpath('/*/Group').each do |node|
group = JiraGroup.new(node)
groups.push(group)
pp 'Found JIRA group:',group.jira_lowerGroupName
end
return groups
end
def self.parse_projects()
# PROJECTS:
# for project we need (identifies, name and description)
# in exported data we have name and key, in Redmine name and descr. will be equal
# the key will be the identifier
projs = []
# $doc.elements.each('/*/Project') do |node|
$doc.xpath('/*/Project').each do |node|
proj = JiraProject.new(node)
projs.push(proj)
end
migrated_projects = {}
projs.each do |p|
#puts "Name and descr.: #{p.red_name} and #{p.red_description}"
#puts "identifier: #{p.red_identifier}"
migrated_projects[p.jira_id] = p
end
#puts migrated_projects
return projs
end
def self.parse_versions()
ret = []
# $doc.elements.each('/*/Action[@type="comment"]') do |node|
$doc.xpath('/*/Version').each do |node|
comment = JiraVersion.new(node)
ret.push(comment)
end
return ret
end
def self.parse_issues()
ret = []
# $doc.elements.collect('/*/Issue'){|i|i}.sort{|a,b|a.attribute('key').to_s<=>b.attribute('key').to_s}.each do |node|
$doc.xpath('/*/Issue').collect{|i|i}.sort{|a,b|a.attribute('key').to_s<=>b.attribute('key').to_s}.each do |node|
issue = JiraIssue.new(node)
ret.push(issue)
end
return ret
end
def self.parse_comments()
ret = []
# $doc.elements.each('/*/Action[@type="comment"]') do |node|
$doc.xpath('/*/Action[@type="comment"]').each do |node|
comment = JiraComment.new(node)
ret.push(comment)
end
return ret
end
def self.parse_attachments()
attachs = []
# $doc.elements.each('/*/FileAttachment') do |node|
$doc.xpath('/*/FileAttachment').each do |node|
attach = JiraAttachment.new(node)
attachs.push(attach)
end
return attachs
end
end
namespace :jira_migration do
task :load_xml => :environment do
file = File.new(JiraMigration::ENTITIES_FILE, 'r:utf-8')
# doc = REXML::Document.new(file)
doc = Nokogiri::XML(file,nil,'utf-8')
$doc = doc
$MIGRATED_USERS_BY_NAME = Hash[User.all.map{|u|[u.login, u]}] #{} # Maps the Jira username to the Redmine Rails User object
# $doc.elements.each("/*/Project") do |p|
$doc.xpath("/*/Project").each do |p|
$MAP_PROJECT_ID_TO_PROJECT_KEY[p['id']] = p['key']
end
#$doc.elements.each("/*/Issue") do |i|
$doc.xpath("/*/Issue").each do |i|
$MAP_ISSUE_TO_PROJECT_KEY[i["id"]] = { :project_key => $MAP_PROJECT_ID_TO_PROJECT_KEY[i["project"]], :issue_key => i['key']}
end
end
desc "Generates the configuration for the map things from Jira to Redmine"
task :generate_conf => [:environment, :load_xml] do
conf_file = JiraMigration::CONF_FILE
conf_exists = File.exists?(conf_file)
if conf_exists
puts "You already have a conf file"
print "You want overwrite it ? [y/N] "
overwrite = STDIN.gets.match(/^y$/i)
end
if !conf_exists or overwrite
# Let's give the user all options to fill out
options = JiraMigration.get_all_options()
File.open(conf_file, "w"){ |f| f.write(options.to_yaml) }
puts "This migration script needs the migration table to continue "
puts "Please... fill the map table on the file: '#{conf_file}' and run again the script"
puts "To start the options again, just remove the file '#{conf_file} and run again the script"
exit(0)
end
end
desc "Gets the configuration from YAML"
task :pre_conf => [:environment, :load_xml] do
conf_file = JiraMigration::CONF_FILE
conf_exists = File.exists?(conf_file)
if !conf_exists
Rake::Task['jira_migration:generate_conf'].invoke
end
$confs = YAML.load_file(conf_file)
end
desc "Migrates Jira Users to Redmine Users"
task :migrate_users => [:environment, :pre_conf] do
users = JiraMigration.parse_jira_users()
users.each do |u|
#pp(u)
user = User.find_by_mail(u.jira_emailAddress)
if user.nil?
new_user = u.migrate
#new_user.update_attribute :must_change_passwd, true
end
end
puts "Migrated Users"
end
desc "Migrates Jira Group to Redmine Group"
task :migrate_groups => [:environment, :pre_conf] do
groups = JiraMigration.get_list_from_tag('/*/Group')
groups.each do |group|
#pp(u)
g = Group.find_by_lastname(group['lowerGroupName'])
if g.nil?
g = Group.new(lastname: group['lowerGroupName'])
end
g.save!
g.reload
end
puts "Migrated Groups"
JiraMigration.migrate_membership
puts "Migrated Membership"
end
desc "Migrates Jira Issue Types to Redmine Trackes"
task :migrate_issue_types => [:environment, :pre_conf] do
JiraMigration.get_jira_issue_types()
types = $confs["types"]
types.each do |key, value|
t = Tracker.find_by_name(value)
if t.nil?
Tracker.new(name: value)
end
t.save!
t.reload
$MIGRATED_ISSUE_TYPES[key] = t
end
puts "Migrated issue types"
end
desc "Migrates Jira Issue Status to Redmine Status"
task :migrate_issue_status => [:environment, :pre_conf] do
JiraMigration.get_jira_statuses()
status = $confs["status"]
status.each do |key, value|
s = IssueStatus.find_by_name(value)
if s.nil?
s = IssueStatus.new(name: value)
end
s.save!
s.reload
$MIGRATED_ISSUE_STATUS[key] = s
end
puts "Migrated issue status"
end
desc "Migrates Jira Issue Priorities to Redmine Priorities"
task :migrate_issue_priorities => [:environment, :pre_conf] do
JiraMigration.get_jira_priorities()
priorities = $confs["priorities"]
priorities.each do |key, value|
p = IssuePriority.find_by_name(value)
if p.nil?
p = IssuePriority.new(name: value)
end
p.save!
p.reload
$MIGRATED_ISSUE_PRIORITIES[key] = p
end
puts "Migrated issue priorities"
end
desc "Migrates Jira Projects to Redmine Projects"
task :migrate_projects => :environment do
projects = JiraMigration.parse_projects()
projects.reject!{|project|$IGNORED_PROJECTS.include?(project.red_name)}
projects.each do |p|
p.migrate
end
end
desc "Migrates Jira Versions to Redmine Versions"
task :migrate_versions => :environment do
versions = JiraMigration.parse_versions()
versions.reject!{|version|version.red_project.nil?}
versions.each do |i|
i.migrate
end
end
desc "Migrates Jira Issues to Redmine Issues"
task :migrate_issues => :environment do
issues = JiraMigration.parse_issues()
issues.reject!{|issue|issue.red_project.nil?}
issues.each do |i|
i.migrate
end
JiraMigration.migrate_fixed_versions
JiraMigration.migrate_issue_links
JiraMigration.migrate_worktime
end
desc "Migrates Jira Issues Comments to Redmine Issues Journals (Notes)"
task :migrate_comments => :environment do
comments = JiraMigration.parse_comments()
comments.reject!{|comment|comment.red_journalized.nil?}