entities.rb 29.1 KB
Newer Older
1
module API
Nihad Abbasov's avatar
Nihad Abbasov committed
2
  module Entities
3 4 5
    class UserSafe < Grape::Entity
      expose :name, :username
    end
6

7
    class UserBasic < UserSafe
8 9 10 11
      expose :id, :state
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
Douwe Maan's avatar
Douwe Maan committed
12 13

      expose :web_url do |user, options|
14
        Gitlab::Routing.url_helpers.user_url(user)
Douwe Maan's avatar
Douwe Maan committed
15
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
16
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
17

18 19
    class User < UserBasic
      expose :created_at
20
      expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
21 22
    end

23 24
    class UserActivity < Grape::Entity
      expose :username
25 26
      expose :last_activity_on
      expose :last_activity_on, as: :last_activity_at # Back-compat
27 28
    end

29 30 31 32
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

33
    class UserPublic < User
34 35
      expose :last_sign_in_at
      expose :confirmed_at
36
      expose :last_activity_on
37
      expose :email
38
      expose :color_scheme_id, :projects_limit, :current_sign_in_at
39
      expose :identities, using: Entities::Identity
40 41
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
42
      expose :two_factor_enabled?, as: :two_factor_enabled
43
      expose :external
44 45 46

      # EE-only
      expose :shared_runners_minutes_limit
47 48
    end

49
    class UserWithPrivateDetails < UserPublic
50
      expose :private_token
51
      expose :admin?, as: :is_admin
52 53
    end

54 55 56 57
    class Email < Grape::Entity
      expose :id, :email
    end

miks's avatar
miks committed
58
    class Hook < Grape::Entity
59
      expose :id, :url, :created_at, :push_events, :tag_push_events, :repository_update_events
60
      expose :enable_ssl_verification
miks's avatar
miks committed
61 62
    end

63
    class ProjectHook < Hook
64
      expose :project_id, :issues_events, :merge_requests_events
65
      expose :note_events, :pipeline_events, :wiki_page_events
66
      expose :job_events
67
    end
Valery Sizov's avatar
Valery Sizov committed
68

69
    class ProjectPushRule < Grape::Entity
Valery Sizov's avatar
Valery Sizov committed
70
      expose :id, :project_id, :created_at
71
      expose :commit_message_regex, :branch_name_regex, :deny_delete_tag
72 73
      expose :member_check, :prevent_secrets, :author_email_regex
      expose :file_name_regex, :max_file_size
Valery Sizov's avatar
Valery Sizov committed
74
    end
75

76
    class BasicProjectDetails < Grape::Entity
77
      expose :id
78
      expose :http_url_to_repo, :web_url
79 80 81 82
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

83 84 85 86 87 88 89 90
    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

Nihad Abbasov's avatar
Nihad Abbasov committed
91
    class Project < Grape::Entity
92
      expose :id, :description, :default_branch, :tag_list
93
      expose :archived?, as: :archived
94
      expose :visibility, :ssh_url_to_repo, :http_url_to_repo, :web_url
95
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
96
      expose :name, :name_with_namespace
97
      expose :path, :path_with_namespace
98 99 100
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
101 102 103
      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]) }
104
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
105
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
106

107
      expose :created_at, :last_activity_at
108 109
      expose :shared_runners_enabled
      expose :lfs_enabled?, as: :lfs_enabled
110
      expose :creator_id
111
      expose :namespace, using: 'API::Entities::Namespace'
112
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
113 114
      expose :import_status
      expose :import_error, if: lambda { |_project, options| options[:user_can_admin_project] }
115 116 117
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
118
      expose :star_count, :forks_count
119
      expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) && project.default_issues_tracker? }
120
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
121
      expose :public_builds, as: :public_jobs
122 123 124
      expose :shared_with_groups do |project, options|
        SharedGroup.represent(project.project_group_links.all, options)
      end
125
      expose :only_allow_merge_if_pipeline_succeeds
126
      expose :repository_storage, if: lambda { |_project, options| options[:current_user].try(:admin?) }
127
      expose :request_access_enabled
128
      expose :only_allow_merge_if_all_discussions_are_resolved
129
      expose :approvals_before_merge
130

131 132 133 134 135 136 137 138
      expose :statistics, using: 'API::Entities::ProjectStatistics', if: :statistics
    end

    class ProjectStatistics < Grape::Entity
      expose :commit_count
      expose :storage_size
      expose :repository_size
      expose :lfs_objects_size
139
      expose :build_artifacts_size, as: :job_artifacts_size
Nihad Abbasov's avatar
Nihad Abbasov committed
140 141
    end

142
    class Member < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
