Redmine 3.4.4

This commit is contained in:
Manuel Cillero 2018-02-02 22:19:29 +01:00
commit 64924a6376
2112 changed files with 259028 additions and 0 deletions

97
lib/tasks/ci.rake Normal file
View file

@ -0,0 +1,97 @@
desc "Run the Continuous Integration tests for Redmine"
task :ci do
# RAILS_ENV and ENV[] can diverge so force them both to test
ENV['RAILS_ENV'] = 'test'
RAILS_ENV = 'test'
Rake::Task["ci:setup"].invoke
Rake::Task["ci:build"].invoke
Rake::Task["ci:teardown"].invoke
end
namespace :ci do
desc "Display info about the build environment"
task :about do
puts "Ruby version: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end
desc "Setup Redmine for a new build"
task :setup do
Rake::Task["tmp:clear"].invoke
Rake::Task["log:clear"].invoke
Rake::Task["db:create:all"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:schema:dump"].invoke
if scms = ENV['SCMS']
scms.split(',').each do |scm|
Rake::Task["test:scm:setup:#{scm}"].invoke
end
else
Rake::Task["test:scm:setup:all"].invoke
end
Rake::Task["test:scm:update"].invoke
end
desc "Build Redmine"
task :build do
if test_suite = ENV['TEST_SUITE']
Rake::Task["test:#{test_suite}"].invoke
else
Rake::Task["test"].invoke
end
# Rake::Task["test:ui"].invoke
end
desc "Finish the build"
task :teardown do
end
end
desc "Creates database.yml for the CI server"
file 'config/database.yml' do
require 'yaml'
database = ENV['DATABASE_ADAPTER']
ruby = ENV['RUBY_VER'].gsub('.', '').gsub('-', '')
branch = ENV['BRANCH'].gsub('.', '').gsub('-', '')
dev_db_name = "ci_#{branch}_#{ruby}_dev"
test_db_name = "ci_#{branch}_#{ruby}_test"
case database
when /(mysql|mariadb)/
dev_conf = {'adapter' => 'mysql2',
'database' => dev_db_name, 'host' => 'localhost',
'encoding' => 'utf8'}
if ENV['RUN_ON_NOT_OFFICIAL']
dev_conf['username'] = 'root'
else
dev_conf['username'] = 'jenkins'
dev_conf['password'] = 'jenkins'
end
test_conf = dev_conf.merge('database' => test_db_name)
when /postgresql/
dev_conf = {'adapter' => 'postgresql', 'database' => dev_db_name,
'host' => 'localhost'}
if ENV['RUN_ON_NOT_OFFICIAL']
dev_conf['username'] = 'postgres'
else
dev_conf['username'] = 'jenkins'
dev_conf['password'] = 'jenkins'
end
test_conf = dev_conf.merge('database' => test_db_name)
when /sqlite3/
dev_conf = {'adapter' => (Object.const_defined?(:JRUBY_VERSION) ?
'jdbcsqlite3' : 'sqlite3'),
'database' => "db/#{dev_db_name}.sqlite3"}
test_conf = dev_conf.merge('database' => "db/#{test_db_name}.sqlite3")
when 'sqlserver'
dev_conf = {'adapter' => 'sqlserver', 'database' => dev_db_name,
'host' => 'mssqlserver', 'port' => 1433,
'username' => 'jenkins', 'password' => 'jenkins'}
test_conf = dev_conf.merge('database' => test_db_name)
else
abort "Unknown database"
end
File.open('config/database.yml', 'w') do |f|
f.write YAML.dump({'development' => dev_conf, 'test' => test_conf})
end
end

35
lib/tasks/ciphering.rake Normal file
View file

@ -0,0 +1,35 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace :db do
desc 'Encrypts SCM and LDAP passwords in the database.'
task :encrypt => :environment do
unless (Repository.encrypt_all(:password) &&
AuthSource.encrypt_all(:account_password))
raise "Some objects could not be saved after encryption, update was rolled back."
end
end
desc 'Decrypts SCM and LDAP passwords in the database.'
task :decrypt => :environment do
unless (Repository.decrypt_all(:password) &&
AuthSource.decrypt_all(:account_password))
raise "Some objects could not be saved after decryption, update was rolled back."
end
end
end

13
lib/tasks/deprecated.rake Normal file
View file

@ -0,0 +1,13 @@
def deprecated_task(name, new_name)
task name=>new_name do
$stderr.puts "\nNote: The rake task #{name} has been deprecated, please use the replacement version #{new_name}"
end
end
deprecated_task :load_default_data, "redmine:load_default_data"
deprecated_task :migrate_from_mantis, "redmine:migrate_from_mantis"
deprecated_task :migrate_from_trac, "redmine:migrate_from_trac"
deprecated_task "db:migrate_plugins", "redmine:plugins:migrate"
deprecated_task "db:migrate:plugin", "redmine:plugins:migrate"
deprecated_task :generate_session_store, :generate_secret_token
deprecated_task "test:rdm_routing", "test:routing"

171
lib/tasks/email.rake Normal file
View file

@ -0,0 +1,171 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace :redmine do
namespace :email do
desc <<-END_DESC
Read an email from standard input.
See redmine:email:receive_imap for more options and examples.
END_DESC
task :read => :environment do
Mailer.with_synched_deliveries do
MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV))
end
end
desc <<-END_DESC
Read emails from an IMAP server.
Available IMAP options:
host=HOST IMAP server host (default: 127.0.0.1)
port=PORT IMAP server port (default: 143)
ssl=SSL Use SSL/TLS? (default: false)
starttls=STARTTLS Use STARTTLS? (default: false)
username=USERNAME IMAP account
password=PASSWORD IMAP password
folder=FOLDER IMAP folder to read (default: INBOX)
Processed emails control options:
move_on_success=MAILBOX move emails that were successfully received
to MAILBOX instead of deleting them
move_on_failure=MAILBOX move emails that were ignored to MAILBOX
User and permissions options:
unknown_user=ACTION how to handle emails from an unknown user
ACTION can be one of the following values:
ignore: email is ignored (default)
accept: accept as anonymous user
create: create a user account
no_permission_check=1 disable permission checking when receiving
the email
no_account_notice=1 disable new user account notification
default_group=foo,bar adds created user to foo and bar groups
Issue attributes control options:
project=PROJECT identifier of the target project
status=STATUS name of the target status
tracker=TRACKER name of the target tracker
category=CATEGORY name of the target category
priority=PRIORITY name of the target priority
assigned_to=ASSIGNEE assignee (username or group name)
fixed_version=VERSION name of the target version
private create new issues as private
allow_override=ATTRS allow email content to set attributes values
ATTRS is a comma separated list of attributes
or 'all' to allow all attributes to be overridable
(see below for details)
Overrides:
ATTRS is a comma separated list of attributes among:
* project, tracker, status, priority, category, assigned_to, fixed_version,
start_date, due_date, estimated_hours, done_ratio
* custom fields names with underscores instead of spaces (case insensitive)
Example: allow_override=project,priority,my_custom_field
If the project option is not set, project is overridable by default for
emails that create new issues.
You can use allow_override=all to allow all attributes to be overridable.
Examples:
# No project specified. Emails MUST contain the 'Project' keyword:
rake redmine:email:receive_imap RAILS_ENV="production" \\
host=imap.foo.bar username=redmine@example.net password=xxx
# Fixed project and default tracker specified, but emails can override
# both tracker and priority attributes:
rake redmine:email:receive_imap RAILS_ENV="production" \\
host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
project=foo \\
tracker=bug \\
allow_override=tracker,priority
END_DESC
task :receive_imap => :environment do
imap_options = {:host => ENV['host'],
:port => ENV['port'],
:ssl => ENV['ssl'],
:starttls => ENV['starttls'],
:username => ENV['username'],
:password => ENV['password'],
:folder => ENV['folder'],
:move_on_success => ENV['move_on_success'],
:move_on_failure => ENV['move_on_failure']}
Mailer.with_synched_deliveries do
Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV))
end
end
desc <<-END_DESC
Read emails from an POP3 server.
Available POP3 options:
host=HOST POP3 server host (default: 127.0.0.1)
port=PORT POP3 server port (default: 110)
username=USERNAME POP3 account
password=PASSWORD POP3 password
apop=1 use APOP authentication (default: false)
ssl=SSL Use SSL? (default: false)
delete_unprocessed=1 delete messages that could not be processed
successfully from the server (default
behaviour is to leave them on the server)
See redmine:email:receive_imap for more options and examples.
END_DESC
task :receive_pop3 => :environment do
pop_options = {:host => ENV['host'],
:port => ENV['port'],
:apop => ENV['apop'],
:ssl => ENV['ssl'],
:username => ENV['username'],
:password => ENV['password'],
:delete_unprocessed => ENV['delete_unprocessed']}
Mailer.with_synched_deliveries do
Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV))
end
end
desc "Send a test email to the user with the provided login name"
task :test, [:login] => :environment do |task, args|
include Redmine::I18n
abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
user = User.find_by_login(args[:login])
abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
ActionMailer::Base.raise_delivery_errors = true
begin
Mailer.with_synched_deliveries do
Mailer.test_email(user).deliver
end
puts l(:notice_email_sent, user.mail)
rescue Exception => e
abort l(:notice_email_error, e.message)
end
end
end
end

