entities.rb 27.6 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
    end

46
    class UserWithAdmin < UserPublic
47
      expose :admin?, as: :is_admin
48 49
    end

50 51 52 53
    class UserWithPrivateDetails < UserWithAdmin
      expose :private_token
    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 68
    end

69
    class BasicProjectDetails < Grape::Entity
70
      expose :id
71
      expose :http_url_to_repo, :web_url
72 73 74 75
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

76 77 78 79 80 81 82 83
    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
84
    class Project < Grape::Entity
85
      expose :id, :description, :default_branch, :tag_list
86
      expose :archived?, as: :archived
87
      expose :visibility, :ssh_url_to_repo, :http_url_to_repo, :web_url
88
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
89
      expose :name, :name_with_namespace
90
      expose :path, :path_with_namespace
91 92 93
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
94 95 96
      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]) }
97
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
98
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
99

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

      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
132
      expose :build_artifacts_size, as: :job_artifacts_size
Nihad Abbasov's avatar
Nihad Abbasov committed
133 134
    end

135
    class Member < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
136
      expose :access_level do |user, options|
137
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
138 139
        member.access_level
      end
140
      expose :expires_at do |user, options|
141
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
142 143
        member.expires_at
      end
144 145 146 147
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
148
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
149
        access_requester.requested_at
150
      end
miks's avatar
miks committed
151 152
    end

153
    class Group < Grape::Entity
154
      expose :id, :name, :path, :description, :visibility
155
      expose :lfs_enabled?, as: :lfs_enabled
156 157 158
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
159
      expose :web_url
160
      expose :request_access_enabled
161
      expose :full_name, :full_path
162 163 164 165

      if ::Group.supports_nested_groups?
        expose :parent_id
      end
166 167 168 169 170 171

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
172
          expose :build_artifacts_size, as: :job_artifacts_size
173 174
        end
      end
175
    end
Andrew8xx8's avatar
Andrew8xx8 committed
176

177
    class GroupDetail < Group
178
      expose :projects, using: Entities::Project
179
      expose :shared_projects, using: Entities::Project
180 181
    end

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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

199
    class RepoBranch < Grape::Entity
200 201
      expose :name

202
      expose :commit, using: Entities::RepoCommit do |repo_branch, options|
203
        options[:project].repository.commit(repo_branch.dereferenced_target)
204 205
      end

206 207 208 209
      expose :merged do |repo_branch, options|
        options[:project].repository.merged_to_root_ref?(repo_branch.name)
      end

210
      expose :protected do |repo_branch, options|
211
        ProtectedBranch.protected?(options[:project], repo_branch.name)
212 213
      end

214
      expose :developers_can_push do |repo_branch, options|
215
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
216
      end
217

218
      expose :developers_can_merge do |repo_branch, options|
219
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
220
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
221
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
222

223
    class RepoTreeObject < Grape::Entity
224
      expose :id, :name, :type, :path
225 226

      expose :mode do |obj, options|
227
        filemode = obj.mode
228 229 230 231 232
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
233
    class ProjectSnippet < Grape::Entity
234
      expose :id, :title, :file_name, :description
235
      expose :author, using: Entities::UserBasic
236
      expose :updated_at, :created_at
237

238 239 240
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
241
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
242

243
    class PersonalSnippet < Grape::Entity
244
      expose :id, :title, :file_name, :description
245 246 247 248 249 250 251 252 253 254 255
      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

256 257
    class ProjectEntity < Grape::Entity
      expose :id, :iid
Felipe Artur's avatar
Felipe Artur committed
258
      expose(:project_id) { |entity| entity&.project.try(:id) }
259 260
      expose :title, :description
      expose :state, :created_at, :updated_at
261 262
    end

263 264
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
265 266 267
      expose :new_file?, as: :new_file
      expose :renamed_file?, as: :renamed_file
      expose :deleted_file?, as: :deleted_file
268 269
    end

Felipe Artur's avatar
Felipe Artur committed
270 271 272 273 274 275
    class Milestone < Grape::Entity
      expose :id, :iid
      expose(:project_id) { |entity| entity&.project_id }
      expose(:group_id) { |entity| entity&.group_id }
      expose :title, :description
      expose :state, :created_at, :updated_at
