entities.rb 43.7 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  module Entities
B
blackst0ne 已提交
3 4 5 6 7 8 9 10 11 12
    class WikiPageBasic < Grape::Entity
      expose :format
      expose :slug
      expose :title
    end

    class WikiPage < WikiPageBasic
      expose :content
    end

13
    class UserSafe < Grape::Entity
14
      expose :id, :name, :username
15
    end
16

17
    class UserBasic < UserSafe
18
      expose :state
19

20 21 22
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
D
Douwe Maan 已提交
23

24
      expose :avatar_path, if: ->(user, options) { options.fetch(:only_path, false) && user.avatar_path }
25
      expose :custom_attributes, using: 'API::Entities::CustomAttribute', if: :with_custom_attributes
26

D
Douwe Maan 已提交
27
      expose :web_url do |user, options|
28
        Gitlab::Routing.url_helpers.user_url(user)
D
Douwe Maan 已提交
29
      end
N
Nihad Abbasov 已提交
30
    end
N
Nihad Abbasov 已提交
31

32
    class User < UserBasic
33
      expose :created_at, if: ->(user, opts) { Ability.allowed?(opts[:current_user], :read_user_profile, user) }
34
      expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
35 36
    end

37 38
    class UserActivity < Grape::Entity
      expose :username
39 40
      expose :last_activity_on
      expose :last_activity_on, as: :last_activity_at # Back-compat
41 42
    end

43 44 45 46
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

47
    class UserPublic < User
48 49
      expose :last_sign_in_at
      expose :confirmed_at
50
      expose :last_activity_on
51
      expose :email
52
      expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
53
      expose :identities, using: Entities::Identity
54 55
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
56
      expose :two_factor_enabled?, as: :two_factor_enabled
57
      expose :external
58
      expose :private_profile
59 60
    end

61
    class UserWithAdmin < UserPublic
62
      expose :admin?, as: :is_admin
63 64
    end

B
Bob Van Landuyt 已提交
65 66 67
    class UserStatus < Grape::Entity
      expose :emoji
      expose :message
68 69 70
      expose :message_html do |entity|
        MarkupHelper.markdown_field(entity, :message)
      end
B
Bob Van Landuyt 已提交
71 72
    end

73 74 75 76
    class Email < Grape::Entity
      expose :id, :email
    end

M
miks 已提交
77
    class Hook < Grape::Entity
78
      expose :id, :url, :created_at, :push_events, :tag_push_events, :merge_requests_events, :repository_update_events
79
      expose :enable_ssl_verification
M
miks 已提交
80 81
    end

82
    class ProjectHook < Hook
83
      expose :project_id, :issues_events, :confidential_issues_events
84
      expose :note_events, :confidential_note_events, :pipeline_events, :wiki_page_events
85
      expose :job_events
86 87
    end

88 89 90 91 92 93 94 95
    class SharedGroup < Grape::Entity
      expose :group_id
      expose :group_name do |group_link, options|
        group_link.group.name
      end
      expose :group_access, as: :group_access_level
    end

T
Tomasz Maczukin 已提交
96 97
    class ProjectIdentity < Grape::Entity
      expose :id, :description
98 99
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
T
Tomasz Maczukin 已提交
100 101 102
      expose :created_at
    end

T
Travis Miller 已提交
103 104 105 106
    class ProjectExportStatus < ProjectIdentity
      include ::API::Helpers::RelatedResourcesHelpers

      expose :export_status
107
      expose :_links, if: lambda { |project, _options| project.export_status == :finished } do
T
Travis Miller 已提交
108 109 110 111 112 113 114 115 116 117
        expose :api_url do |project|
          expose_url(api_v4_projects_export_download_path(id: project.id))
        end

        expose :web_url do |project|
          Gitlab::Routing.url_helpers.download_export_project_url(project)
        end
      end
    end

J
James Lopez 已提交
118 119
    class ProjectImportStatus < ProjectIdentity
      expose :import_status
J
James Lopez 已提交
120 121 122

      # TODO: Use `expose_nil` once we upgrade the grape-entity gem
      expose :import_error, if: lambda { |status, _ops| status.import_error }
J
James Lopez 已提交
123 124
    end

F
Francisco Lopez 已提交
125
    class BasicProjectDetails < ProjectIdentity
F
Francisco Lopez 已提交
126 127 128 129 130 131 132 133 134 135 136
      include ::API::ProjectsRelationBuilder

      expose :default_branch
      # 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
I
Imre Farkas 已提交
137
      expose :ssh_url_to_repo, :http_url_to_repo, :web_url, :readme_url
138 139 140
      expose :avatar_url do |project, options|
        project.avatar_url(only_path: false)
      end
141
      expose :star_count, :forks_count
F
Francisco Lopez 已提交
142
      expose :last_activity_at
143

144
      expose :namespace, using: 'API::Entities::NamespaceBasic'
145 146
      expose :custom_attributes, using: 'API::Entities::CustomAttribute', if: :with_custom_attributes

147
      def self.preload_relation(projects_relation, options =  {})
148 149 150 151
        # 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-ce/merge_requests/20555
152
        projects_relation.preload(:project_feature, :route)
153 154
                         .preload(:import_state, :tags)
                         .preload(namespace: [:route, :owner])
155
      end
156 157
    end

158
    class Project < BasicProjectDetails
159 160 161 162 163 164 165
      include ::API::Helpers::RelatedResourcesHelpers

      expose :_links do
        expose :self do |project|
          expose_url(api_v4_projects_path(id: project.id))
        end

166
        expose :issues, if: -> (project, options) { issues_available?(project, options) } do |project|
167 168 169
          expose_url(api_v4_projects_issues_path(id: project.id))
        end

