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
22e1bcaf
Commit
22e1bcaf
authored
Jan 30, 2020
by
Rajendra Kadam
Committed by
Stan Hu
Jan 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate project classes into own class files
parent
64e213a3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
119 additions
and
89 deletions
+119
-89
changelogs/unreleased/refactoring-entities-file-5.yml
changelogs/unreleased/refactoring-entities-file-5.yml
+5
-0
lib/api/entities.rb
lib/api/entities.rb
+0
-89
lib/api/entities/basic_project_details.rb
lib/api/entities/basic_project_details.rb
+55
-0
lib/api/entities/container_expiration_policy.rb
lib/api/entities/container_expiration_policy.rb
+14
-0
lib/api/entities/project_import_status.rb
lib/api/entities/project_import_status.rb
+14
-0
lib/api/entities/project_statistics.rb
lib/api/entities/project_statistics.rb
+14
-0
lib/api/entities/remote_mirror.rb
lib/api/entities/remote_mirror.rb
+17
-0
No files found.
changelogs/unreleased/refactoring-entities-file-5.yml
0 → 100644
View file @
22e1bcaf
---
title
:
Add separate classes for project related classes
merge_request
:
23887
author
:
Rajendra Kadam
type
:
added
lib/api/entities.rb
View file @
22e1bcaf
...
...
@@ -2,86 +2,6 @@
module
API
module
Entities
class
RemoteMirror
<
Grape
::
Entity
expose
:id
expose
:enabled
expose
:safe_url
,
as: :url
expose
:update_status
expose
:last_update_at
expose
:last_update_started_at
expose
:last_successful_update_at
expose
:last_error
expose
:only_protected_branches
end
class
ContainerExpirationPolicy
<
Grape
::
Entity
expose
:cadence
expose
:enabled
expose
:keep_n
expose
:older_than
expose
:name_regex
expose
:next_run_at
end
class
ProjectImportStatus
<
ProjectIdentity
expose
:import_status
# TODO: Use `expose_nil` once we upgrade the grape-entity gem
expose
:import_error
,
if:
lambda
{
|
project
,
_ops
|
project
.
import_state
&
.
last_error
}
do
|
project
|
project
.
import_state
.
last_error
end
end
class
BasicProjectDetails
<
ProjectIdentity
include
::
API
::
ProjectsRelationBuilder
expose
:default_branch
,
if:
->
(
project
,
options
)
{
Ability
.
allowed?
(
options
[
:current_user
],
:download_code
,
project
)
}
# Avoids an N+1 query: https://github.com/mbleigh/acts-as-taggable-on/issues/91#issuecomment-168273770
expose
:tag_list
do
|
project
|
# project.tags.order(:name).pluck(:name) is the most suitable option
# to avoid loading all the ActiveRecord objects but, if we use it here
# it override the preloaded associations and makes a query
# (fixed in https://github.com/rails/rails/pull/25976).
project
.
tags
.
map
(
&
:name
).
sort
end
expose
:ssh_url_to_repo
,
:http_url_to_repo
,
:web_url
,
:readme_url
expose
:license_url
,
if: :license
do
|
project
|
license
=
project
.
repository
.
license_blob
if
license
Gitlab
::
Routing
.
url_helpers
.
project_blob_url
(
project
,
File
.
join
(
project
.
default_branch
,
license
.
path
))
end
end
expose
:license
,
with:
'API::Entities::LicenseBasic'
,
if: :license
do
|
project
|
project
.
repository
.
license
end
expose
:avatar_url
do
|
project
,
options
|
project
.
avatar_url
(
only_path:
false
)
end
expose
:star_count
,
:forks_count
expose
:last_activity_at
expose
:namespace
,
using:
'API::Entities::NamespaceBasic'
expose
:custom_attributes
,
using:
'API::Entities::CustomAttribute'
,
if: :with_custom_attributes
# rubocop: disable CodeReuse/ActiveRecord
def
self
.
preload_relation
(
projects_relation
,
options
=
{})
# Preloading tags, should be done with using only `:tags`,
# as `:tags` are defined as: `has_many :tags, through: :taggings`
# N+1 is solved then by using `subject.tags.map(&:name)`
# MR describing the solution: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/20555
projects_relation
.
preload
(
:project_feature
,
:route
)
.
preload
(
:import_state
,
:tags
)
.
preload
(
:auto_devops
)
.
preload
(
namespace:
[
:route
,
:owner
])
end
# rubocop: enable CodeReuse/ActiveRecord
end
class
Project
<
BasicProjectDetails
include
::
API
::
Helpers
::
RelatedResourcesHelpers
...
...
@@ -204,15 +124,6 @@ module API
end
end
class
ProjectStatistics
<
Grape
::
Entity
expose
:commit_count
expose
:storage_size
expose
:repository_size
expose
:wiki_size
expose
:lfs_objects_size
expose
:build_artifacts_size
,
as: :job_artifacts_size
end
class
ProjectDailyFetches
<
Grape
::
Entity
expose
:fetch_count
,
as: :count
expose
:date
...
...
lib/api/entities/basic_project_details.rb
0 → 100644
View file @
22e1bcaf
# frozen_string_literal: true
module
API
module
Entities
class
BasicProjectDetails
<
ProjectIdentity
include
::
API
::
ProjectsRelationBuilder
expose
:default_branch
,
if:
->
(
project
,
options
)
{
Ability
.
allowed?
(
options
[
:current_user
],
:download_code
,
project
)
}
# Avoids an N+1 query: https://github.com/mbleigh/acts-as-taggable-on/issues/91#issuecomment-168273770
expose
:tag_list
do
|
project
|
# project.tags.order(:name).pluck(:name) is the most suitable option
# to avoid loading all the ActiveRecord objects but, if we use it here
# it override the preloaded associations and makes a query
# (fixed in https://github.com/rails/rails/pull/25976).
project
.
tags
.
map
(
&
:name
).
sort
end
expose
:ssh_url_to_repo
,
:http_url_to_repo
,
:web_url
,
:readme_url
expose
:license_url
,
if: :license
do
|
project
|
license
=
project
.
repository
.
license_blob
if
license
Gitlab
::
Routing
.
url_helpers
.
project_blob_url
(
project
,
File
.
join
(
project
.
default_branch
,
license
.
path
))
end
end
expose
:license
,
with:
'API::Entities::LicenseBasic'
,
if: :license
do
|
project
|
project
.
repository
.
license
end
expose
:avatar_url
do
|
project
,
options
|
project
.
avatar_url
(
only_path:
false
)
end
expose
:star_count
,
:forks_count
expose
:last_activity_at
expose
:namespace
,
using:
'API::Entities::NamespaceBasic'
expose
:custom_attributes
,
using:
'API::Entities::CustomAttribute'
,
if: :with_custom_attributes
# rubocop: disable CodeReuse/ActiveRecord
def
self
.
preload_relation
(
projects_relation
,
options
=
{})
# Preloading tags, should be done with using only `:tags`,
# as `:tags` are defined as: `has_many :tags, through: :taggings`
# N+1 is solved then by using `subject.tags.map(&:name)`
# MR describing the solution: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/20555
projects_relation
.
preload
(
:project_feature
,
:route
)
.
preload
(
:import_state
,
:tags
)
.
preload
(
:auto_devops
)
.
preload
(
namespace:
[
:route
,
:owner
])
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
lib/api/entities/container_expiration_policy.rb
0 → 100644
View file @
22e1bcaf
# frozen_string_literal: true
module
API
module
Entities
class
ContainerExpirationPolicy
<
Grape
::
Entity
expose
:cadence
expose
:enabled
expose
:keep_n
expose
:older_than
expose
:name_regex
expose
:next_run_at
end
end
end
lib/api/entities/project_import_status.rb
0 → 100644
View file @
22e1bcaf
# frozen_string_literal: true
module
API
module
Entities
class
ProjectImportStatus
<
ProjectIdentity
expose
:import_status
# TODO: Use `expose_nil` once we upgrade the grape-entity gem
expose
:import_error
,
if:
lambda
{
|
project
,
_ops
|
project
.
import_state
&
.
last_error
}
do
|
project
|
project
.
import_state
.
last_error
end
end
end
end
lib/api/entities/project_statistics.rb
0 → 100644
View file @
22e1bcaf
# frozen_string_literal: true
module
API
module
Entities
class
ProjectStatistics
<
Grape
::
Entity
expose
:commit_count
expose
:storage_size
expose
:repository_size
expose
:wiki_size
expose
:lfs_objects_size
expose
:build_artifacts_size
,
as: :job_artifacts_size
end
end
end
lib/api/entities/remote_mirror.rb
0 → 100644
View file @
22e1bcaf
# frozen_string_literal: true
module
API
module
Entities
class
RemoteMirror
<
Grape
::
Entity
expose
:id
expose
:enabled
expose
:safe_url
,
as: :url
expose
:update_status
expose
:last_update_at
expose
:last_update_started_at
expose
:last_successful_update_at
expose
:last_error
expose
:only_protected_branches
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