143
      expose :access_level do |user, options|
144
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
145 146
        member.access_level
      end
147
      expose :expires_at do |user, options|
148
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
149 150
        member.expires_at
      end
151 152 153 154
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
155
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
156
        access_requester.requested_at
157
      end
miks's avatar
miks committed
158 159
    end

160 161 162 163
    class LdapGroupLink < Grape::Entity
      expose :cn, :group_access, :provider
    end

164
    class Group < Grape::Entity
165
      expose :id, :name, :path, :description, :visibility
Douwe Maan's avatar
Douwe Maan committed
166

167
      ## EE-only
168 169 170 171
      expose :ldap_cn, :ldap_access
      expose :ldap_group_links,
        using: Entities::LdapGroupLink,
        if: lambda { |group, options| group.ldap_group_links.any? }
172
      ## EE-only
Douwe Maan's avatar
Douwe Maan committed
173

174
      expose :lfs_enabled?, as: :lfs_enabled
175 176 177
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
178
      expose :web_url
179
      expose :request_access_enabled
180
      expose :full_name, :full_path
181 182 183 184

      if ::Group.supports_nested_groups?
        expose :parent_id
      end
185 186 187 188 189 190

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
191
          expose :build_artifacts_size, as: :job_artifacts_size
192 193
        end
      end
194
    end
Andrew8xx8's avatar
Andrew8xx8 committed
195

196
    class GroupDetail < Group
197
      expose :projects, using: Entities::Project
198
      expose :shared_projects, using: Entities::Project
199 200 201

      # EE-only
      expose :shared_runners_minutes_limit
202 203
    end

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    class RepoCommit < Grape::Entity
      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

    class RepoCommitStats < Grape::Entity
      expose :additions, :deletions, :total
    end

    class RepoCommitDetail < RepoCommit
      expose :stats, using: Entities::RepoCommitStats
      expose :status
    end

221
    class RepoBranch < Grape::Entity
222 223
      expose :name

224
      expose :commit, using: Entities::RepoCommit do |repo_branch, options|
225
        options[:project].repository.commit(repo_branch.dereferenced_target)
226 227
      end

228 229 230 231
      expose :merged do |repo_branch, options|
        options[:project].repository.merged_to_root_ref?(repo_branch.name)
      end

232
      expose :protected do |repo_branch, options|
233
        ProtectedBranch.protected?(options[:project], repo_branch.name)
234 235
      end

236
      expose :developers_can_push do |repo_branch, options|
237
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
238
      end
239

240
      expose :developers_can_merge do |repo_branch, options|
241
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
242
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
243
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
244

245
    class RepoTreeObject < Grape::Entity
246
      expose :id, :name, :type, :path
247 248

      expose :mode do |obj, options|
249
        filemode = obj.mode
250 251 252 253 254
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
255
    class ProjectSnippet < Grape::Entity
256
      expose :id, :title, :file_name, :description
257
      expose :author, using: Entities::UserBasic
258
      expose :updated_at, :created_at
259

260 261 262
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
263
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
264

265
    class PersonalSnippet < Grape::Entity
266
      expose :id, :title, :file_name, :description
267 268 269 270 271 272 273 274 275 276 277
      expose :author, using: Entities::UserBasic
      expose :updated_at, :created_at

      expose :web_url do |snippet|
        Gitlab::UrlBuilder.build(snippet)
      end
      expose :raw_url do |snippet|
        Gitlab::UrlBuilder.build(snippet) + "/raw"
      end
    end

278 279
    class ProjectEntity < Grape::Entity
      expose :id, :iid
280
      expose(:project_id) { |entity| entity.project.id }
281 282
      expose :title, :description
      expose :state, :created_at, :updated_at
283 284
    end

285 286
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
287 288 289
      expose :new_file?, as: :new_file
      expose :renamed_file?, as: :renamed_file
      expose :deleted_file?, as: :deleted_file
290 291
    end

292
    class Milestone < ProjectEntity
293
      expose :due_date
294
      expose :start_date
Nihad Abbasov's avatar
Nihad Abbasov committed
295 296
    end

297
    class IssueBasic < ProjectEntity
298
      expose :label_names, as: :labels
299
      expose :milestone, using: Entities::Milestone
300
      expose :assignees, :author, using: Entities::UserBasic
301

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

306
      expose :user_notes_count
307
      expose :upvotes, :downvotes
308
      expose :due_date
309
      expose :confidential
310
      expose :weight
311 312 313 314

      expose :web_url do |issue, options|
        Gitlab::UrlBuilder.build(issue)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
315
    end
Alex Denisov's avatar
Alex Denisov committed
316