170
        expose :merge_requests, if: -> (project, options) { mrs_available?(project, options) } do |project|
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
          expose_url(api_v4_projects_merge_requests_path(id: project.id))
        end

        expose :repo_branches do |project|
          expose_url(api_v4_projects_repository_branches_path(id: project.id))
        end

        expose :labels do |project|
          expose_url(api_v4_projects_labels_path(id: project.id))
        end

        expose :events do |project|
          expose_url(api_v4_projects_events_path(id: project.id))
        end

        expose :members do |project|
          expose_url(api_v4_projects_members_path(id: project.id))
        end
      end

191
      expose :archived?, as: :archived
192
      expose :visibility
193
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
194
      expose :resolve_outdated_diff_discussions
F
Felipe Artur 已提交
195 196 197
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
198 199 200
      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]) }
201
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
202
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
F
Felipe Artur 已提交
203

204 205
      expose :shared_runners_enabled
      expose :lfs_enabled?, as: :lfs_enabled
206
      expose :creator_id
207
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda { |project, options| project.forked? }
208 209
      expose :import_status
      expose :import_error, if: lambda { |_project, options| options[:user_can_admin_project] }
210

211
      expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) }
212
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
213
      expose :public_builds, as: :public_jobs
214
      expose :ci_config_path
215
      expose :shared_with_groups do |project, options|
216
        SharedGroup.represent(project.project_group_links, options)
217
      end
J
James Lopez 已提交
218
      expose :only_allow_merge_if_pipeline_succeeds
219
      expose :request_access_enabled
220
      expose :only_allow_merge_if_all_discussions_are_resolved
221
      expose :printing_merge_request_link_enabled
222
      expose :merge_method
M
Markus Koller 已提交
223 224

      expose :statistics, using: 'API::Entities::ProjectStatistics', if: :statistics
225 226

      def self.preload_relation(projects_relation, options =  {})
227 228 229 230
        # 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-ce/merge_requests/20555
231 232 233 234
        super(projects_relation).preload(:group)
                                .preload(project_group_links: :group,
                                         fork_network: :root_project,
                                         forked_project_link: :forked_from_project,
235
                                         forked_from_project: [:route, :forks, :tags, namespace: :route])
236 237 238
      end

      def self.forks_counting_projects(projects_relation)
239
        projects_relation + projects_relation.map(&:forked_from_project).compact
240
      end
M
Markus Koller 已提交
241 242 243 244 245 246 247
    end

    class ProjectStatistics < Grape::Entity
      expose :commit_count
      expose :storage_size
      expose :repository_size
      expose :lfs_objects_size
248
      expose :build_artifacts_size, as: :job_artifacts_size
N
Nihad Abbasov 已提交
249 250
    end

251 252 253 254
    class Member < Grape::Entity
      expose :user, merge: true, using: UserBasic
      expose :access_level
      expose :expires_at
255 256
    end

257 258 259
    class AccessRequester < Grape::Entity
      expose :user, merge: true, using: UserBasic
      expose :requested_at
M
miks 已提交
260 261
    end

262 263 264 265 266 267 268 269
    class BasicGroupDetails < Grape::Entity
      expose :id
      expose :web_url
      expose :name
    end

    class Group < BasicGroupDetails
      expose :path, :description, :visibility
270
      expose :lfs_enabled?, as: :lfs_enabled
271 272
      expose :avatar_url do |group, options|
        group.avatar_url(only_path: false)
273
      end
274
      expose :request_access_enabled
275
      expose :full_name, :full_path
276 277 278 279

      if ::Group.supports_nested_groups?
        expose :parent_id
      end
M
Markus Koller 已提交
280

281 282
      expose :custom_attributes, using: 'API::Entities::CustomAttribute', if: :with_custom_attributes

M
Markus Koller 已提交
283 284 285 286 287
      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
288
          expose :build_artifacts_size, as: :job_artifacts_size
M
Markus Koller 已提交
289 290
        end
      end
291
    end
A
Andrew8xx8 已提交
292

293
    class GroupDetail < Group
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      expose :projects, using: Entities::Project do |group, options|
        GroupProjectsFinder.new(
          group: group,
          current_user: options[:current_user],
          options: { only_owned: true }
        ).execute
      end

      expose :shared_projects, using: Entities::Project do |group, options|
        GroupProjectsFinder.new(
          group: group,
          current_user: options[:current_user],
          options: { only_shared: true }
        ).execute
      end
309 310
    end

311 312 313 314
    class DiffRefs < Grape::Entity
      expose :base_sha, :head_sha, :start_sha
    end

315
    class Commit < Grape::Entity
316 317 318 319 320 321 322
      expose :id, :short_id, :title, :created_at
      expose :parent_ids
      expose :safe_message, as: :message
      expose :author_name, :author_email, :authored_date
      expose :committer_name, :committer_email, :committed_date
    end

323
    class CommitStats < Grape::Entity
324 325 326
      expose :additions, :deletions, :total
    end

327 328 329 330
    class CommitWithStats < Commit
      expose :stats, using: Entities::CommitStats
    end

331
    class CommitDetail < Commit
332
      expose :stats, using: Entities::CommitStats, if: :stats
333
      expose :status
334
      expose :last_pipeline, using: 'API::Entities::PipelineBasic'
335
      expose :project_id
336 337
    end

338
    class BasicRef < Grape::Entity
339
      expose :type, :name
340 341
    end

R
Robert Schilling 已提交
342
    class Branch < Grape::Entity
343 344
      expose :name

345
      expose :commit, using: Entities::Commit do |repo_branch, options|
346
        options[:project].repository.commit(repo_branch.dereferenced_target)
347 348
      end

R
Robert Schilling 已提交
349
      expose :merged do |repo_branch, options|
350 351 352 353 354
        if options[:merged_branch_names]
          options[:merged_branch_names].include?(repo_branch.name)
        else
          options[:project].repository.merged_to_root_ref?(repo_branch)
        end
R
Robert Schilling 已提交
355 356
      end