View file

@ -0,0 +1,22 @@
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :extract_fixtures => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
i = "000"
File.open("#{Rails.root}/#{table_name}.yml", 'w' ) do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
# cast extracted values
ActiveRecord::Base.connection.columns(table_name).each { |col|
record[col.name] = col.type_cast(record[col.name]) if record[col.name]
}
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end

View file

@ -0,0 +1,24 @@
desc 'Generates a secret token for the application.'
file 'config/initializers/secret_token.rb' do
path = File.join(Rails.root, 'config', 'initializers', 'secret_token.rb')
secret = SecureRandom.hex(40)
File.open(path, 'w') do |f|
f.write <<"EOF"
# This file was generated by 'rake generate_secret_token', and should
# not be made visible to public.
# If you have a load-balancing Redmine cluster, you will need to use the
# same version of this file on each machine. And be sure to restart your
# server when you modify this file.
#
# Your secret key for verifying cookie session data integrity. If you
# change this key, all old sessions will become invalid! Make sure the
# secret is at least 30 characters and all random, no regular words or
# you'll be exposed to dictionary attacks.
RedmineApp::Application.config.secret_key_base = '#{secret}'
EOF
end
end
desc 'Generates a secret token for the application.'
task :generate_secret_token => ['config/initializers/secret_token.rb']

View file

@ -0,0 +1,36 @@
desc 'Load Redmine default configuration data. Language is chosen interactively or by setting REDMINE_LANG environment variable.'
namespace :redmine do
task :load_default_data => :environment do
require 'custom_field'
include Redmine::I18n
set_language_if_valid('en')
envlang = ENV['REDMINE_LANG']
if !envlang || !set_language_if_valid(envlang)
puts
while true
print "Select language: "
print valid_languages.collect(&:to_s).sort.join(", ")
print " [#{current_language}] "
STDOUT.flush
lang = STDIN.gets.chomp!
break if lang.empty?
break if set_language_if_valid(lang)
puts "Unknown language!"
end
STDOUT.flush
puts "===================================="
end
begin
Redmine::DefaultData::Loader.load(current_language)
puts "Default configuration data loaded."
rescue Redmine::DefaultData::DataAlreadyLoaded => error
puts error.message
rescue => error
puts "Error: " + error.message
puts "Default configuration data was not loaded."
end
end
end

180
lib/tasks/locales.rake Normal file
View file

