entities.rb 28.1 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
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
D
Douwe Maan 已提交
12 13

      expose :web_url do |user, options|
14
        Gitlab::Routing.url_helpers.user_url(user)
D
Douwe Maan 已提交
15
      end
N
Nihad Abbasov 已提交
16
    end
N
Nihad Abbasov 已提交
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

M
miks 已提交
58
    class Hook < Grape::Entity
59
      expose :id, :url, :created_at, :push_events, :tag_push_events, :repository_update_events
60
      expose :enable_ssl_verification
M
miks 已提交
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

N
Nihad Abbasov 已提交
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
F
Felipe Artur 已提交
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]) }
F
Felipe Artur 已提交
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
J
James Lopez 已提交
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
M
Markus Koller 已提交
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
N
Nihad Abbasov 已提交
133 134
    end

135
    class Member < UserBasic
D
Dmitriy Zaporozhets 已提交
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
N
Nihad Abbasov 已提交
150
      end
M
miks 已提交
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
R
Rémy Coutable 已提交
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
M
Markus Koller 已提交
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
M
Markus Koller 已提交
173 174
        end
      end
175
    end
A
Andrew8xx8 已提交
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

R
Robert Schilling 已提交
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
N
Nihad Abbasov 已提交
221
    end
N
Nihad Abbasov 已提交
222

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

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

N
Nihad Abbasov 已提交
233
    class ProjectSnippet < Grape::Entity
234
      expose :id, :title, :file_name, :description
235
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
236
      expose :updated_at, :created_at
237

238 239 240
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
N
Nihad Abbasov 已提交
241
    end
N
Nihad Abbasov 已提交
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
F
Felipe Artur 已提交
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

F
Felipe Artur 已提交
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
V
Valery Sizov 已提交
277
      expose :start_date
N
Nihad Abbasov 已提交
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
Z.J. van de Weg 已提交
289
      expose :user_notes_count
Z
Zeger-Jan van de Weg 已提交
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
N
Nihad Abbasov 已提交
297
    end
A
Alex Denisov 已提交
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
V
Valery Sizov 已提交
318
      expose :target_branch, :source_branch
319 320 321 322 323 324 325 326 327 328 329 330 331 332
      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
333 334
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
335 336 337 338
      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 已提交
339
      expose :work_in_progress?, as: :work_in_progress
340
      expose :milestone, using: Entities::Milestone
J
James Lopez 已提交
341
      expose :merge_when_pipeline_succeeds
342
      expose :merge_status
343 344
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z
Z.J. van de Weg 已提交
345
      expose :user_notes_count
346 347
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
348 349 350 351

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
A
Alex Denisov 已提交
352
    end
V
Valeriy Sizov 已提交
353

354 355 356 357 358 359
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
    end

360 361
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
D
Douwe Maan 已提交
362
        compare.raw_diffs(limits: false).to_a
363 364 365
      end
    end

366 367 368
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
369
    end
370

371
    class MergeRequestDiffFull < MergeRequestDiff
372 373 374
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
D
Douwe Maan 已提交
375
        compare.raw_diffs(limits: false).to_a
376 377 378
      end
    end

379
    class SSHKey < Grape::Entity
380
      expose :id, :title, :key, :created_at, :can_push
V
Valeriy Sizov 已提交
381
    end
382

383
    class SSHKeyWithUser < SSHKey
384
      expose :user, using: Entities::UserPublic
385 386
    end

387
    class Note < Grape::Entity
388 389
      expose :id
      expose :note, as: :body
390
      expose :attachment_identifier, as: :attachment
391
      expose :author, using: Entities::UserBasic
392
      expose :created_at, :updated_at
393
      expose :system?, as: :system
D
Dmitriy Zaporozhets 已提交
394
      expose :noteable_id, :noteable_type
395
    end
396

Z
Z.J. van de Weg 已提交
397 398 399 400 401 402 403 404
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

405 406 407 408
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
409

410 411
    class CommitNote < Grape::Entity
      expose :note
412 413 414
      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? }
415
      expose :author, using: Entities::UserBasic
416
      expose :created_at
417 418
    end

K
Kamil Trzcinski 已提交
419 420
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
421
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
K
Kamil Trzcinski 已提交
422
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
423 424
    end

D
Dmitriy Zaporozhets 已提交
425 426 427 428
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
429
      expose :created_at
D
Dmitriy Zaporozhets 已提交
430 431
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
432 433

      expose :author_username do |event, options|
Z
Z.J. van de Weg 已提交
434
        event.author&.username
435
      end
D
Dmitriy Zaporozhets 已提交
436
    end
437

438
    class ProjectGroupLink < Grape::Entity
439
      expose :id, :project_id, :group_id, :group_access, :expires_at