357
      expose :protected do |repo_branch, options|
E
Eric 已提交
358
        ::ProtectedBranch.protected?(options[:project], repo_branch.name)
359 360
      end

361
      expose :developers_can_push do |repo_branch, options|
362
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
363
      end
364

365
      expose :developers_can_merge do |repo_branch, options|
366
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
367 368 369 370
      end

      expose :can_push do |repo_branch, options|
        Gitlab::UserAccess.new(options[:current_user], project: options[:project]).can_push_to_branch?(repo_branch.name)
371
      end
N
Nihad Abbasov 已提交
372
    end
N
Nihad Abbasov 已提交
373

374
    class TreeObject < Grape::Entity
375
      expose :id, :name, :type, :path
376 377

      expose :mode do |obj, options|
M
mhasbini 已提交
378
        filemode = obj.mode
379 380 381 382 383
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

J
Jarka Kadlecová 已提交
384
    class Snippet < Grape::Entity
385
      expose :id, :title, :file_name, :description, :visibility
386
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
387
      expose :updated_at, :created_at
J
Jarka Kadlecová 已提交
388 389
      expose :project_id
      expose :web_url do |snippet|
390 391
        Gitlab::UrlBuilder.build(snippet)
      end
N
Nihad Abbasov 已提交
392
    end
N
Nihad Abbasov 已提交
393

J
Jarka Kadlecová 已提交
394 395
    class ProjectSnippet < Snippet
    end
396

J
Jarka Kadlecová 已提交
397
    class PersonalSnippet < Snippet
398 399 400 401 402
      expose :raw_url do |snippet|
        Gitlab::UrlBuilder.build(snippet) + "/raw"
      end
    end

403 404
    class ProjectEntity < Grape::Entity
      expose :id, :iid
F
Felipe Artur 已提交
405
      expose(:project_id) { |entity| entity&.project.try(:id) }
406 407
      expose :title, :description
      expose :state, :created_at, :updated_at
408 409
    end

410
    class Diff < Grape::Entity
M
micael.bergeron 已提交
411
      expose :old_path, :new_path, :a_mode, :b_mode
412 413 414
      expose :new_file?, as: :new_file
      expose :renamed_file?, as: :renamed_file
      expose :deleted_file?, as: :deleted_file
415
      expose :json_safe_diff, as: :diff
416 417
    end

E
Eric 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
    class ProtectedRefAccess < Grape::Entity
      expose :access_level
      expose :access_level_description do |protected_ref_access|
        protected_ref_access.humanize
      end
    end

    class ProtectedBranch < Grape::Entity
      expose :name
      expose :push_access_levels, using: Entities::ProtectedRefAccess
      expose :merge_access_levels, using: Entities::ProtectedRefAccess
    end

F
Felipe Artur 已提交
431 432
    class Milestone < Grape::Entity
      expose :id, :iid
F
Felipe Artur 已提交
433 434
      expose :project_id, if: -> (entity, options) { entity&.project_id }
      expose :group_id, if: -> (entity, options) { entity&.group_id }
F
Felipe Artur 已提交
435 436
      expose :title, :description
      expose :state, :created_at, :updated_at
437
      expose :due_date
V
Valery Sizov 已提交
438
      expose :start_date
439 440 441 442

      expose :web_url do |milestone, _options|
        Gitlab::UrlBuilder.build(milestone)
      end
N
Nihad Abbasov 已提交
443 444
    end

445
    class IssueBasic < ProjectEntity
446
      expose :closed_at
H
haseeb 已提交
447
      expose :closed_by, using: Entities::UserBasic
448 449 450 451
      expose :labels do |issue, options|
        # Avoids an N+1 query since labels are preloaded
        issue.labels.map(&:title).sort
      end
452
      expose :milestone, using: Entities::Milestone
453 454 455 456 457
      expose :assignees, :author, using: Entities::UserBasic

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

Z
Z.J. van de Weg 已提交
459
      expose :user_notes_count
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
      expose :upvotes do |issue, options|
        if options[:issuable_metadata]
          # Avoids an N+1 query when metadata is included
          options[:issuable_metadata][issue.id].upvotes
        else
          issue.upvotes
        end
      end
      expose :downvotes do |issue, options|
        if options[:issuable_metadata]
          # Avoids an N+1 query when metadata is included
          options[:issuable_metadata][issue.id].downvotes
        else
          issue.downvotes
        end
      end
476
      expose :due_date
477
      expose :confidential
478
      expose :discussion_locked
479 480 481 482

      expose :web_url do |issue, options|
        Gitlab::UrlBuilder.build(issue)
      end
483 484 485 486

      expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |issue|
        issue
      end
N
Nihad Abbasov 已提交
487
    end
A
Alex Denisov 已提交
488

489
    class Issue < IssueBasic
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
      include ::API::Helpers::RelatedResourcesHelpers

      expose :_links do
        expose :self do |issue|
          expose_url(api_v4_project_issue_path(id: issue.project_id, issue_iid: issue.iid))
        end

        expose :notes do |issue|
          expose_url(api_v4_projects_issues_notes_path(id: issue.project_id, noteable_id: issue.iid))
        end

        expose :award_emoji do |issue|
          expose_url(api_v4_projects_issues_award_emoji_path(id: issue.project_id, issue_iid: issue.iid))
        end

        expose :project do |issue|
          expose_url(api_v4_projects_path(id: issue.project_id))
        end
      end

510 511 512 513 514
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

515
    class IssuableTimeStats < Grape::Entity
516 517 518 519
      format_with(:time_tracking_formatter) do |time_spent|
        Gitlab::TimeTrackingFormatter.output(time_spent)
      end

520 521 522
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
523 524 525 526 527 528 529 530 531

      with_options(format_with: :time_tracking_formatter) do
        expose :total_time_spent, as: :human_total_time_spent
      end

      def total_time_spent
        # Avoids an N+1 query since timelogs are preloaded
        object.timelogs.map(&:time_spent).sum
      end