317 318 319 320 321 322
    class Issue < IssueBasic
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

323 324 325 326 327
    class RelatedIssue < Issue
      expose :issue_link_id
    end

    class IssueLink < Grape::Entity
328 329
      expose :source, as: :source_issue, using: Entities::IssueBasic
      expose :target, as: :target_issue, using: Entities::IssueBasic
330 331
    end

332 333 334 335 336 337 338
    class IssuableTimeStats < Grape::Entity
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
      expose :human_total_time_spent
    end

339 340 341 342 343
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

344
    class MergeRequestBasic < ProjectEntity
345
      expose :target_branch, :source_branch
346
      expose :upvotes, :downvotes
347 348
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
349
      expose :label_names, as: :labels
350
      expose :work_in_progress?, as: :work_in_progress
351
      expose :milestone, using: Entities::Milestone
352
      expose :merge_when_pipeline_succeeds
353
      expose :merge_status
354 355
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
356
      expose :user_notes_count
357
      expose :approvals_before_merge
358 359
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
Sean McGivern's avatar
Sean McGivern committed
360
      expose :squash
361 362 363 364

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
Alex Denisov's avatar
Alex Denisov committed
365
    end
366

367 368 369 370 371 372
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
    end

373 374
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
Douwe Maan's avatar
Douwe Maan committed
375
        compare.raw_diffs(limits: false).to_a
376 377 378
      end
    end

Patricio Cano's avatar
Patricio Cano committed
379
    class Approvals < Grape::Entity
Patricio Cano's avatar
Patricio Cano committed
380
      expose :user, using: Entities::UserBasic
Patricio Cano's avatar
Patricio Cano committed
381 382 383 384
    end

    class MergeRequestApprovals < ProjectEntity
      expose :merge_status
385 386
      expose :approvals_required
      expose :approvals_left
Patricio Cano's avatar
Patricio Cano committed
387
      expose :approvals, as: :approved_by, using: Entities::Approvals
388
      expose :approvers_left, as: :suggested_approvers, using: Entities::UserBasic
389 390 391 392 393 394 395 396

      expose :user_has_approved do |merge_request, options|
        merge_request.has_approved?(options[:current_user])
      end

      expose :user_can_approve do |merge_request, options|
        merge_request.can_approve?(options[:current_user])
      end
Patricio Cano's avatar
Patricio Cano committed
397 398
    end

399 400 401
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
402
    end
403

404
    class MergeRequestDiffFull < MergeRequestDiff
405 406 407
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
Douwe Maan's avatar
Douwe Maan committed
408
        compare.raw_diffs(limits: false).to_a
409 410 411
      end
    end

412
    class SSHKey < Grape::Entity
413
      expose :id, :title, :key, :created_at, :can_push
414
    end
415

416
    class SSHKeyWithUser < SSHKey
417
      expose :user, using: Entities::UserPublic
418 419
    end

420
    class Note < Grape::Entity
421 422
      expose :id
      expose :note, as: :body
423
      expose :attachment_identifier, as: :attachment
424
      expose :author, using: Entities::UserBasic
425
      expose :created_at, :updated_at
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
426
      expose :system?, as: :system
427
      expose :noteable_id, :noteable_type
428
    end
429

430 431 432 433 434 435 436 437
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

438 439 440 441
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
442

443 444
    class CommitNote < Grape::Entity
      expose :note
445 446 447
      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? }
448
      expose :author, using: Entities::UserBasic
449
      expose :created_at
450 451
    end

452 453
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
454
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
Kamil Trzcinski's avatar
Kamil Trzcinski committed
455
      expose :author, using: Entities::UserBasic
456 457
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
458 459 460 461
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
462
      expose :created_at
463 464
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
465 466

      expose :author_username do |event, options|
467
        event.author&.username
468
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
469
    end
470 471 472 473

    class LdapGroup < Grape::Entity
      expose :cn
    end
474 475

    class ProjectGroupLink < Grape::Entity
476
      expose :id, :project_id, :group_id, :group_access, :expires_at
477
    end
478

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
479 480 481 482
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling's avatar
Robert Schilling committed
483
      expose :action_name
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
484
      expose :target_type
485 486

      expose :target do |todo, options|
487 488
        target = todo.target_type == 'Commit' ? 'RepoCommit' : todo.target_type
        Entities.const_get(target).represent(todo.target, options)
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
489 490 491 492 493
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
494
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
495 496 497 498 499 500 501 502 503 504

        Gitlab::Application.routes.url_helpers.public_send(target_url,
          todo.project.namespace, todo.project, todo.target, anchor: target_anchor)
      end

      expose :body
      expose :state
      expose :created_at
    end

