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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
e645ab22
Commit
e645ab22
authored
Aug 15, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'forks-count-cache' into 'master'
Cache the number of forks of a project See merge request !13535
parents
19cdcb6c
aef9f1eb
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
141 additions
and
1 deletion
+141
-1
app/models/project.rb
app/models/project.rb
+4
-1
app/services/projects/destroy_service.rb
app/services/projects/destroy_service.rb
+2
-0
app/services/projects/fork_service.rb
app/services/projects/fork_service.rb
+6
-0
app/services/projects/forks_count_service.rb
app/services/projects/forks_count_service.rb
+30
-0
app/services/projects/unlink_fork_service.rb
app/services/projects/unlink_fork_service.rb
+6
-0
changelogs/unreleased/forks-count-cache.yml
changelogs/unreleased/forks-count-cache.yml
+5
-0
lib/api/projects.rb
lib/api/projects.rb
+2
-0
lib/api/v3/projects.rb
lib/api/v3/projects.rb
+2
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+10
-0
spec/requests/api/projects_spec.rb
spec/requests/api/projects_spec.rb
+8
-0
spec/requests/api/v3/projects_spec.rb
spec/requests/api/v3/projects_spec.rb
+8
-0
spec/services/projects/fork_service_spec.rb
spec/services/projects/fork_service_spec.rb
+8
-0
spec/services/projects/forks_count_service_spec.rb
spec/services/projects/forks_count_service_spec.rb
+40
-0
spec/services/projects/unlink_fork_service_spec.rb
spec/services/projects/unlink_fork_service_spec.rb
+10
-0
No files found.
app/models/project.rb
View file @
e645ab22
...
@@ -196,7 +196,6 @@ class Project < ActiveRecord::Base
...
@@ -196,7 +196,6 @@ class Project < ActiveRecord::Base
accepts_nested_attributes_for
:import_data
accepts_nested_attributes_for
:import_data
delegate
:name
,
to: :owner
,
allow_nil:
true
,
prefix:
true
delegate
:name
,
to: :owner
,
allow_nil:
true
,
prefix:
true
delegate
:count
,
to: :forks
,
prefix:
true
delegate
:members
,
to: :team
,
prefix:
true
delegate
:members
,
to: :team
,
prefix:
true
delegate
:add_user
,
:add_users
,
to: :team
delegate
:add_user
,
:add_users
,
to: :team
delegate
:add_guest
,
:add_reporter
,
:add_developer
,
:add_master
,
to: :team
delegate
:add_guest
,
:add_reporter
,
:add_developer
,
:add_master
,
to: :team
...
@@ -1396,6 +1395,10 @@ class Project < ActiveRecord::Base
...
@@ -1396,6 +1395,10 @@ class Project < ActiveRecord::Base
# @deprecated cannot remove yet because it has an index with its name in elasticsearch
# @deprecated cannot remove yet because it has an index with its name in elasticsearch
alias_method
:path_with_namespace
,
:full_path
alias_method
:path_with_namespace
,
:full_path
def
forks_count
Projects
::
ForksCountService
.
new
(
self
).
count
end
private
private
def
cross_namespace_reference?
(
from
)
def
cross_namespace_reference?
(
from
)
...
...
app/services/projects/destroy_service.rb
View file @
e645ab22
...
@@ -128,6 +128,8 @@ module Projects
...
@@ -128,6 +128,8 @@ module Projects
project
.
repository
.
before_delete
project
.
repository
.
before_delete
Repository
.
new
(
wiki_path
,
project
,
disk_path:
repo_path
).
before_delete
Repository
.
new
(
wiki_path
,
project
,
disk_path:
repo_path
).
before_delete
Projects
::
ForksCountService
.
new
(
project
).
delete_cache
end
end
end
end
end
end
app/services/projects/fork_service.rb
View file @
e645ab22
...
@@ -21,11 +21,17 @@ module Projects
...
@@ -21,11 +21,17 @@ module Projects
builds_access_level
=
@project
.
project_feature
.
builds_access_level
builds_access_level
=
@project
.
project_feature
.
builds_access_level
new_project
.
project_feature
.
update_attributes
(
builds_access_level:
builds_access_level
)
new_project
.
project_feature
.
update_attributes
(
builds_access_level:
builds_access_level
)
refresh_forks_count
new_project
new_project
end
end
private
private
def
refresh_forks_count
Projects
::
ForksCountService
.
new
(
@project
).
refresh_cache
end
def
allowed_visibility_level
def
allowed_visibility_level
project_level
=
@project
.
visibility_level
project_level
=
@project
.
visibility_level
...
...
app/services/projects/forks_count_service.rb
0 → 100644
View file @
e645ab22
module
Projects
# Service class for getting and caching the number of forks of a project.
class
ForksCountService
def
initialize
(
project
)
@project
=
project
end
def
count
Rails
.
cache
.
fetch
(
cache_key
)
{
uncached_count
}
end
def
refresh_cache
Rails
.
cache
.
write
(
cache_key
,
uncached_count
)
end
def
delete_cache
Rails
.
cache
.
delete
(
cache_key
)
end
private
def
uncached_count
@project
.
forks
.
count
end
def
cache_key
[
'projects'
,
@project
.
id
,
'forks_count'
]
end
end
end
app/services/projects/unlink_fork_service.rb
View file @
e645ab22
...
@@ -13,7 +13,13 @@ module Projects
...
@@ -13,7 +13,13 @@ module Projects
::
MergeRequests
::
CloseService
.
new
(
@project
,
@current_user
).
execute
(
mr
)
::
MergeRequests
::
CloseService
.
new
(
@project
,
@current_user
).
execute
(
mr
)
end
end
refresh_forks_count
(
@project
.
forked_from_project
)
@project
.
forked_project_link
.
destroy
@project
.
forked_project_link
.
destroy
end
end
def
refresh_forks_count
(
project
)
Projects
::
ForksCountService
.
new
(
project
).
refresh_cache
end
end
end
end
end
changelogs/unreleased/forks-count-cache.yml
0 → 100644
View file @
e645ab22
---
title
:
Cache the number of forks of a project
merge_request
:
13535
author
:
type
:
other
lib/api/projects.rb
View file @
e645ab22
...
@@ -351,6 +351,8 @@ module API
...
@@ -351,6 +351,8 @@ module API
if
user_project
.
forked_from_project
.
nil?
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
)
user_project
.
create_forked_project_link
(
forked_to_project_id:
user_project
.
id
,
forked_from_project_id:
forked_from_project
.
id
)
::
Projects
::
ForksCountService
.
new
(
forked_from_project
).
refresh_cache
else
else
render_api_error!
(
"Project already forked"
,
409
)
render_api_error!
(
"Project already forked"
,
409
)
end
end
...
...
lib/api/v3/projects.rb
View file @
e645ab22
...
@@ -388,6 +388,8 @@ module API
...
@@ -388,6 +388,8 @@ module API
if
user_project
.
forked_from_project
.
nil?
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
)
user_project
.
create_forked_project_link
(
forked_to_project_id:
user_project
.
id
,
forked_from_project_id:
forked_from_project
.
id
)
::
Projects
::
ForksCountService
.
new
(
forked_from_project
).
refresh_cache
else
else
render_api_error!
(
"Project already forked"
,
409
)
render_api_error!
(
"Project already forked"
,
409
)
end
end
...
...
spec/models/project_spec.rb
View file @
e645ab22
...
@@ -2310,4 +2310,14 @@ describe Project do
...
@@ -2310,4 +2310,14 @@ describe Project do
end
end
end
end
end
end
describe
'#forks_count'
do
it
'returns the number of forks'
do
project
=
build
(
:project
)
allow
(
project
.
forks
).
to
receive
(
:count
).
and_return
(
1
)
expect
(
project
.
forks_count
).
to
eq
(
1
)
end
end
end
end
spec/requests/api/projects_spec.rb
View file @
e645ab22
...
@@ -1065,6 +1065,14 @@ describe API::Projects do
...
@@ -1065,6 +1065,14 @@ describe API::Projects do
expect
(
project_fork_target
.
forked?
).
to
be_truthy
expect
(
project_fork_target
.
forked?
).
to
be_truthy
end
end
it
'refreshes the forks count cachce'
do
expect
(
project_fork_source
.
forks_count
).
to
be_zero
post
api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/
#{
project_fork_source
.
id
}
"
,
admin
)
expect
(
project_fork_source
.
forks_count
).
to
eq
(
1
)
end
it
'fails if forked_from project which does not exist'
do
it
'fails if forked_from project which does not exist'
do
post
api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/9999"
,
admin
)
post
api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/9999"
,
admin
)
expect
(
response
).
to
have_http_status
(
404
)
expect
(
response
).
to
have_http_status
(
404
)
...
...
spec/requests/api/v3/projects_spec.rb
View file @
e645ab22
...
@@ -1004,6 +1004,14 @@ describe API::V3::Projects do
...
@@ -1004,6 +1004,14 @@ describe API::V3::Projects do
expect
(
project_fork_target
.
forked?
).
to
be_truthy
expect
(
project_fork_target
.
forked?
).
to
be_truthy
end
end
it
'refreshes the forks count cachce'
do
expect
(
project_fork_source
.
forks_count
).
to
be_zero
post
v3_api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/
#{
project_fork_source
.
id
}
"
,
admin
)
expect
(
project_fork_source
.
forks_count
).
to
eq
(
1
)
end
it
'fails if forked_from project which does not exist'
do
it
'fails if forked_from project which does not exist'
do
post
v3_api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/9999"
,
admin
)
post
v3_api
(
"/projects/
#{
project_fork_target
.
id
}
/fork/9999"
,
admin
)
expect
(
response
).
to
have_http_status
(
404
)
expect
(
response
).
to
have_http_status
(
404
)
...
...
spec/services/projects/fork_service_spec.rb
View file @
e645ab22
...
@@ -50,6 +50,14 @@ describe Projects::ForkService do
...
@@ -50,6 +50,14 @@ describe Projects::ForkService do
expect
(
@from_project
.
avatar
.
file
).
to
be_exists
expect
(
@from_project
.
avatar
.
file
).
to
be_exists
end
end
it
'flushes the forks count cache of the source project'
do
expect
(
@from_project
.
forks_count
).
to
be_zero
fork_project
(
@from_project
,
@to_user
)
expect
(
@from_project
.
forks_count
).
to
eq
(
1
)
end
end
end
end
end
...
...
spec/services/projects/forks_count_service_spec.rb
0 → 100644
View file @
e645ab22
require
'spec_helper'
describe
Projects
::
ForksCountService
do
let
(
:project
)
{
build
(
:project
,
id:
42
)
}
let
(
:service
)
{
described_class
.
new
(
project
)
}
describe
'#count'
do
it
'returns the number of forks'
do
allow
(
service
).
to
receive
(
:uncached_count
).
and_return
(
1
)
expect
(
service
.
count
).
to
eq
(
1
)
end
it
'caches the forks count'
,
:use_clean_rails_memory_store_caching
do
expect
(
service
).
to
receive
(
:uncached_count
).
once
.
and_return
(
1
)
2
.
times
{
service
.
count
}
end
end
describe
'#refresh_cache'
,
:use_clean_rails_memory_store_caching
do
it
'refreshes the cache'
do
expect
(
service
).
to
receive
(
:uncached_count
).
once
.
and_return
(
1
)
service
.
refresh_cache
expect
(
service
.
count
).
to
eq
(
1
)
end
end
describe
'#delete_cache'
,
:use_clean_rails_memory_store_caching
do
it
'removes the cache'
do
expect
(
service
).
to
receive
(
:uncached_count
).
twice
.
and_return
(
1
)
service
.
count
service
.
delete_cache
service
.
count
end
end
end
spec/services/projects/unlink_fork_service_spec.rb
View file @
e645ab22
...
@@ -29,4 +29,14 @@ describe Projects::UnlinkForkService do
...
@@ -29,4 +29,14 @@ describe Projects::UnlinkForkService do
subject
.
execute
subject
.
execute
end
end
it
'refreshes the forks count cache of the source project'
do
source
=
fork_project
.
forked_from_project
expect
(
source
.
forks_count
).
to
eq
(
1
)
subject
.
execute
expect
(
source
.
forks_count
).
to
be_zero
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