1396 issues

This commit is contained in:
Karel Pičman 2022-08-29 10:10:31 +02:00
parent 77564178c2
commit acdea2d81c

View File

@ -28,31 +28,78 @@ Converted project must have Document module enabled
Available options: Available options:
* project - id or identifier of a project (default to all projects) * project - id or identifier of a project (default to all projects)
* dry_run - perform just a check without any conversion * dry_run - perform just a check without any conversion
* issues - Convert also files attached to issues
Example: Example:
rake redmine:dmsf_convert_documents project=test RAILS_ENV="production" rake redmine:dmsf_convert_documents project=test RAILS_ENV="production"
rake redmine:dmsf_convert_documents project=test dry_run=1 RAILS_ENV="production" rake redmine:dmsf_convert_documents project=test dry_run=1 RAILS_ENV="production"
rake redmine:dmsf_convert_documents project=test issues=1 RAILS_ENV="production"
END_DESC END_DESC
class DmsfConvertDocuments class DmsfConvertDocuments
def self.convert(options={}) def initialize
projects = [] @dry_run = ENV['dry_run']
if options[:project] @projects = []
p = Project.find(options[:project]) if ENV['project']
projects << p if p&.module_enabled?('documents') p = Project.find(ENV['project'])
@projects << p if p&.active?
else else
projects = Project.active.has_module('documents').to_a @projects.concat(Project.active.to_a)
end end
if projects.any? @issues = ENV['issues']
projects.each do |project| end
def convert_projects
if @projects.any?
@projects.each do |project|
STDOUT.puts "Processing project: #{project.name}" STDOUT.puts "Processing project: #{project.name}"
unless options[:dry_run] convert_documents(project) if project.module_enabled?('documents')
project.enable_module! 'dmsf' convert_issues(project) if (@issues && project.module_enabled?('issue_tracking'))
#project.save!
end end
fail = false else
STDERR.puts 'No project(s) with Documents module enabled found.'
end
end
def convert_issues(project)
STDOUT.puts 'Issues'
unless Setting.plugin_redmine_dmsf['dmsf_act_as_attachable']
STDERR.puts "'Act as attachable' must be checked in the plugin's settings"
return
end
project.issues.each do |issue|
if issue.attachments.any?
STDOUT.puts "Processing: #{issue}"
project.enable_module!('dmsf') unless @dry_run
# <issue.id> - <issue.subject> folder
STDOUT.puts "Creating #{issue.id} - #{DmsfFolder::get_valid_title(issue.subject)} folder"
unless @dry_run
folder = issue.system_folder(true, project.id)
end
files = []
attachments = []
issue.attachments.each do |attachment|
@fail = false
create_document_from_attachment(project, folder, attachment, files)
attachments << attachment unless @fail
end
unless @dry_run
attachments.each do |attachment|
issue.init_journal User.anonymous
issue.attachments.delete attachment
end
end
end
end
end
def convert_documents(project)
@fail = false
folders = [] folders = []
if project.documents.any?
STDOUT.puts 'Documents'
project.enable_module!('dmsf') unless @dry_run
project.documents.each do |document| project.documents.each do |document|
STDOUT.puts "Processing document: #{document.title}" STDOUT.puts "Processing document: #{document.title}"
folder = DmsfFolder.new folder = DmsfFolder.new
@ -72,7 +119,7 @@ class DmsfConvertDocuments
end end
folder.title = folder.title + suffix folder.title = folder.title + suffix
folder.description = document.description folder.description = document.description
if options[:dry_run] if @dry_run
STDOUT.puts "Dry run folder: #{folder.title}" STDOUT.puts "Dry run folder: #{folder.title}"
STDERR.puts(folder.errors.full_messages.to_sentence) if folder.invalid? STDERR.puts(folder.errors.full_messages.to_sentence) if folder.invalid?
else else
@ -82,13 +129,27 @@ class DmsfConvertDocuments
rescue => e rescue => e
STDERR.puts "Creating folder: #{folder.title} failed" STDERR.puts "Creating folder: #{folder.title} failed"
STDERR.puts e.message STDERR.puts e.message
fail = true @fail = true
next next
end end
end end
folders << folder folders << folder
files = [] files = []
@fail = false
document.attachments.each do |attachment| document.attachments.each do |attachment|
create_document_from_attachment(project, folder, attachment, files)
end
document.destroy unless @dry_run || @fail
end
end
unless @dry_run || @fail
project.disable_module!('documents')
end
end
private
def create_document_from_attachment(project, folder, attachment, files)
begin begin
file = DmsfFile.new file = DmsfFile.new
file.project_id = project.id file.project_id = project.id
@ -105,10 +166,10 @@ class DmsfConvertDocuments
file.name = DmsfFileRevision.remove_extension(file.name) + suffix + File.extname(file.name) file.name = DmsfFileRevision.remove_extension(file.name) + suffix + File.extname(file.name)
unless File.exist?(attachment.diskfile) unless File.exist?(attachment.diskfile)
STDERR.puts "Creating file: #{attachment.filename} failed, attachment file #{attachment.diskfile} doesn't exist" STDERR.puts "Creating file: #{attachment.filename} failed, attachment file #{attachment.diskfile} doesn't exist"
fail = true @fail = true
next return
end end
if options[:dry_run] if @dry_run
file.id = attachment.id # Just to have an ID there file.id = attachment.id # Just to have an ID there
STDOUT.puts "Dry run file: #{file.name}" STDOUT.puts "Dry run file: #{file.name}"
STDERR.puts(file.errors.full_messages.to_sentence) if file.invalid? STDERR.puts(file.errors.full_messages.to_sentence) if file.invalid?
@ -125,47 +186,34 @@ class DmsfConvertDocuments
revision.updated_at = attachment.created_on revision.updated_at = attachment.created_on
revision.major_version = 0 revision.major_version = 0
revision.minor_version = 1 revision.minor_version = 1
revision.comment = 'Converted from Documents' revision.comment = "Converted from #{folder.system ? 'Issues' : 'Documents'}"
revision.mime_type = attachment.content_type revision.mime_type = attachment.content_type
revision.disk_filename = revision.new_storage_filename revision.disk_filename = revision.new_storage_filename
unless options[:dry_run] unless @dry_run
FileUtils.cp attachment.diskfile, revision.disk_file(false) FileUtils.cp attachment.diskfile, revision.disk_file(false)
revision.size = File.size(revision.disk_file(false)) revision.size = File.size(revision.disk_file(false))
end end
if options[:dry_run] if @dry_run
STDOUT.puts "Dry run revision: #{revision.title}" STDOUT.puts "Dry run revision: #{revision.title}"
STDERR.puts(revision.errors.full_messages.to_sentence) if revision.invalid? STDERR.puts(revision.errors.full_messages.to_sentence) if revision.invalid?
else else
revision.save! revision.save!
end end
files << file files << file
attachment.destroy unless options[:dry_run] attachment.destroy unless @dry_run
STDOUT.puts "Created file: #{file.name}" unless options[:dry_run] STDOUT.puts "Created file: #{file.name}" unless @dry_run
rescue => e rescue => e
STDERR.puts "Creating file: #{attachment.filename} failed" STDERR.puts "Creating file: #{attachment.filename} failed"
STDERR.puts e.message STDERR.puts e.message
fail = true @fail = true
end end
end end
document.destroy unless options[:dry_run] || fail
end
unless options[:dry_run] || fail
project.disable_module!('documents')
end
end
else
STDERR.puts 'No project(s) with Documents module enabled found.'
end
end end
end
namespace :redmine do namespace :redmine do
task :dmsf_convert_documents => :environment do task :dmsf_convert_documents => :environment do
options = {} convert = DmsfConvertDocuments.new
options[:project] = ENV['project'] convert.convert_projects
options[:dry_run] = ENV['dry_run']
options[:invalid] = ENV['invalid']
DmsfConvertDocuments.convert options
end end
end end