#446: cleanup, added specs for Project#fork

This commit is contained in:
Vokhmin Alexey V 2015-01-14 23:56:26 +03:00
parent 351f12eab2
commit cba5ff2d5e
6 changed files with 71 additions and 30 deletions

View File

@ -70,7 +70,7 @@ class Api::V1::ProjectsController < Api::V1::BaseController
def fork def fork
owner = (Group.find params[:group_id] if params[:group_id].present?) || current_user owner = (Group.find params[:group_id] if params[:group_id].present?) || current_user
authorize! :write, owner if owner.class == Group 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' render_json_response forked, 'Project has been forked successfully'
else else
render_validation_error forked, 'Project has not been forked' render_validation_error forked, 'Project has not been forked'

View File

@ -119,7 +119,7 @@ class Projects::ProjectsController < Projects::BaseController
authorize! :write, owner if owner.class == Group authorize! :write, owner if owner.class == Group
is_alias = params[:alias] == 'true' 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") redirect_to forked, notice: t("flash.project.forked")
else else
flash[:warning] = t("flash.project.fork_error") flash[:warning] = t("flash.project.fork_error")

View File

@ -173,6 +173,8 @@ module Git
# Create link for GIT # Create link for GIT
FileUtils.ln_sf alias_path, alias_from.path FileUtils.ln_sf alias_path, alias_from.path
end end
# Create folder
FileUtils.mkdir_p File.join(APP_CONFIG['git_path'], 'git_projects', owner_uname || owner.uname)
# Create link for GIT # Create link for GIT
FileUtils.ln_sf alias_path, path FileUtils.ln_sf alias_path, path
else else

View File

@ -219,7 +219,7 @@ class Project < ActiveRecord::Base
end end
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 new_name = new_name.presence || name
dup.tap do |c| dup.tap do |c|
c.name = new_name c.name = new_name

View File

@ -11,7 +11,7 @@
== render "form", f: f == render "form", f: f
== render 'build_schedule' == render 'build_schedule'
- if true #@project_aliases.present? - if @project_aliases.present?
hr.col-sm-12 hr.col-sm-12
== render 'aliases' == render 'aliases'

View File

@ -3,31 +3,69 @@ require 'spec_helper'
describe Project do describe Project do
before { stub_symlink_methods } before { stub_symlink_methods }
context 'creation' do context '#fork' do
let(:root_project) { FactoryGirl.create(:project) } let(:root_project) { FactoryGirl.create(:project) }
let(:child_project) { root_project.fork(FactoryGirl.create(:user)) } let(:child_project) { root_project.fork(FactoryGirl.create(:user)) }
let(:child_child_project) { child_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) }
context 'creation' do
it { root_project } it { root_project }
it { child_project }
it { child_child_project } 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 '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 end
context 'for destroy' 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)) }
context 'root project' do it 'root project' do
before { root_project.destroy } child_child_project # init chain of projects
expect do
it "should not be delete child" do root_project.destroy
Project.where(id: child_project).count.should == 1 end.to change(Project, :count).by(-1)
end end
it "should not be delete child of the child" do it 'middle child node' do
Project.where(id: child_child_project).count.should == 1 child_child_project # init chain of projects
expect do
child_project.destroy
end.to change(Project, :count).by(-1)
end 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 end
pending 'when will be available orphan_strategy: :adopt' do pending 'when will be available orphan_strategy: :adopt' do
@ -44,6 +82,7 @@ describe Project do
end end
end end
end end
end
context 'attach personal repository' do context 'attach personal repository' do
let(:user) { FactoryGirl.create(:user) } let(:user) { FactoryGirl.create(:user) }