entities.rb 24.7 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 17
    class User < UserBasic
      expose :created_at
      expose :is_admin?, as: :is_admin
18
      expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
19 20
    end

21 22 23 24
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

25
    class UserPublic < User
26 27
      expose :last_sign_in_at
      expose :confirmed_at
28
      expose :email
29
      expose :color_scheme_id, :projects_limit, :current_sign_in_at
30
      expose :identities, using: Entities::Identity
31 32
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
33
      expose :two_factor_enabled?, as: :two_factor_enabled
34
      expose :external
35 36
    end

37
    class UserWithPrivateToken < UserPublic
38
      expose :private_token
39 40
    end

41 42 43 44
    class Email < Grape::Entity
      expose :id, :email
    end

M
miks 已提交
45
    class Hook < Grape::Entity
46
      expose :id, :url, :created_at, :push_events, :tag_push_events
47
      expose :enable_ssl_verification
M
miks 已提交
48 49
    end

50
    class ProjectHook < Hook
51
      expose :project_id, :issues_events, :merge_requests_events
52
      expose :note_events, :pipeline_events, :wiki_page_events
53
      expose :build_events, as: :job_events
54 55
    end

56
    class BasicProjectDetails < Grape::Entity
57
      expose :id
58
      expose :http_url_to_repo, :web_url
59 60 61 62
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

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

      # Expose old field names with the new permissions methods to keep API compatible
81 82 83
      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]) }
84
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
85
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
F
Felipe Artur 已提交
86

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

      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
113
      expose :build_artifacts_size, as: :job_artifacts_size
N
Nihad Abbasov 已提交
114 115
    end

116
    class Member < UserBasic
D
Dmitriy Zaporozhets 已提交
117
      expose :access_level do |user, options|
118
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
119 120
        member.access_level
      end
121
      expose :expires_at do |user, options|
122
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
123 124
        member.expires_at
      end
125 126 127 128
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
129
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
130
        access_requester.requested_at
N
Nihad Abbasov 已提交
131
      end
M
miks 已提交
132 133
    end

134
    class Group < Grape::Entity
135
      expose :id, :name, :path, :description, :visibility
136
      expose :lfs_enabled?, as: :lfs_enabled
D
Douwe Maan 已提交
137
      expose :avatar_url
R
Rémy Coutable 已提交
138
      expose :web_url
139
      expose :request_access_enabled
140
      expose :full_name, :full_path
141
      expose :parent_id
M
Markus Koller 已提交
142 143 144 145 146 147

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
148
          expose :build_artifacts_size, as: :job_artifacts_size
M
Markus Koller 已提交
149 150
        end
      end
151
    end
A
Andrew8xx8 已提交
152

153
    class GroupDetail < Group
154
      expose :projects, using: Entities::Project
155
      expose :shared_projects, using: Entities::Project
156 157
    end

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    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

175
    class RepoBranch < Grape::Entity
176 177
      expose :name

178
      expose :commit, using: Entities::RepoCommit do |repo_branch, options|
179
        options[:project].repository.commit(repo_branch.dereferenced_target)
180 181
      end

R
Robert Schilling 已提交
182 183 184 185
      expose :merged do |repo_branch, options|
        options[:project].repository.merged_to_root_ref?(repo_branch.name)
      end

186
      expose :protected do |repo_branch, options|
R
Robert Schilling 已提交
187
        options[:project].protected_branch?(repo_branch.name)
188 189
      end

190
      expose :developers_can_push do |repo_branch, options|
191
        project = options[:project]
192 193
        access_levels = project.protected_branches.matching(repo_branch.name).map(&:push_access_levels).flatten
        access_levels.any? { |access_level| access_level.access_level == Gitlab::Access::DEVELOPER }
194
      end
195

196
      expose :developers_can_merge do |repo_branch, options|
197
        project = options[:project]
198 199
        access_levels = project.protected_branches.matching(repo_branch.name).map(&:merge_access_levels).flatten
        access_levels.any? { |access_level| access_level.access_level == Gitlab::Access::DEVELOPER }
200
      end
N
Nihad Abbasov 已提交
201
    end
N
Nihad Abbasov 已提交
202

203
    class RepoTreeObject < Grape::Entity
204
      expose :id, :name, :type, :path
205 206 207 208 209 210 211 212

      expose :mode do |obj, options|
        filemode = obj.mode.to_s(8)
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

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

218 219 220
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
N
Nihad Abbasov 已提交
221
    end
N
Nihad Abbasov 已提交
222

223 224 225 226 227 228 229 230 231 232 233 234 235
    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

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

243 244 245 246 247
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

248
    class Milestone < ProjectEntity
249
      expose :due_date
V
Valery Sizov 已提交
250
      expose :start_date
N
Nihad Abbasov 已提交
251 252
    end

253
    class IssueBasic < ProjectEntity
254
      expose :label_names, as: :labels
255 256
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
257

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

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

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

274 275 276 277 278 279 280
    class IssuableTimeStats < Grape::Entity
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
      expose :human_total_time_spent
    end

281 282 283 284 285
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

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

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

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

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

319 320 321
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
322
    end
323

