entities.rb 24.6 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 8
    class UserBasic < UserSafe
      expose :id, :state, :avatar_url
D
Douwe Maan 已提交
9 10

      expose :web_url do |user, options|
11
        Gitlab::Routing.url_helpers.user_url(user)
D
Douwe Maan 已提交
12
      end
N
Nihad Abbasov 已提交
13
    end
N
Nihad Abbasov 已提交
14

15 16
    class User < UserBasic
      expose :created_at
17
      expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
18 19
    end

20 21
    class UserActivity < Grape::Entity
      expose :username
22 23
      expose :last_activity_on
      expose :last_activity_on, as: :last_activity_at # Back-compat
24 25
    end

26 27 28 29
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

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

43
    class UserWithPrivateDetails < UserPublic
44
      expose :private_token
45
      expose :admin?, as: :is_admin
46 47
    end

48 49 50 51
    class Email < Grape::Entity
      expose :id, :email
    end

M
miks 已提交
52
    class Hook < Grape::Entity
53
      expose :id, :url, :created_at, :push_events, :tag_push_events
54
      expose :enable_ssl_verification
M
miks 已提交
55 56
    end

57
    class ProjectHook < Hook
58
      expose :project_id, :issues_events, :merge_requests_events
59
      expose :note_events, :pipeline_events, :wiki_page_events
60
      expose :build_events, as: :job_events
61 62
    end

63
    class BasicProjectDetails < Grape::Entity
64
      expose :id
65
      expose :http_url_to_repo, :web_url
66 67 68 69
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

70 71 72 73 74 75 76 77
    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 已提交
78
    class Project < Grape::Entity
79
      expose :id, :description, :default_branch, :tag_list
80
      expose :archived?, as: :archived
81
      expose :visibility, :ssh_url_to_repo, :http_url_to_repo, :web_url
82
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
83
      expose :name, :name_with_namespace
84
      expose :path, :path_with_namespace
F
Felipe Artur 已提交
85 86 87
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
88 89 90
      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]) }
91
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
92
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
F
Felipe Artur 已提交
93

94
      expose :created_at, :last_activity_at
95 96
      expose :shared_runners_enabled
      expose :lfs_enabled?, as: :lfs_enabled
97
      expose :creator_id
98
      expose :namespace, using: 'API::Entities::Namespace'
99
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
S
sue445 已提交
100
      expose :avatar_url
101
      expose :star_count, :forks_count
102
      expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) && project.default_issues_tracker? }
103
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
104
      expose :public_builds, as: :public_jobs
105 106 107
      expose :shared_with_groups do |project, options|
        SharedGroup.represent(project.project_group_links.all, options)
      end
J
James Lopez 已提交
108
      expose :only_allow_merge_if_pipeline_succeeds
109
      expose :request_access_enabled
110
      expose :only_allow_merge_if_all_discussions_are_resolved
M
Markus Koller 已提交
111 112 113 114 115 116 117 118 119

      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
120
      expose :build_artifacts_size, as: :job_artifacts_size
N
Nihad Abbasov 已提交
121 122
    end

123
    class Member < UserBasic
D
Dmitriy Zaporozhets 已提交
124
      expose :access_level do |user, options|
125
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
126 127
        member.access_level
      end
128
      expose :expires_at do |user, options|
129
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
130 131
        member.expires_at
      end
132 133 134 135
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
136
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
137
        access_requester.requested_at
N
Nihad Abbasov 已提交
138
      end
M
miks 已提交
139 140
    end

141
    class Group < Grape::Entity
142
      expose :id, :name, :path, :description, :visibility
143
      expose :lfs_enabled?, as: :lfs_enabled
D
Douwe Maan 已提交
144
      expose :avatar_url
R
Rémy Coutable 已提交
145
      expose :web_url
146
      expose :request_access_enabled
147
      expose :full_name, :full_path
148
      expose :parent_id
M
Markus Koller 已提交
149 150 151 152 153 154

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
155
          expose :build_artifacts_size, as: :job_artifacts_size
M
Markus Koller 已提交
156 157
        end
      end
158
    end
A
Andrew8xx8 已提交
159

160
    class GroupDetail < Group
161
      expose :projects, using: Entities::Project
162
      expose :shared_projects, using: Entities::Project
163 164
    end

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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

182
    class RepoBranch < Grape::Entity
183 184
      expose :name

185
      expose :commit, using: Entities::RepoCommit do |repo_branch, options|
186
        options[:project].repository.commit(repo_branch.dereferenced_target)
187 188
      end

R
Robert Schilling 已提交
189 190 191 192
      expose :merged do |repo_branch, options|
        options[:project].repository.merged_to_root_ref?(repo_branch.name)
      end

193
      expose :protected do |repo_branch, options|
194
        ProtectedBranch.protected?(options[:project], repo_branch.name)
195 196
      end

197
      expose :developers_can_push do |repo_branch, options|
198
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
199
      end
200

201
      expose :developers_can_merge do |repo_branch, options|
202
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
203
      end
N
Nihad Abbasov 已提交
204
    end
N
Nihad Abbasov 已提交
205

206
    class RepoTreeObject < Grape::Entity
207
      expose :id, :name, :type, :path
208 209

      expose :mode do |obj, options|
M
mhasbini 已提交
210
        filemode = obj.mode
211 212 213 214 215
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

N
Nihad Abbasov 已提交
216 217
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
218
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
219
      expose :updated_at, :created_at
220

221 222 223
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
N
Nihad Abbasov 已提交
224
    end
N
Nihad Abbasov 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238
    class PersonalSnippet < Grape::Entity
      expose :id, :title, :file_name
      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

239 240
    class ProjectEntity < Grape::Entity
      expose :id, :iid
241
      expose(:project_id) { |entity| entity.project.id }
242 243
      expose :title, :description
      expose :state, :created_at, :updated_at
244 245
    end

246 247 248 249 250
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

251
    class Milestone < ProjectEntity
252
      expose :due_date
V
Valery Sizov 已提交
253
      expose :start_date
N
Nihad Abbasov 已提交
254 255
    end

256
    class IssueBasic < ProjectEntity
257
      expose :label_names, as: :labels
258 259
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
260

Z
Z.J. van de Weg 已提交
261
      expose :user_notes_count
Z
Zeger-Jan van de Weg 已提交
262
      expose :upvotes, :downvotes
263
      expose :due_date
264
      expose :confidential
265 266 267 268

      expose :web_url do |issue, options|
        Gitlab::UrlBuilder.build(issue)
      end
N
Nihad Abbasov 已提交
269
    end
A
Alex Denisov 已提交
270

271 272 273 274 275 276
    class Issue < IssueBasic
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

277 278 279 280 281 282 283
    class IssuableTimeStats < Grape::Entity
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
      expose :human_total_time_spent
    end

284 285 286 287 288
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

289
    class MergeRequestBasic < ProjectEntity
V
Valery Sizov 已提交
290
      expose :target_branch, :source_branch
291
      expose :upvotes, :downvotes
292 293
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
294
      expose :label_names, as: :labels
B
Ben Boeckel 已提交
295
      expose :work_in_progress?, as: :work_in_progress
296
      expose :milestone, using: Entities::Milestone
J
James Lopez 已提交
297
      expose :merge_when_pipeline_succeeds
298
      expose :merge_status
299 300
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z
Z.J. van de Weg 已提交
301
      expose :user_notes_count
302 303
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
304 305 306 307

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

310 311 312 313 314 315
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
    end

316 317
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
318
        compare.raw_diffs(all_diffs: true).to_a
319 320 321
      end
    end

322 323 324
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
325
    end
326

327
    class MergeRequestDiffFull < MergeRequestDiff
328 329 330
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
D
Dmitriy Zaporozhets 已提交
331
        compare.raw_diffs(all_diffs: true).to_a
332 333 334
      end
    end

335
    class SSHKey < Grape::Entity
336
      expose :id, :title, :key, :created_at, :can_push
V
Valeriy Sizov 已提交
337
    end
338

339
    class SSHKeyWithUser < SSHKey
340
      expose :user, using: Entities::UserPublic
341 342
    end

343
    class Note < Grape::Entity
344 345
      expose :id
      expose :note, as: :body
346
      expose :attachment_identifier, as: :attachment
347
      expose :author, using: Entities::UserBasic
348
      expose :created_at, :updated_at
349
      expose :system?, as: :system
D
Dmitriy Zaporozhets 已提交
350
      expose :noteable_id, :noteable_type
351
    end
352

Z
Z.J. van de Weg 已提交
353 354 355 356 357 358 359 360
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

361 362 363 364
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
365

366 367
    class CommitNote < Grape::Entity
      expose :note
368 369 370
      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? }
371
      expose :author, using: Entities::UserBasic
372
      expose :created_at