@ -0,0 +1,180 @@
desc 'Updates and checks locales against en.yml'
task :locales do
%w(locales:update locales:check_interpolation).collect do |task|
Rake::Task[task].invoke
end
end
namespace :locales do
desc 'Updates language files based on en.yml content (only works for new top level keys).'
task :update do
dir = ENV['DIR'] || './config/locales'
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
files.sort.each do |file|
puts "Updating file #{file}"
file_strings = YAML.load(File.read(file))
file_strings = file_strings[file_strings.keys.first]
missing_keys = en_strings.keys - file_strings.keys
next if missing_keys.empty?
puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
lang = File.open(file, 'a')
missing_keys.each do |key|
{key => en_strings[key]}.to_yaml.each_line do |line|
next if line =~ /^---/ || line.empty?
puts " #{line}"
lang << " #{line}"
end
end
lang.close
end
end
desc 'Checks interpolation arguments in locals against en.yml'
task :check_interpolation do
dir = ENV['DIR'] || './config/locales'
en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
files.sort.each do |file|
puts "parsing #{file}..."
file_strings = YAML.load_file(file)
unless file_strings.is_a?(Hash)
puts "#{file}: content is not a Hash (#{file_strings.class.name})"
next
end
unless file_strings.keys.size == 1
puts "#{file}: content has multiple keys (#{file_strings.keys.size})"
next
end
file_strings = file_strings[file_strings.keys.first]
file_strings.each do |key, string|
next unless string.is_a?(String)
string.scan /%\{\w+\}/ do |match|
unless en_strings[key].nil? || en_strings[key].include?(match)
puts "#{file}: #{key} uses #{match} not found in en.yml"
end
end
end
end
end
desc <<-END_DESC
Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
Options:
key=key_1,key_2 Comma-separated list of keys to delete
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
END_DESC
task :remove_key do
dir = ENV['DIR'] || './config/locales'
files = Dir.glob(File.join(dir,'*.yml'))
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
# Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
files.each do |path|
# Skip certain locales
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
puts "Deleting selected keys from #{path}"
orig_content = File.open(path, 'r') {|file| file.read}
File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
end
end
desc <<-END_DESC
Adds a new top-level translation string to all locale file (only works for childless keys, probably doesn\'t work on windows, doesn't check for duplicates).
Options:
key="some_key=foo"
key1="another_key=bar"
key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
skip=en,de Comma-separated list of locale files to ignore (filename without extension)
END_DESC
task :add_key do
dir = ENV['DIR'] || './config/locales'
files = Dir.glob(File.join(dir,'*.yml'))
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
keys_regex = /\Akey(\d+|_.+)?\z/
adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
key_list = adds.collect {|v| v[0]}.join(", ")
files.each do |path|
# Skip certain locales
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
# TODO: Check for duplicate/existing keys
puts "Adding #{key_list} to #{path}"
File.open(path, 'a') do |file|
adds.each do |kv|
Hash[*kv].to_yaml.each_line do |line|
file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
end
end
end
end
end
desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
task :dup do
dir = ENV['DIR'] || './config/locales'
files = Dir.glob(File.join(dir,'*.yml'))
skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
key = ENV['key']
new_key = ENV['new_key']
abort "Missing key argument" if key.blank?
abort "Missing new_key argument" if new_key.blank?
files.each do |path|
# Skip certain locales
(puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
puts "Adding #{new_key} to #{path}"
strings = File.read(path)
unless strings =~ /^( #{key}: .+)$/
puts "Key not found in #{path}"
next
end
line = $1
File.open(path, 'a') do |file|
file.puts(line.sub(key, new_key))
end
end
end
desc 'Check parsing yaml by psych library on Ruby 1.9.'
# On Fedora 12 and 13, if libyaml-devel is available,
# in case of installing by rvm,
# Ruby 1.9 default yaml library is psych.
task :check_parsing_by_psych do
begin
require 'psych'
parser = Psych::Parser.new
dir = ENV['DIR'] || './config/locales'
files = Dir.glob(File.join(dir,'*.yml'))
files.sort.each do |filename|
next if File.directory? filename
puts "parsing #{filename}..."
begin
parser.parse File.open(filename)
rescue Exception => e1
puts(e1.message)
puts("")
end
end
rescue Exception => e
puts(e.message)
end
end
end

6
lib/tasks/metrics.rake Normal file
View file

@ -0,0 +1,6 @@
begin
require 'metric_fu'
rescue LoadError
# Metric-fu not installed
# http://metric-fu.rubyforge.org/
end

View file

@ -0,0 +1,516 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
desc 'Mantis migration script'
require 'active_record'
require 'pp'
namespace :redmine do
task :migrate_from_mantis => :environment do
module MantisMigrate
new_status = IssueStatus.find_by_position(1)
assigned_status = IssueStatus.find_by_position(2)
resolved_status = IssueStatus.find_by_position(3)
feedback_status = IssueStatus.find_by_position(4)
closed_status = IssueStatus.where(:is_closed => true).first
STATUS_MAPPING = {10 => new_status, # new
20 => feedback_status, # feedback
30 => new_status, # acknowledged
40 => new_status, # confirmed
50 => assigned_status, # assigned
80 => resolved_status, # resolved
90 => closed_status # closed
}
priorities = IssuePriority.all
DEFAULT_PRIORITY = priorities[2]
PRIORITY_MAPPING = {10 => priorities[1], # none
20 => priorities[1], # low
30 => priorities[2], # normal
40 => priorities[3], # high
50 => priorities[4], # urgent
60 => priorities[5] # immediate
}
TRACKER_BUG = Tracker.find_by_position(1)
TRACKER_FEATURE = Tracker.find_by_position(2)
roles = Role.where(:builtin => 0).order('position ASC').all
manager_role = roles[0]
developer_role = roles[1]
DEFAULT_ROLE = roles.last
ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
25 => DEFAULT_ROLE, # reporter
40 => DEFAULT_ROLE, # updater
55 => developer_role, # developer
70 => manager_role, # manager
90 => manager_role # administrator
}
CUSTOM_FIELD_TYPE_MAPPING = {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
}
RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
2 => IssueRelation::TYPE_RELATES, # parent of
3 => IssueRelation::TYPE_RELATES, # child of
0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
4 => IssueRelation::TYPE_DUPLICATES # has duplicate
}
class MantisUser < ActiveRecord::Base
self.table_name = :mantis_user_table
def firstname
@firstname = realname.blank? ? username : realname.split.first[0..29]
@firstname
end
def lastname
@lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
@lastname = '-' if @lastname.blank?
@lastname
end
def email
if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
!User.find_by_mail(read_attribute(:email))
@email = read_attribute(:email)
else
@email = "#{username}@foo.bar"
end
end
def username
read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
end
end
class MantisProject < ActiveRecord::Base
self.table_name = :mantis_project_table
has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
def identifier
read_attribute(:name).downcase.gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
end
end
class MantisVersion < ActiveRecord::Base
self.table_name = :mantis_project_version_table
def version
read_attribute(:version)[0..29]
end
def description
read_attribute(:description)[0..254]
end
end
class MantisCategory < ActiveRecord::Base
self.table_name = :mantis_project_category_table
end
class MantisProjectUser < ActiveRecord::Base
self.table_name = :mantis_project_user_list_table
end
class MantisBug < ActiveRecord::Base
self.table_name = :mantis_bug_table
belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
end
class MantisBugText < ActiveRecord::Base
self.table_name = :mantis_bug_text_table
# Adds Mantis steps_to_reproduce and additional_information fields
# to description if any
def full_description
full_description = description
full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
full_description
end
end
class MantisBugNote < ActiveRecord::Base
self.table_name = :mantis_bugnote_table
belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
end
class MantisBugNoteText < ActiveRecord::Base
self.table_name = :mantis_bugnote_text_table
end
class MantisBugFile < ActiveRecord::Base
self.table_name = :mantis_bug_file_table
def size
filesize
end
def original_filename
MantisMigrate.encode(filename)
end
def content_type
file_type
end
def read(*args)
if @read_finished
nil
else
@read_finished = true
content
end
end
end
class MantisBugRelationship < ActiveRecord::Base
self.table_name = :mantis_bug_relationship_table
end
class MantisBugMonitor < ActiveRecord::Base
self.table_name = :mantis_bug_monitor_table
end
class MantisNews < ActiveRecord::Base
self.table_name = :mantis_news_table
end
class MantisCustomField < ActiveRecord::Base
self.table_name = :mantis_custom_field_table
set_inheritance_column :none
has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
def format
read_attribute :type
end
def name
read_attribute(:name)[0..29]
end
end
class MantisCustomFieldProject < ActiveRecord::Base
self.table_name = :mantis_custom_field_project_table
end
class MantisCustomFieldString < ActiveRecord::Base
self.table_name = :mantis_custom_field_string_table
end
def self.migrate
# Users
print "Migrating users"
User.where("login <> 'admin'").delete_all
users_map = {}
users_migrated = 0
MantisUser.all.each do |user|
u = User.new :firstname => encode(user.firstname),
:lastname => encode(user.lastname),
:mail => user.email,
:last_login_on => user.last_visit
u.login = user.username
u.password = 'mantis'
u.status = User::STATUS_LOCKED if user.enabled != 1
u.admin = true if user.access_level == 90
next unless u.save!
users_migrated += 1
users_map[user.id] = u.id
print '.'
end
puts
# Projects
print "Migrating projects"
Project.destroy_all
projects_map = {}
versions_map = {}
categories_map = {}
MantisProject.all.each do |project|
p = Project.new :name => encode(project.name),
:description => encode(project.description)
p.identifier = project.identifier
next unless p.save
projects_map[project.id] = p.id
p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG)
p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE)
print '.'
# Project members
project.members.each do |member|
m = Member.new :user => User.find_by_id(users_map[member.user_id]),
:roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
m.project = p
m.save
end
# Project versions
project.versions.each do |version|
v = Version.new :name => encode(version.version),
:description => encode(version.description),
:effective_date => (version.date_order ? version.date_order.to_date : nil)
v.project = p
v.save
versions_map[version.id] = v.id
end
# Project categories
project.categories.each do |category|
g = IssueCategory.new :name => category.category[0,30]
g.project = p
g.save
categories_map[category.category] = g.id
end
end
puts
# Bugs
print "Migrating bugs"
Issue.destroy_all
issues_map = {}
keep_bug_ids = (Issue.count == 0)
MantisBug.find_each(:batch_size => 200) do |bug|
next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
i = Issue.new :project_id => projects_map[bug.project_id],
:subject => encode(bug.summary),
:description => encode(bug.bug_text.full_description),
:priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
:created_on => bug.date_submitted,
:updated_on => bug.last_updated
i.author = User.find_by_id(users_map[bug.reporter_id])
i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
i.status = STATUS_MAPPING[bug.status] || i.status
i.id = bug.id if keep_bug_ids
next unless i.save
issues_map[bug.id] = i.id
print '.'
STDOUT.flush
# Assignee
# Redmine checks that the assignee is a project member
if (bug.handler_id && users_map[bug.handler_id])
i.assigned_to = User.find_by_id(users_map[bug.handler_id])
i.save(:validate => false)
end
# Bug notes
bug.bug_notes.each do |note|
next unless users_map[note.reporter_id]
n = Journal.new :notes => encode(note.bug_note_text.note),
:created_on => note.date_submitted
n.user = User.find_by_id(users_map[note.reporter_id])
n.journalized = i
n.save
end
# Bug files
bug.bug_files.each do |file|
a = Attachment.new :created_on => file.date_added
a.file = file
a.author = User.first
a.container = i
a.save
end
# Bug monitors
bug.bug_monitors.each do |monitor|
next unless users_map[monitor.user_id]
i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
end
end
# update issue id sequence if needed (postgresql)
Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
puts
# Bug relationships
print "Migrating bug relations"
MantisBugRelationship.all.each do |relation|
next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
pp r unless r.save
print '.'
STDOUT.flush
end
puts
# News
print "Migrating news"
News.destroy_all
MantisNews.where('project_id > 0').all.each do |news|
next unless projects_map[news.project_id]
n = News.new :project_id => projects_map[news.project_id],
:title => encode(news.headline[0..59]),
:description => encode(news.body),
:created_on => news.date_posted
n.author = User.find_by_id(users_map[news.poster_id])
n.save
print '.'
STDOUT.flush
end
puts
# Custom fields
print "Migrating custom fields"
IssueCustomField.destroy_all
MantisCustomField.all.each do |field|
f = IssueCustomField.new :name => field.name[0..29],
:field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
:min_length => field.length_min,
:max_length => field.length_max,
:regexp => field.valid_regexp,
:possible_values => field.possible_values.split('|'),
:is_required => field.require_report?
next unless f.save
print '.'
STDOUT.flush
# Trackers association
f.trackers = Tracker.all
# Projects association
field.projects.each do |project|
f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
end
# Values
field.values.each do |value|
v = CustomValue.new :custom_field_id => f.id,
:value => value.value
v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
v.save
end unless f.new_record?
end
puts
puts
puts "Users: #{users_migrated}/#{MantisUser.count}"
puts "Projects: #{Project.count}/#{MantisProject.count}"
puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
puts "Versions: #{Version.count}/#{MantisVersion.count}"
puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
puts "Bugs: #{Issue.count}/#{MantisBug.count}"
puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
puts "News: #{News.count}/#{MantisNews.count}"
puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
end
def self.encoding(charset)
@charset = charset
end
def self.establish_connection(params)
constants.each do |const|
klass = const_get(const)
next unless klass.respond_to? 'establish_connection'
klass.establish_connection params
end
end
def self.encode(text)
text.to_s.force_encoding(@charset).encode('UTF-8')
end
end
puts
if Redmine::DefaultData::Loader.no_data?
puts "Redmine configuration need to be loaded before importing data."
puts "Please, run this first:"
puts
puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
exit
end
puts "WARNING: Your Redmine data will be deleted during this process."
print "Are you sure you want to continue ? [y/N] "
STDOUT.flush
break unless STDIN.gets.match(/^y$/i)
# Default Mantis database settings
db_params = {:adapter => 'mysql2',
:database => 'bugtracker',
:host => 'localhost',
:username => 'root',
:password => '' }
puts
puts "Please enter settings for your Mantis database"
[:adapter, :host, :database, :username, :password].each do |param|
print "#{param} [#{db_params[param]}]: "
value = STDIN.gets.chomp!
db_params[param] = value unless value.blank?
end
while true
print "encoding [UTF-8]: "
STDOUT.flush
encoding = STDIN.gets.chomp!
encoding = 'UTF-8' if encoding.blank?
break if MantisMigrate.encoding encoding
puts "Invalid encoding!"
end
puts
# Make sure bugs can refer bugs in other projects
Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
old_notified_events = Setting.notified_events
old_password_min_length = Setting.password_min_length
begin
# Turn off email notifications temporarily
Setting.notified_events = []
Setting.password_min_length = 4
# Run the migration
MantisMigrate.establish_connection db_params
MantisMigrate.migrate
ensure
# Restore previous settings
Setting.notified_events = old_notified_events
Setting.password_min_length = old_password_min_length
end
end
end

