entities.rb 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
module API
  module V3
    module Entities
      class ProjectSnippet < Grape::Entity
        expose :id, :title, :file_name
        expose :author, using: ::API::Entities::UserBasic
        expose :updated_at, :created_at
        expose(:expires_at) { |snippet| nil }

        expose :web_url do |snippet, options|
          Gitlab::UrlBuilder.build(snippet)
        end
      end
14 15 16 17 18 19 20 21 22 23 24 25 26 27

      class Note < Grape::Entity
        expose :id
        expose :note, as: :body
        expose :attachment_identifier, as: :attachment
        expose :author, using: ::API::Entities::UserBasic
        expose :created_at, :updated_at
        expose :system?, as: :system
        expose :noteable_id, :noteable_type
        # upvote? and downvote? are deprecated, always return false
        expose(:upvote?)    { |note| false }
        expose(:downvote?)  { |note| false }
      end

28 29 30 31 32
      class PushEventPayload < Grape::Entity
        expose :commit_count, :action, :ref_type, :commit_from, :commit_to
        expose :ref, :commit_title
      end

33
      class Event < Grape::Entity
34
        expose :project_id, :action_name
35
        expose :target_id, :target_type, :author_id
36
        expose :target_title
37 38 39 40
        expose :created_at
        expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
        expose :author, using: ::API::Entities::UserBasic, if: ->(event, options) { event.author }

41 42 43 44 45
        expose :push_event_payload,
          as: :push_data,
          using: PushEventPayload,
          if: -> (event, _) { event.push? }

46 47 48 49 50 51 52 53 54 55 56 57
        expose :author_username do |event, options|
          event.author&.username
        end
      end

      class AwardEmoji < Grape::Entity
        expose :id
        expose :name
        expose :user, using: ::API::Entities::UserBasic
        expose :created_at, :updated_at
        expose :awardable_id, :awardable_type
      end
58 59 60 61 62 63 64 65 66

      class Project < Grape::Entity
        expose :id, :description, :default_branch, :tag_list
        expose :public?, as: :public
        expose :archived?, as: :archived
        expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
        expose :owner, using: ::API::Entities::UserBasic, unless: ->(project, options) { project.group }
        expose :name, :name_with_namespace
        expose :path, :path_with_namespace
67
        expose :resolve_outdated_diff_discussions
68 69 70 71 72 73 74 75 76 77 78 79 80 81
        expose :container_registry_enabled

        # Expose old field names with the new permissions methods to keep API compatible
        expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:current_user]) }
        expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:current_user]) }
        expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:current_user]) }
        expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
        expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }

        expose :created_at, :last_activity_at
        expose :shared_runners_enabled
        expose :lfs_enabled?, as: :lfs_enabled
        expose :creator_id
        expose :namespace, using: 'API::Entities::Namespace'
82
        expose :forked_from_project, using: ::API::Entities::BasicProjectDetails, if: lambda { |project, options| project.forked? }
83 84 85
        expose :avatar_url do |user, options|
          user.avatar_url(only_path: false)
        end
86 87 88 89 90 91 92 93 94 95 96
        expose :star_count, :forks_count
        expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) && project.default_issues_tracker? }
        expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
        expose :public_builds
        expose :shared_with_groups do |project, options|
          ::API::Entities::SharedGroup.represent(project.project_group_links.all, options)
        end
        expose :only_allow_merge_if_pipeline_succeeds, as: :only_allow_merge_if_build_succeeds
        expose :request_access_enabled
        expose :only_allow_merge_if_all_discussions_are_resolved

Toon Claes's avatar
Toon Claes committed
97
        expose :statistics, using: '::API::V3::Entities::ProjectStatistics', if: :statistics
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
      end

      class ProjectWithAccess < Project
        expose :permissions do
          expose :project_access, using: ::API::Entities::ProjectAccess do |project, options|
            project.project_members.find_by(user_id: options[:current_user].id)
          end

          expose :group_access, using: ::API::Entities::GroupAccess do |project, options|
            if project.group
              project.group.group_members.find_by(user_id: options[:current_user].id)
            end
          end
        end
      end

      class MergeRequest < Grape::Entity
        expose :id, :iid
        expose(:project_id) { |entity| entity.project.id }
        expose :title, :description
        expose :state, :created_at, :updated_at
        expose :target_branch, :source_branch
        expose :upvotes, :downvotes
        expose :author, :assignee, using: ::API::Entities::UserBasic
        expose :source_project_id, :target_project_id
        expose :label_names, as: :labels
        expose :work_in_progress?, as: :work_in_progress
        expose :milestone, using: ::API::Entities::Milestone
        expose :merge_when_pipeline_succeeds, as: :merge_when_build_succeeds
        expose :merge_status
        expose :diff_head_sha, as: :sha
        expose :merge_commit_sha
        expose :subscribed do |merge_request, options|
          merge_request.subscribed?(options[:current_user], options[:project])
        end
        expose :user_notes_count
        expose :should_remove_source_branch?, as: :should_remove_source_branch
        expose :force_remove_source_branch?, as: :force_remove_source_branch

        expose :web_url do |merge_request, options|
          Gitlab::UrlBuilder.build(merge_request)
        end
      end
141 142 143 144

      class Group < Grape::Entity
        expose :id, :name, :path, :description, :visibility_level
        expose :lfs_enabled?, as: :lfs_enabled
145 146 147
        expose :avatar_url do |user, options|
          user.avatar_url(only_path: false)
        end
148 149 150
        expose :web_url
        expose :request_access_enabled
        expose :full_name, :full_path
