Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
0cf76a0b
Commit
0cf76a0b
authored
Apr 24, 2015
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use Projects::CreateService to fork projects so that after-create hooks are run.
parent
62117f2f
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
42 additions
and
69 deletions
+42
-69
app/models/project.rb
app/models/project.rb
+14
-4
app/services/projects/create_service.rb
app/services/projects/create_service.rb
+7
-1
app/services/projects/fork_service.rb
app/services/projects/fork_service.rb
+14
-52
app/views/events/event/_created_project.html.haml
app/views/events/event/_created_project.html.haml
+1
-1
spec/requests/api/fork_spec.rb
spec/requests/api/fork_spec.rb
+0
-1
spec/services/projects/fork_service_spec.rb
spec/services/projects/fork_service_spec.rb
+6
-10
No files found.
app/models/project.rb
View file @
0cf76a0b
...
...
@@ -686,6 +686,15 @@ class Project < ActiveRecord::Base
end
def
create_repository
if
forked?
if
gitlab_shell
.
fork_repository
(
forked_from_project
.
path_with_namespace
,
self
.
namespace
.
path
)
ensure_satellite_exists
true
else
errors
.
add
(
:base
,
'Failed to fork repository'
)
false
end
else
if
gitlab_shell
.
add_repository
(
path_with_namespace
)
true
else
...
...
@@ -693,6 +702,7 @@ class Project < ActiveRecord::Base
false
end
end
end
def
repository_exists?
!!
repository
.
exists?
...
...
app/services/projects/create_service.rb
View file @
0cf76a0b
...
...
@@ -5,6 +5,8 @@ module Projects
end
def
execute
forked_from_project_id
=
params
.
delete
(
:forked_from_project_id
)
@project
=
Project
.
new
(
params
)
# Make sure that the user is allowed to use the specified visibility
...
...
@@ -45,10 +47,14 @@ module Projects
@project
.
creator
=
current_user
if
forked_from_project_id
@project
.
build_forked_project_link
(
forked_from_project_id:
forked_from_project_id
)
end
Project
.
transaction
do
@project
.
save
unless
@project
.
import?
if
@project
.
persisted?
&&
!
@project
.
import?
unless
@project
.
create_repository
raise
'Failed to create repository'
end
...
...
app/services/projects/fork_service.rb
View file @
0cf76a0b
module
Projects
class
ForkService
<
BaseService
include
Gitlab
::
ShellAdapter
def
execute
@from_project
=
@project
project_params
=
{
visibility_level:
@from_project
.
visibility_level
,
description:
@from_project
.
description
,
new_params
=
{
forked_from_project_id:
@project
.
id
,
visibility_level:
@project
.
visibility_level
,
description:
@project
.
description
,
name:
@project
.
name
,
path:
@project
.
path
,
namespace_id:
@params
[
:namespace
].
try
(
:id
)
||
current_user
.
namespace
.
id
}
project
=
Project
.
new
(
project_params
)
project
.
name
=
@from_project
.
name
project
.
path
=
@from_project
.
path
project
.
creator
=
@current_user
if
@from_project
.
avatar
.
present?
&&
@from_project
.
avatar
.
image?
project
.
avatar
=
@from_project
.
avatar
end
if
namespace
=
@params
[
:namespace
]
project
.
namespace
=
namespace
else
project
.
namespace
=
@current_user
.
namespace
end
unless
@current_user
.
can?
(
:create_projects
,
project
.
namespace
)
project
.
errors
.
add
(
:namespace
,
'insufficient access rights'
)
return
project
if
@project
.
avatar
.
present?
&&
@project
.
avatar
.
image?
new_params
[
:avatar
]
=
@project
.
avatar
end
# If the project cannot save, we do not want to trigger the project destroy
# as this can have the side effect of deleting a repo attached to an existing
# project with the same name and namespace
if
project
.
valid?
begin
Project
.
transaction
do
#First save the DB entries as they can be rolled back if the repo fork fails
project
.
build_forked_project_link
(
forked_to_project_id:
project
.
id
,
forked_from_project_id:
@from_project
.
id
)
if
project
.
save
project
.
team
<<
[
@current_user
,
:master
,
@current_user
]
end
#Now fork the repo
unless
gitlab_shell
.
fork_repository
(
@from_project
.
path_with_namespace
,
project
.
namespace
.
path
)
raise
'forking failed in gitlab-shell'
end
new_project
=
CreateService
.
new
(
current_user
,
new_params
).
execute
project
.
ensure_satellite_exists
end
if
@from_project
.
gitlab_ci?
ForkRegistrationWorker
.
perform_async
(
@from_project
.
id
,
project
.
id
,
@current_user
.
private_token
)
end
rescue
=>
ex
project
.
errors
.
add
(
:base
,
'Fork transaction failed.'
)
project
.
destroy
if
new_project
.
persisted?
if
@project
.
gitlab_ci?
ForkRegistrationWorker
.
perform_async
(
@project
.
id
,
new_project
.
id
,
@current_user
.
private_token
)
end
else
project
.
errors
.
add
(
:base
,
'Invalid fork destination'
)
end
project
new_
project
end
end
end
app/views/events/event/_created_project.html.haml
View file @
0cf76a0b
...
...
@@ -18,7 +18,7 @@
%a
.twitter-share-button
{
|
href:
"https://twitter.com/share"
,
|
"data-url"
=>
event
.
project
.
web_url
,
|
"data-text"
=>
"I just #{event.
project.imported? ? "
imported
" : "
created
"} a new project i
n GitLab! GitLab is version control on your server."
,
|
"data-text"
=>
"I just #{event.
action_name} a new project o
n GitLab! GitLab is version control on your server."
,
|
"data-size"
=>
"medium"
,
|
"data-related"
=>
"gitlab"
,
|
"data-hashtags"
=>
"gitlab"
,
|
...
...
spec/requests/api/fork_spec.rb
View file @
0cf76a0b
...
...
@@ -50,7 +50,6 @@ describe API::API, api: true do
it
'should fail if forked project exists in the user namespace'
do
post
api
(
"/projects/fork/
#{
project
.
id
}
"
,
user
)
expect
(
response
.
status
).
to
eq
(
409
)
expect
(
json_response
[
'message'
][
'base'
]).
to
eq
([
'Invalid fork destination'
])
expect
(
json_response
[
'message'
][
'name'
]).
to
eq
([
'has already been taken'
])
expect
(
json_response
[
'message'
][
'path'
]).
to
eq
([
'has already been taken'
])
end
...
...
spec/services/projects/fork_service_spec.rb
View file @
0cf76a0b
...
...
@@ -27,7 +27,7 @@ describe Projects::ForkService do
it
"fails due to transaction failure"
do
@to_project
=
fork_project
(
@from_project
,
@to_user
,
false
)
expect
(
@to_project
.
errors
).
not_to
be_empty
expect
(
@to_project
.
errors
[
:base
]).
to
include
(
"F
ork transaction failed.
"
)
expect
(
@to_project
.
errors
[
:base
]).
to
include
(
"F
ailed to fork repository
"
)
end
end
...
...
@@ -36,8 +36,8 @@ describe Projects::ForkService do
@existing_project
=
create
(
:project
,
creator_id:
@to_user
.
id
,
name:
@from_project
.
name
,
namespace:
@to_namespace
)
@to_project
=
fork_project
(
@from_project
,
@to_user
)
expect
(
@existing_project
.
persisted?
).
to
be_truthy
expect
(
@to_project
.
errors
[
:
base
]).
to
include
(
"Invalid fork destination"
)
expect
(
@to_project
.
errors
[
:
base
]).
not_to
include
(
"Fork transaction failed."
)
expect
(
@to_project
.
errors
[
:
name
]).
to
eq
([
'has already been taken'
]
)
expect
(
@to_project
.
errors
[
:
path
]).
to
eq
([
'has already been taken'
]
)
end
end
...
...
@@ -81,7 +81,7 @@ describe Projects::ForkService do
context
'fork project for group when user not owner'
do
it
'group developer should fail to fork project into the group'
do
to_project
=
fork_project
(
@project
,
@developer
,
true
,
@opts
)
expect
(
to_project
.
errors
[
:namespace
]).
to
eq
([
'i
nsufficient access rights
'
])
expect
(
to_project
.
errors
[
:namespace
]).
to
eq
([
'i
s not valid
'
])
end
end
...
...
@@ -91,7 +91,6 @@ describe Projects::ForkService do
namespace:
@group
)
to_project
=
fork_project
(
@project
,
@group_owner
,
true
,
@opts
)
expect
(
existing_project
.
persisted?
).
to
be_truthy
expect
(
to_project
.
errors
[
:base
]).
to
eq
([
'Invalid fork destination'
])
expect
(
to_project
.
errors
[
:name
]).
to
eq
([
'has already been taken'
])
expect
(
to_project
.
errors
[
:path
]).
to
eq
([
'has already been taken'
])
end
...
...
@@ -99,10 +98,7 @@ describe Projects::ForkService do
end
def
fork_project
(
from_project
,
user
,
fork_success
=
true
,
params
=
{})
context
=
Projects
::
ForkService
.
new
(
from_project
,
user
,
params
)
shell
=
double
(
'gitlab_shell'
)
shell
.
stub
(
fork_repository:
fork_success
)
context
.
stub
(
gitlab_shell:
shell
)
context
.
execute
allow_any_instance_of
(
Gitlab
::
Shell
).
to
receive
(
:fork_repository
).
and_return
(
fork_success
)
Projects
::
ForkService
.
new
(
from_project
,
user
,
params
).
execute
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment