1# frozen_string_literal: true
2
3module API
4  module Entities
5    class Package < Grape::Entity
6      include ::API::Helpers::RelatedResourcesHelpers
7      extend ::API::Entities::EntityHelpers
8
9      expose :id
10
11      expose :name do |package|
12        if package.conan?
13          package.conan_recipe
14        else
15          package.name
16        end
17      end
18
19      expose :conan_package_name, if: ->(package) { package.conan? } do |package|
20        package.name
21      end
22
23      expose :version
24      expose :package_type
25      expose :status
26
27      expose :_links do
28        expose :web_path do |package, opts|
29          if package.infrastructure_package?
30            ::Gitlab::Routing.url_helpers.namespace_project_infrastructure_registry_path(opts[:namespace], package.project, package)
31          else
32            ::Gitlab::Routing.url_helpers.project_package_path(package.project, package)
33          end
34        end
35
36        expose :delete_api_path, if: can_destroy(:package, &:project) do |package|
37          expose_url api_v4_projects_packages_path(package_id: package.id, id: package.project_id)
38        end
39      end
40
41      expose :created_at
42      expose :project_id, if: ->(_, opts) { opts[:group] }
43      expose :project_path, if: ->(obj, opts) { opts[:group] && Ability.allowed?(opts[:user], :read_project, obj.project) }
44      expose :tags
45
46      expose :pipeline, if: ->(package) { package.original_build_info }, using: Package::Pipeline
47      expose :pipelines, if: ->(package) { package.pipelines.present? }, using: Package::Pipeline
48
49      expose :versions, using: ::API::Entities::PackageVersion, unless: ->(_, opts) { opts[:collection] }
50
51      private
52
53      def project_path
54        object.project.full_path
55      end
56    end
57  end
58end
59