324
    class MergeRequestDiffFull < MergeRequestDiff
325 326 327
      expose :commits, using: Entities::RepoCommit

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

332
    class SSHKey < Grape::Entity
333
      expose :id, :title, :key, :created_at, :can_push
V
Valeriy Sizov 已提交
334
    end
335

336
    class SSHKeyWithUser < SSHKey
337
      expose :user, using: Entities::UserPublic
338 339
    end

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

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

358 359 360 361
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
362

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

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

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

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

391
    class ProjectGroupLink < Grape::Entity
392
      expose :id, :project_id, :group_id, :group_access, :expires_at
393 394
    end

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

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

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

        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

421
    class Namespace < Grape::Entity
422
      expose :id, :name, :path, :kind, :full_path
423
    end
424

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

434
    class ProjectAccess < MemberAccess
435 436
    end

437
    class GroupAccess < MemberAccess
438 439
    end

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    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

455 456
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
457
      expose :push_events, :issues_events, :merge_requests_events
458
      expose :tag_push_events, :note_events, :pipeline_events
459
      expose :build_events, as: :job_events
460 461 462 463 464 465 466 467 468
      # 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

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

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

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

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

F
Francesco Coda Zabetta 已提交
492 493 494
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
495

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

500 501 502
      expose :priority do |label, options|
        label.priority(options[:project])
      end
503 504

      expose :subscribed do |label, options|
505
        label.subscribed?(options[:current_user], options[:project])
506
      end
507
    end
508

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

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

527
      expose :commits, using: Entities::RepoCommit do |compare, options|
528
        Commit.decorate(compare.commits, nil)
529
      end
530

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

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
536
        compare.diffs.overflow?
537 538 539
      end

      expose :same, as: :compare_same_ref
540
    end
541 542 543 544

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
545 546 547 548

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
549 550 551 552 553 554 555 556

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

    class Release < Grape::Entity
587 588
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
589
    end
590 591

    class RepoTag < Grape::Entity
592
      expose :name, :message
593

594
      expose :commit do |repo_tag, options|
595
        options[:project].repository.commit(repo_tag.dereferenced_target)
596 597
      end

598 599
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
600 601
      end
    end
K
Kamil Trzcinski 已提交
602

T
Tomasz Maczukin 已提交
603
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
604 605 606 607 608 609 610
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

611 612
    class RunnerDetails < Runner
      expose :tag_list
613
      expose :run_untagged
614
      expose :locked
615
      expose :version, :revision, :platform, :architecture
616
      expose :contacted_at
617
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
618
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
619
        if options[:current_user].is_admin?
620 621
          runner.projects
        else
622
          options[:current_user].authorized_projects.where(id: runner.projects)
623 624
        end
      end
625 626
    end

627 628 629 630
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

631
    class JobArtifactFile < Grape::Entity
632 633 634
      expose :filename, :size
    end

635 636 637 638
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

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

T
Tomasz Maczukin 已提交
649
    class Trigger < Grape::Entity
650
      expose :id
651 652 653
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
T
Tomasz Maczukin 已提交
654
    end
655

656
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
657
      expose :key, :value
658
    end
659

660 661
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z
Z.J. van de Weg 已提交
662 663 664 665

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

669
    class EnvironmentBasic < Grape::Entity
N
Nick Thomas 已提交
670
      expose :id, :name, :slug, :external_url
671 672
    end

673
    class Environment < EnvironmentBasic
674
      expose :project, using: Entities::BasicProjectDetails
Z
Z.J. van de Weg 已提交
675 676 677 678
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
679 680
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
681
      expose :deployable,  using: Entities::Job
682 683
    end

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

Z
ZJ van de Weg 已提交
696
    class TemplatesList < Grape::Entity
697 698 699
      expose :name
    end

Z
ZJ van de Weg 已提交
700
    class Template < Grape::Entity
701 702
      expose :name, :content
    end
703 704 705 706 707

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

709
    class PersonalAccessToken < Grape::Entity
710 711 712 713 714 715 716
      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

717
    class PersonalAccessTokenWithToken < PersonalAccessToken
718 719
      expose :token
    end
720 721 722 723

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    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 已提交
741

742 743 744
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
T
Tomasz Maczukin 已提交
745

746
      class Step < Grape::Entity
T
Tomasz Maczukin 已提交
747
        expose :name, :script, :timeout, :when, :allow_failure
748
      end
T
Tomasz Maczukin 已提交
749

750 751 752
      class Image < Grape::Entity
        expose :name
      end
T
Tomasz Maczukin 已提交
753

754 755
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
T
Tomasz Maczukin 已提交
756 757
      end

758 759
      class Cache < Grape::Entity
        expose :key, :untracked, :paths
T
Tomasz Maczukin 已提交
760 761
      end

762 763 764
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
T
Tomasz Maczukin 已提交
765

766 767 768 769 770
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
T
Tomasz Maczukin 已提交
771
        expose :id, :name, :token
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
        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 已提交
799
        expose :dependencies, using: Dependency
800
      end
T
Tomasz Maczukin 已提交
801
    end
N
Nihad Abbasov 已提交
802 803
  end
end