diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index bf67f75b4..d984bdcd5 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -70,7 +70,7 @@ class Api::V1::ProjectsController < Api::V1::BaseController def fork owner = (Group.find params[:group_id] if params[:group_id].present?) || current_user authorize! :write, owner if owner.class == Group - if forked = @project.fork(owner, params[:fork_name]) and forked.valid? + if forked = @project.fork(owner, new_name: params[:fork_name]) and forked.valid? render_json_response forked, 'Project has been forked successfully' else render_validation_error forked, 'Project has not been forked' diff --git a/app/controllers/projects/projects_controller.rb b/app/controllers/projects/projects_controller.rb index ef69a882c..9a5f5a23f 100644 --- a/app/controllers/projects/projects_controller.rb +++ b/app/controllers/projects/projects_controller.rb @@ -119,7 +119,7 @@ class Projects::ProjectsController < Projects::BaseController authorize! :write, owner if owner.class == Group is_alias = params[:alias] == 'true' - if forked = @project.fork(owner, params[:fork_name], is_alias) and forked.valid? + if forked = @project.fork(owner, new_name: params[:fork_name], is_alias: is_alias) and forked.valid? redirect_to forked, notice: t("flash.project.forked") else flash[:warning] = t("flash.project.fork_error") diff --git a/app/models/concerns/git.rb b/app/models/concerns/git.rb index ae297a617..714648ee9 100644 --- a/app/models/concerns/git.rb +++ b/app/models/concerns/git.rb @@ -173,6 +173,8 @@ module Git # Create link for GIT FileUtils.ln_sf alias_path, alias_from.path end + # Create folder + FileUtils.mkdir_p File.join(APP_CONFIG['git_path'], 'git_projects', owner_uname || owner.uname) # Create link for GIT FileUtils.ln_sf alias_path, path else diff --git a/app/models/project.rb b/app/models/project.rb index 45046c66a..0d33a5720 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -219,7 +219,7 @@ class Project < ActiveRecord::Base end end - def fork(new_owner, new_name = name, is_alias = false) + def fork(new_owner, new_name: nil, is_alias: false) new_name = new_name.presence || name dup.tap do |c| c.name = new_name diff --git a/app/views/projects/projects/edit.html.slim b/app/views/projects/projects/edit.html.slim index 4e58d99fa..6089156f4 100644 --- a/app/views/projects/projects/edit.html.slim +++ b/app/views/projects/projects/edit.html.slim @@ -11,7 +11,7 @@ == render "form", f: f == render 'build_schedule' - - if true #@project_aliases.present? + - if @project_aliases.present? hr.col-sm-12 == render 'aliases' diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index b3c2daf17..fc1e27d22 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3,43 +3,82 @@ require 'spec_helper' describe Project do before { stub_symlink_methods } - context 'creation' do - let(:root_project) { FactoryGirl.create(:project) } - let(:child_project) { root_project.fork(FactoryGirl.create(:user)) } + context '#fork' do + let(:root_project) { FactoryGirl.create(:project) } + let(:child_project) { root_project.fork(FactoryGirl.create(:user)) } let(:child_child_project) { child_project.fork(FactoryGirl.create(:user)) } + let(:alias_project) { root_project.fork(FactoryGirl.create(:user), is_alias: true) } + let(:alias_alias_project) { alias_project.fork(FactoryGirl.create(:user), is_alias: true) } - it { root_project } - it { child_project } - it { child_child_project } - end + context 'creation' do - context 'for destroy' do - let!(:root_project) { FactoryGirl.create(:project) } - let!(:child_project) { root_project.fork(FactoryGirl.create(:user)) } - let!(:child_child_project) { child_project.fork(FactoryGirl.create(:user)) } + it { root_project } - context 'root project' do - before { root_project.destroy } - - it "should not be delete child" do - Project.where(id: child_project).count.should == 1 + it 'creates child project' do + expect(child_project).to be_valid + expect(child_project.parent).to eq(root_project) + expect(child_project.alias_from).to be_nil + expect{ Grit::Repo.new(child_project.path) }.to_not raise_exception end - it "should not be delete child of the child" do - Project.where(id: child_child_project).count.should == 1 + it 'creates child-child project' do + expect(child_child_project).to be_valid + expect(child_child_project.parent).to eq(child_project) + expect(child_child_project.alias_from).to be_nil + expect{ Grit::Repo.new(child_child_project.path) }.to_not raise_exception + end + + it 'creates alias project' do + expect(alias_project).to be_valid + expect(alias_project.parent).to eq(root_project) + expect(alias_project.alias_from).to eq(root_project) + expect{ Grit::Repo.new(alias_project.path) }.to_not raise_exception + end + + it 'creates alias-alias project' do + expect(alias_alias_project).to be_valid + expect(alias_alias_project.parent).to eq(alias_project) + expect(alias_alias_project.alias_from).to eq(root_project) + expect{ Grit::Repo.new(alias_alias_project.path) }.to_not raise_exception end end - pending 'when will be available orphan_strategy: :adopt' do - context 'middle node' do - before{ child_project.destroy } + context 'for destroy' do - it "should set root project as a parent for orphan child" do - Project.find(child_child_project).ancestry == root_project - end + it 'root project' do + child_child_project # init chain of projects + expect do + root_project.destroy + end.to change(Project, :count).by(-1) + end - it "should not be delete child of the child" do - Project.where(id: child_child_project).count.should == 1 + it 'middle child node' do + child_child_project # init chain of projects + expect do + child_project.destroy + end.to change(Project, :count).by(-1) + end + + it 'middle alias node' do + alias_alias_project # init chain of projects + expect do + alias_project.destroy + end.to change(Project, :count).by(-1) + expect{ Grit::Repo.new(root_project.path) }.to_not raise_exception + expect{ Grit::Repo.new(alias_project.path) }.to raise_exception + end + + pending 'when will be available orphan_strategy: :adopt' do + context 'middle node' do + before{ child_project.destroy } + + it "should set root project as a parent for orphan child" do + Project.find(child_child_project).ancestry == root_project + end + + it "should not be delete child of the child" do + Project.where(id: child_child_project).count.should == 1 + end end end end