532 533
    end

534 535 536 537 538
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

H
haseeb 已提交
539 540
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
541 542 543 544

      expose :web_url do |pipeline, _options|
        Gitlab::Routing.url_helpers.project_pipeline_url(pipeline.project, pipeline)
      end
H
haseeb 已提交
545 546
    end

S
Stan Hu 已提交
547 548 549 550 551 552 553
    class MergeRequestSimple < ProjectEntity
      expose :title
      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
    end

554
    class MergeRequestBasic < ProjectEntity
555
      expose :title_html, if: -> (_, options) { options[:render_html] } do |entity|
P
Phil Hughes 已提交
556
        MarkupHelper.markdown_field(entity, :title)
557 558
      end
      expose :description_html, if: -> (_, options) { options[:render_html] } do |entity|
P
Phil Hughes 已提交
559
        MarkupHelper.markdown_field(entity, :description)
560
      end
V
Valery Sizov 已提交
561
      expose :target_branch, :source_branch
562 563 564 565 566 567 568 569 570 571 572 573 574 575
      expose :upvotes do |merge_request, options|
        if options[:issuable_metadata]
          options[:issuable_metadata][merge_request.id].upvotes
        else
          merge_request.upvotes
        end
      end
      expose :downvotes do |merge_request, options|
        if options[:issuable_metadata]
          options[:issuable_metadata][merge_request.id].downvotes
        else
          merge_request.downvotes
        end
      end
576 577
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
578 579 580 581
      expose :labels do |merge_request, options|
        # Avoids an N+1 query since labels are preloaded
        merge_request.labels.map(&:title).sort
      end
B
Ben Boeckel 已提交
582
      expose :work_in_progress?, as: :work_in_progress
583
      expose :milestone, using: Entities::Milestone
J
James Lopez 已提交
584
      expose :merge_when_pipeline_succeeds
585 586 587 588 589 590

      # Ideally we should deprecate `MergeRequest#merge_status` exposure and
      # use `MergeRequest#mergeable?` instead (boolean).
      # See https://gitlab.com/gitlab-org/gitlab-ce/issues/42344 for more
      # information.
      expose :merge_status do |merge_request|
591 592
        merge_request.check_if_can_be_merged
        merge_request.merge_status
593
      end
594 595
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z
Z.J. van de Weg 已提交
596
      expose :user_notes_count
597
      expose :discussion_locked
598 599
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
600 601 602
      expose :allow_collaboration, if: -> (merge_request, _) { merge_request.for_fork? }
      # Deprecated
      expose :allow_collaboration, as: :allow_maintainer_to_push, if: -> (merge_request, _) { merge_request.for_fork? }
603 604 605 606

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
607 608 609 610

      expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |merge_request|
        merge_request
      end
611 612

      expose :squash
A
Alex Denisov 已提交
613
    end
V
Valeriy Sizov 已提交
614

615 616 617 618
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
619 620 621 622

      expose :changes_count do |merge_request, _options|
        merge_request.merge_request_diff.real_size
      end
H
haseeb 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

      expose :merged_by, using: Entities::UserBasic do |merge_request, _options|
        merge_request.metrics&.merged_by
      end

      expose :merged_at do |merge_request, _options|
        merge_request.metrics&.merged_at
      end

      expose :closed_by, using: Entities::UserBasic do |merge_request, _options|
        merge_request.metrics&.latest_closed_by
      end

      expose :closed_at do |merge_request, _options|
        merge_request.metrics&.latest_closed_at
      end

      expose :latest_build_started_at, if: -> (_, options) { build_available?(options) } do |merge_request, _options|
        merge_request.metrics&.latest_build_started_at
      end

      expose :latest_build_finished_at, if: -> (_, options) { build_available?(options) } do |merge_request, _options|
        merge_request.metrics&.latest_build_finished_at
      end

      expose :first_deployed_to_production_at, if: -> (_, options) { build_available?(options) } do |merge_request, _options|
        merge_request.metrics&.first_deployed_to_production_at
      end

      expose :pipeline, using: Entities::PipelineBasic, if: -> (_, options) { build_available?(options) } do |merge_request, _options|
        merge_request.metrics&.pipeline
      end

656 657
      expose :diff_refs, using: Entities::DiffRefs

H
haseeb 已提交
658 659 660
      def build_available?(options)
        options[:project]&.feature_available?(:builds, options[:current_user])
      end
661 662
    end

663
    class MergeRequestChanges < MergeRequest
664
      expose :diffs, as: :changes, using: Entities::Diff do |compare, _|
D
Douwe Maan 已提交
665
        compare.raw_diffs(limits: false).to_a
666 667 668
      end
    end

669 670 671
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
672
    end
673

674
    class MergeRequestDiffFull < MergeRequestDiff
675
      expose :commits, using: Entities::Commit
676

677
      expose :diffs, using: Entities::Diff do |compare, _|
D
Douwe Maan 已提交
678
        compare.raw_diffs(limits: false).to_a
679 680 681
      end
    end

682
    class SSHKey < Grape::Entity
683
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
684
    end
685

686
    class SSHKeyWithUser < SSHKey
687
      expose :user, using: Entities::UserPublic
688 689
    end

690 691 692 693 694
    class DeployKeysProject < Grape::Entity
      expose :deploy_key, merge: true, using: Entities::SSHKey
      expose :can_push
    end

R
Robert Schilling 已提交
695 696 697 698
    class GPGKey < Grape::Entity
      expose :id, :key, :created_at
    end

699 700 701 702 703
    class DiffPosition < Grape::Entity
      expose :base_sha, :start_sha, :head_sha, :old_path, :new_path,
        :position_type
    end

704
    class Note < Grape::Entity