276
      expose :due_date
277
      expose :start_date
Nihad Abbasov's avatar
Nihad Abbasov committed
278 279
    end

280
    class IssueBasic < ProjectEntity
281
      expose :label_names, as: :labels
282
      expose :milestone, using: Entities::Milestone
283 284 285 286 287
      expose :assignees, :author, using: Entities::UserBasic

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

Z.J. van de Weg's avatar
Z.J. van de Weg committed
289
      expose :user_notes_count
290
      expose :upvotes, :downvotes
291
      expose :due_date
292
      expose :confidential
293 294 295 296

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

299 300 301 302 303 304
    class Issue < IssueBasic
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

305 306 307 308 309 310 311
    class IssuableTimeStats < Grape::Entity
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
      expose :human_total_time_spent
    end

312 313 314 315 316
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

317
    class MergeRequestBasic < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
318
      expose :target_branch, :source_branch
319
      expose :upvotes, :downvotes
320 321
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
322
      expose :label_names, as: :labels
323
      expose :work_in_progress?, as: :work_in_progress
324
      expose :milestone, using: Entities::Milestone
325
      expose :merge_when_pipeline_succeeds
326
      expose :merge_status
327 328
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z.J. van de Weg's avatar
Z.J. van de Weg committed
329
      expose :user_notes_count
330 331
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
332 333 334 335

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

338 339 340 341 342 343
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
    end

344 345
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
Douwe Maan's avatar
Douwe Maan committed
346
        compare.raw_diffs(limits: false).to_a
347 348 349
      end
    end

350 351 352
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
353
    end
354

355
    class MergeRequestDiffFull < MergeRequestDiff
356 357 358
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
Douwe Maan's avatar
Douwe Maan committed
359
        compare.raw_diffs(limits: false).to_a
360 361 362
      end
    end

363
    class SSHKey < Grape::Entity
364
      expose :id, :title, :key, :created_at, :can_push
365
    end
366

367
    class SSHKeyWithUser < SSHKey
368
      expose :user, using: Entities::UserPublic
369 370
    end

371
    class Note < Grape::Entity
372 373
      expose :id
      expose :note, as: :body
374
      expose :attachment_identifier, as: :attachment
375
      expose :author, using: Entities::UserBasic
376
      expose :created_at, :updated_at
377
      expose :system?, as: :system
378
      expose :noteable_id, :noteable_type
379
    end
380

381 382 383 384 385 386 387 388
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

389 390 391 392
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
393

394 395
    class CommitNote < Grape::Entity
      expose :note
396 397 398
      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? }
399
      expose :author, using: Entities::UserBasic
400
      expose :created_at
401 402
    end

403 404
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
405
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
Kamil Trzcinski's avatar
Kamil Trzcinski committed
406
      expose :author, using: Entities::UserBasic
407 408
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
409 410 411 412
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
413
      expose :created_at
414 415
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
416 417

      expose :author_username do |event, options|
418
        event.author&.username
419
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
420
    end
421

422
    class ProjectGroupLink < Grape::Entity
423
      expose :id, :project_id, :group_id, :group_access, :expires_at
424 425
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
426 427 428 429
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling's avatar
Robert Schilling committed
430
      expose :action_name
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
431
      expose :target_type
432 433

      expose :target do |todo, options|
434 435
        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
436 437 438 439 440
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
441
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
442

443
        Gitlab::Routing.url_helpers.public_send(target_url,
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
444 445 446 447 448 449 450 451
          todo.project.namespace, todo.project, todo.target, anchor: target_anchor)
      end

      expose :body
      expose :state
      expose :created_at
    end

452
    class Namespace < Grape::Entity
453 454
      expose :id, :name, :path, :kind, :full_path, :parent_id

455 456 457 458 459 460
      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)
461
      end
462
    end
463

464
    class MemberAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
465
      expose :access_level
466 467
      expose :notification_level do |member, options|
        if member.notification_setting