440 441
    end

D
Douglas Barbosa Alexandre 已提交
442 443 444 445
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
R
Robert Schilling 已提交
446
      expose :action_name
D
Douglas Barbosa Alexandre 已提交
447
      expose :target_type
448 449

      expose :target do |todo, options|
450 451
        target = todo.target_type == 'Commit' ? 'RepoCommit' : todo.target_type
        Entities.const_get(target).represent(todo.target, options)
D
Douglas Barbosa Alexandre 已提交
452 453 454 455 456
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
457
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
D
Douglas Barbosa Alexandre 已提交
458

459
        Gitlab::Routing.url_helpers.public_send(target_url,
D
Douglas Barbosa Alexandre 已提交
460 461 462 463 464 465 466 467
          todo.project.namespace, todo.project, todo.target, anchor: target_anchor)
      end

      expose :body
      expose :state
      expose :created_at
    end

468
    class Namespace < Grape::Entity
469 470
      expose :id, :name, :path, :kind, :full_path, :parent_id

471 472 473 474 475 476
      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)
477
      end
478
    end
479

480
    class MemberAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
481
      expose :access_level
482 483
      expose :notification_level do |member, options|
        if member.notification_setting
484
          ::NotificationSetting.levels[member.notification_setting.level]
485 486
        end
      end
487 488
    end

489
    class ProjectAccess < MemberAccess
490 491
    end

492
    class GroupAccess < MemberAccess
493 494
    end

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    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

510 511
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
512
      expose :push_events, :issues_events, :merge_requests_events
513
      expose :tag_push_events, :note_events, :pipeline_events
514
      expose :job_events
515 516
      # Expose serialized properties
      expose :properties do |service, options|
517 518 519
        field_names = service.fields
          .select { |field| options[:include_passwords] || field[:type] != 'password' }
          .map { |field| field[:name] }
520 521 522 523
        service.properties.slice(*field_names)
      end
    end

524 525 526
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
527 528 529 530 531
          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
532 533 534
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
535
          if project.group
536 537 538 539 540
            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
541
          end
542 543 544
        end
      end
    end
545

A
Andre Guedes 已提交
546
    class LabelBasic < Grape::Entity
R
Rares Sfirlogea 已提交
547
      expose :id, :name, :color, :description
A
Andre Guedes 已提交
548 549 550
    end

    class Label < LabelBasic
551
      expose :open_issues_count do |label, options|
F
Francesco Coda Zabetta 已提交
552 553
        label.open_issues_count(options[:current_user])
      end
554

F
Francesco Coda Zabetta 已提交
555 556 557
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
558

F
Francesco Coda Zabetta 已提交
559 560
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
561 562
      end

563 564 565
      expose :priority do |label, options|
        label.priority(options[:project])
      end
566 567

      expose :subscribed do |label, options|
568
        label.subscribed?(options[:current_user], options[:project])
569
      end
570
    end
571

A
Andre Guedes 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584
    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

585 586
    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
587
        Commit.decorate(compare.commits, nil).last
588
      end
589

590
      expose :commits, using: Entities::RepoCommit do |compare, options|
591
        Commit.decorate(compare.commits, nil)
592
      end
593

594
      expose :diffs, using: Entities::RepoDiff do |compare, options|
D
Douwe Maan 已提交
595
        compare.diffs(limits: false).to_a
596
      end
597 598

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
599
        compare.diffs.overflow?
600 601 602
      end

      expose :same, as: :compare_same_ref
603
    end
604 605 606 607

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
608 609 610 611

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
612 613 614 615 616 617 618 619

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
620
      expose :after_sign_up_text
621 622 623 624
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
625 626 627
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
628 629
      expose :max_attachment_size
      expose :session_expire_delay
630 631 632
      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) }
633
      expose :default_artifacts_expire_in
634
      expose :domain_whitelist
635 636
      expose :domain_blacklist_enabled
      expose :domain_blacklist
637 638
      expose :user_oauth_applications
      expose :after_sign_out_path
639
      expose :container_registry_token_expire_delay
640
      expose :repository_storage
641
      expose :repository_storages
642 643
      expose :koding_enabled
      expose :koding_url
644 645
      expose :plantuml_enabled
      expose :plantuml_url
646
      expose :terminal_max_session_time
647
      expose :polling_interval_multiplier
R
Robin Bobbitt 已提交
648 649 650
      expose :help_page_hide_commercial_content
      expose :help_page_text
      expose :help_page_support_url
651
    end
D
Dmitriy Zaporozhets 已提交
652 653

    class Release < Grape::Entity
654 655
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
656
    end
657 658

    class RepoTag < Grape::Entity
659
      expose :name, :message
660

661
      expose :commit do |repo_tag, options|
662
        options[:project].repository.commit(repo_tag.dereferenced_target)
663 664
      end

665 666
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
667 668
      end
    end
K
Kamil Trzcinski 已提交
669

T
Tomasz Maczukin 已提交
670
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
671 672 673 674 675 676 677
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

678 679
    class RunnerDetails < Runner
      expose :tag_list
680
      expose :run_untagged
681
      expose :locked
682
      expose :version, :revision, :platform, :architecture
683
      expose :contacted_at
B
blackst0ne 已提交
684
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
685
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
B
blackst0ne 已提交
686
        if options[:current_user].admin?
687 688
          runner.projects
        else
689
          options[:current_user].authorized_projects.where(id: runner.projects)
690 691
        end
      end
692 693
    end

694 695 696 697
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

698
    class JobArtifactFile < Grape::Entity
699 700 701
      expose :filename, :size
    end

702 703 704 705
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

706
    class Job < Grape::Entity
T
Tomasz Maczukin 已提交
707
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
708
      expose :created_at, :started_at, :finished_at
T
Tomasz Maczukin 已提交
709
      expose :user, with: User
Z
Z.J. van de Weg 已提交
710
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
711
      expose :commit, with: RepoCommit
T
Tomasz Maczukin 已提交
712
      expose :runner, with: Runner
713
      expose :pipeline, with: PipelineBasic
714
    end
715

T
Tomasz Maczukin 已提交
716
    class Trigger < Grape::Entity
717
      expose :id
718 719 720
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
T
Tomasz Maczukin 已提交
721
    end
722

723
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
724
      expose :key, :value
725
      expose :protected?, as: :protected
726
    end
727

728 729
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z
Z.J. van de Weg 已提交
730 731 732 733

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

737 738 739
    class PipelineSchedule < Grape::Entity
      expose :id
      expose :description, :ref, :cron, :cron_timezone, :next_run_at, :active
740
      expose :created_at, :updated_at
741 742 743
      expose :owner, using: Entities::UserBasic
    end

S
Shinya Maeda 已提交
744 745 746 747
    class PipelineScheduleDetails < PipelineSchedule
      expose :last_pipeline, using: Entities::PipelineBasic
    end

748
    class EnvironmentBasic < Grape::Entity
N
Nick Thomas 已提交
749
      expose :id, :name, :slug, :external_url
750 751
    end

752
    class Environment < EnvironmentBasic
753
      expose :project, using: Entities::BasicProjectDetails
Z
Z.J. van de Weg 已提交
754 755 756 757
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
758 759
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
760
      expose :deployable,  using: Entities::Job
761 762
    end

763
    class RepoLicense < Grape::Entity
764 765
      expose :key, :name, :nickname
      expose :featured, as: :popular
766 767 768
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
769 770 771
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
772 773
      expose :content
    end
774

Z
ZJ van de Weg 已提交
775
    class TemplatesList < Grape::Entity
776 777 778
      expose :name
    end

Z
ZJ van de Weg 已提交
779
    class Template < Grape::Entity
780 781
      expose :name, :content
    end
782 783 784 785 786

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

788
    class PersonalAccessToken < Grape::Entity
789 790 791 792 793 794 795
      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

796
    class PersonalAccessTokenWithToken < PersonalAccessToken
797 798
      expose :token
    end
799 800 801 802

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
803

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    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

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    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 已提交
842

843 844 845
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
T
Tomasz Maczukin 已提交
846

847
      class Step < Grape::Entity
T
Tomasz Maczukin 已提交
848
        expose :name, :script, :timeout, :when, :allow_failure
849
      end
T
Tomasz Maczukin 已提交
850

851
      class Image < Grape::Entity
852 853 854
        expose :name, :entrypoint
      end

855
      class Service < Image
856
        expose :alias, :command
857
      end
T
Tomasz Maczukin 已提交
858

859 860
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
T
Tomasz Maczukin 已提交
861 862
      end

863
      class Cache < Grape::Entity
864
        expose :key, :untracked, :paths, :policy
T
Tomasz Maczukin 已提交
865 866
      end

867 868 869
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
T
Tomasz Maczukin 已提交
870

871 872 873 874 875
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
T
Tomasz Maczukin 已提交
876
        expose :id, :name, :token
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
        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
900
        expose :services, using: Service
901 902 903
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
T
Tomasz Maczukin 已提交
904
        expose :dependencies, using: Dependency
905
      end
T
Tomasz Maczukin 已提交
906
    end
907 908 909 910

    class UserAgentDetail < Grape::Entity
      expose :user_agent
      expose :ip_address
J
James Lopez 已提交
911
      expose :submitted, as: :akismet_submitted
912
    end
N
Nihad Abbasov 已提交
913 914
  end
end