S
sue445 已提交
705 706 707
      # Only Issue and MergeRequest have iid
      NOTEABLE_TYPES_WITH_IID = %w(Issue MergeRequest).freeze

708
      expose :id
J
Jan Provaznik 已提交
709
      expose :type
710
      expose :note, as: :body
711
      expose :attachment_identifier, as: :attachment
712
      expose :author, using: Entities::UserBasic
713
      expose :created_at, :updated_at
714
      expose :system?, as: :system
D
Dmitriy Zaporozhets 已提交
715
      expose :noteable_id, :noteable_type
S
sue445 已提交
716

717
      expose :position, if: ->(note, options) { note.is_a?(DiffNote) } do |note|
718 719 720 721 722 723 724
        note.position.to_h
      end

      expose :resolvable?, as: :resolvable
      expose :resolved?, as: :resolved, if: ->(note, options) { note.resolvable? }
      expose :resolved_by, using: Entities::UserBasic, if: ->(note, options) { note.resolvable? }

S
sue445 已提交
725 726
      # Avoid N+1 queries as much as possible
      expose(:noteable_iid) { |note| note.noteable.iid if NOTEABLE_TYPES_WITH_IID.include?(note.noteable_type) }
727
    end
728

J
Jan Provaznik 已提交
729 730 731 732 733 734
    class Discussion < Grape::Entity
      expose :id
      expose :individual_note?, as: :individual_note
      expose :notes, using: Entities::Note
    end

I
Imre 已提交
735 736 737 738 739 740
    class Avatar < Grape::Entity
      expose :avatar_url do |avatarable, options|
        avatarable.avatar_url(only_path: false, size: options[:size])
      end
    end

Z
Z.J. van de Weg 已提交
741 742 743 744 745 746 747 748
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

749 750 751 752
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
753

754 755
    class CommitNote < Grape::Entity
      expose :note
756 757 758
      expose(:path) { |note| note.diff_file.try(:file_path) if note.diff_note? }
      expose(:line) { |note| note.diff_line.try(:new_line) if note.diff_note? }
      expose(:line_type) { |note| note.diff_line.try(:type) if note.diff_note? }
759
      expose :author, using: Entities::UserBasic
760
      expose :created_at
761 762
    end

K
Kamil Trzcinski 已提交
763 764
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
765
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
K
Kamil Trzcinski 已提交
766
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
767 768
    end

769 770 771 772 773
    class PushEventPayload < Grape::Entity
      expose :commit_count, :action, :ref_type, :commit_from, :commit_to
      expose :ref, :commit_title
    end

D
Dmitriy Zaporozhets 已提交
774
    class Event < Grape::Entity
775
      expose :project_id, :action_name
S
sue445 已提交
776
      expose :target_id, :target_iid, :target_type, :author_id
777
      expose :target_title
778
      expose :created_at
D
Dmitriy Zaporozhets 已提交
779 780
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
781

782 783 784 785 786
      expose :push_event_payload,
        as: :push_data,
        using: PushEventPayload,
        if: -> (event, _) { event.push? }

787
      expose :author_username do |event, options|
Z
Z.J. van de Weg 已提交
788
        event.author&.username
789
      end
D
Dmitriy Zaporozhets 已提交
790
    end
791

792
    class ProjectGroupLink < Grape::Entity
793
      expose :id, :project_id, :group_id, :group_access, :expires_at
794 795
    end

D
Douglas Barbosa Alexandre 已提交
796 797
    class Todo < Grape::Entity
      expose :id
798 799
      expose :project, using: Entities::ProjectIdentity, if: -> (todo, _) { todo.project_id }
      expose :group, using: 'API::Entities::NamespaceBasic', if: -> (todo, _) { todo.group_id }
D
Douglas Barbosa Alexandre 已提交
800
      expose :author, using: Entities::UserBasic
R
Robert Schilling 已提交
801
      expose :action_name
D
Douglas Barbosa Alexandre 已提交
802
      expose :target_type
803 804

      expose :target do |todo, options|
805
        todo_target_class(todo.target_type).represent(todo.target, options)
D
Douglas Barbosa Alexandre 已提交
806 807 808 809
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
810
        target_url    = "#{todo.parent.class.to_s.underscore}_#{target_type}_url"
811
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
D
Douglas Barbosa Alexandre 已提交
812

813 814
        Gitlab::Routing
          .url_helpers
815
          .public_send(target_url, todo.parent, todo.target, anchor: target_anchor) # rubocop:disable GitlabSecurity/PublicSend
D
Douglas Barbosa Alexandre 已提交
816 817 818 819 820
      end

      expose :body
      expose :state
      expose :created_at
821 822 823 824

      def todo_target_class(target_type)
        ::API::Entities.const_get(target_type)
      end
D
Douglas Barbosa Alexandre 已提交
825 826
    end

827
    class NamespaceBasic < Grape::Entity
828
      expose :id, :name, :path, :kind, :full_path, :parent_id
829
    end
830

831
    class Namespace < NamespaceBasic
832 833 834 835 836 837
      expose :members_count_with_descendants, if: -> (namespace, opts) { expose_members_count_with_descendants?(namespace, opts) } do |namespace, _|
        namespace.users_with_descendants.count
      end

      def expose_members_count_with_descendants?(namespace, opts)
        namespace.kind == 'group' && Ability.allowed?(opts[:current_user], :admin_group, namespace)
838
      end
839
    end
840

841
    class MemberAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
842
      expose :access_level
843
      expose :notification_level do |member, options|
844 845 846
        if member.notification_setting
          ::NotificationSetting.levels[member.notification_setting.level]
        end
847
      end
848 849
    end

850
    class ProjectAccess < MemberAccess
851 852
    end

853
    class GroupAccess < MemberAccess