151 152 153 154

        if ::Group.supports_nested_groups?
          expose :parent_id
        end
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

        expose :statistics, if: :statistics do
          with_options format_with: -> (value) { value.to_i } do
            expose :storage_size
            expose :repository_size
            expose :lfs_objects_size
            expose :build_artifacts_size
          end
        end
      end

      class GroupDetail < Group
        expose :projects, using: Entities::Project
        expose :shared_projects, using: Entities::Project
      end
170

171 172 173 174
      class ApplicationSetting < Grape::Entity
        expose :id
        expose :default_projects_limit
        expose :signup_enabled
175 176
        expose :password_authentication_enabled_for_web, as: :password_authentication_enabled
        expose :password_authentication_enabled_for_web, as: :signin_enabled
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        expose :gravatar_enabled
        expose :sign_in_text
        expose :after_sign_up_text
        expose :created_at
        expose :updated_at
        expose :home_page_url
        expose :default_branch_protection
        expose :restricted_visibility_levels
        expose :max_attachment_size
        expose :session_expire_delay
        expose :default_project_visibility
        expose :default_snippet_visibility
        expose :default_group_visibility
        expose :domain_whitelist
        expose :domain_blacklist_enabled
        expose :domain_blacklist
        expose :user_oauth_applications
        expose :after_sign_out_path
        expose :container_registry_token_expire_delay
        expose :repository_storage
        expose :repository_storages
        expose :koding_enabled
        expose :koding_url
        expose :plantuml_enabled
        expose :plantuml_url
        expose :terminal_max_session_time
      end

205 206 207
      class Environment < ::API::Entities::EnvironmentBasic
        expose :project, using: Entities::Project
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
208 209

      class Trigger < Grape::Entity
210
        expose :token, :created_at, :updated_at, :last_used
Kamil Trzcinski's avatar
Kamil Trzcinski committed
211
        expose :owner, using: ::API::Entities::UserBasic
Kamil Trzcinski's avatar
Kamil Trzcinski committed
212 213 214 215 216
      end

      class TriggerRequest < Grape::Entity
        expose :id, :variables
      end
217 218 219 220 221 222

      class Build < Grape::Entity
        expose :id, :status, :stage, :name, :ref, :tag, :coverage
        expose :created_at, :started_at, :finished_at
        expose :user, with: ::API::Entities::User
        expose :artifacts_file, using: ::API::Entities::JobArtifactFile, if: -> (build, opts) { build.artifacts? }
223
        expose :commit, with: ::API::Entities::Commit
224 225 226
        expose :runner, with: ::API::Entities::Runner
        expose :pipeline, with: ::API::Entities::PipelineBasic
      end
227 228 229 230 231 232 233

      class BuildArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Deployment < Grape::Entity
        expose :id, :iid, :ref, :sha, :created_at
Toon Claes's avatar
Toon Claes committed
234 235
        expose :user,        using: ::API::Entities::UserBasic
        expose :environment, using: ::API::Entities::EnvironmentBasic
236 237 238 239
        expose :deployable,  using: Entities::Build
      end

      class MergeRequestChanges < MergeRequest
240
        expose :diffs, as: :changes, using: ::API::Entities::Diff do |compare, _|
Douwe Maan's avatar
Douwe Maan committed
241
          compare.raw_diffs(limits: false).to_a
242 243 244 245 246 247 248 249 250 251 252 253 254
        end
      end

      class ProjectStatistics < Grape::Entity
        expose :commit_count
        expose :storage_size
        expose :repository_size
        expose :lfs_objects_size
        expose :build_artifacts_size
      end

      class ProjectService < Grape::Entity
        expose :id, :title, :created_at, :updated_at, :active
255 256 257
        expose :push_events, :issues_events, :confidential_issues_events
        expose :merge_requests_events, :tag_push_events, :note_events
        expose :pipeline_events
258
        expose :job_events, as: :build_events
259 260
        # Expose serialized properties
        expose :properties do |service, options|
Stan Hu's avatar
Stan Hu committed
261
          service.properties.slice(*service.api_field_names)
262 263 264
        end
      end

Toon Claes's avatar
Toon Claes committed
265
      class ProjectHook < ::API::Entities::Hook
266 267 268
        expose :project_id, :issues_events, :confidential_issues_events
        expose :merge_requests_events, :note_events, :pipeline_events
        expose :wiki_page_events
269
        expose :job_events, as: :build_events
270
      end
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      class ProjectEntity < Grape::Entity
        expose :id, :iid
        expose(:project_id) { |entity| entity&.project.try(:id) }
        expose :title, :description
        expose :state, :created_at, :updated_at
      end

      class IssueBasic < ProjectEntity
        expose :label_names, as: :labels
        expose :milestone, using: ::API::Entities::Milestone
        expose :assignees, :author, using: ::API::Entities::UserBasic

        expose :assignee, using: ::API::Entities::UserBasic do |issue, options|
          issue.assignees.first
        end

        expose :user_notes_count
        expose :upvotes, :downvotes
        expose :due_date
        expose :confidential

        expose :web_url do |issue, options|
          Gitlab::UrlBuilder.build(issue)
        end
      end

      class Issue < IssueBasic
299 300 301 302
        unexpose :assignees
        expose :assignee do |issue, options|
          ::API::Entities::UserBasic.represent(issue.assignees.first, options)
        end
303 304 305
        expose :subscribed do |issue, options|
          issue.subscribed?(options[:current_user], options[:project] || issue.project)
        end
306
      end
307 308 309
    end
  end
end