373 374
    end

K
Kamil Trzcinski 已提交
375 376
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
377
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
K
Kamil Trzcinski 已提交
378
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
379 380
    end

D
Dmitriy Zaporozhets 已提交
381 382 383 384
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
385
      expose :created_at
D
Dmitriy Zaporozhets 已提交
386 387
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
388 389

      expose :author_username do |event, options|
Z
Z.J. van de Weg 已提交
390
        event.author&.username
391
      end
D
Dmitriy Zaporozhets 已提交
392
    end
393

394
    class ProjectGroupLink < Grape::Entity
395
      expose :id, :project_id, :group_id, :group_access, :expires_at
396 397
    end

D
Douglas Barbosa Alexandre 已提交
398 399 400 401
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
R
Robert Schilling 已提交
402
      expose :action_name
D
Douglas Barbosa Alexandre 已提交
403
      expose :target_type
404 405

      expose :target do |todo, options|
406 407
        target = todo.target_type == 'Commit' ? 'RepoCommit' : todo.target_type
        Entities.const_get(target).represent(todo.target, options)
D
Douglas Barbosa Alexandre 已提交
408 409 410 411 412
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
413
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
D
Douglas Barbosa Alexandre 已提交
414 415 416 417 418 419 420 421 422 423

        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

424
    class Namespace < Grape::Entity
425
      expose :id, :name, :path, :kind, :full_path
426
    end
427

428
    class MemberAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
429
      expose :access_level
430 431
      expose :notification_level do |member, options|
        if member.notification_setting
432
          ::NotificationSetting.levels[member.notification_setting.level]
433 434
        end
      end
435 436
    end

437
    class ProjectAccess < MemberAccess
438 439
    end

440
    class GroupAccess < MemberAccess
441 442
    end

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    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

458 459
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
460
      expose :push_events, :issues_events, :merge_requests_events
461
      expose :tag_push_events, :note_events, :pipeline_events
462
      expose :build_events, as: :job_events
463 464 465 466 467 468 469 470 471
      # Expose serialized properties
      expose :properties do |service, options|
        field_names = service.fields.
          select { |field| options[:include_passwords] || field[:type] != 'password' }.
          map { |field| field[:name] }
        service.properties.slice(*field_names)
      end
    end

472 473 474
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
475
          project.project_members.find_by(user_id: options[:current_user].id)
476 477 478
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
479
          if project.group
480
            project.group.group_members.find_by(user_id: options[:current_user].id)
481
          end
482 483 484
        end
      end
    end
485

A
Andre Guedes 已提交
486
    class LabelBasic < Grape::Entity
R
Rares Sfirlogea 已提交
487
      expose :id, :name, :color, :description
A
Andre Guedes 已提交
488 489 490
    end

    class Label < LabelBasic
491
      expose :open_issues_count do |label, options|
F
Francesco Coda Zabetta 已提交
492 493
        label.open_issues_count(options[:current_user])
      end
494

F
Francesco Coda Zabetta 已提交
495 496 497
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
498

F
Francesco Coda Zabetta 已提交
499 500
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
501 502
      end

503 504 505
      expose :priority do |label, options|
        label.priority(options[:project])
      end
506 507

      expose :subscribed do |label, options|
508
        label.subscribed?(options[:current_user], options[:project])
509
      end
510
    end
511

A
Andre Guedes 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524
    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

525 526
    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
527
        Commit.decorate(compare.commits, nil).last
528
      end
529

530
      expose :commits, using: Entities::RepoCommit do |compare, options|
531
        Commit.decorate(compare.commits, nil)
532
      end
533

534
      expose :diffs, using: Entities::RepoDiff do |compare, options|
J
Jacob Vosmaer 已提交
535
        compare.diffs(all_diffs: true).to_a
536
      end
537 538

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
539
        compare.diffs.overflow?
540 541 542
      end

      expose :same, as: :compare_same_ref
543
    end
544 545 546 547

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
548 549 550 551

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
552 553 554 555 556 557 558 559

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
560
      expose :after_sign_up_text
561 562 563 564
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
565 566 567
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
568 569
      expose :max_attachment_size
      expose :session_expire_delay
570 571 572
      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) }
573
      expose :default_artifacts_expire_in
574
      expose :domain_whitelist
575 576
      expose :domain_blacklist_enabled
      expose :domain_blacklist
577 578
      expose :user_oauth_applications
      expose :after_sign_out_path
579
      expose :container_registry_token_expire_delay
