Skip to content

Instantly share code, notes, and snippets.

@clm-a
Forked from codatory/pitoval_import.rake
Created July 18, 2012 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clm-a/3137578 to your computer and use it in GitHub Desktop.
Save clm-a/3137578 to your computer and use it in GitHub Desktop.
Pivotal Tracker Importer for Redmine
begin
require 'ruport'
rescue LoadError => err
warn "Couldn't load ruport gem: #{err}"
end
# ugly Ruport monkey patching to avoid the call of FasterCSV
module Ruport::Data
class Table
module FromCSV
def get_table_from_csv(msg,param,options={},&block) #:nodoc:
options = {:has_names => true,
:csv_options => {} }.merge(options)
adjust_options_for_fcsv_headers(options)
table = self.new(options) do |feeder|
first_line = true
CSV.send(msg,param,options[:csv_options]) do |row|
if first_line
adjust_for_headers(feeder.data,row,options)
first_line = false
next if options[:has_names]
end
if block
handle_csv_row_proc(feeder,row,options,block)
else
feeder << row
end
end
end
return table
end
end
end
end
namespace :import do
desc 'Import tickets from CSV File'
task :csv => :environment do
csv_file = Ruport::Data::Table.load(Rails.root.join('db', 'seeds', 'import.csv'), :records => true)
csv_file.rename_columns({
'Story' => 'subject',
# 'Story Type' => 'story_type', ## Story type is not available without the Backlogs plugin
'Current State' => 'state',
'Description' => 'description',
'Estimate' => 'estimate'
})
csv_file.each do |row|
issue = Issue.new
story = row.to_hash
issue.project_id = 1
case story['story_type']
when 'chore'
issue.tracker_id = 4
when 'bug'
issue.tracker_id = 1
when 'feature'
issue.tracker_id = 2
else
issue.tracker_id = nil
end
issue.subject = story['subject']
issue.description = story['description']
# issue.story_points = story['estimate']
issue.author = User.first
case story['state']
when 'unscheduled'
issue.status_id = 7
when 'unstarted'
issue.status_id = 1
when 'started'
issue.status_id = 2
when 'finished'
issue.done_ratio = 100
issue.status_id = 3
when 'accepted'
issue.status_id = 5
else
issue.status_id = 8
end
if issue.save
print '.'
else
print "! #{story.id} - #{issue.errors.first} !"
end
end
end
end
@thebeline
Copy link

How does this work? I would like to use this, but am fairly new to ruby... Hence, I don't get the 'gist' of this.... :-D

Quick explanation please? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment