|
# updated feb-09
|
|
# bmishkin@sagebit.com
|
|
#
|
|
# to install up-to-date lighthouse api gem:
|
|
# gem install Caged-lighthouse-api --source=http://gems.github.com
|
|
#
|
|
# to import a project, create identically named redmine project
|
|
# to enable issue state -> ticket status, you need to create identically-named statuses in redmine before starting
|
|
|
|
require 'lighthouse-api'
|
|
namespace :import do
|
|
task :lighthouse => :environment do
|
|
|
|
Lighthouse.account = ENV["LH_ACCOUNT"]
|
|
Lighthouse.token = ENV["LH_TOKEN"]
|
|
|
|
users = User.find(:all)
|
|
find_user = lambda do |lh_user|
|
|
names = lh_user.name.split(/\s/)
|
|
names[1] = "Unknown" if !names[1] || names[1].strip.blank?
|
|
result = User.find(:all).detect {|u|
|
|
"#{u.firstname} #{u.lastname}" == names.join(' ')
|
|
}
|
|
unless result
|
|
result = User.find_by_admin(1) unless ENV['CREATE_USERS']
|
|
end
|
|
unless result
|
|
begin
|
|
login = names.first.downcase.gsub(/[^\w]/, '') +
|
|
(rand * 1000 + 100).to_s.first(3)
|
|
|
|
result = User.new(
|
|
:firstname => names.shift,
|
|
:lastname => names.join(' '),
|
|
:mail => "#{login}@fixme.com"
|
|
)
|
|
result.login = login
|
|
result.password = result.password_confirmation = login
|
|
result.save!
|
|
puts "Created a user: " +
|
|
"#{result.firstname} #{result.lastname} (#{result.login})"
|
|
rescue => e
|
|
raise "Failed creating user from: #{lh_user.inspect} -- #{e.inspect}"
|
|
end
|
|
end
|
|
result
|
|
end
|
|
|
|
cat_for_tag = lambda do |project, tag|
|
|
project.issue_categories.find_by_name(tag) ||
|
|
project.issue_categories.create(:name => tag)
|
|
end
|
|
|
|
# TODO: attempt to match priorities somehow?
|
|
priority = Enumeration.find_by_opt_and_name('IPRI', 'Normal')
|
|
|
|
puts "Identifying matching projects..."
|
|
projects = Lighthouse::Project.find(:all).collect do |lh_project|
|
|
rm_project = Project.find_by_name(lh_project.name)
|
|
puts "#{lh_project.name} => #{rm_project || '???'}"
|
|
|
|
# NOTE: disabled because creating projects involves a number of decisions
|
|
# about trackers, issue statuses, workflows, users, modules, etc that we
|
|
# don't want to get into.
|
|
#
|
|
#if rm_project
|
|
# puts "#{lh_project.name} => #{rm_project}"
|
|
#else
|
|
# rm_project = Project.create!(
|
|
# :name => lh_project.name,
|
|
# :identifier => lh_project.name.downcase.gsub(/[^\w]/, '-').first(20),
|
|
# :created_on => lh_project.created_at,
|
|
# :updated_on => lh_project.updated_at,
|
|
# :is_public => lh_project.public
|
|
# )
|
|
# puts "#{lh_project.name} => #{rm_project} [CREATED]"
|
|
#end
|
|
|
|
{lh_project => rm_project}
|
|
end
|
|
|
|
project_pairs = projects.inject({}) {|acc, hash| acc.update(hash)}
|
|
|
|
project_pairs.each_pair do |lh_project, rm_project|
|
|
next unless rm_project
|
|
|
|
if rm_project.trackers.empty?
|
|
raise "No trackers found for #{project.name!}. Cannot create an issue."
|
|
end
|
|
|
|
lh_project.milestones.each do |milestone|
|
|
next if rm_project.versions.find_by_name(milestone.title)
|
|
v = rm_project.versions.create!(
|
|
:name => milestone.title,
|
|
:description => milestone.goals,
|
|
:effective_date => milestone.due_on ? milestone.due_on.to_date : nil,
|
|
:created_on => milestone.created_at,
|
|
:updated_on => milestone.updated_at
|
|
)
|
|
puts "Created new target version for #{rm_project.name}: #{v.name}"
|
|
end
|
|
|
|
pg = 0
|
|
while (tix = lh_project.tickets(:page => (pg += 1), :q => 'sort:number status:any')).length > 0
|
|
puts "Chewing tickets on page #{pg}"
|
|
tix.each do |ticket_lite|
|
|
ticket = Lighthouse::Ticket.find(
|
|
ticket_lite.to_param,
|
|
:params => ticket_lite.instance_variable_get(:@prefix_options)
|
|
)
|
|
next if Issue.find_by_subject(ticket.title)
|
|
first_version = ticket.versions.shift
|
|
|
|
ticket_creator = Lighthouse::User.find(ticket.user_id) unless ticket.user_id.blank?
|
|
ticket_assignee = Lighthouse::User.find(ticket.assigned_user_id) unless ticket.assigned_user_id.blank?
|
|
issue = Issue.new
|
|
issue.project = rm_project
|
|
issue.tracker = rm_project.trackers.first
|
|
issue.author = find_user.call(ticket_creator) unless ticket_creator.blank?
|
|
issue.assigned_to = find_user.call(ticket_assignee) unless ticket_assignee.blank?
|
|
issue.priority = priority
|
|
issue.status = IssueStatus.find(
|
|
:first,
|
|
:conditions => ["UCASE(name) = ?", ticket.state.upcase]
|
|
) || IssueStatus.default
|
|
issue.fixed_version = rm_project.versions.find_by_name(
|
|
lh_project.milestones.detect {|ms| ms.id == ticket.milestone_id}.title
|
|
) unless ticket.milestone_id.blank?
|
|
issue.attributes = {
|
|
:subject => ticket.title,
|
|
:description => first_version.body || "[No description]",
|
|
:done_ratio => 0,
|
|
:created_on => ticket.created_at,
|
|
:updated_on => ticket.updated_at
|
|
}
|
|
|
|
if ticket.tags && ticket.tags.any?
|
|
issue.category = cat_for_tag.call(rm_project, ticket.tags.first)
|
|
end
|
|
|
|
if issue.save
|
|
puts "Added ticket [##{ticket.number}] to #{rm_project.name}: '#{issue.subject}' : #{ticket.state.upcase}"
|
|
else
|
|
puts "Failed to add issue: #{issue.inspect} - " +
|
|
issue.errors.full_messages.join(', ')
|
|
end
|
|
|
|
ticket.versions.each do |version|
|
|
next if version.body.blank?
|
|
|
|
version_author = Lighthouse::User.find(version.user_id)
|
|
issue.instance_variable_set(:@current_journal, nil)
|
|
issue.init_journal(find_user.call(version_author), version.body)
|
|
cj = issue.instance_variable_get(:@current_journal)
|
|
cj.created_on = version.created_at
|
|
issue.save!
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|