Project

General

Profile

Feature #1385 » migrate_from_jira.rake

Tom Rochette, 2011-06-04 19:09

 
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require 'active_record'
19
require 'iconv'
20
require 'pp'
21
require 'enumerator'
22

    
23
namespace :redmine do
24
	desc 'Jira migration script'
25
	task :migrate_from_jira => :environment do
26
		
27
		module JiraMigrate
28
			if IssuePriority.all == []
29
				["Trivial","Minor","Major","Critical","Blocker"].each {|t| IssuePriority.create(:name => t)}
30
				default = IssuePriority.find_by_name("Trivial")
31
				default.is_default = true
32
				default.save
33
			end
34
			priorities = IssuePriority.all
35
			DEFAULT_PRIORITY = priorities[0]
36

    
37
			DEFAULT_STATUS = (IssueStatus.default ? IssueStatus.default : IssueStatus.create(:name => "Open", :is_default => true))
38
			assigned_status = IssueStatus.find_or_create_by_name("Assigned")
39
			resolved_status = IssueStatus.find_or_create_by_name("Resolved")
40
			feedback_status = IssueStatus.find_or_create_by_name("Feedback")
41
			closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
42
			closed_status = IssueStatus.create(:name => "closed", :is_closed => true) unless closed_status # added this if the status did not exist
43

    
44

    
45
			STATUS_MAPPING = {'open' => DEFAULT_STATUS,
46
												'reopened' => feedback_status,
47
												'resolved' => resolved_status,
48
												'in progress' => assigned_status,
49
												'closed' => closed_status
50
												}
51

    
52
#			TRACKER_BUG = Tracker.find_by_position(1)
53
#			TRACKER_FEATURE = Tracker.find_by_position(2)
54
#			TRACKER_TASK = Tracker.find_by_position(4)
55
			TRACKER_BUG = Tracker.find_or_create_by_name("Bug")
56
			TRACKER_FEATURE = Tracker.find_or_create_by_name("Feature")
57
			TRACKER_TASK = Tracker.find_or_create_by_name("Task")
58
			DEFAULT_TRACKER = TRACKER_BUG
59
			TRACKER_MAPPING = {'bug' => TRACKER_BUG,
60
												 'enhancement' => TRACKER_FEATURE,
61
												 'task' => TRACKER_TASK,
62
												 'new feature' =>TRACKER_FEATURE
63
												 }
64
			DEFAULT_TRACKER = TRACKER_BUG
65

    
66
			PRIORITY_MAPPING = {'trivial' => priorities[0],
67
													'minor' => priorities[1],
68
													'major' => priorities[2],
69
													'critical' => priorities[3],
70
													'blocker' => priorities[4]
71
													}
72
			if !Role.find_all_by_builtin(0)
73
				Role.create(:name => "Manager")
74
				Role.create(:name => "Developer")
75
			end
76
			roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
77
			manager_role = roles[0]
78
			developer_role = roles[1]
79
			DEFAULT_ROLE = roles.last
80
			ROLE_MAPPING = {'admin' => manager_role,
81
											'developer' => developer_role
82
											}
83

    
84
			class JiraIssue
85
				TITLE_RX = /^\[([^\]]+)-(\d+)\]\s*(.*)$/o
86

    
87
				attr_reader :node
88

    
89
				def initialize node
90
					@node = node
91
				end
92

    
93
				def [] name
94
					node.elements[name.to_s]
95
				end
96

    
97
				def method_missing name, *args
98
					if name.to_s =~ /^[a-z]+$/ and args.empty?
99
						n = self[name]
100
						return n ? n.text : nil
101
					end
102
					super
103
				end
104

    
105
				def project_id
106
					method_missing(:title)[TITLE_RX, 1]
107
				end
108

    
109
				def issue_id
110
					method_missing(:title)[TITLE_RX, 2].to_i
111
				end
112

    
113
				def title
114
					method_missing(:title)[TITLE_RX, 3]
115
				end
116
				
117
				def type
118
					method_missing :type
119
				end
120

    
121
				def inspect
122
					"#<#{self.class} project_id=%p issue_id=%d title=%p>" % [project_id, issue_id, title]
123
				end
124
			end
125
			
126
			def self.find_or_create_user(username, fullname, project=nil)
127
				if username == '-1'
128
					return nil
129
				end
130
				
131
				u = User.find_by_login(username)
132
				if !u
133
					# Create a new user if not found
134
					mail = username[0,limit_for(User, 'mail')]
135
					mail = "#{mail}@foo.bar" unless mail.include?("@")
136
					if fullname
137
					  firstname, lastname = fullname.split ' ', 2 
138
				  else
139
				    firstname = lastname = username
140
			    end
141
			    firstname = firstname || username
142
			    lastname = lastname || username