468
          ::NotificationSetting.levels[member.notification_setting.level]
469 470
        end
      end
471 472
    end

473
    class ProjectAccess < MemberAccess
474 475
    end

476
    class GroupAccess < MemberAccess
477 478
    end

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    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

494 495
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
496
      expose :push_events, :issues_events, :merge_requests_events
497
      expose :tag_push_events, :note_events, :pipeline_events
498
      expose :job_events
499 500
      # Expose serialized properties
      expose :properties do |service, options|
501 502 503
        field_names = service.fields
          .select { |field| options[:include_passwords] || field[:type] != 'password' }
          .map { |field| field[:name] }
504 505 506 507
        service.properties.slice(*field_names)
      end
    end

508 509 510
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
511 512 513 514 515
          if options.key?(:project_members)
            (options[:project_members] || []).find { |member| member.source_id == project.id }
          else
            project.project_members.find_by(user_id: options[:current_user].id)
          end
516 517 518
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
519
          if project.group
520 521 522 523 524
            if options.key?(:group_members)
              (options[:group_members] || []).find { |member| member.source_id == project.namespace_id }
            else
              project.group.group_members.find_by(user_id: options[:current_user].id)
            end
525
          end
526 527 528
        end
      end
    end
529

530
    class LabelBasic < Grape::Entity
Rares Sfirlogea's avatar
Rares Sfirlogea committed
531
      expose :id, :name, :color, :description
532 533 534
    end

    class Label < LabelBasic
535
      expose :open_issues_count do |label, options|
Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
536 537
        label.open_issues_count(options[:current_user])
      end
538

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
539 540 541
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
542

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
543 544
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
545 546
      end

547 548 549
      expose :priority do |label, options|
        label.priority(options[:project])
      end
550 551

      expose :subscribed do |label, options|
552
        label.subscribed?(options[:current_user], options[:project])
553
      end
554
    end
555

556 557 558 559 560 561 562 563 564 565 566 567 568
    class List < Grape::Entity
      expose :id
      expose :label, using: Entities::LabelBasic
      expose :position
    end

    class Board < Grape::Entity
      expose :id
      expose :lists, using: Entities::List do |board|
        board.lists.destroyable
      end
    end

569 570
    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
571
        Commit.decorate(compare.commits, nil).last
572
      end
573

574
      expose :commits, using: Entities::RepoCommit do |compare, options|
575
        Commit.decorate(compare.commits, nil)
576
      end
577

578
      expose :diffs, using: Entities::RepoDiff do |compare, options|
Douwe Maan's avatar
Douwe Maan committed
579
        compare.diffs(limits: false).to_a
580
      end
581 582

      expose :compare_timeout do |compare, options|
583
        compare.diffs.overflow?
584 585 586
      end

      expose :same, as: :compare_same_ref
587
    end
588 589 590 591

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
592 593 594 595

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
596 597 598 599 600 601 602 603

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
604
      expose :after_sign_up_text
605 606 607 608
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
609 610 611
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
612 613
      expose :max_attachment_size
      expose :session_expire_delay
614 615 616
      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) }
617
      expose :default_artifacts_expire_in
618
      expose :domain_whitelist
619 620
      expose :domain_blacklist_enabled
      expose :domain_blacklist
621 622
      expose :user_oauth_applications
      expose :after_sign_out_path
623
      expose :container_registry_token_expire_delay
624
      expose :repository_storage
625
      expose :repository_storages
626 627
      expose :koding_enabled
      expose :koding_url
628 629
      expose :plantuml_enabled
      expose :plantuml_url
630
      expose :terminal_max_session_time
631
      expose :polling_interval_multiplier
632 633 634
      expose :help_page_hide_commercial_content
      expose :help_page_text
      expose :help_page_support_url
635
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
636 637

    class Release < Grape::Entity
638 639
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
640
    end
641 642

    class RepoTag < Grape::Entity
643
      expose :name, :message
644

645
      expose :commit do |repo_tag, options|
646
        options[:project].repository.commit(repo_tag.dereferenced_target)
647 648
      end

649 650
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
651 652
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
653

654
    class Runner < Grape::Entity
655 656 657 658 659 660 661
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

662 663
    class RunnerDetails < Runner
      expose :tag_list
664
      expose :run_untagged
665
      expose :locked
666
      expose :version, :revision, :platform, :architecture
667
      expose :contacted_at
668
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
669
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
670
        if options[:current_user].admin?
671 672
          runner.projects
        else
673
          options[:current_user].authorized_projects.where(id: runner.projects)
674 675
        end
      end
676 677
    end

678 679 680 681
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

682
    class JobArtifactFile < Grape::Entity
683 684 685
      expose :filename, :size
    end

686 687 688 689
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

690
    class Job < Grape::Entity
691
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
692
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
693
      expose :user, with: User
Z.J. van de Weg's avatar
Z.J. van de Weg committed
694
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
695
      expose :commit, with: RepoCommit
696
      expose :runner, with: Runner
697
      expose :pipeline, with: PipelineBasic
698
    end
699

700
    class Trigger < Grape::Entity
701
      expose :id
702 703 704
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
705
    end
706

707
    class Variable < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
708
      expose :key, :value
709
      expose :protected?, as: :protected
710
    end
711

712 713
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
714 715 716 717

      expose :user, with: Entities::UserBasic
      expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
      expose :duration
718
      expose :coverage
719 720
    end

721 722 723
    class PipelineSchedule < Grape::Entity
      expose :id
      expose :description, :ref, :cron, :cron_timezone, :next_run_at, :active
724
      expose :created_at, :updated_at
725 726 727
      expose :owner, using: Entities::UserBasic
    end

Shinya Maeda's avatar
Shinya Maeda committed
728 729 730 731
    class PipelineScheduleDetails < PipelineSchedule
      expose :last_pipeline, using: Entities::PipelineBasic
    end

732
    class EnvironmentBasic < Grape::Entity
Nick Thomas's avatar
Nick Thomas committed
733
      expose :id, :name, :slug, :external_url
734 735
    end

736
    class Environment < EnvironmentBasic
737
      expose :project, using: Entities::BasicProjectDetails
Z.J. van de Weg's avatar
Z.J. van de Weg committed
738 739 740 741
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
742 743
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
744
      expose :deployable,  using: Entities::Job
745 746
    end

747
    class RepoLicense < Grape::Entity
748 749
      expose :key, :name, :nickname
      expose :featured, as: :popular
750 751 752
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
753 754 755
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
756 757
      expose :content
    end
758

759
    class TemplatesList < Grape::Entity
760 761 762
      expose :name
    end

763
    class Template < Grape::Entity
764 765
      expose :name, :content
    end
766 767 768 769 770

    class BroadcastMessage < Grape::Entity
      expose :id, :message, :starts_at, :ends_at, :color, :font
      expose :active?, as: :active
    end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
771

772
    class PersonalAccessToken < Grape::Entity
773 774 775 776 777 778 779
      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

780
    class PersonalAccessTokenWithToken < PersonalAccessToken
781 782
      expose :token
    end
783 784 785 786

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
787

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
    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

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    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
826

827 828 829
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
830

831
      class Step < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
832
        expose :name, :script, :timeout, :when, :allow_failure
833
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
834

835
      class Image < Grape::Entity
836 837 838
        expose :name, :entrypoint
      end

839
      class Service < Image
840
        expose :alias, :command
841
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
842

843 844
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
Tomasz Maczukin's avatar
Tomasz Maczukin committed
845 846
      end

847
      class Cache < Grape::Entity
848
        expose :key, :untracked, :paths, :policy
Tomasz Maczukin's avatar
Tomasz Maczukin committed
849 850
      end

851 852 853
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
854

855 856 857 858 859
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
860
        expose :id, :name, :token
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
        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
884
        expose :services, using: Service
885 886 887
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
888
        expose :dependencies, using: Dependency
889
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
890
    end
891 892 893 894

    class UserAgentDetail < Grape::Entity
      expose :user_agent
      expose :ip_address
895
      expose :submitted, as: :akismet_submitted
896
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
897 898
  end
end