importing.rb revision a9a0428a54199be3b810a0871c947383c2f916d3
1516N/Amodule Repository::Importing
1452N/A extend ActiveSupport::Concern
1452N/A
1452N/A SOURCE_TYPES = %w( git svn )
1452N/A STATES = %w( pending processing done failed )
1452N/A
1452N/A included do
1452N/A include StateUpdater
1452N/A
1452N/A @queue = 'import'
1452N/A
1452N/A validates_inclusion_of :state, in: STATES
1452N/A validates_inclusion_of :source_type, in: SOURCE_TYPES, if: :remote?
1452N/A
1452N/A after_create :async_clone, if: :remote?
1452N/A end
1452N/A
1452N/A def remote?
1452N/A source_address?
1452N/A end
1452N/A
1452N/A # do not allow new actions if running
1452N/A def locked?
1452N/A %w( pending processing ).include?(state)
1452N/A end
1452N/A
1452N/A # enqueues a clone job
1452N/A def async_clone
1452N/A async_remote :clone
1452N/A end
1452N/A
1452N/A # enqueues a synchronize job
1452N/A def async_synchronize
1452N/A async_remote :synchronize
1452N/A end
1452N/A
1452N/A # enqueues a remote job
1452N/A def async_remote(method)
1452N/A raise "object is #{state}" if locked?
1452N/A update_state! 'pending'
1452N/A async :remote_send, method
1452N/A end
1452N/A
1452N/A def remote_send(method)
1452N/A update_state! 'processing'
1452N/A do_or_set_failed do
1452N/A remote_repository.send method
1452N/A update_state! 'done'
1452N/A end
1452N/A end
1452N/A
1452N/A def remote_repository
1452N/A RemoteRepository.instance(self, user)
1452N/A end
1452N/A
1452N/A module ClassMethods
1452N/A # creates a new repository and imports the contents from the remote repository
1452N/A def import_remote(type, user, source, name, params={})
1452N/A raise ArgumentError, "invalid source type: #{type}" unless SOURCE_TYPES.include?(type)
1452N/A raise Repository::ImportError, "#{source} is not a #{type} repository" unless GitRepository.send "is_#{type}_repository?", source
1452N/A
1452N/A params[:name] = name
1452N/A params[:source_type] = type
1452N/A params[:source_address] = source
1452N/A
1452N/A r = Repository.create!(params)
1452N/A r.user = user
1452N/A r.save!
1452N/A r
1452N/A
1452N/A end
1452N/A end
1452N/A
1452N/Aend