854 855
    end

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    class NotificationSetting < Grape::Entity
      expose :level
      expose :events, if: ->(notification_setting, _) { notification_setting.custom? } do
        ::NotificationSetting::EMAIL_EVENTS.each do |event|
          expose event
        end
      end
    end

    class GlobalNotificationSetting < NotificationSetting
      expose :notification_email do |notification_setting, options|
        notification_setting.user.notification_email
      end
    end

871 872
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
873 874
      expose :push_events, :issues_events, :confidential_issues_events
      expose :merge_requests_events, :tag_push_events, :note_events
875
      expose :confidential_note_events, :pipeline_events, :wiki_page_events
876
      expose :job_events
877 878
      # Expose serialized properties
      expose :properties do |service, options|
S
Stan Hu 已提交
879
        service.properties.slice(*service.api_field_names)
880 881 882
      end
    end

883 884 885
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
886 887
          if options[:project_members]
            options[:project_members].find { |member| member.source_id == project.id }
888 889
          else
            project.project_member(options[:current_user])
890
          end
891 892 893
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
894
          if project.group
895 896
            if options[:group_members]
              options[:group_members].find { |member| member.source_id == project.namespace_id }
897 898
            else
              project.group.group_member(options[:current_user])
899
            end
900
          end
901 902
        end
      end
903 904 905 906

      def self.preload_relation(projects_relation, options = {})
        relation = super(projects_relation, options)

907 908 909 910 911 912 913
        # MySQL doesn't support LIMIT inside an IN subquery
        if Gitlab::Database.mysql?
          project_ids = relation.pluck('projects.id')
          namespace_ids = relation.pluck(:namespace_id)
        else
          project_ids = relation.select('projects.id')
          namespace_ids = relation.select(:namespace_id)
914 915
        end

916 917 918 919 920 921 922 923 924
        options[:project_members] = options[:current_user]
          .project_members
          .where(source_id: project_ids)
          .preload(:source, user: [notification_settings: :source])

        options[:group_members] = options[:current_user]
          .group_members
          .where(source_id: namespace_ids)
          .preload(:source, user: [notification_settings: :source])
925 926 927

        relation
      end
928
    end
929

A
Andre Guedes 已提交
930
    class LabelBasic < Grape::Entity
R
Rares Sfirlogea 已提交
931
      expose :id, :name, :color, :description
A
Andre Guedes 已提交
932 933 934
    end

    class Label < LabelBasic
935
      expose :open_issues_count do |label, options|
F
Francesco Coda Zabetta 已提交
936 937
        label.open_issues_count(options[:current_user])
      end
938

F
Francesco Coda Zabetta 已提交
939 940 941
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
942

F
Francesco Coda Zabetta 已提交
943 944
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
945 946
      end

947 948 949
      expose :priority do |label, options|
        label.priority(options[:project])
      end
950 951

      expose :subscribed do |label, options|
952
        label.subscribed?(options[:current_user], options[:project])
953
      end
954
    end
955

A
Andre Guedes 已提交
956 957 958 959 960 961 962 963
    class List < Grape::Entity
      expose :id
      expose :label, using: Entities::LabelBasic
      expose :position
    end

    class Board < Grape::Entity
      expose :id
F
Felipe Artur 已提交
964 965
      expose :project, using: Entities::BasicProjectDetails

A
Andre Guedes 已提交
966 967 968 969 970
      expose :lists, using: Entities::List do |board|
        board.lists.destroyable
      end
    end

971
    class Compare < Grape::Entity
972 973
      expose :commit, using: Entities::Commit do |compare, options|
        ::Commit.decorate(compare.commits, nil).last
974
      end
975

976 977
      expose :commits, using: Entities::Commit do |compare, options|
        ::Commit.decorate(compare.commits, nil)
978
      end
979

980
      expose :diffs, using: Entities::Diff do |compare, options|
D
Douwe Maan 已提交
981
        compare.diffs(limits: false).to_a
982
      end
983 984

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
985
        compare.diffs.overflow?
986 987 988
      end

      expose :same, as: :compare_same_ref
989
    end
990 991 992 993

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
994 995 996 997

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
998 999

    class ApplicationSetting < Grape::Entity
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
      def self.exposed_attributes
        attributes = ::ApplicationSettingsHelper.visible_attributes
        attributes.delete(:performance_bar_allowed_group_path)
        attributes.delete(:performance_bar_enabled)

        attributes
      end

      expose :id, :performance_bar_allowed_group_id
      expose(*exposed_attributes)
1010 1011 1012 1013 1014 1015
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
      expose(:default_project_visibility) { |setting, _options| Gitlab::VisibilityLevel.string_level(setting.default_project_visibility) }
      expose(:default_snippet_visibility) { |setting, _options| Gitlab::VisibilityLevel.string_level(setting.default_snippet_visibility) }
      expose(:default_group_visibility) { |setting, _options| Gitlab::VisibilityLevel.string_level(setting.default_group_visibility) }
1016 1017 1018 1019

      # support legacy names, can be removed in v5
      expose :password_authentication_enabled_for_web, as: :password_authentication_enabled
      expose :password_authentication_enabled_for_web, as: :signin_enabled
1020
    end
D
Dmitriy Zaporozhets 已提交
1021 1022

    class Release < Grape::Entity
1023 1024
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
1025
    end
1026

R
Robert Schilling 已提交
1027
    class Tag < Grape::Entity
1028
      expose :name, :message, :target
1029

1030
      expose :commit, using: Entities::Commit do |repo_tag, options|
1031
        options[:project].repository.commit(repo_tag.dereferenced_target)
1032 1033
      end

1034 1035
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
1036 1037
      end
    end
K
Kamil Trzcinski 已提交
1038

T
Tomasz Maczukin 已提交
1039
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
1040 1041
      expose :id
      expose :description
1042
      expose :ip_address
T
Tomasz Maczukin 已提交
1043
      expose :active
1044
      expose :instance_type?, as: :is_shared