505
    class Namespace < Grape::Entity
506
      expose :plan, if: lambda { |_, options| options[:current_user] && options[:current_user].admin? }
507
      expose :id, :name, :path, :kind, :full_path
508
    end
509

510
    class MemberAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
511
      expose :access_level
512 513
      expose :notification_level do |member, options|
        if member.notification_setting
514
          ::NotificationSetting.levels[member.notification_setting.level]
515 516
        end
      end
517 518
    end

519
    class ProjectAccess < MemberAccess
520 521
    end

522
    class GroupAccess < MemberAccess
523 524
    end

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    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

540 541
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
542
      expose :push_events, :issues_events, :merge_requests_events
543
      expose :tag_push_events, :note_events, :pipeline_events
544
      expose :job_events
545 546
      # Expose serialized properties
      expose :properties do |service, options|
547 548 549
        field_names = service.fields
          .select { |field| options[:include_passwords] || field[:type] != 'password' }
          .map { |field| field[:name] }
550 551 552 553
        service.properties.slice(*field_names)
      end
    end

554 555 556
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
557
          project.project_members.find_by(user_id: options[:current_user].id)
558 559 560
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
561
          if project.group
562
            project.group.group_members.find_by(user_id: options[:current_user].id)
563
          end
564 565 566
        end
      end
    end
567

568
    class LabelBasic < Grape::Entity
Rares Sfirlogea's avatar
Rares Sfirlogea committed
569
      expose :id, :name, :color, :description
570 571 572
    end

    class Label < LabelBasic
573
      expose :open_issues_count do |label, options|
Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
574 575
        label.open_issues_count(options[:current_user])
      end
576

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
577 578 579
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
580

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
581 582
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
583 584
      end

585 586 587
      expose :priority do |label, options|
        label.priority(options[:project])
      end
588 589

      expose :subscribed do |label, options|
590
        label.subscribed?(options[:current_user], options[:project])
591
      end
592
    end
593

594 595 596 597 598 599 600 601
    class List < Grape::Entity
      expose :id
      expose :label, using: Entities::LabelBasic
      expose :position
    end

    class Board < Grape::Entity
      expose :id
602 603 604
      expose :name
      expose :project, using: Entities::BasicProjectDetails
      expose :milestone
605 606 607 608 609
      expose :lists, using: Entities::List do |board|
        board.lists.destroyable
      end
    end

610 611
    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
612
        Commit.decorate(compare.commits, nil).last
613
      end
614

615
      expose :commits, using: Entities::RepoCommit do |compare, options|
616
        Commit.decorate(compare.commits, nil)
617
      end
618

619
      expose :diffs, using: Entities::RepoDiff do |compare, options|
Douwe Maan's avatar
Douwe Maan committed
620
        compare.diffs(limits: false).to_a
621
      end
622 623

      expose :compare_timeout do |compare, options|
624
        compare.diffs.overflow?
625 626 627
      end

      expose :same, as: :compare_same_ref
628
    end
629 630 631 632

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
633 634 635 636

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
637 638 639 640 641 642 643 644

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
645
      expose :after_sign_up_text
646 647 648 649
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
650 651 652
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
653 654
      expose :max_attachment_size
      expose :session_expire_delay
655 656 657
      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) }
658
      expose :default_artifacts_expire_in
659
      expose :domain_whitelist
660 661
      expose :domain_blacklist_enabled
      expose :domain_blacklist
662 663
      expose :user_oauth_applications
      expose :after_sign_out_path
664
      expose :container_registry_token_expire_delay
665
      expose :repository_storages
666 667
      expose :koding_enabled
      expose :koding_url
668 669
      expose :plantuml_enabled
      expose :plantuml_url
670
      expose :terminal_max_session_time
671
      expose :polling_interval_multiplier
672 673 674
      expose :help_page_hide_commercial_content
      expose :help_page_text
      expose :help_page_support_url
675
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
676 677

    class Release < Grape::Entity
678 679
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
680
    end
681 682

    class RepoTag < Grape::Entity
683
      expose :name, :message
684

685
      expose :commit do |repo_tag, options|
686
        options[:project].repository.commit(repo_tag.dereferenced_target)
687 688
      end

689 690
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
691 692
      end
    end
693 694

    class License < Grape::Entity
695
      expose :starts_at, :expires_at, :licensee, :add_ons
696 697 698 699 700 701 702 703 704

      expose :user_limit do |license, options|
        license.restricted?(:active_user_count) ? license.restrictions[:active_user_count] : 0
      end

      expose :active_users do |license, options|
        ::User.active.count
      end
    end
705