580
      expose :repository_storage
581
      expose :repository_storages
582 583
      expose :koding_enabled
      expose :koding_url
584 585
      expose :plantuml_enabled
      expose :plantuml_url
586
      expose :terminal_max_session_time
587
      expose :polling_interval_multiplier
588
    end
D
Dmitriy Zaporozhets 已提交
589 590

    class Release < Grape::Entity
591 592
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
593
    end
594 595

    class RepoTag < Grape::Entity
596
      expose :name, :message
597

598
      expose :commit do |repo_tag, options|
599
        options[:project].repository.commit(repo_tag.dereferenced_target)
600 601
      end

602 603
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
604 605
      end
    end
K
Kamil Trzcinski 已提交
606

T
Tomasz Maczukin 已提交
607
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
608 609 610 611 612 613 614
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

615 616
    class RunnerDetails < Runner
      expose :tag_list
617
      expose :run_untagged
618
      expose :locked
619
      expose :version, :revision, :platform, :architecture
620
      expose :contacted_at
B
blackst0ne 已提交
621
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
622
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
B
blackst0ne 已提交
623
        if options[:current_user].admin?
624 625
          runner.projects
        else
626
          options[:current_user].authorized_projects.where(id: runner.projects)
627 628
        end
      end
629 630
    end

631 632 633 634
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

635
    class JobArtifactFile < Grape::Entity
636 637 638
      expose :filename, :size
    end

639 640 641 642
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

643
    class Job < Grape::Entity
T
Tomasz Maczukin 已提交
644
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
645
      expose :created_at, :started_at, :finished_at
T
Tomasz Maczukin 已提交
646
      expose :user, with: User
Z
Z.J. van de Weg 已提交
647
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
648
      expose :commit, with: RepoCommit
T
Tomasz Maczukin 已提交
649
      expose :runner, with: Runner
650
      expose :pipeline, with: PipelineBasic
651
    end
652

T
Tomasz Maczukin 已提交
653
    class Trigger < Grape::Entity
654
      expose :id
655 656 657
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
T
Tomasz Maczukin 已提交
658
    end
659

660
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
661
      expose :key, :value
662
    end
663

664 665
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z
Z.J. van de Weg 已提交
666 667 668 669

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

673
    class EnvironmentBasic < Grape::Entity
N
Nick Thomas 已提交
674
      expose :id, :name, :slug, :external_url
675 676
    end

677
    class Environment < EnvironmentBasic
678
      expose :project, using: Entities::BasicProjectDetails
Z
Z.J. van de Weg 已提交
679 680 681 682
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
683 684
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
685
      expose :deployable,  using: Entities::Job
686 687
    end

688
    class RepoLicense < Grape::Entity
689 690
      expose :key, :name, :nickname
      expose :featured, as: :popular
691 692 693
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
694 695 696
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
697 698
      expose :content
    end
699

Z
ZJ van de Weg 已提交
700
    class TemplatesList < Grape::Entity
701 702 703
      expose :name
    end

Z
ZJ van de Weg 已提交
704
    class Template < Grape::Entity
705 706
      expose :name, :content
    end
707 708 709 710 711

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

713
    class PersonalAccessToken < Grape::Entity
714 715 716 717 718 719 720
      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

721
    class PersonalAccessTokenWithToken < PersonalAccessToken
722 723
      expose :token
    end
724 725 726 727

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
    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 已提交
745

746 747 748
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
T
Tomasz Maczukin 已提交
749

750
      class Step < Grape::Entity
T
Tomasz Maczukin 已提交
751
        expose :name, :script, :timeout, :when, :allow_failure
752
      end
T
Tomasz Maczukin 已提交
753

754 755 756
      class Image < Grape::Entity
        expose :name
      end
T
Tomasz Maczukin 已提交
757

758 759
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
T
Tomasz Maczukin 已提交
760 761
      end

762 763
      class Cache < Grape::Entity
        expose :key, :untracked, :paths
T
Tomasz Maczukin 已提交
764 765
      end

766 767 768
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
T
Tomasz Maczukin 已提交
769

770 771 772 773 774
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
T
Tomasz Maczukin 已提交
775
        expose :id, :name, :token
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
        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
        expose :services, using: Image
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
T
Tomasz Maczukin 已提交
803
        expose :dependencies, using: Dependency
804
      end
T
Tomasz Maczukin 已提交
805
    end
N
Nihad Abbasov 已提交
806 807
  end
end