2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2012-06-27 07:32:56 -04:00
|
|
|
module Entities
|
2017-08-15 13:44:37 -04:00
|
|
|
class Project < BasicProjectDetails
|
2017-07-25 05:35:45 -04:00
|
|
|
include ::API::Helpers::RelatedResourcesHelpers
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :container_registry_url, as: :container_registry_image_prefix, documentation: { type: 'string', example: 'registry.gitlab.example.com/gitlab/gitlab-client' }, if: -> (_, _) { Gitlab.config.registry.enabled }
|
2021-03-03 07:11:16 -05:00
|
|
|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose :_links do
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :self, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4' } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :issues, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/issues' }, if: -> (project, options) { issues_available?(project, options) } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_issues_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :merge_requests, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/merge_requests' }, if: -> (project, options) { mrs_available?(project, options) } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_merge_requests_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :repo_branches, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/repository/branches' } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_repository_branches_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :labels, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/labels' } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_labels_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :events, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/events' } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_events_path(id: project.id))
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :members, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/members' } do |project|
|
2017-07-25 05:35:45 -04:00
|
|
|
expose_url(api_v4_projects_members_path(id: project.id))
|
|
|
|
end
|
2022-04-20 20:10:20 -04:00
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :cluster_agents, documentation: { type: 'string', example: 'https://gitlab.example.com/api/v4/projects/4/cluster_agents' } do |project|
|
2022-04-20 20:10:20 -04:00
|
|
|
expose_url(api_v4_projects_cluster_agents_path(id: project.id))
|
|
|
|
end
|
2017-07-25 05:35:45 -04:00
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :packages_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :empty_repo?, as: :empty_repo, documentation: { type: 'boolean' }
|
|
|
|
expose :archived?, as: :archived, documentation: { type: 'boolean' }
|
|
|
|
expose :visibility, documentation: { type: 'string', example: 'public' }
|
2014-03-17 08:33:41 -04:00
|
|
|
expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :resolve_outdated_diff_discussions, documentation: { type: 'boolean' }
|
2022-08-10 20:09:45 -04:00
|
|
|
expose :container_expiration_policy,
|
|
|
|
using: Entities::ContainerExpirationPolicy,
|
|
|
|
if: -> (project, _) { project.container_expiration_policy }
|
2016-08-01 18:31:21 -04:00
|
|
|
|
|
|
|
# Expose old field names with the new permissions methods to keep API compatible
|
2019-05-15 07:19:16 -04:00
|
|
|
# TODO: remove in API v5, replaced by *_access_level
|
2022-11-04 08:10:55 -04:00
|
|
|
expose(:issues_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:issues, options[:current_user]) }
|
|
|
|
expose(:merge_requests_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:merge_requests, options[:current_user]) }
|
|
|
|
expose(:wiki_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:wiki, options[:current_user]) }
|
|
|
|
expose(:jobs_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:builds, options[:current_user]) }
|
|
|
|
expose(:snippets_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
|
|
|
|
expose(:container_registry_enabled, documentation: { type: 'boolean' }) { |project, options| project.feature_available?(:container_registry, options[:current_user]) }
|
|
|
|
expose :service_desk_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :service_desk_address, documentation: { type: 'string', example: 'address@example.com' }, if: -> (project, options) do
|
2021-12-06 22:12:22 -05:00
|
|
|
Ability.allowed?(options[:current_user], :admin_issue, project)
|
|
|
|
end
|
2016-08-01 18:31:21 -04:00
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose(:can_create_merge_request_in, documentation: { type: 'boolean' }) do |project, options|
|
2020-01-30 07:08:54 -05:00
|
|
|
Ability.allowed?(options[:current_user], :create_merge_request_in, project)
|
|
|
|
end
|
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose(:issues_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :issues) }
|
|
|
|
expose(:repository_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :repository) }
|
|
|
|
expose(:merge_requests_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :merge_requests) }
|
|
|
|
expose(:forking_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :forking) }
|
|
|
|
expose(:wiki_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :wiki) }
|
|
|
|
expose(:builds_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :builds) }
|
|
|
|
expose(:snippets_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :snippets) }
|
|
|
|
expose(:pages_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :pages) }
|
|
|
|
expose(:operations_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :operations) }
|
|
|
|
expose(:analytics_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :analytics) }
|
|
|
|
expose(:container_registry_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :container_registry) }
|
|
|
|
expose(:security_and_compliance_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :security_and_compliance) }
|
|
|
|
expose(:releases_access_level, documentation: { type: 'string', example: 'enabled' }) { |project, options| project_feature_string_access_level(project, :releases) }
|
|
|
|
|
|
|
|
expose :emails_disabled, documentation: { type: 'boolean' }
|
|
|
|
expose :shared_runners_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :lfs_enabled?, as: :lfs_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :creator_id, documentation: { type: 'integer', example: 1 }
|
2019-11-19 11:17:35 -05:00
|
|
|
expose :forked_from_project, using: Entities::BasicProjectDetails, if: ->(project, options) do
|
|
|
|
project.forked? && Ability.allowed?(options[:current_user], :read_project, project.forked_from_project)
|
|
|
|
end
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :mr_default_target_self, if: -> (project) { project.forked? }, documentation: { type: 'boolean' }
|
2022-01-11 13:16:38 -05:00
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :import_url, documentation: { type: 'string', example: 'https://gitlab.com/gitlab/gitlab.git' }, if: -> (project, options) { Ability.allowed?(options[:current_user], :admin_project, project) } do |project|
|
2022-03-22 11:07:25 -04:00
|
|
|
project[:import_url]
|
|
|
|
end
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :import_type, documentation: { type: 'string', example: 'git' }, if: -> (project, options) { Ability.allowed?(options[:current_user], :admin_project, project) }
|
|
|
|
expose :import_status, documentation: { type: 'string', example: 'none' }
|
|
|
|
expose :import_error, documentation: { type: 'string', example: 'Import error' }, if: lambda { |_project, options| options[:user_can_admin_project] } do |project|
|
2018-11-27 04:41:27 -05:00
|
|
|
project.import_state&.last_error
|
|
|
|
end
|
2017-09-26 04:49:53 -04:00
|
|
|
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :open_issues_count, documentation: { type: 'integer', example: 1 }, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) }
|
|
|
|
expose :runners_token, documentation: { type: 'string', example: 'b8547b1dc37721d05889db52fa2f02' }, if: lambda { |_project, options| options[:user_can_admin_project] }
|
|
|
|
expose :ci_default_git_depth, documentation: { type: 'integer', example: 20 }
|
|
|
|
expose :ci_forward_deployment_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose(:ci_job_token_scope_enabled, documentation: { type: 'boolean' }) { |p, _| p.ci_outbound_job_token_scope_enabled? }
|
|
|
|
expose :ci_separated_caches, documentation: { type: 'boolean' }
|
|
|
|
expose :ci_opt_in_jwt, documentation: { type: 'boolean' }
|
|
|
|
expose :ci_allow_fork_pipelines_to_run_in_parent_project, documentation: { type: 'boolean' }
|
|
|
|
expose :public_builds, as: :public_jobs, documentation: { type: 'boolean' }
|
|
|
|
expose :build_git_strategy, documentation: { type: 'string', example: 'fetch' }, if: lambda { |project, options| options[:user_can_admin_project] } do |project, options|
|
2019-05-15 08:35:55 -04:00
|
|
|
project.build_allow_git_fetch ? 'fetch' : 'clone'
|
|
|
|
end
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :build_timeout, documentation: { type: 'integer', example: 3600 }
|
|
|
|
expose :auto_cancel_pending_pipelines, documentation: { type: 'string', example: 'enabled' }
|
|
|
|
expose :ci_config_path, documentation: { type: 'string', example: '' }, if: -> (project, options) { Ability.allowed?(options[:current_user], :download_code, project) }
|
|
|
|
expose :shared_with_groups, documentation: { is_array: true } do |project, options|
|
2021-10-28 14:14:18 -04:00
|
|
|
user = options[:current_user]
|
|
|
|
|
|
|
|
SharedGroupWithProject.represent(project.visible_group_links(for_user: user), options)
|
2016-07-08 03:34:36 -04:00
|
|
|
end
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :only_allow_merge_if_pipeline_succeeds, documentation: { type: 'boolean' }
|
|
|
|
expose :allow_merge_on_skipped_pipeline, documentation: { type: 'boolean' }
|
|
|
|
expose :restrict_user_defined_variables, documentation: { type: 'boolean' }
|
|
|
|
expose :request_access_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :only_allow_merge_if_all_discussions_are_resolved, documentation: { type: 'boolean' }
|
|
|
|
expose :remove_source_branch_after_merge, documentation: { type: 'boolean' }
|
|
|
|
expose :printing_merge_request_link_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :merge_method, documentation: { type: 'string', example: 'merge' }
|
|
|
|
expose :squash_option, documentation: { type: 'string', example: 'default_off' }
|
|
|
|
expose :enforce_auth_checks_on_uploads, documentation: { type: 'boolean' }
|
|
|
|
expose :suggestion_commit_message, documentation: { type: 'string', example: 'Suggestion message' }
|
|
|
|
expose :merge_commit_template, documentation: { type: 'string', example: '%(title)' }
|
|
|
|
expose :squash_commit_template, documentation: { type: 'string', example: '%(source_branch)' }
|
|
|
|
expose :issue_branch_template, documentation: { type: 'string', example: '%(title)' }
|
2019-01-30 23:21:35 -05:00
|
|
|
expose :statistics, using: 'API::Entities::ProjectStatistics', if: -> (project, options) {
|
2019-03-22 19:44:35 -04:00
|
|
|
options[:statistics] && Ability.allowed?(options[:current_user], :read_statistics, project)
|
2019-01-30 23:21:35 -05:00
|
|
|
}
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :auto_devops_enabled?, as: :auto_devops_enabled, documentation: { type: 'boolean' }
|
|
|
|
expose :auto_devops_deploy_strategy, documentation: { type: 'string', example: 'continuous' } do |project, options|
|
2019-05-15 10:27:54 -04:00
|
|
|
project.auto_devops.nil? ? 'continuous' : project.auto_devops.deploy_strategy
|
|
|
|
end
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :autoclose_referenced_issues, documentation: { type: 'boolean' }
|
|
|
|
expose :repository_storage, documentation: { type: 'string', example: 'default' }, if: ->(project, options) {
|
2020-03-02 16:08:01 -05:00
|
|
|
Ability.allowed?(options[:current_user], :change_repository_storage, project)
|
|
|
|
}
|
2022-11-04 08:10:55 -04:00
|
|
|
expose :keep_latest_artifacts_available?, as: :keep_latest_artifact, documentation: { type: 'boolean' }
|
|
|
|
expose :runner_token_expiration_interval, documentation: { type: 'integer', example: 3600 }
|
2017-11-30 06:52:38 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-09-10 05:11:07 -04:00
|
|
|
def self.preload_resource(project)
|
|
|
|
ActiveRecord::Associations::Preloader.new.preload(project, project_group_links: { group: :route })
|
|
|
|
end
|
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
def self.preload_relation(projects_relation, options = {})
|
2021-06-11 02:10:03 -04:00
|
|
|
# Preloading topics, should be done with using only `:topics`,
|
2021-09-28 05:11:19 -04:00
|
|
|
# as `:topics` are defined as: `has_many :topics, through: :project_topics`
|
2021-06-11 02:10:03 -04:00
|
|
|
# N+1 is solved then by using `subject.topics.map(&:name)`
|
2019-09-18 10:02:45 -04:00
|
|
|
# MR describing the solution: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/20555
|
2021-03-31 08:08:55 -04:00
|
|
|
super(projects_relation).preload(group: :namespace_settings)
|
2019-06-07 07:51:42 -04:00
|
|
|
.preload(:ci_cd_settings)
|
2020-06-10 17:09:29 -04:00
|
|
|
.preload(:project_setting)
|
2020-01-08 16:08:08 -05:00
|
|
|
.preload(:container_expiration_policy)
|
2019-05-28 12:09:02 -04:00
|
|
|
.preload(:auto_devops)
|
2021-04-08 08:09:18 -04:00
|
|
|
.preload(:service_desk_setting)
|
2018-12-27 09:37:46 -05:00
|
|
|
.preload(project_group_links: { group: :route },
|
2017-11-30 16:17:17 -05:00
|
|
|
fork_network: :root_project,
|
2018-08-31 13:16:34 -04:00
|
|
|
fork_network_member: :forked_from_project,
|
2021-09-28 05:11:19 -04:00
|
|
|
forked_from_project: [:route, :topics, :group, :project_feature, namespace: [:route, :owner]])
|
2017-11-30 06:52:38 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-11-30 06:52:38 -05:00
|
|
|
|
2021-09-08 08:12:01 -04:00
|
|
|
def self.execute_batch_counting(projects_relation)
|
|
|
|
# Call the count methods on every project, so the BatchLoader would load them all at
|
|
|
|
# once when the entities are rendered
|
|
|
|
projects_relation.each(&:open_issues_count)
|
|
|
|
projects_relation.map(&:forked_from_project).compact.each(&:forks_count)
|
|
|
|
|
|
|
|
super
|
2017-11-30 06:52:38 -05:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
|
|
|
|
def self.repositories_for_preload(projects_relation)
|
|
|
|
super + projects_relation.map(&:forked_from_project).compact.map(&:repository)
|
|
|
|
end
|
2016-11-22 11:58:10 -05:00
|
|
|
end
|
2012-06-27 07:32:56 -04:00
|
|
|
end
|
|
|
|
end
|
2019-08-27 11:11:15 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Entities::Project.prepend_mod_with('API::Entities::Project', with_descendants: true)
|