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
c9871e84
Commit
c9871e84
authored
Dec 07, 2017
by
Francisco Javier López
Committed by
Douwe Maan
Dec 07, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
The API isn't using the appropriate services for managing forks
parent
af09fb85
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
271 additions
and
173 deletions
+271
-173
app/services/projects/fork_service.rb
app/services/projects/fork_service.rb
+29
-7
changelogs/unreleased/fj-40752-forks-api-not-using-services.yml
...logs/unreleased/fj-40752-forks-api-not-using-services.yml
+5
-0
db/post_migrate/20171205190711_reschedule_fork_network_creation_caller.rb
...20171205190711_reschedule_fork_network_creation_caller.rb
+27
-0
db/schema.rb
db/schema.rb
+1
-1
lib/api/projects.rb
lib/api/projects.rb
+11
-10
spec/services/projects/fork_service_spec.rb
spec/services/projects/fork_service_spec.rb
+198
-155
No files found.
app/services/projects/fork_service.rb
View file @
c9871e84
module
Projects
class
ForkService
<
BaseService
def
execute
def
execute
(
fork_to_project
=
nil
)
if
fork_to_project
link_existing_project
(
fork_to_project
)
else
fork_new_project
end
end
private
def
link_existing_project
(
fork_to_project
)
return
if
fork_to_project
.
forked?
link_fork_network
(
fork_to_project
)
fork_to_project
end
def
fork_new_project
new_params
=
{
forked_from_project_id:
@project
.
id
,
visibility_level:
allowed_visibility_level
,
...
...
@@ -21,15 +39,11 @@ module Projects
builds_access_level
=
@project
.
project_feature
.
builds_access_level
new_project
.
project_feature
.
update_attributes
(
builds_access_level:
builds_access_level
)
refresh_forks_count
link_fork_network
(
new_project
)
new_project
end
private
def
fork_network
if
@project
.
fork_network
@project
.
fork_network
...
...
@@ -43,9 +57,17 @@ module Projects
end
end
def
link_fork_network
(
new
_project
)
fork_network
.
fork_network_members
.
create
(
project:
new
_project
,
def
link_fork_network
(
fork_to
_project
)
fork_network
.
fork_network_members
.
create
(
project:
fork_to
_project
,
forked_from_project:
@project
)
# TODO: remove this when ForkedProjectLink model is removed
unless
fork_to_project
.
forked_project_link
fork_to_project
.
create_forked_project_link
(
forked_to_project:
fork_to_project
,
forked_from_project:
@project
)
end
refresh_forks_count
end
def
refresh_forks_count
...
...
changelogs/unreleased/fj-40752-forks-api-not-using-services.yml
0 → 100644
View file @
c9871e84
---
title
:
Using appropiate services in the API for managing forks
merge_request
:
15709
author
:
type
:
fixed
db/post_migrate/20171205190711_reschedule_fork_network_creation_caller.rb
0 → 100644
View file @
c9871e84
class
RescheduleForkNetworkCreationCaller
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
MIGRATION
=
'PopulateForkNetworksRange'
.
freeze
BATCH_SIZE
=
100
DELAY_INTERVAL
=
15
.
seconds
disable_ddl_transaction!
class
ForkedProjectLink
<
ActiveRecord
::
Base
include
EachBatch
self
.
table_name
=
'forked_project_links'
end
def
up
say
'Populating the `fork_networks` based on existing `forked_project_links`'
queue_background_migration_jobs_by_range_at_intervals
(
ForkedProjectLink
,
MIGRATION
,
DELAY_INTERVAL
,
batch_size:
BATCH_SIZE
)
end
def
down
# nothing
end
end
db/schema.rb
View file @
c9871e84
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
20171
124150326
)
do
ActiveRecord
::
Schema
.
define
(
version:
20171
205190711
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
lib/api/projects.rb
View file @
c9871e84
...
...
@@ -367,15 +367,16 @@ module API
post
":id/fork/:forked_from_id"
do
authenticated_as_admin!
forked_from_project
=
find_project!
(
params
[
:forked_from_id
])
not_found!
(
"Source Project"
)
unless
forked_from_project
fork_from_project
=
find_project!
(
params
[
:forked_from_id
])
if
user_project
.
forked_from_project
.
nil?
user_project
.
create_forked_project_link
(
forked_to_project_id:
user_project
.
id
,
forked_from_project_id:
forked_from_project
.
id
)
not_found!
(
"Source Project"
)
unless
fork_from_project
::
Projects
::
ForksCountService
.
new
(
forked_from_project
).
refresh_cache
result
=
::
Projects
::
ForkService
.
new
(
fork_from_project
,
current_user
).
execute
(
user_project
)
if
result
present
user_project
.
reload
,
with:
Entities
::
Project
else
render_api_error!
(
"Project already forked"
,
409
)
render_api_error!
(
"Project already forked"
,
409
)
if
user_project
.
forked?
end
end
...
...
@@ -383,11 +384,11 @@ module API
delete
":id/fork"
do
authorize!
:remove_fork_project
,
user_project
if
user_project
.
forked?
destroy_conditionally!
(
user_project
.
forked_project_link
)
else
not_modified!
result
=
destroy_conditionally!
(
user_project
)
do
::
Projects
::
UnlinkForkService
.
new
(
user_project
,
current_user
).
execute
end
result
?
status
(
204
)
:
not_modified!
end
desc
'Share the project with a group'
do
...
...
spec/services/projects/fork_service_spec.rb
View file @
c9871e84
...
...
@@ -3,7 +3,7 @@ require 'spec_helper'
describe
Projects
::
ForkService
do
include
ProjectForksHelper
let
(
:gitlab_shell
)
{
Gitlab
::
Shell
.
new
}
context
'when forking a new project'
do
describe
'fork by user'
do
before
do
@from_user
=
create
(
:user
)
...
...
@@ -210,4 +210,47 @@ describe Projects::ForkService do
end
end
end
end
context
'when linking fork to an existing project'
do
let
(
:fork_from_project
)
{
create
(
:project
,
:public
)
}
let
(
:fork_to_project
)
{
create
(
:project
,
:public
)
}
let
(
:user
)
{
create
(
:user
)
}
subject
{
described_class
.
new
(
fork_from_project
,
user
)
}
def
forked_from_project
(
project
)
project
.
fork_network_member
&
.
forked_from_project
end
context
'if project is already forked'
do
it
'does not create fork relation'
do
allow
(
fork_to_project
).
to
receive
(
:forked?
).
and_return
(
true
)
expect
(
forked_from_project
(
fork_to_project
)).
to
be_nil
expect
(
subject
.
execute
(
fork_to_project
)).
to
be_nil
expect
(
forked_from_project
(
fork_to_project
)).
to
be_nil
end
end
context
'if project is not forked'
do
it
'creates fork relation'
do
expect
(
fork_to_project
.
forked?
).
to
be
false
expect
(
forked_from_project
(
fork_to_project
)).
to
be_nil
subject
.
execute
(
fork_to_project
)
expect
(
fork_to_project
.
forked?
).
to
be
true
expect
(
forked_from_project
(
fork_to_project
)).
to
eq
fork_from_project
expect
(
fork_to_project
.
forked_from_project
).
to
eq
fork_from_project
end
it
'flushes the forks count cache of the source project'
do
expect
(
fork_from_project
.
forks_count
).
to
be_zero
subject
.
execute
(
fork_to_project
)
expect
(
fork_from_project
.
forks_count
).
to
eq
(
1
)
end
end
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