143
					u = User.new :firstname => firstname[0,limit_for(User, 'firstname')],
144
											 :lastname => lastname[0,limit_for(User, 'lastname')],
145
											 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
146
					u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
147
					u.password = 'jira'
148
					# finally, a default user is used if the new user is not valid
149
					u = User.find(:first) unless u.save
150
				end
151
				# Make sure he is a member of the project
152
				if project && !u.member_of?(project)
153
					roles = [DEFAULT_ROLE]
154
					roles = [ROLE_MAPPING['admin']] if u.admin
155
					Member.create(:user => u, :project => project, :roles => roles)
156
					u.reload
157
				end
158
				u
159
			end
160

    
161
			def self.clean_html html
162
				text = html.
163
					# normalize whitespace
164
					gsub(/\s+/m, ' ').
165
					# add in line breaks
166
					gsub(/<br.*?>\s*/i, "\n").
167
					# remove all tags
168
					gsub(/<.*?>/, ' ').
169
					# handle entities
170
					gsub(/&amp;/, '&').gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&nbsp;/, ' ').gsub(/&quot;/, '"').
171
					# clean up
172
					squeeze(' ').gsub(/ *$/, '').strip
173
#				puts "cleaned html from #{html.inspect} to #{text.inspect}"
174
				text
175
			end
176

    
177
			def self.migrate
178
				migrated_projects = 0
179
				migrated_components = 0
180
				migrated_issues = 0
181

    
182
				open issue_xml do |file|
183
				  puts "loading file"
184
					doc = REXML::Document.new file
185
					item_nodes = doc.elements.to_enum :each, '/rss/channel/item'
186
					puts "items enumerated"
187
					issues = item_nodes.map { |item_node| JiraIssue.new item_node }
188
					issues_by_project = issues.group_by { |issue| issue.project_id }
189

    
190
					# Projects
191
					print "Migrating projects"
192
					project_from_project_id = {}
193
					issues_by_project.keys.sort.each do |project_id|
194
						print '.'
195
						STDOUT.flush
196
						identifier = project_id.downcase #+ '-' + @target_project.id.to_s
197
						project = Project.find_by_identifier(identifier)
198
						if !project
199
							# create the target project
200
							project = Project.new :name => identifier.humanize,
201
																		:description => "Imported project from jira (#{identifier.upcase})." #identifier.humanize
202
							project.identifier = identifier
203
							puts "Unable to create a sub project with identifier '#{identifier}'!" unless project.save
204
							project.move_to_child_of(@target_project.id)
205
							# enable issues for the created project
206
							project.enabled_module_names = ['issue_tracking']
207
						end
208
						project.trackers << TRACKER_BUG unless project.trackers.find(TRACKER_BUG)
209
						project.trackers << TRACKER_FEATURE unless project.trackers.find(TRACKER_FEATURE)
210
						project.trackers << TRACKER_TASK unless project.trackers.find(TRACKER_TASK)
211
						project_from_project_id[project_id] = project
212
						migrated_projects += 1
213
					end
214
					puts
215

    
216
					# Components
217
					print "Migrating components"
218
					component_from_project_and_name = {}
219
					issues_by_project.each do |project_id, project_issues|
220
						components = project_issues.map { |issue| issue.component }.flatten.uniq.compact
221
						components.each do |component|
222
							print '.'
223
							STDOUT.flush
224
							c = IssueCategory.new :project => project_from_project_id[project_id],
225
																		:name => encode(component[0, limit_for(IssueCategory, 'name')])
226
							next unless c.save
227
							component_from_project_and_name[[project_id, component]] = c
228
							migrated_components += 1
229
						end
230
					end
231
					puts
232

    
233
#{:status=>["Open", "Closed", "In Progress", "Resolved"]}
234
#{:priority=>["High", "Critical", "Medium"]}
235
#{:type=>["Bug", "Task", "Enhancement", "New Feature"]}
236

    
237
					#p :status => issues.map { |i| i.status }.uniq
238
					#p :priority => issues.map { |i| i.priority }.uniq
239
					#p :type => issues.map { |i| i.method_missing(:type) }.uniq
240

    
241
					# Issues
242
					print "Migrating issues "
243
					issues_by_project.each do |project_id, project_issues|
244
						project = project_from_project_id[project_id]
245
						project_issues.each do |issue|
246
							print '.'
247
							STDOUT.flush
248

    
249

    
250
							i = Issue.new :project => project,
251
														:subject => encode(issue.title[0, limit_for(Issue, 'subject')]),
252
														:description => clean_html(issue.description || issue.title), #convert_wiki_text(encode(ticket.description)),
253
														:priority => PRIORITY_MAPPING[issue.priority.to_s.downcase] || DEFAULT_PRIORITY,
254
														:created_on => issue.created ? Time.parse(issue.created) : Time.now,
255
														:updated_on => issue.updated ? Time.parse(issue.updated) : Time.now
256

    
257
							#puts "I : #{i.inspect}"
258
							i.status = STATUS_MAPPING[issue.status.to_s.downcase] || DEFAULT_STATUS
259
							i.tracker = TRACKER_MAPPING[issue.type.to_s.downcase] || DEFAULT_TRACKER
260

    
261
							if reporter = issue['reporter']
262
								if reporter.attributes['username'] == "-1" or reporter.attributes['username'] == -1
263
									username = "unknown"
264
									name = "Unknown"
265
								else
266
									username = reporter.attributes['username']
267
									name = reporter.text
268
								end
269
								i.author = find_or_create_user username, name
270
							end
271

    
272
							i.category = component_from_project_and_name[[project_id, issue.component]] if issue.component
273

    
274
							i.save!
275

    
276
							# Assignee
277
							if assignee = issue['assignee']
278
								if assignee.attributes["username"] == "-1" or assignee.attributes["username"] == -1
279
									username = "unassigned"
280
									name = "Unassigned"
281
								else
282
									username = assignee.attributes['username']
283
									name = assignee.text
284
								end
285
								i.assigned_to = find_or_create_user username, name, project
286
								i.save
287
							end
288

    
289
							# force the issue update date back. it gets overwritten on save time.
290
							Issue.connection.execute <<-end
291
								update issues set updated_on = '#{Time.parse(issue.updated).to_s :db}' where id = #{i.id}
292
							end
293

    
294
							migrated_issues += 1
295

    
296
							# Comments
297
							if comments = issue.node.elements['comments']
298
								last_comment = '1'
299
								comments.elements.to_a('.//comment').each do |comment|
300
									author, created = comment.attributes['author'], comment.attributes['created']
301
									comment_text = clean_html comment.text
302
									n = Journal.new :notes => comment_text,
303
																	:created_on => DateTime.parse(created)
304
									n.user = find_or_create_user(author, "#{author.capitalize} -")
305
									n.journalized = i
306
									n.save unless n.details.empty? && n.notes.blank?
307
								end
308
							end
309
						end
310
					end
311
					puts
312

    
313
					puts
314
					puts "Projects:        #{migrated_projects}/#{issues_by_project.keys.length}"
315
					puts "Components:      #{migrated_components}/#{migrated_components}" # hmmm
316
					puts "Issues:          #{migrated_issues}/#{issues.length}"
317
				end
318
			end
319
			
320
			def self.limit_for(klass, attribute)
321
				klass.columns_hash[attribute.to_s].limit
322
			end
323
			
324
			def self.encoding(charset)
325
				@ic = Iconv.new('UTF-8', charset)
326
			rescue Iconv::InvalidEncoding
327
				puts "Invalid encoding!"
328
				return false
329
			end
330
			
331
      def self.set_issue_xml file
332
        @@issue_xml = file
333
      end
334
      
335
      mattr_reader :issue_xml
336

    
337
			def self.target_project_identifier(identifier)
338
				project = Project.find_by_identifier(identifier)
339
				if !project
340
					# create the target project
341
#					project = Project.new :name => identifier.humanize,
342
#																:description => identifier.humanize
343
					project = Project.new :name => 'Jira Import',
344
																:description => 'All issues imported from jira.'
345
					# project.parent_id
346
					project.identifier = identifier
347
					puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
348
					# enable issues for the created project
349
					project.enabled_module_names = ['issue_tracking']
350
				end        
351
				#project.trackers << TRACKER_BUG
352
				#project.trackers << TRACKER_FEATURE          
353
				@target_project = project.new_record? ? nil : project
354
			end
355
						
356
		private
357
			def self.encode(text)
358
				@ic.iconv text
359
			rescue
360
				text
361
			end
362
		end
363
		
364
		puts
365
		puts "WARNING: a new project will be added to Redmine during this process."
366
		print "Are you sure you want to continue ? [y/N] "
367
		exit 0 unless STDIN.gets.match(/^y$/i)  
368
		puts
369

    
370
		def prompt(text, options = {}, &block)
371
			default = options[:default] || ''
372
			while true
373
				print "#{text} [#{default}]: "
374
				value = STDIN.gets.chomp!
375
				value = default if value.blank?
376
				break if yield value
377
			end
378
		end
379
		
380
		prompt('Jira issue xml file', :default => 'SearchRequest.xml') {|file| JiraMigrate.set_issue_xml file}
381
    		prompt('Target project identifier') {|identifier| JiraMigrate.target_project_identifier identifier}
382
		puts
383

    
384
		JiraMigrate.migrate
385
	end
386
end
(6-6/9)