T
Tomasz Maczukin 已提交
1045
      expose :name
1046
      expose :online?, as: :online
1047
      expose :status
T
Tomasz Maczukin 已提交
1048 1049
    end

1050 1051
    class RunnerDetails < Runner
      expose :tag_list
1052
      expose :run_untagged
1053
      expose :locked
1054
      expose :maximum_timeout
S
Shinya Maeda 已提交
1055
      expose :access_level
1056
      expose :version, :revision, :platform, :architecture
1057
      expose :contacted_at
1058
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.instance_type? }
1059
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
B
blackst0ne 已提交
1060
        if options[:current_user].admin?
1061 1062
          runner.projects
        else
1063
          options[:current_user].authorized_projects.where(id: runner.projects)
1064 1065
        end
      end
1066 1067 1068 1069 1070 1071 1072
      expose :groups, with: Entities::BasicGroupDetails do |runner, options|
        if options[:current_user].admin?
          runner.groups
        else
          options[:current_user].authorized_groups.where(id: runner.groups)
        end
      end
1073 1074
    end

1075 1076 1077 1078
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

1079
    class JobArtifactFile < Grape::Entity
1080 1081 1082
      expose :filename, :size
    end

T
Tomasz Maczukin 已提交
1083
    class JobBasic < Grape::Entity
T
Tomasz Maczukin 已提交
1084
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
1085
      expose :created_at, :started_at, :finished_at
M
Mehdi Lahmam 已提交
1086
      expose :duration
T
Tomasz Maczukin 已提交
1087
      expose :user, with: User
1088
      expose :commit, with: Commit
1089
      expose :pipeline, with: PipelineBasic
1090 1091 1092 1093

      expose :web_url do |job, _options|
        Gitlab::Routing.url_helpers.project_job_url(job.project, job)
      end
1094
    end
1095

T
Tomasz Maczukin 已提交
1096 1097 1098
    class Job < JobBasic
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
      expose :runner, with: Runner
1099
      expose :artifacts_expire_at
T
Tomasz Maczukin 已提交
1100 1101 1102
    end

    class JobBasicWithProject < JobBasic
T
Tomasz Maczukin 已提交
1103 1104 1105
      expose :project, with: ProjectIdentity
    end

T
Tomasz Maczukin 已提交
1106
    class Trigger < Grape::Entity
1107
      expose :id
1108
      expose :token, :description
1109
      expose :created_at, :updated_at, :last_used
1110
      expose :owner, using: Entities::UserBasic
T
Tomasz Maczukin 已提交
1111
    end
1112

1113
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
1114
      expose :key, :value
S
Shinya Maeda 已提交
1115
      expose :protected?, as: :protected, if: -> (entity, _) { entity.respond_to?(:protected?) }
1116
    end
1117

1118 1119
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z
Z.J. van de Weg 已提交
1120 1121 1122 1123

      expose :user, with: Entities::UserBasic
      expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
      expose :duration
1124
      expose :coverage
Z
Z.J. van de Weg 已提交
1125 1126
    end

1127 1128 1129
    class PipelineSchedule < Grape::Entity
      expose :id
      expose :description, :ref, :cron, :cron_timezone, :next_run_at, :active
1130
      expose :created_at, :updated_at
1131 1132 1133
      expose :owner, using: Entities::UserBasic
    end

S
Shinya Maeda 已提交
1134 1135
    class PipelineScheduleDetails < PipelineSchedule
      expose :last_pipeline, using: Entities::PipelineBasic
1136
      expose :variables, using: Entities::Variable
S
Shinya Maeda 已提交
1137 1138
    end

1139
    class EnvironmentBasic < Grape::Entity
N
Nick Thomas 已提交
1140
      expose :id, :name, :slug, :external_url
1141 1142
    end

1143
    class Environment < EnvironmentBasic
1144
      expose :project, using: Entities::BasicProjectDetails
Z
Z.J. van de Weg 已提交
1145 1146 1147 1148
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
1149 1150
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
1151
      expose :deployable,  using: Entities::Job
1152 1153
    end

1154
    class License < Grape::Entity
1155 1156
      expose :key, :name, :nickname
      expose :featured, as: :popular
1157 1158 1159
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
1160 1161 1162
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
1163 1164
      expose :content
    end
1165

Z
ZJ van de Weg 已提交
1166
    class TemplatesList < Grape::Entity
1167 1168 1169
      expose :name
    end

Z
ZJ van de Weg 已提交
1170
    class Template < Grape::Entity
1171 1172
      expose :name, :content
    end
1173 1174 1175 1176 1177

    class BroadcastMessage < Grape::Entity
      expose :id, :message, :starts_at, :ends_at, :color, :font
      expose :active?, as: :active
    end
T
Tomasz Maczukin 已提交
1178

1179
    class PersonalAccessToken < Grape::Entity
1180 1181 1182 1183 1184 1185 1186
      expose :id, :name, :revoked, :created_at, :scopes
      expose :active?, as: :active
      expose :expires_at do |personal_access_token|
        personal_access_token.expires_at ? personal_access_token.expires_at.strftime("%Y-%m-%d") : nil
      end
    end

1187
    class PersonalAccessTokenWithToken < PersonalAccessToken
1188 1189
      expose :token
    end
1190 1191 1192 1193

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
1194

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
    class FeatureGate < Grape::Entity
      expose :key
      expose :value
    end

    class Feature < Grape::Entity
      expose :name
      expose :state
      expose :gates, using: FeatureGate do |model|
        model.gates.map do |gate|
          value = model.gate_values[gate.key]

          # By default all gate values are populated. Only show relevant ones.
          if (value.is_a?(Integer) && value.zero?) || (value.is_a?(Set) && value.empty?)
            next
          end

          { key: gate.key, value: value }
        end.compact
      end
    end

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    module JobRequest
      class JobInfo < Grape::Entity
        expose :name, :stage
        expose :project_id, :project_name
      end

      class GitInfo < Grape::Entity
        expose :repo_url, :ref, :sha, :before_sha
        expose :ref_type do |model|
          if model.tag
            'tag'
          else
            'branch'
          end
        end
      end
