Commit f9eb3fc0 authored by James Lopez's avatar James Lopez

WIP - initial export service and spec

parent 99f08b3f
module Projects
module ImportExport
extend self
def export_path(project_name:)
File.join(storage_path, "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_gitlab_export_#{project_name}")
end
def project_atts
%i(id name path description issues_enabled wall_enabled merge_requests_enabled wiki_enabled snippets_enabled visibility_level archived)
end
def project_tree
%i(issues merge_requests labels milestones snippets releases events commit_statuses)
end
private
def storage_path
File.join(Settings.shared['path'], 'tmp/project_exports')
end
end
end
module Projects
module ImportExport
class ExportService < BaseService
def execute(options = {})
save_project_tree
end
private
def save_project_tree
Projects::ImportExport::ProjectTreeSaver.save(project: project)
end
end
end
end
module Projects
module ImportExport
class ProjectTreeSaver
attr_reader :full_path
def initialize(project: )
@project = project
end
def save
@full_path = File.join(export_path, project_filename)
save_to_disk
end
private
def save_to_disk
FileUtils.mkdir_p(export_path)
File.write(full_path, project_json_tree)
true
rescue
#TODO: handle error
false
end
def export_path
@export_path ||= ImportExport.export_path(@project.name)
end
def project_filename
# TODO sanitize name
"#{@project.name}.json"
end
def project_json_tree
# TODO confirm children, also add subchildren (i.e comments)
# TODO confirm atts for children
@project.to_json(only: ImportExport.project_atts, include: ImportExport.project_tree)
end
end
end
end
require 'spec_helper'
describe Projects::ImportExport::ProjectTreeSaver, services: true do
describe :save do
let(:user) { create(:user) }
let!(:project) { create(:project, :public, name: 'searchable_project') }
let(:export_path) { "#{Dir::tmpdir}/project_tree_saver_spec" }
let(:project_tree_saver) { Projects::ImportExport::ProjectTreeSaver.new(project: project) }
before(:each) do
project.team << [user, :master]
allow_any_instance_of(Projects::ImportExport::ProjectTreeSaver).to receive(:export_path).and_return(export_path)
end
after(:each) do
FileUtils.rm_rf(export_path)
end
it 'saves project successfully' do
expect(project_tree_saver.save).to be true
end
it 'saves the correct json' do
project_tree_saver.save
expect(project_json(project_tree_saver.full_path)).to include({ "name" => project.name })
end
end
def project_json(filename)
JSON.parse(IO.read(filename))
end
# TODO: Remove this. Current JSON pretty printed:
# {
# "id": 1,
# "name": "searchable_project",
# "path": "gitlabhq",
# "description": null,
# "issues_enabled": true,
# "wall_enabled": false,
# "merge_requests_enabled": true,
# "wiki_enabled": true,
# "snippets_enabled": true,
# "visibility_level": 20,
# "archived": false,
# "issues": [
#
# ],
# "merge_requests": [
#
# ],
# "labels": [
#
# ],
# "milestones": [
#
# ],
# "snippets": [
#
# ],
# "releases": [
#
# ],
# "events": [
# {
# "id": 1,
# "target_type": null,
# "target_id": null,
# "title": null,
# "data": null,
# "project_id": 1,
# "created_at": "2016-03-07T17:05:20.926Z",
# "updated_at": "2016-03-07T17:05:20.926Z",
# "action": 8,
# "author_id": 3
# }
# ],
# "commit_statuses": [
#
# ]
# }
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment