Read about our upcoming Code of Conduct on this issue

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

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

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

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

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

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

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

46
    class UserWithPrivateDetails < UserPublic
47
      expose :private_token
48
      expose :admin?, as: :is_admin
49
50
    end

51
52
53
54
    class Email < Grape::Entity
      expose :id, :email
    end

miks's avatar
miks committed
55
    class Hook < Grape::Entity
56
      expose :id, :url, :created_at, :push_events, :tag_push_events
57
      expose :enable_ssl_verification
miks's avatar
miks committed
58
59
    end

60
    class ProjectHook < Hook
61
      expose :project_id, :issues_events, :merge_requests_events
62
      expose :note_events, :pipeline_events, :wiki_page_events
63
      expose :build_events, as: :job_events
64
65
    end

66
    class BasicProjectDetails < Grape::Entity
67
      expose :id
68
      expose :http_url_to_repo, :web_url
69
70
71
72
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

73
74
75
76
77
78
79
80
    class SharedGroup < Grape::Entity
      expose :group_id
      expose :group_name do |group_link, options|
        group_link.group.name
      end
      expose :group_access, as: :group_access_level
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
81
    class Project < Grape::Entity
82
      expose :id, :description, :default_branch, :tag_list
83
      expose :archived?, as: :archived
84
      expose :visibility, :ssh_url_to_repo, :http_url_to_repo, :web_url
85
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
86
      expose :name, :name_with_namespace
87
      expose :path, :path_with_namespace
Felipe Artur's avatar
Felipe Artur committed
88
89
90
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
91
92
93
      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]) }
94
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
95
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
Felipe Artur's avatar
Felipe Artur committed
96

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

      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
125
      expose :build_artifacts_size, as: :job_artifacts_size
Nihad Abbasov's avatar
Nihad Abbasov committed
126
127
    end

128
    class Member < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
129
      expose :access_level do |user, options|
130
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
131
132
        member.access_level
      end
133
      expose :expires_at do |user, options|
134
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
135
136
        member.expires_at
      end
137
138
139
140
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
141
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
142
        access_requester.requested_at
143
      end
miks's avatar
miks committed
144
145
    end

146
    class Group < Grape::Entity
147
      expose :id, :name, :path, :description, :visibility
148
      expose :lfs_enabled?, as: :lfs_enabled
149
150
151
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
152
      expose :web_url
153
      expose :request_access_enabled
154
      expose :full_name, :full_path
155
      expose :parent_id
Markus Koller's avatar
Markus Koller committed
156
157
158
159
160
161

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
162
          expose :build_artifacts_size, as: :job_artifacts_size
Markus Koller's avatar
Markus Koller committed
163
164
        end
      end
165
    end
Andrew8xx8's avatar
Andrew8xx8 committed
166

167
    class GroupDetail < Group
168
      expose :projects, using: Entities::Project
169
      expose :shared_projects, using: Entities::Project
170
171
    end

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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

189
    class RepoBranch < Grape::Entity
190
191
      expose :name

192
      expose :commit, using: Entities::RepoCommit do |repo_branch, options|
193
        options[:project].repository.commit(repo_branch.dereferenced_target)
194
195
      end

Robert Schilling's avatar
Robert Schilling committed
196
197
198
199
      expose :merged do |repo_branch, options|
        options[:project].repository.merged_to_root_ref?(repo_branch.name)
      end

200
      expose :protected do |repo_branch, options|
201
        ProtectedBranch.protected?(options[:project], repo_branch.name)
202
203
      end

204
      expose :developers_can_push do |repo_branch, options|
205
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
206
      end
207

208
      expose :developers_can_merge do |repo_branch, options|
209
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
210
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
211
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
212

213
    class RepoTreeObject < Grape::Entity
214
      expose :id, :name, :type, :path
215
216

      expose :mode do |obj, options|
mhasbini's avatar
mhasbini committed
217
        filemode = obj.mode
218
219
220
221
222
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
223
224
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
225
      expose :author, using: Entities::UserBasic
Robert Speicher's avatar
Robert Speicher committed
226
      expose :updated_at, :created_at
227

228
229
230
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
231
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
232

233
234
235
236
237
238
239
240
241
242
243
244
245
    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

246
247
    class ProjectEntity < Grape::Entity
      expose :id, :iid
248
      expose(:project_id) { |entity| entity.project.id }
249
250
      expose :title, :description
      expose :state, :created_at, :updated_at
251
252
    end

253
254
255
256
257
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

258
    class Milestone < ProjectEntity
259
      expose :due_date
260
      expose :start_date
Nihad Abbasov's avatar
Nihad Abbasov committed
261
262
    end

263
    class IssueBasic < ProjectEntity
264
      expose :label_names, as: :labels
265
      expose :milestone, using: Entities::Milestone
266
267
268
269
270
      expose :assignees, :author, using: Entities::UserBasic

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

Z.J. van de Weg's avatar
Z.J. van de Weg committed
272
      expose :user_notes_count
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
273
      expose :upvotes, :downvotes
274
      expose :due_date
275
      expose :confidential
276
277
278
279

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

282
283
284
285
286
287
    class Issue < IssueBasic
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

288
289
290
291
292
293
294
    class IssuableTimeStats < Grape::Entity
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
      expose :human_total_time_spent
    end

295
296
297
298
299
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

300
    class MergeRequestBasic < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
301
      expose :target_branch, :source_branch
302
      expose :upvotes, :downvotes
303
304
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
305
      expose :label_names, as: :labels
306
      expose :work_in_progress?, as: :work_in_progress
307
      expose :milestone, using: Entities::Milestone
308
      expose :merge_when_pipeline_succeeds
309
      expose :merge_status
310
311
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z.J. van de Weg's avatar
Z.J. van de Weg committed
312
      expose :user_notes_count
313
314
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
315
316
317
318

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
Alex Denisov's avatar
Alex Denisov committed
319
    end
Valeriy Sizov's avatar
Valeriy Sizov committed
320

321
322
323
324
325
326
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
    end

327
328
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
329
        compare.raw_diffs(all_diffs: true).to_a
330
331
332
      end
    end

333
334
335
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
336
    end
337

338
    class MergeRequestDiffFull < MergeRequestDiff
339
340
341
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
342
        compare.raw_diffs(all_diffs: true).to_a
343
344
345
      end
    end

346
    class SSHKey < Grape::Entity
347
      expose :id, :title, :key, :created_at, :can_push
Valeriy Sizov's avatar
Valeriy Sizov committed
348
    end
349

350
    class SSHKeyWithUser < SSHKey
351
      expose :user, using: Entities::UserPublic
352
353
    end

354
    class Note < Grape::Entity
355
356
      expose :id
      expose :note, as: :body
357
      expose :attachment_identifier, as: :attachment
358
      expose :author, using: Entities::UserBasic
359
      expose :created_at, :updated_at
360
      expose :system?, as: :system
361
      expose :noteable_id, :noteable_type
362
    end
363

364
365
366
367
368
369
370
371
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

372
373
374
375
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
376

377
378
    class CommitNote < Grape::Entity
      expose :note
379
380
381
      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? }
382
      expose :author, using: Entities::UserBasic
383
      expose :created_at
384
385
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
386
387
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
388
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
Kamil Trzcinski's avatar
Kamil Trzcinski committed
389
      expose :author, using: Entities::UserBasic
Kamil Trzcinski's avatar
Kamil Trzcinski committed
390
391
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
392
393
394
395
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
396
      expose :created_at
397
398
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
399
400

      expose :author_username do |event, options|
Z.J. van de Weg's avatar
Z.J. van de Weg committed
401
        event.author&.username
402
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
403
    end
404

405
    class ProjectGroupLink < Grape::Entity
406
      expose :id, :project_id, :group_id, :group_access, :expires_at
407
408
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
409
410
411
412
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling's avatar
Robert Schilling committed
413
      expose :action_name
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
414
      expose :target_type
415
416

      expose :target do |todo, options|
417
418
        target = todo.target_type == 'Commit' ? 'RepoCommit' : todo.target_type
        Entities.const_get(target).represent(todo.target, options)
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
419
420
421
422
423
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
424
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
425
426
427
428
429
430
431
432
433
434

        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

435
    class Namespace < Grape::Entity
436
      expose :id, :name, :path, :kind, :full_path
437
    end
438

439
    class MemberAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
440
      expose :access_level
441
442
      expose :notification_level do |member, options|
        if member.notification_setting
443
          ::NotificationSetting.levels[member.notification_setting.level]
444
445
        end
      end
446
447
    end

448
    class ProjectAccess < MemberAccess
449
450
    end

451
    class GroupAccess < MemberAccess
452
453
    end

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    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

469
470
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
471
      expose :push_events, :issues_events, :merge_requests_events
472
      expose :tag_push_events, :note_events, :pipeline_events
473
      expose :build_events, as: :job_events
474
475
476
477
478
479
480
481
482
      # 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

483
484
485
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
486
          project.project_members.find_by(user_id: options[:current_user].id)
487
488
489
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
490
          if project.group
491
            project.group.group_members.find_by(user_id: options[:current_user].id)
492
          end
493
494
495
        end
      end
    end
496

Andre Guedes's avatar
Andre Guedes committed
497
    class LabelBasic < Grape::Entity
Rares Sfirlogea's avatar
Rares Sfirlogea committed
498
      expose :id, :name, :color, :description
Andre Guedes's avatar
Andre Guedes committed
499
500
501
    end

    class Label < LabelBasic
502
      expose :open_issues_count do |label, options|
Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
503
504
        label.open_issues_count(options[:current_user])
      end
505

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
506
507
508
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
509

Francesco Coda Zabetta's avatar
Francesco Coda Zabetta committed
510
511
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
512
513
      end

514
515
516
      expose :priority do |label, options|
        label.priority(options[:project])
      end
517
518

      expose :subscribed do |label, options|
519
        label.subscribed?(options[:current_user], options[:project])
520
      end
521
    end
522

Andre Guedes's avatar
Andre Guedes committed
523
524
525
526
527
528
529
530
531
532
533
534
535
    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

536
537
    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
538
        Commit.decorate(compare.commits, nil).last
539
      end
540

541
      expose :commits, using: Entities::RepoCommit do |compare, options|
542
        Commit.decorate(compare.commits, nil)
543
      end
544

545
      expose :diffs, using: Entities::RepoDiff do |compare, options|
546
        compare.diffs(all_diffs: true).to_a
547
      end
548
549

      expose :compare_timeout do |compare, options|
550
        compare.diffs.overflow?
551
552
553
      end

      expose :same, as: :compare_same_ref
554
    end
555
556
557
558

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
559
560
561
562

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
563
564
565
566
567
568
569
570

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
571
      expose :after_sign_up_text
572
573
574
575
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
576
577
578
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
579
580
      expose :max_attachment_size
      expose :session_expire_delay
581
582
583
      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) }
584
      expose :default_artifacts_expire_in
585
      expose :domain_whitelist
586
587
      expose :domain_blacklist_enabled
      expose :domain_blacklist
588
589
      expose :user_oauth_applications
      expose :after_sign_out_path
590
      expose :container_registry_token_expire_delay
591
      expose :repository_storage
592
      expose :repository_storages
593
594
      expose :koding_enabled
      expose :koding_url
595
596
      expose :plantuml_enabled
      expose :plantuml_url
597
      expose :terminal_max_session_time
598
      expose :polling_interval_multiplier
599
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
600
601

    class Release < Grape::Entity
602
603
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
604
    end
605
606

    class RepoTag < Grape::Entity
607
      expose :name, :message
608

609
      expose :commit do |repo_tag, options|
610
        options[:project].repository.commit(repo_tag.dereferenced_target)
611
612
      end

613
614
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
615
616
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
617

618
    class Runner < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
619
620
621
622
623
624
625
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

626
627
    class RunnerDetails < Runner
      expose :tag_list
628
      expose :run_untagged
629
      expose :locked
630
      expose :version, :revision, :platform, :architecture
631
      expose :contacted_at
blackst0ne's avatar
blackst0ne committed
632
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
633
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
blackst0ne's avatar
blackst0ne committed
634
        if options[:current_user].admin?
635
636
          runner.projects
        else
637
          options[:current_user].authorized_projects.where(id: runner.projects)
638
639
        end
      end
640
641
    end

642
643
644
645
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

646
    class JobArtifactFile < Grape::Entity
647
648
649
      expose :filename, :size
    end

650
651
652
653
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

654
    class Job < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
655
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
656
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
657
      expose :user, with: User
Z.J. van de Weg's avatar
Z.J. van de Weg committed
658
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
659
      expose :commit, with: RepoCommit
660
      expose :runner, with: Runner
661
      expose :pipeline, with: PipelineBasic
662
    end
663

Tomasz Maczukin's avatar
Tomasz Maczukin committed
664
    class Trigger < Grape::Entity
665
      expose :id
666
667
668
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
Tomasz Maczukin's avatar
Tomasz Maczukin committed
669
    end
670

671
    class Variable < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
672
      expose :key, :value
673
    end
674

675
676
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z.J. van de Weg's avatar
Z.J. van de Weg committed
677
678
679
680

      expose :user, with: Entities::UserBasic
      expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
      expose :duration
681
      expose :coverage
Z.J. van de Weg's avatar
Z.J. van de Weg committed
682
683
    end

684
    class EnvironmentBasic < Grape::Entity
Nick Thomas's avatar
Nick Thomas committed
685
      expose :id, :name, :slug, :external_url
686
687
    end

688
    class Environment < EnvironmentBasic
689
      expose :project, using: Entities::BasicProjectDetails
Z.J. van de Weg's avatar
Z.J. van de Weg committed
690
691
692
693
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
694
695
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
696
      expose :deployable,  using: Entities::Job
697
698
    end

699
    class RepoLicense < Grape::Entity
700
701
      expose :key, :name, :nickname
      expose :featured, as: :popular
702
703
704
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
705
706
707
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
708
709
      expose :content
    end
710

ZJ van de Weg's avatar
ZJ van de Weg committed
711
    class TemplatesList < Grape::Entity
712
713
714
      expose :name
    end

ZJ van de Weg's avatar
ZJ van de Weg committed
715
    class Template < Grape::Entity
716
717
      expose :name, :content
    end
718
719
720
721
722

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

724
    class PersonalAccessToken < Grape::Entity
725
726
727
728
729
730
731
      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

732
    class PersonalAccessTokenWithToken < PersonalAccessToken
733
734
      expose :token
    end
735
736
737
738

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
739

740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
    module JobRequest
      class JobInfo < Grape::Entity
        expose :name, :stage
        expose :project_id, :project_name
      end

      class GitInfo < Grape::Entity
        expose :repo_url, :ref, :sha, :before_sha
        expose :ref_type do |model|
          if model.tag
            'tag'
          else
            'branch'
          end
        end
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
756

757
758
759
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
760

761
      class Step < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
762
        expose :name, :script, :timeout, :when, :allow_failure
763
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
764

765
766
767
      class Image < Grape::Entity
        expose :name
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
768

769
770
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
Tomasz Maczukin's avatar
Tomasz Maczukin committed
771
772
      end

773
774
      class Cache < Grape::Entity
        expose :key, :untracked, :paths
Tomasz Maczukin's avatar
Tomasz Maczukin committed
775
776
      end

777
778
779
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
780

781
782
783
784
785
      class ArtifactFile < Grape::Entity
        expose :filename, :size
      end

      class Dependency < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
786
        expose :id, :name, :token
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
        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
814
        expose :dependencies, using: Dependency
815
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
816
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
817
818
  end
end