T
Tomasz Maczukin 已提交
1233

1234
      class RunnerInfo < Grape::Entity
T
Tomasz Maczukin 已提交
1235
        expose :metadata_timeout, as: :timeout
F
Francisco Javier López 已提交
1236
        expose :runner_session_url
1237
      end
T
Tomasz Maczukin 已提交
1238

1239
      class Step < Grape::Entity
T
Tomasz Maczukin 已提交
1240
        expose :name, :script, :timeout, :when, :allow_failure
1241
      end
T
Tomasz Maczukin 已提交
1242

1243
      class Image < Grape::Entity
1244 1245 1246
        expose :name, :entrypoint
      end

1247
      class Service < Image
1248
        expose :alias, :command
1249
      end
T
Tomasz Maczukin 已提交
1250

1251
      class Artifacts < Grape::Entity
S
Shinya Maeda 已提交
1252 1253 1254 1255 1256 1257 1258
        expose :name
        expose :untracked
        expose :paths
        expose :when
        expose :expire_in
        expose :artifact_type
        expose :artifact_format
T
Tomasz Maczukin 已提交
1259 1260
      end

1261
      class Cache < Grape::Entity
1262
        expose :key, :untracked, :paths, :policy
T
Tomasz Maczukin 已提交
1263 1264
      end

1265 1266 1267
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
T
Tomasz Maczukin 已提交
1268

1269
      class Dependency < Grape::Entity
T
Tomasz Maczukin 已提交
1270
        expose :id, :name, :token
1271
        expose :artifacts_file, using: JobArtifactFile, if: ->(job, _) { job.artifacts? }
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
      end

      class Response < Grape::Entity
        expose :id
        expose :token
        expose :allow_git_fetch

        expose :job_info, using: JobInfo do |model|
          model
        end

        expose :git_info, using: GitInfo do |model|
          model
        end

        expose :runner_info, using: RunnerInfo do |model|
          model
        end

        expose :variables
        expose :steps, using: Step
        expose :image, using: Image
1294
        expose :services, using: Service
1295 1296 1297
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
T
Tomasz Maczukin 已提交
1298
        expose :dependencies, using: Dependency
1299
        expose :features
1300
      end
T
Tomasz Maczukin 已提交
1301
    end
1302 1303 1304 1305

    class UserAgentDetail < Grape::Entity
      expose :user_agent
      expose :ip_address
J
James Lopez 已提交
1306
      expose :submitted, as: :akismet_submitted
1307
    end
1308 1309 1310 1311 1312 1313

    class RepositoryStorageHealth < Grape::Entity
      expose :storage_name
      expose :failing_on_hosts
      expose :total_failures
    end
1314 1315 1316 1317 1318

    class CustomAttribute < Grape::Entity
      expose :key
      expose :value
    end
T
Travis Miller 已提交
1319

1320 1321 1322 1323 1324
    class PagesDomainCertificateExpiration < Grape::Entity
      expose :expired?, as: :expired
      expose :expiration
    end

T
Travis Miller 已提交
1325 1326 1327 1328 1329 1330 1331
    class PagesDomainCertificate < Grape::Entity
      expose :subject
      expose :expired?, as: :expired
      expose :certificate
      expose :certificate_text
    end

1332 1333 1334
    class PagesDomainBasic < Grape::Entity
      expose :domain
      expose :url
1335
      expose :project_id
1336 1337 1338 1339
      expose :verified?, as: :verified
      expose :verification_code, as: :verification_code
      expose :enabled_until

1340 1341 1342 1343 1344 1345 1346 1347
      expose :certificate,
        as: :certificate_expiration,
        if: ->(pages_domain, _) { pages_domain.certificate? },
        using: PagesDomainCertificateExpiration do |pages_domain|
        pages_domain
      end
    end

T
Travis Miller 已提交
1348 1349 1350
    class PagesDomain < Grape::Entity
      expose :domain
      expose :url
1351 1352 1353 1354
      expose :verified?, as: :verified
      expose :verification_code, as: :verification_code
      expose :enabled_until

T
Travis Miller 已提交
1355
      expose :certificate,
1356 1357
        if: ->(pages_domain, _) { pages_domain.certificate? },
        using: PagesDomainCertificate do |pages_domain|
T
Travis Miller 已提交
1358 1359 1360
        pages_domain
      end
    end
N
Nicolas MERELLI 已提交
1361 1362 1363 1364 1365

    class Application < Grape::Entity
      expose :uid, as: :application_id
      expose :redirect_uri, as: :callback_url
    end
1366 1367 1368 1369 1370

    # Use with care, this exposes the secret
    class ApplicationWithSecret < Application
      expose :secret
    end
J
Jarka Kadlecová 已提交
1371 1372 1373 1374 1375 1376 1377 1378

    class Blob < Grape::Entity
      expose :basename
      expose :data
      expose :filename
      expose :id
      expose :ref
      expose :startline
1379
      expose :project_id
J
Jarka Kadlecová 已提交
1380
    end
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

    class BasicBadgeDetails < Grape::Entity
      expose :link_url
      expose :image_url
      expose :rendered_link_url do |badge, options|
        badge.rendered_link_url(options.fetch(:project, nil))
      end
      expose :rendered_image_url do |badge, options|
        badge.rendered_image_url(options.fetch(:project, nil))
      end
    end

    class Badge < BasicBadgeDetails
      expose :id
      expose :kind do |badge|
        badge.type == 'ProjectBadge' ? 'project' : 'group'
      end
    end
N
Nihad Abbasov 已提交
1399 1400
  end
end