Kamil Trzcinski's avatar
Kamil Trzcinski committed
706 707 708
    class TriggerRequest < Grape::Entity
      expose :id, :variables
    end
709 710 711 712 713 714 715 716 717

    class Runner < Grape::Entity
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

718 719
    class RunnerDetails < Runner
      expose :tag_list
720
      expose :run_untagged
721
      expose :locked
722
      expose :version, :revision, :platform, :architecture
723
      expose :contacted_at
724
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
725
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
726
        if options[:current_user].admin?
727 728
          runner.projects
        else
729
          options[:current_user].authorized_projects.where(id: runner.projects)
730 731
        end
      end
732 733
    end

734 735 736 737
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

738
    class JobArtifactFile < Grape::Entity
739 740 741
      expose :filename, :size
    end

742 743 744 745
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

746
    class Job < Grape::Entity
747 748 749
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
      expose :created_at, :started_at, :finished_at
      expose :user, with: User
Z.J. van de Weg's avatar
Z.J. van de Weg committed
750
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
751
      expose :commit, with: RepoCommit
752
      expose :runner, with: Runner
753
      expose :pipeline, with: PipelineBasic
754 755 756
    end

    class Trigger < Grape::Entity
757
      expose :id
758 759 760
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
761 762 763 764
    end

    class Variable < Grape::Entity
      expose :key, :value
765
      expose :protected?, as: :protected
766
    end
767

768 769
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
770 771 772 773

      expose :user, with: Entities::UserBasic
      expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
      expose :duration
774
      expose :coverage
775 776
    end

777 778 779
    class PipelineSchedule < Grape::Entity
      expose :id
      expose :description, :ref, :cron, :cron_timezone, :next_run_at, :active
780
      expose :created_at, :updated_at
781 782 783
      expose :owner, using: Entities::UserBasic
    end

Shinya Maeda's avatar
Shinya Maeda committed
784 785 786 787
    class PipelineScheduleDetails < PipelineSchedule
      expose :last_pipeline, using: Entities::PipelineBasic
    end

788
    class EnvironmentBasic < Grape::Entity
Nick Thomas's avatar
Nick Thomas committed
789
      expose :id, :name, :slug, :external_url
790 791
    end

792
    class Environment < EnvironmentBasic
793
      expose :project, using: Entities::BasicProjectDetails
794 795 796 797 798 799
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
800
      expose :deployable,  using: Entities::Job
801 802
    end

803
    class RepoLicense < Grape::Entity
804 805
      expose :key, :name, :nickname
      expose :featured, as: :popular
806 807 808
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
809 810 811
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
812 813
      expose :content
    end
814

815
    class TemplatesList < Grape::Entity
816 817 818
      expose :name
    end

819
    class Template < Grape::Entity
820 821
      expose :name, :content
    end
822 823 824 825 826

    class BroadcastMessage < Grape::Entity
      expose :id, :message, :starts_at, :ends_at, :color, :font
      expose :active?, as: :active
    end
827 828

    class GeoNodeStatus < Grape::Entity
829
      expose :id
830
      expose :health
831
      expose :healthy?, as: :healthy
832 833 834
      expose :repositories_count
      expose :repositories_synced_count
      expose :repositories_failed_count
835 836
      expose :lfs_objects_count
      expose :lfs_objects_synced_count
837 838
      expose :attachments_count
      expose :attachments_synced_count
Nick Thomas's avatar
Nick Thomas committed
839 840
    end

841
    class PersonalAccessToken < Grape::Entity
842 843 844 845 846 847 848
      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

849
    class PersonalAccessTokenWithToken < PersonalAccessToken
850 851
      expose :token
    end
852 853 854 855

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    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

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    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
Tomasz Maczukin's avatar
Tomasz Maczukin committed
895

896 897 898
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
899

900
      class Step < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
901
        expose :name, :script, :timeout, :when, :allow_failure
902
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
903

904
      class Image < Grape::Entity
905 906 907
        expose :name, :entrypoint
      end

908
      class Service < Image
909
        expose :alias, :command
910
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
911

912 913
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
Tomasz Maczukin's avatar
Tomasz Maczukin committed
914 915
      end

916 917
      class Cache < Grape::Entity
        expose :key, :untracked, :paths
Tomasz Maczukin's avatar
Tomasz Maczukin committed
918 919
      end

920 921 922
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
923

924 925 926 927 928
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
929
        expose :id, :name, :token
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
        expose :artifacts_file, using: ArtifactFile, if: ->(job, _) { job.artifacts? }
      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
953
        expose :services, using: Service
954 955 956
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
957
        expose :dependencies, using: Dependency
958
      end
959
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
960 961
  end
end