View file

@ -0,0 +1,777 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'active_record'
require 'pp'
namespace :redmine do
desc 'Trac migration script'
task :migrate_from_trac => :environment do
module TracMigrate
TICKET_MAP = []
new_status = IssueStatus.find_by_position(1)
assigned_status = IssueStatus.find_by_position(2)
resolved_status = IssueStatus.find_by_position(3)
feedback_status = IssueStatus.find_by_position(4)
closed_status = IssueStatus.where(:is_closed => true).first
STATUS_MAPPING = {'new' => new_status,
'reopened' => feedback_status,
'assigned' => assigned_status,
'closed' => closed_status
}
priorities = IssuePriority.all
DEFAULT_PRIORITY = priorities[0]
PRIORITY_MAPPING = {'lowest' => priorities[0],
'low' => priorities[0],
'normal' => priorities[1],
'high' => priorities[2],
'highest' => priorities[3],
# ---
'trivial' => priorities[0],
'minor' => priorities[1],
'major' => priorities[2],
'critical' => priorities[3],
'blocker' => priorities[4]
}
TRACKER_BUG = Tracker.find_by_position(1)
TRACKER_FEATURE = Tracker.find_by_position(2)
DEFAULT_TRACKER = TRACKER_BUG
TRACKER_MAPPING = {'defect' => TRACKER_BUG,
'enhancement' => TRACKER_FEATURE,
'task' => TRACKER_FEATURE,
'patch' =>TRACKER_FEATURE
}
roles = Role.where(:builtin => 0).order('position ASC').all
manager_role = roles[0]
developer_role = roles[1]
DEFAULT_ROLE = roles.last
ROLE_MAPPING = {'admin' => manager_role,
'developer' => developer_role
}
class ::Time
class << self
alias :real_now :now
def now
real_now - @fake_diff.to_i
end
def fake(time)
@fake_diff = real_now - time
res = yield
@fake_diff = 0
res
end
end
end
class TracComponent < ActiveRecord::Base
self.table_name = :component
end
class TracMilestone < ActiveRecord::Base
self.table_name = :milestone
# If this attribute is set a milestone has a defined target timepoint
def due
if read_attribute(:due) && read_attribute(:due) > 0
Time.at(read_attribute(:due)).to_date
else
nil
end
end
# This is the real timepoint at which the milestone has finished.
def completed
if read_attribute(:completed) && read_attribute(:completed) > 0
Time.at(read_attribute(:completed)).to_date
else
nil
end
end
def description
# Attribute is named descr in Trac v0.8.x
has_attribute?(:descr) ? read_attribute(:descr) : read_attribute(:description)
end
end
class TracTicketCustom < ActiveRecord::Base
self.table_name = :ticket_custom
end
class TracAttachment < ActiveRecord::Base
self.table_name = :attachment
set_inheritance_column :none
def time; Time.at(read_attribute(:time)) end
def original_filename
filename
end
def content_type
''
end
def exist?
File.file? trac_fullpath
end
def open
File.open("#{trac_fullpath}", 'rb') {|f|
@file = f
yield self
}
end
def read(*args)
@file.read(*args)
end
def description
read_attribute(:description).to_s.slice(0,255)
end
private
def trac_fullpath
attachment_type = read_attribute(:type)
#replace exotic characters with their hex representation to avoid invalid filenames
trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) do |x|
codepoint = x.codepoints.to_a[0]
sprintf('%%%02x', codepoint)
end
"#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
end
end
class TracTicket < ActiveRecord::Base
self.table_name = :ticket
set_inheritance_column :none
# ticket changes: only migrate status changes and comments
has_many :ticket_changes, :class_name => "TracTicketChange", :foreign_key => :ticket
has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
def attachments
TracMigrate::TracAttachment.all(:conditions => ["type = 'ticket' AND id = ?", self.id.to_s])
end
def ticket_type
read_attribute(:type)
end
def summary
read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
end
def description
read_attribute(:description).blank? ? summary : read_attribute(:description)
end
def time; Time.at(read_attribute(:time)) end
def changetime; Time.at(read_attribute(:changetime)) end
end
class TracTicketChange < ActiveRecord::Base
self.table_name = :ticket_change
def self.columns
# Hides Trac field 'field' to prevent clash with AR field_changed? method (Rails 3.0)
super.select {|column| column.name.to_s != 'field'}
end
def time; Time.at(read_attribute(:time)) end
end
TRAC_WIKI_PAGES = %w(InterMapTxt InterTrac InterWiki RecentChanges SandBox TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \
TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization \
TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery \
TracReports TracRevisionLog TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets \
TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting \
WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks \
CamelCase TitleIndex)
class TracWikiPage < ActiveRecord::Base
self.table_name = :wiki
set_primary_key :name
def self.columns
# Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0)
super.select {|column| column.name.to_s != 'readonly'}
end
def attachments
TracMigrate::TracAttachment.all(:conditions => ["type = 'wiki' AND id = ?", self.id.to_s])
end
def time; Time.at(read_attribute(:time)) end
end
class TracPermission < ActiveRecord::Base
self.table_name = :permission
end
class TracSessionAttribute < ActiveRecord::Base
self.table_name = :session_attribute
end
def self.find_or_create_user(username, project_member = false)
return User.anonymous if username.blank?
u = User.find_by_login(username)
if !u
# Create a new user if not found
mail = username[0, User::MAIL_LENGTH_LIMIT]
if mail_attr = TracSessionAttribute.find_by_sid_and_name(username, 'email')
mail = mail_attr.value
end
mail = "#{mail}@foo.bar" unless mail.include?("@")
name = username
if name_attr = TracSessionAttribute.find_by_sid_and_name(username, 'name')
name = name_attr.value
end
name =~ (/(\w+)(\s+\w+)?/)
fn = ($1 || "-").strip
ln = ($2 || '-').strip
u = User.new :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-'),
:firstname => fn[0, limit_for(User, 'firstname')],
:lastname => ln[0, limit_for(User, 'lastname')]
u.login = username[0, User::LOGIN_LENGTH_LIMIT].gsub(/[^a-z0-9_\-@\.]/i, '-')
u.password = 'trac'
u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
# finally, a default user is used if the new user is not valid
u = User.first unless u.save
end
# Make sure user is a member of the project
if project_member && !u.member_of?(@target_project)
role = DEFAULT_ROLE
if u.admin
role = ROLE_MAPPING['admin']
elsif TracPermission.find_by_username_and_action(username, 'developer')
role = ROLE_MAPPING['developer']
end
Member.create(:user => u, :project => @target_project, :roles => [role])
u.reload
end
u
end
# Basic wiki syntax conversion
def self.convert_wiki_text(text)
# Titles
text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
# External Links
text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
# Ticket links:
# [ticket:234 Text],[ticket:234 This is a test]
text = text.gsub(/\[ticket\:([^\ ]+)\ (.+?)\]/, '"\2":/issues/show/\1')
# ticket:1234
# #1 is working cause Redmine uses the same syntax.
text = text.gsub(/ticket\:([^\ ]+)/, '#\1')
# Milestone links:
# [milestone:"0.1.0 Mercury" Milestone 0.1.0 (Mercury)]
# The text "Milestone 0.1.0 (Mercury)" is not converted,
# cause Redmine's wiki does not support this.
text = text.gsub(/\[milestone\:\"([^\"]+)\"\ (.+?)\]/, 'version:"\1"')
# [milestone:"0.1.0 Mercury"]
text = text.gsub(/\[milestone\:\"([^\"]+)\"\]/, 'version:"\1"')
text = text.gsub(/milestone\:\"([^\"]+)\"/, 'version:"\1"')
# milestone:0.1.0
text = text.gsub(/\[milestone\:([^\ ]+)\]/, 'version:\1')
text = text.gsub(/milestone\:([^\ ]+)/, 'version:\1')
# Internal Links
text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:([^\s\]]+)\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:([^\s\]]+)\s(.*)\]/) {|s| "[[#{$1.delete(',./?;|:')}|#{$2.delete(',./?;|:')}]]"}
# Links to pages UsingJustWikiCaps
text = text.gsub(/([^!]|^)(^| )([A-Z][a-z]+[A-Z][a-zA-Z]+)/, '\\1\\2[[\3]]')
# Normalize things that were supposed to not be links
# like !NotALink
text = text.gsub(/(^| )!([A-Z][A-Za-z]+)/, '\1\2')
# Revisions links
text = text.gsub(/\[(\d+)\]/, 'r\1')
# Ticket number re-writing
text = text.gsub(/#(\d+)/) do |s|
if $1.length < 10
# TICKET_MAP[$1.to_i] ||= $1
"\##{TICKET_MAP[$1.to_i] || $1}"
else
s
end
end
# We would like to convert the Code highlighting too
# This will go into the next line.
shebang_line = false
# Regular expression for start of code
pre_re = /\{\{\{/
# Code highlighting...
shebang_re = /^\#\!([a-z]+)/
# Regular expression for end of code
pre_end_re = /\}\}\}/
# Go through the whole text..extract it line by line
text = text.gsub(/^(.*)$/) do |line|
m_pre = pre_re.match(line)
if m_pre
line = '<pre>'
else
m_sl = shebang_re.match(line)
if m_sl
shebang_line = true
line = '<code class="' + m_sl[1] + '">'
end
m_pre_end = pre_end_re.match(line)
if m_pre_end
line = '</pre>'
if shebang_line
line = '</code>' + line
end
end
end
line
end
# Highlighting
text = text.gsub(/'''''([^\s])/, '_*\1')
text = text.gsub(/([^\s])'''''/, '\1*_')
text = text.gsub(/'''/, '*')
text = text.gsub(/''/, '_')
text = text.gsub(/__/, '+')
text = text.gsub(/~~/, '-')
text = text.gsub(/`/, '@')
text = text.gsub(/,,/, '~')
# Lists
text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
text
end
def self.migrate
establish_connection
# Quick database test
TracComponent.count
migrated_components = 0
migrated_milestones = 0
migrated_tickets = 0
migrated_custom_values = 0
migrated_ticket_attachments = 0
migrated_wiki_edits = 0
migrated_wiki_attachments = 0
#Wiki system initializing...
@target_project.wiki.destroy if @target_project.wiki
@target_project.reload
wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
wiki_edit_count = 0
# Components
print "Migrating components"
issues_category_map = {}
TracComponent.all.each do |component|
print '.'
STDOUT.flush
c = IssueCategory.new :project => @target_project,
:name => encode(component.name[0, limit_for(IssueCategory, 'name')])
next unless c.save
issues_category_map[component.name] = c
migrated_components += 1
end
puts
# Milestones
print "Migrating milestones"
version_map = {}
TracMilestone.all.each do |milestone|
print '.'
STDOUT.flush
# First we try to find the wiki page...
p = wiki.find_or_new_page(milestone.name.to_s)
p.content = WikiContent.new(:page => p) if p.new_record?
p.content.text = milestone.description.to_s
p.content.author = find_or_create_user('trac')
p.content.comments = 'Milestone'
p.save
v = Version.new :project => @target_project,
:name => encode(milestone.name[0, limit_for(Version, 'name')]),
:description => nil,
:wiki_page_title => milestone.name.to_s,
:effective_date => milestone.completed
next unless v.save
version_map[milestone.name] = v
migrated_milestones += 1
end
puts
# Custom fields
# TODO: read trac.ini instead
print "Migrating custom fields"
custom_field_map = {}
TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
print '.'
STDOUT.flush
# Redmine custom field name
field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
# Find if the custom already exists in Redmine
f = IssueCustomField.find_by_name(field_name)
# Or create a new one
f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
:field_format => 'string')
next if f.new_record?
f.trackers = Tracker.all
f.projects << @target_project
custom_field_map[field.name] = f
end
puts
# Trac 'resolution' field as a Redmine custom field
r = IssueCustomField.where(:name => "Resolution").first
r = IssueCustomField.new(:name => 'Resolution',
:field_format => 'list',
:is_filter => true) if r.nil?
r.trackers = Tracker.all
r.projects << @target_project
r.possible_values = (r.possible_values + %w(fixed invalid wontfix duplicate worksforme)).flatten.compact.uniq
r.save!
custom_field_map['resolution'] = r
# Tickets
print "Migrating tickets"
TracTicket.find_each(:batch_size => 200) do |ticket|
print '.'
STDOUT.flush
i = Issue.new :project => @target_project,
:subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
:description => convert_wiki_text(encode(ticket.description)),
:priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
:created_on => ticket.time
i.author = find_or_create_user(ticket.reporter)
i.category = issues_category_map[ticket.component] unless ticket.component.blank?
i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
i.status = STATUS_MAPPING[ticket.status] || i.default_status
i.id = ticket.id unless Issue.exists?(ticket.id)
next unless Time.fake(ticket.changetime) { i.save }
TICKET_MAP[ticket.id] = i.id
migrated_tickets += 1
# Owner
unless ticket.owner.blank?
i.assigned_to = find_or_create_user(ticket.owner, true)
Time.fake(ticket.changetime) { i.save }
end
# Comments and status/resolution changes
ticket.ticket_changes.group_by(&:time).each do |time, changeset|
status_change = changeset.select {|change| change.field == 'status'}.first
resolution_change = changeset.select {|change| change.field == 'resolution'}.first
comment_change = changeset.select {|change| change.field == 'comment'}.first
n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
:created_on => time
n.user = find_or_create_user(changeset.first.author)
n.journalized = i
if status_change &&
STATUS_MAPPING[status_change.oldvalue] &&
STATUS_MAPPING[status_change.newvalue] &&
(STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
n.details << JournalDetail.new(:property => 'attr',
:prop_key => 'status_id',
:old_value => STATUS_MAPPING[status_change.oldvalue].id,
:value => STATUS_MAPPING[status_change.newvalue].id)
end
if resolution_change
n.details << JournalDetail.new(:property => 'cf',
:prop_key => custom_field_map['resolution'].id,
:old_value => resolution_change.oldvalue,
:value => resolution_change.newvalue)
end
n.save unless n.details.empty? && n.notes.blank?
end
# Attachments
ticket.attachments.each do |attachment|
next unless attachment.exist?
attachment.open {
a = Attachment.new :created_on => attachment.time
a.file = attachment
a.author = find_or_create_user(attachment.author)
a.container = i
a.description = attachment.description
migrated_ticket_attachments += 1 if a.save
}
end
# Custom fields
custom_values = ticket.customs.inject({}) do |h, custom|
if custom_field = custom_field_map[custom.name]
h[custom_field.id] = custom.value
migrated_custom_values += 1
end
h
end
if custom_field_map['resolution'] && !ticket.resolution.blank?
custom_values[custom_field_map['resolution'].id] = ticket.resolution
end
i.custom_field_values = custom_values
i.save_custom_field_values
end
# update issue id sequence if needed (postgresql)
Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
puts
# Wiki
print "Migrating wiki"
if wiki.save
TracWikiPage.order('name, version').all.each do |page|
# Do not migrate Trac manual wiki pages
next if TRAC_WIKI_PAGES.include?(page.name)
wiki_edit_count += 1
print '.'
STDOUT.flush
p = wiki.find_or_new_page(page.name)
p.content = WikiContent.new(:page => p) if p.new_record?
p.content.text = page.text
p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
p.content.comments = page.comment
Time.fake(page.time) { p.new_record? ? p.save : p.content.save }
next if p.content.new_record?
migrated_wiki_edits += 1
# Attachments
page.attachments.each do |attachment|
next unless attachment.exist?
next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
attachment.open {
a = Attachment.new :created_on => attachment.time
a.file = attachment
a.author = find_or_create_user(attachment.author)
a.description = attachment.description
a.container = p
migrated_wiki_attachments += 1 if a.save
}
end
end
wiki.reload
wiki.pages.each do |page|
page.content.text = convert_wiki_text(page.content.text)
Time.fake(page.content.updated_on) { page.content.save }
end
end
puts
puts
puts "Components: #{migrated_components}/#{TracComponent.count}"
puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => {:type => 'ticket'}).to_s
puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
puts "Wiki edits: #{migrated_wiki_edits}/#{wiki_edit_count}"
puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => {:type => 'wiki'}).to_s
end
def self.limit_for(klass, attribute)
klass.columns_hash[attribute.to_s].limit
end
def self.encoding(charset)
@charset = charset
end
def self.set_trac_directory(path)
@@trac_directory = path
raise "This directory doesn't exist!" unless File.directory?(path)
raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
@@trac_directory
rescue Exception => e
puts e
return false
end
def self.trac_directory
@@trac_directory
end
def self.set_trac_adapter(adapter)
return false if adapter.blank?
raise "Unknown adapter: #{adapter}!" unless %w(sqlite3 mysql postgresql).include?(adapter)
# If adapter is sqlite or sqlite3, make sure that trac.db exists
raise "#{trac_db_path} doesn't exist!" if %w(sqlite3).include?(adapter) && !File.exist?(trac_db_path)
@@trac_adapter = adapter
rescue Exception => e
puts e
return false
end
def self.set_trac_db_host(host)
return nil if host.blank?
@@trac_db_host = host
end
def self.set_trac_db_port(port)
return nil if port.to_i == 0
@@trac_db_port = port.to_i
end
def self.set_trac_db_name(name)
return nil if name.blank?
@@trac_db_name = name
end
def self.set_trac_db_username(username)
@@trac_db_username = username
end
def self.set_trac_db_password(password)
@@trac_db_password = password
end
def self.set_trac_db_schema(schema)
@@trac_db_schema = schema
end
mattr_reader :trac_directory, :trac_adapter, :trac_db_host, :trac_db_port, :trac_db_name, :trac_db_schema, :trac_db_username, :trac_db_password
def self.trac_db_path; "#{trac_directory}/db/trac.db" end
def self.trac_attachments_directory; "#{trac_directory}/attachments" end
def self.target_project_identifier(identifier)
project = Project.find_by_identifier(identifier)
if !project
# create the target project
project = Project.new :name => identifier.humanize,
:description => ''
project.identifier = identifier
puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
# enable issues and wiki for the created project
project.enabled_module_names = ['issue_tracking', 'wiki']
else
puts
puts "This project already exists in your Redmine database."
print "Are you sure you want to append data to this project ? [Y/n] "
STDOUT.flush
exit if STDIN.gets.match(/^n$/i)
end
project.trackers << TRACKER_BUG unless project.trackers.include?(TRACKER_BUG)
project.trackers << TRACKER_FEATURE unless project.trackers.include?(TRACKER_FEATURE)
@target_project = project.new_record? ? nil : project
@target_project.reload
end
def self.connection_params
if trac_adapter == 'sqlite3'
{:adapter => 'sqlite3',
:database => trac_db_path}
else
{:adapter => trac_adapter,
:database => trac_db_name,
:host => trac_db_host,
:port => trac_db_port,
:username => trac_db_username,
:password => trac_db_password,
:schema_search_path => trac_db_schema
}
end
end
def self.establish_connection
constants.each do |const|
klass = const_get(const)
next unless klass.respond_to? 'establish_connection'
klass.establish_connection connection_params
end
end
def self.encode(text)
text.to_s.force_encoding(@charset).encode('UTF-8')
end
end
puts
if Redmine::DefaultData::Loader.no_data?
puts "Redmine configuration need to be loaded before importing data."
puts "Please, run this first:"
puts
puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
exit
end
puts "WARNING: a new project will be added to Redmine during this process."
print "Are you sure you want to continue ? [y/N] "
STDOUT.flush
break unless STDIN.gets.match(/^y$/i)
puts
def prompt(text, options = {}, &block)
default = options[:default] || ''
while true
print "#{text} [#{default}]: "
STDOUT.flush
value = STDIN.gets.chomp!
value = default if value.blank?
break if yield value
end
end
DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory.strip}
prompt('Trac database adapter (sqlite3, mysql2, postgresql)', :default => 'sqlite3') {|adapter| TracMigrate.set_trac_adapter adapter}
unless %w(sqlite3).include?(TracMigrate.trac_adapter)
prompt('Trac database host', :default => 'localhost') {|host| TracMigrate.set_trac_db_host host}
prompt('Trac database port', :default => DEFAULT_PORTS[TracMigrate.trac_adapter]) {|port| TracMigrate.set_trac_db_port port}
prompt('Trac database name') {|name| TracMigrate.set_trac_db_name name}
prompt('Trac database schema', :default => 'public') {|schema| TracMigrate.set_trac_db_schema schema}
prompt('Trac database username') {|username| TracMigrate.set_trac_db_username username}
prompt('Trac database password') {|password| TracMigrate.set_trac_db_password password}
end
prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
puts
old_notified_events = Setting.notified_events
old_password_min_length = Setting.password_min_length
begin
# Turn off email notifications temporarily
Setting.notified_events = []
Setting.password_min_length = 4
# Run the migration
TracMigrate.migrate
ensure
# Restore previous settings
Setting.notified_events = old_notified_events
Setting.password_min_length = old_password_min_length
end
end
end

View file

@ -0,0 +1,9 @@
namespace :redmine do
desc "List all permissions and the actions registered with them"
task :permissions => :environment do
puts "Permission Name - controller/action pairs"
Redmine::AccessControl.permissions.sort {|a,b| a.name.to_s <=> b.name.to_s }.each do |permission|
puts ":#{permission.name} - #{permission.actions.join(', ')}"
end
end
end

194
lib/tasks/redmine.rake Normal file
View file

@ -0,0 +1,194 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace :redmine do
namespace :attachments do
desc 'Removes uploaded files left unattached after one day.'
task :prune => :environment do
Attachment.prune
end
desc 'Moves attachments stored at the root of the file directory (ie. created before Redmine 2.3) to their subdirectories'
task :move_to_subdirectories => :environment do
Attachment.move_from_root_to_target_directory
end
desc 'Updates attachment digests to SHA256'
task :update_digests => :environment do
Attachment.update_digests_to_sha256
end
end
namespace :tokens do
desc 'Removes expired tokens.'
task :prune => :environment do
Token.destroy_expired
end
end
namespace :watchers do
desc 'Removes watchers from what they can no longer view.'
task :prune => :environment do
Watcher.prune
end
end
desc 'Fetch changesets from the repositories'
task :fetch_changesets => :environment do
Repository.fetch_changesets
end
desc 'Migrates and copies plugins assets.'
task :plugins do
Rake::Task["redmine:plugins:migrate"].invoke
Rake::Task["redmine:plugins:assets"].invoke
end
desc <<-DESC
FOR EXPERIMENTAL USE ONLY, Moves Redmine data from production database to the development database.
This task should only be used when you need to move data from one DBMS to a different one (eg. MySQL to PostgreSQL).
WARNING: All data in the development database is deleted.
DESC
task :migrate_dbms => :environment do
ActiveRecord::Base.establish_connection :development
target_tables = ActiveRecord::Base.connection.tables
ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection :production
tables = ActiveRecord::Base.connection.tables.sort - %w(schema_migrations plugin_schema_info)
if (tables - target_tables).any?
list = (tables - target_tables).map {|table| "* #{table}"}.join("\n")
abort "The following table(s) are missing from the target database:\n#{list}"
end
tables.each do |table_name|
Source = Class.new(ActiveRecord::Base)
Target = Class.new(ActiveRecord::Base)
Target.establish_connection(:development)
[Source, Target].each do |klass|
klass.table_name = table_name
klass.reset_column_information
klass.inheritance_column = "foo"
klass.record_timestamps = false
end
Target.primary_key = (Target.column_names.include?("id") ? "id" : nil)
source_count = Source.count
puts "Migrating %6d records from #{table_name}..." % source_count
Target.delete_all
offset = 0
while (objects = Source.offset(offset).limit(5000).order("1,2").to_a) && objects.any?
offset += objects.size
Target.transaction do
objects.each do |object|
new_object = Target.new(object.attributes)
new_object.id = object.id if Target.primary_key
new_object.save(:validate => false)
end
end
end
Target.connection.reset_pk_sequence!(table_name) if Target.primary_key
target_count = Target.count
abort "Some records were not migrated" unless source_count == target_count
Object.send(:remove_const, :Target)
Object.send(:remove_const, :Source)
end
end
namespace :plugins do
desc 'Migrates installed plugins.'
task :migrate => :environment do
name = ENV['NAME']
version = nil
version_string = ENV['VERSION']
if version_string
if version_string =~ /^\d+$/
version = version_string.to_i
if name.nil?
abort "The VERSION argument requires a plugin NAME."
end
else
abort "Invalid VERSION #{version_string} given."
end
end
begin
Redmine::Plugin.migrate(name, version)
rescue Redmine::PluginNotFound
abort "Plugin #{name} was not found."
end
Rake::Task["db:schema:dump"].invoke
end
desc 'Copies plugins assets into the public directory.'
task :assets => :environment do
name = ENV['NAME']
begin
Redmine::Plugin.mirror_assets(name)
rescue Redmine::PluginNotFound
abort "Plugin #{name} was not found."
end
end
desc 'Runs the plugins tests.'
task :test do
Rake::Task["redmine:plugins:test:units"].invoke
Rake::Task["redmine:plugins:test:functionals"].invoke
Rake::Task["redmine:plugins:test:integration"].invoke
end
namespace :test do
desc 'Runs the plugins unit tests.'
Rake::TestTask.new :units => "db:test:prepare" do |t|
t.libs << "test"
t.verbose = true
t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/unit/**/*_test.rb"
end
desc 'Runs the plugins functional tests.'
Rake::TestTask.new :functionals => "db:test:prepare" do |t|
t.libs << "test"
t.verbose = true
t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/functional/**/*_test.rb"
end
desc 'Runs the plugins integration tests.'
Rake::TestTask.new :integration => "db:test:prepare" do |t|
t.libs << "test"
t.verbose = true
t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/integration/**/*_test.rb"
end
desc 'Runs the plugins ui tests.'
Rake::TestTask.new :ui => "db:test:prepare" do |t|
t.libs << "test"
t.verbose = true
t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/ui/**/*_test.rb"
end
end
end
end
# Load plugins' rake tasks
Dir[File.join(Rails.root, "plugins/*/lib/tasks/**/*.rake")].sort.each { |ext| load ext }

45
lib/tasks/reminder.rake Normal file
View file

@ -0,0 +1,45 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
desc <<-END_DESC
Send reminders about issues due in the next days.
Available options:
* days => number of days to remind about (defaults to 7)
* tracker => id of tracker (defaults to all trackers)
* project => id or identifier of project (defaults to all projects)
* users => comma separated list of user/group ids who should be reminded
* version => name of target version for filtering issues (defaults to none)
Example:
rake redmine:send_reminders days=7 users="1,23, 56" RAILS_ENV="production"
END_DESC
namespace :redmine do
task :send_reminders => :environment do
options = {}
options[:days] = ENV['days'].to_i if ENV['days']
options[:project] = ENV['project'] if ENV['project']
options[:tracker] = ENV['tracker'].to_i if ENV['tracker']
options[:users] = (ENV['users'] || '').split(',').each(&:strip!)
options[:version] = ENV['version'] if ENV['version']
Mailer.with_synched_deliveries do
Mailer.reminders(options)
end
end
end

112
lib/tasks/testing.rake Normal file
View file

@ -0,0 +1,112 @@
namespace :test do
desc 'Measures test coverage'
task :coverage do
rm_f "coverage"
ENV["COVERAGE"] = "1"
Rake::Task["test"].invoke
end
desc 'Run unit and functional scm tests'
task :scm do
errors = %w(test:scm:units test:scm:functionals).collect do |task|
begin
Rake::Task[task].invoke
nil
rescue => e
task
end
end.compact
abort "Errors running #{errors.to_sentence(:locale => :en)}!" if errors.any?
end
namespace :scm do
namespace :setup do
desc "Creates directory for test repositories"
task :create_dir => :environment do
FileUtils.mkdir_p Rails.root + '/tmp/test'
end
supported_scms = [:subversion, :cvs, :bazaar, :mercurial, :git, :darcs, :filesystem]
desc "Creates a test subversion repository"
task :subversion => :create_dir do
repo_path = "tmp/test/subversion_repository"
unless File.exists?(repo_path)
system "svnadmin create #{repo_path}"
system "gunzip < test/fixtures/repositories/subversion_repository.dump.gz | svnadmin load #{repo_path}"
end
end
desc "Creates a test mercurial repository"
task :mercurial => :create_dir do
repo_path = "tmp/test/mercurial_repository"
unless File.exists?(repo_path)
bundle_path = "test/fixtures/repositories/mercurial_repository.hg"
system "hg init #{repo_path}"
system "hg -R #{repo_path} pull #{bundle_path}"
end
end
def extract_tar_gz(prefix)
unless File.exists?("tmp/test/#{prefix}_repository")
# system "gunzip < test/fixtures/repositories/#{prefix}_repository.tar.gz | tar -xv -C tmp/test"
system "tar -xvz -C tmp/test -f test/fixtures/repositories/#{prefix}_repository.tar.gz"
end
end
(supported_scms - [:subversion, :mercurial]).each do |scm|
desc "Creates a test #{scm} repository"
task scm => :create_dir do
extract_tar_gz(scm)
end
end
desc "Creates all test repositories"
task :all => supported_scms
end
desc "Updates installed test repositories"
task :update => :environment do
require 'fileutils'
Dir.glob("tmp/test/*_repository").each do |dir|
next unless File.basename(dir) =~ %r{^(.+)_repository$} && File.directory?(dir)
scm = $1
next unless fixture = Dir.glob("test/fixtures/repositories/#{scm}_repository.*").first
next if File.stat(dir).ctime > File.stat(fixture).mtime
FileUtils.rm_rf dir
Rake::Task["test:scm:setup:#{scm}"].execute
end
end
Rake::TestTask.new(:units => "db:test:prepare") do |t|
t.libs << "test"
t.verbose = true
t.warning = false
t.test_files = FileList['test/unit/repository*_test.rb'] + FileList['test/unit/lib/redmine/scm/**/*_test.rb']
end
Rake::Task['test:scm:units'].comment = "Run the scm unit tests"
Rake::TestTask.new(:functionals => "db:test:prepare") do |t|
t.libs << "test"
t.verbose = true
t.warning = false
t.test_files = FileList['test/functional/repositories*_test.rb']
end
Rake::Task['test:scm:functionals'].comment = "Run the scm functional tests"
end
Rake::TestTask.new(:routing) do |t|
t.libs << "test"
t.verbose = true
t.test_files = FileList['test/integration/routing/*_test.rb'] + FileList['test/integration/api_test/*_routing_test.rb']
end
Rake::Task['test:routing'].comment = "Run the routing tests"
Rake::TestTask.new(:ui => "db:test:prepare") do |t|
t.libs << "test"
t.verbose = true
t.test_files = FileList['test/ui/**/*_test_ui.rb']
end
Rake::Task['test:ui'].comment = "Run the UI tests with Capybara (PhantomJS listening on port 4444 is required)"
end

21
lib/tasks/yardoc.rake Normal file
View file

@ -0,0 +1,21 @@
begin
require 'yard'
YARD::Rake::YardocTask.new do |t|
files = ['app/**/*.rb']
files << Dir['lib/**/*.rb', 'plugins/**/*.rb'].reject {|f| f.match(/test/) }
t.files = files
static_files = ['doc/CHANGELOG',
'doc/COPYING',
'doc/INSTALL',
'doc/RUNNING_TESTS',
'doc/UPGRADING'].join(',')
t.options += ['--output-dir', './doc/app', '--files', static_files]
end
rescue LoadError
# yard not installed (gem install yard)
# http://yardoc.org
end