entities.rb 35.6 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  module Entities
B
blackst0ne 已提交
3 4 5 6 7 8 9 10 11 12
    class WikiPageBasic < Grape::Entity
      expose :format
      expose :slug
      expose :title
    end

    class WikiPage < WikiPageBasic
      expose :content
    end

13
    class UserSafe < Grape::Entity
14
      expose :id, :name, :username
15
    end
16

17
    class UserBasic < UserSafe
18
      expose :state
19

20 21 22
      expose :avatar_url do |user, options|
        user.avatar_url(only_path: false)
      end
D
Douwe Maan 已提交
23

24 25
      expose :avatar_path, if: ->(user, options) { options.fetch(:only_path, false) && user.avatar_path }

D
Douwe Maan 已提交
26
      expose :web_url do |user, options|
27
        Gitlab::Routing.url_helpers.user_url(user)
D
Douwe Maan 已提交
28
      end
N
Nihad Abbasov 已提交
29
    end
N
Nihad Abbasov 已提交
30

31 32
    class User < UserBasic
      expose :created_at
33
      expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
34 35
    end

36 37
    class UserActivity < Grape::Entity
      expose :username
38 39
      expose :last_activity_on
      expose :last_activity_on, as: :last_activity_at # Back-compat
40 41
    end

42 43 44 45
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

46
    class UserPublic < User
47 48
      expose :last_sign_in_at
      expose :confirmed_at
49
      expose :last_activity_on
50
      expose :email
51
      expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
52
      expose :identities, using: Entities::Identity
53 54
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
55
      expose :two_factor_enabled?, as: :two_factor_enabled
56
      expose :external
57 58
    end

59
    class UserWithAdmin < UserPublic
60
      expose :admin?, as: :is_admin
61 62
    end

63 64 65 66
    class Email < Grape::Entity
      expose :id, :email
    end

M
miks 已提交
67
    class Hook < Grape::Entity
68
      expose :id, :url, :created_at, :push_events, :tag_push_events, :repository_update_events
69
      expose :enable_ssl_verification
M
miks 已提交
70 71
    end

72
    class ProjectHook < Hook
73
      expose :project_id, :issues_events, :merge_requests_events
74
      expose :note_events, :pipeline_events, :wiki_page_events
75
      expose :job_events
76 77
    end

78 79 80 81 82 83 84 85
    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

T
Tomasz Maczukin 已提交
86 87
    class ProjectIdentity < Grape::Entity
      expose :id, :description
88 89
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
T
Tomasz Maczukin 已提交
90 91 92
      expose :created_at
    end

F
Francisco Lopez 已提交
93
    class BasicProjectDetails < ProjectIdentity
F
Francisco Lopez 已提交
94 95 96 97 98 99 100 101 102 103 104
      include ::API::ProjectsRelationBuilder

      expose :default_branch
      # Avoids an N+1 query: https://github.com/mbleigh/acts-as-taggable-on/issues/91#issuecomment-168273770
      expose :tag_list do |project|
        # project.tags.order(:name).pluck(:name) is the most suitable option
        # to avoid loading all the ActiveRecord objects but, if we use it here
        # it override the preloaded associations and makes a query
        # (fixed in https://github.com/rails/rails/pull/25976).
        project.tags.map(&:name).sort
      end
T
Tomasz Maczukin 已提交
105
      expose :ssh_url_to_repo, :http_url_to_repo, :web_url
106 107 108
      expose :avatar_url do |project, options|
        project.avatar_url(only_path: false)
      end
109
      expose :star_count, :forks_count
F
Francisco Lopez 已提交
110
      expose :last_activity_at
111

112
      def self.preload_relation(projects_relation, options =  {})
113 114 115 116
        projects_relation.preload(:project_feature, :route)
                         .preload(namespace: [:route, :owner],
                                  tags: :taggings)
      end
117 118
    end

119
    class Project < BasicProjectDetails
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      include ::API::Helpers::RelatedResourcesHelpers

      expose :_links do
        expose :self do |project|
          expose_url(api_v4_projects_path(id: project.id))
        end

        expose :issues, if: -> (*args) { issues_available?(*args) } do |project|
          expose_url(api_v4_projects_issues_path(id: project.id))
        end

        expose :merge_requests, if: -> (*args) { mrs_available?(*args) } do |project|
          expose_url(api_v4_projects_merge_requests_path(id: project.id))
        end

        expose :repo_branches do |project|
          expose_url(api_v4_projects_repository_branches_path(id: project.id))
        end

        expose :labels do |project|
          expose_url(api_v4_projects_labels_path(id: project.id))
        end

        expose :events do |project|
          expose_url(api_v4_projects_events_path(id: project.id))
        end

        expose :members do |project|
          expose_url(api_v4_projects_members_path(id: project.id))
        end
      end

152
      expose :archived?, as: :archived
153
      expose :visibility
154
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
155
      expose :resolve_outdated_diff_discussions
F
Felipe Artur 已提交
156 157 158
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
159 160 161
      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]) }
162
      expose(:jobs_enabled) { |project, options| project.feature_available?(:builds, options[:current_user]) }
163
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:current_user]) }
F
Felipe Artur 已提交
164

165 166
      expose :shared_runners_enabled
      expose :lfs_enabled?, as: :lfs_enabled
167
      expose :creator_id
168
      expose :namespace, using: 'API::Entities::NamespaceBasic'
169
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda { |project, options| project.forked? }
170 171
      expose :import_status
      expose :import_error, if: lambda { |_project, options| options[:user_can_admin_project] }
172

173
      expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:current_user]) }
174
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
175
      expose :public_builds, as: :public_jobs
176
      expose :ci_config_path
177
      expose :shared_with_groups do |project, options|
178
        SharedGroup.represent(project.project_group_links, options)
179
      end
J
James Lopez 已提交
180
      expose :only_allow_merge_if_pipeline_succeeds
181
      expose :request_access_enabled
182
      expose :only_allow_merge_if_all_discussions_are_resolved
183
      expose :printing_merge_request_link_enabled
M
Markus Koller 已提交
184 185

      expose :statistics, using: 'API::Entities::ProjectStatistics', if: :statistics
186 187

      def self.preload_relation(projects_relation, options =  {})
188 189 190 191 192
        super(projects_relation).preload(:group)
                                .preload(project_group_links: :group,
                                         fork_network: :root_project,
                                         forked_project_link: :forked_from_project,
                                         forked_from_project: [:route, :forks, namespace: :route, tags: :taggings])
193 194 195
      end

      def self.forks_counting_projects(projects_relation)
196
        projects_relation + projects_relation.map(&:forked_from_project).compact
197
      end
M
Markus Koller 已提交
198 199 200 201 202 203 204
    end

    class ProjectStatistics < Grape::Entity
      expose :commit_count
      expose :storage_size
      expose :repository_size
      expose :lfs_objects_size
205
      expose :build_artifacts_size, as: :job_artifacts_size
N
Nihad Abbasov 已提交
206 207
    end

208
    class Member < UserBasic
D
Dmitriy Zaporozhets 已提交
209
      expose :access_level do |user, options|
210
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
211 212
        member.access_level
      end
213
      expose :expires_at do |user, options|
214
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
215 216
        member.expires_at
      end
217 218 219 220
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
221
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
222
        access_requester.requested_at
N
Nihad Abbasov 已提交
223
      end
M
miks 已提交
224 225
    end

226
    class Group < Grape::Entity
227
      expose :id, :name, :path, :description, :visibility
228
      expose :lfs_enabled?, as: :lfs_enabled
229 230
      expose :avatar_url do |group, options|
        group.avatar_url(only_path: false)
231
      end
R
Rémy Coutable 已提交
232
      expose :web_url
233
      expose :request_access_enabled
234
      expose :full_name, :full_path
235 236 237 238

      if ::Group.supports_nested_groups?
        expose :parent_id
      end
M
Markus Koller 已提交
239 240 241 242 243 244

      expose :statistics, if: :statistics do
        with_options format_with: -> (value) { value.to_i } do
          expose :storage_size
          expose :repository_size
          expose :lfs_objects_size
245
          expose :build_artifacts_size, as: :job_artifacts_size
M
Markus Koller 已提交
246 247
        end
      end
248
    end
A
Andrew8xx8 已提交
249

250
    class GroupDetail < Group
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      expose :projects, using: Entities::Project do |group, options|
        GroupProjectsFinder.new(
          group: group,
          current_user: options[:current_user],
          options: { only_owned: true }
        ).execute
      end

      expose :shared_projects, using: Entities::Project do |group, options|
        GroupProjectsFinder.new(
          group: group,
          current_user: options[:current_user],
          options: { only_shared: true }
        ).execute
      end
266 267
    end

268
    class Commit < Grape::Entity
269 270 271 272 273 274 275
      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

276
    class CommitStats < Grape::Entity
277 278 279
      expose :additions, :deletions, :total
    end

280 281
    class CommitDetail < Commit
      expose :stats, using: Entities::CommitStats
282
      expose :status
283
      expose :last_pipeline, using: 'API::Entities::PipelineBasic'
284 285
    end

286
    class Branch < Grape::Entity
287 288
      expose :name

289
      expose :commit, using: Entities::Commit do |repo_branch, options|
290
        options[:project].repository.commit(repo_branch.dereferenced_target)
291 292
      end

R
Robert Schilling 已提交
293
      expose :merged do |repo_branch, options|
294 295 296 297 298
        if options[:merged_branch_names]
          options[:merged_branch_names].include?(repo_branch.name)
        else
          options[:project].repository.merged_to_root_ref?(repo_branch)
        end
R
Robert Schilling 已提交
299 300
      end

301
      expose :protected do |repo_branch, options|
E
Eric 已提交
302
        ::ProtectedBranch.protected?(options[:project], repo_branch.name)
303 304
      end

305
      expose :developers_can_push do |repo_branch, options|
306
        options[:project].protected_branches.developers_can?(:push, repo_branch.name)
307
      end
308

309
      expose :developers_can_merge do |repo_branch, options|
310
        options[:project].protected_branches.developers_can?(:merge, repo_branch.name)
311
      end
N
Nihad Abbasov 已提交
312
    end
N
Nihad Abbasov 已提交
313

314
    class TreeObject < Grape::Entity
315
      expose :id, :name, :type, :path
316 317

      expose :mode do |obj, options|
M
mhasbini 已提交
318
        filemode = obj.mode
319 320 321 322 323
        filemode = "0" + filemode if filemode.length < 6
        filemode
      end
    end

N
Nihad Abbasov 已提交
324
    class ProjectSnippet < Grape::Entity
325
      expose :id, :title, :file_name, :description
326
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
327
      expose :updated_at, :created_at
328

329 330 331
      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
N
Nihad Abbasov 已提交
332
    end
N
Nihad Abbasov 已提交
333

334
    class PersonalSnippet < Grape::Entity
335
      expose :id, :title, :file_name, :description
336 337 338 339 340 341 342 343 344 345 346
      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

347 348
    class ProjectEntity < Grape::Entity
      expose :id, :iid
F
Felipe Artur 已提交
349
      expose(:project_id) { |entity| entity&.project.try(:id) }
350 351
      expose :title, :description
      expose :state, :created_at, :updated_at
352 353
    end

354
    class Diff < Grape::Entity
M
micael.bergeron 已提交
355
      expose :old_path, :new_path, :a_mode, :b_mode
356 357 358
      expose :new_file?, as: :new_file
      expose :renamed_file?, as: :renamed_file
      expose :deleted_file?, as: :deleted_file
359
      expose :json_safe_diff, as: :diff
360 361
    end

E
Eric 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374
    class ProtectedRefAccess < Grape::Entity
      expose :access_level
      expose :access_level_description do |protected_ref_access|
        protected_ref_access.humanize
      end
    end

    class ProtectedBranch < Grape::Entity
      expose :name
      expose :push_access_levels, using: Entities::ProtectedRefAccess
      expose :merge_access_levels, using: Entities::ProtectedRefAccess
    end

F
Felipe Artur 已提交
375 376
    class Milestone < Grape::Entity
      expose :id, :iid
F
Felipe Artur 已提交
377 378
      expose :project_id, if: -> (entity, options) { entity&.project_id }
      expose :group_id, if: -> (entity, options) { entity&.group_id }
F
Felipe Artur 已提交
379 380
      expose :title, :description
      expose :state, :created_at, :updated_at
381
      expose :due_date
V
Valery Sizov 已提交
382
      expose :start_date
N
Nihad Abbasov 已提交
383 384
    end

385
    class IssueBasic < ProjectEntity
386
      expose :closed_at
387 388 389 390
      expose :labels do |issue, options|
        # Avoids an N+1 query since labels are preloaded
        issue.labels.map(&:title).sort
      end
391
      expose :milestone, using: Entities::Milestone
392 393 394 395 396
      expose :assignees, :author, using: Entities::UserBasic

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

Z
Z.J. van de Weg 已提交
398
      expose :user_notes_count
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
      expose :upvotes do |issue, options|
        if options[:issuable_metadata]
          # Avoids an N+1 query when metadata is included
          options[:issuable_metadata][issue.id].upvotes
        else
          issue.upvotes
        end
      end
      expose :downvotes do |issue, options|
        if options[:issuable_metadata]
          # Avoids an N+1 query when metadata is included
          options[:issuable_metadata][issue.id].downvotes
        else
          issue.downvotes
        end
      end
415
      expose :due_date
416
      expose :confidential
417
      expose :discussion_locked
418 419 420 421

      expose :web_url do |issue, options|
        Gitlab::UrlBuilder.build(issue)
      end
422 423 424 425

      expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |issue|
        issue
      end
N
Nihad Abbasov 已提交
426
    end
A
Alex Denisov 已提交
427

428
    class Issue < IssueBasic
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      include ::API::Helpers::RelatedResourcesHelpers

      expose :_links do
        expose :self do |issue|
          expose_url(api_v4_project_issue_path(id: issue.project_id, issue_iid: issue.iid))
        end

        expose :notes do |issue|
          expose_url(api_v4_projects_issues_notes_path(id: issue.project_id, noteable_id: issue.iid))
        end

        expose :award_emoji do |issue|
          expose_url(api_v4_projects_issues_award_emoji_path(id: issue.project_id, issue_iid: issue.iid))
        end

        expose :project do |issue|
          expose_url(api_v4_projects_path(id: issue.project_id))
        end
      end

449 450 451 452 453
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user], options[:project] || issue.project)
      end
    end

454
    class IssuableTimeStats < Grape::Entity
455 456 457 458
      format_with(:time_tracking_formatter) do |time_spent|
        Gitlab::TimeTrackingFormatter.output(time_spent)
      end

459 460 461
      expose :time_estimate
      expose :total_time_spent
      expose :human_time_estimate
462 463 464 465 466 467 468 469 470

      with_options(format_with: :time_tracking_formatter) do
        expose :total_time_spent, as: :human_total_time_spent
      end

      def total_time_spent
        # Avoids an N+1 query since timelogs are preloaded
        object.timelogs.map(&:time_spent).sum
      end
471 472
    end

473 474 475 476 477
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

S
Stan Hu 已提交
478 479 480 481 482 483 484
    class MergeRequestSimple < ProjectEntity
      expose :title
      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
    end

485
    class MergeRequestBasic < ProjectEntity
V
Valery Sizov 已提交
486
      expose :target_branch, :source_branch
487 488 489 490 491 492 493 494 495 496 497 498 499 500
      expose :upvotes do |merge_request, options|
        if options[:issuable_metadata]
          options[:issuable_metadata][merge_request.id].upvotes
        else
          merge_request.upvotes
        end
      end
      expose :downvotes do |merge_request, options|
        if options[:issuable_metadata]
          options[:issuable_metadata][merge_request.id].downvotes
        else
          merge_request.downvotes
        end
      end
501 502
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
503 504 505 506
      expose :labels do |merge_request, options|
        # Avoids an N+1 query since labels are preloaded
        merge_request.labels.map(&:title).sort
      end
B
Ben Boeckel 已提交
507
      expose :work_in_progress?, as: :work_in_progress
508
      expose :milestone, using: Entities::Milestone
J
James Lopez 已提交
509
      expose :merge_when_pipeline_succeeds
510
      expose :merge_status
511 512
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
Z
Z.J. van de Weg 已提交
513
      expose :user_notes_count
514
      expose :discussion_locked
515 516
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
517 518 519 520

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
521 522 523 524

      expose :time_stats, using: 'API::Entities::IssuableTimeStats' do |merge_request|
        merge_request
      end
A
Alex Denisov 已提交
525
    end
V
Valeriy Sizov 已提交
526

527 528 529 530
    class MergeRequest < MergeRequestBasic
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user], options[:project])
      end
531 532 533 534

      expose :changes_count do |merge_request, _options|
        merge_request.merge_request_diff.real_size
      end
535 536
    end

537
    class MergeRequestChanges < MergeRequest
538
      expose :diffs, as: :changes, using: Entities::Diff do |compare, _|
D
Douwe Maan 已提交
539
        compare.raw_diffs(limits: false).to_a
540 541 542
      end
    end

543 544 545
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
546
    end
547

548
    class MergeRequestDiffFull < MergeRequestDiff
549
      expose :commits, using: Entities::Commit
550

551
      expose :diffs, using: Entities::Diff do |compare, _|
D
Douwe Maan 已提交
552
        compare.raw_diffs(limits: false).to_a
553 554 555
      end
    end

556
    class SSHKey < Grape::Entity
557
      expose :id, :title, :key, :created_at, :can_push
V
Valeriy Sizov 已提交
558
    end
559

560
    class SSHKeyWithUser < SSHKey
561
      expose :user, using: Entities::UserPublic
562 563
    end

R
Robert Schilling 已提交
564 565 566 567
    class GPGKey < Grape::Entity
      expose :id, :key, :created_at
    end

568
    class Note < Grape::Entity
S
sue445 已提交
569 570 571
      # Only Issue and MergeRequest have iid
      NOTEABLE_TYPES_WITH_IID = %w(Issue MergeRequest).freeze

572 573
      expose :id
      expose :note, as: :body
574
      expose :attachment_identifier, as: :attachment
575
      expose :author, using: Entities::UserBasic
576
      expose :created_at, :updated_at
577
      expose :system?, as: :system
D
Dmitriy Zaporozhets 已提交
578
      expose :noteable_id, :noteable_type
S
sue445 已提交
579 580 581

      # Avoid N+1 queries as much as possible
      expose(:noteable_iid) { |note| note.noteable.iid if NOTEABLE_TYPES_WITH_IID.include?(note.noteable_type) }
582
    end
583

Z
Z.J. van de Weg 已提交
584 585 586 587 588 589 590 591
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

592 593 594 595
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
596

597 598
    class CommitNote < Grape::Entity
      expose :note
599 600 601
      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? }
602
      expose :author, using: Entities::UserBasic
603
      expose :created_at
604 605
    end

K
Kamil Trzcinski 已提交
606 607
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
608
             :created_at, :started_at, :finished_at, :allow_failure, :coverage
K
Kamil Trzcinski 已提交
609
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
610 611
    end

612 613 614 615 616
    class PushEventPayload < Grape::Entity
      expose :commit_count, :action, :ref_type, :commit_from, :commit_to
      expose :ref, :commit_title
    end

D
Dmitriy Zaporozhets 已提交
617
    class Event < Grape::Entity
618
      expose :project_id, :action_name
S
sue445 已提交
619
      expose :target_id, :target_iid, :target_type, :author_id
620
      expose :target_title
621
      expose :created_at
D
Dmitriy Zaporozhets 已提交
622 623
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
624

625 626 627 628 629
      expose :push_event_payload,
        as: :push_data,
        using: PushEventPayload,
        if: -> (event, _) { event.push? }

630
      expose :author_username do |event, options|
Z
Z.J. van de Weg 已提交
631
        event.author&.username
632
      end
D
Dmitriy Zaporozhets 已提交
633
    end
634

635
    class ProjectGroupLink < Grape::Entity
636
      expose :id, :project_id, :group_id, :group_access, :expires_at
637 638
    end

D
Douglas Barbosa Alexandre 已提交
639 640 641 642
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
R
Robert Schilling 已提交
643
      expose :action_name
D
Douglas Barbosa Alexandre 已提交
644
      expose :target_type
645 646

      expose :target do |todo, options|
647
        Entities.const_get(todo.target_type).represent(todo.target, options)
D
Douglas Barbosa Alexandre 已提交
648 649 650 651 652
      end

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

655 656 657
        Gitlab::Routing
          .url_helpers
          .public_send(target_url, todo.project.namespace, todo.project, todo.target, anchor: target_anchor) # rubocop:disable GitlabSecurity/PublicSend
D
Douglas Barbosa Alexandre 已提交
658 659 660 661 662 663 664
      end

      expose :body
      expose :state
      expose :created_at
    end

665
    class NamespaceBasic < Grape::Entity
666
      expose :id, :name, :path, :kind, :full_path, :parent_id
667
    end
668

669
    class Namespace < NamespaceBasic
670 671 672 673 674 675
      expose :members_count_with_descendants, if: -> (namespace, opts) { expose_members_count_with_descendants?(namespace, opts) } do |namespace, _|
        namespace.users_with_descendants.count
      end

      def expose_members_count_with_descendants?(namespace, opts)
        namespace.kind == 'group' && Ability.allowed?(opts[:current_user], :admin_group, namespace)
676
      end
677
    end
678

679
    class MemberAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
680
      expose :access_level
681
      expose :notification_level do |member, options|
682 683 684
        if member.notification_setting
          ::NotificationSetting.levels[member.notification_setting.level]
        end
685
      end
686 687
    end

688
    class ProjectAccess < MemberAccess
689 690
    end

691
    class GroupAccess < MemberAccess
692 693
    end

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    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

709 710
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
711 712 713
      expose :push_events, :issues_events, :confidential_issues_events
      expose :merge_requests_events, :tag_push_events, :note_events
      expose :pipeline_events, :wiki_page_events
714
      expose :job_events
715 716
      # Expose serialized properties
      expose :properties do |service, options|
717 718 719
        field_names = service.fields
          .select { |field| options[:include_passwords] || field[:type] != 'password' }
          .map { |field| field[:name] }
720 721 722 723
        service.properties.slice(*field_names)
      end
    end

724 725 726
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
727
          if options.key?(:project_members)
728 729 730
            (options[:project_members] || []).find { |member| member.source_id == project.id }
          else
            project.project_member(options[:current_user])
731
          end
732 733 734
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
735
          if project.group
736
            if options.key?(:group_members)
737 738 739
              (options[:group_members] || []).find { |member| member.source_id == project.namespace_id }
            else
              project.group.group_member(options[:current_user])
740
            end
741
          end
742 743
        end
      end
744 745 746 747 748 749 750 751 752 753 754 755 756 757

      def self.preload_relation(projects_relation, options = {})
        relation = super(projects_relation, options)

        unless options.key?(:group_members)
          relation = relation.preload(group: [group_members: [:source, user: [notification_settings: :source]]])
        end

        unless options.key?(:project_members)
          relation = relation.preload(project_members: [:source, user: [notification_settings: :source]])
        end

        relation
      end
758
    end
759

A
Andre Guedes 已提交
760
    class LabelBasic < Grape::Entity
R
Rares Sfirlogea 已提交
761
      expose :id, :name, :color, :description
A
Andre Guedes 已提交
762 763 764
    end

    class Label < LabelBasic
765
      expose :open_issues_count do |label, options|
F
Francesco Coda Zabetta 已提交
766 767
        label.open_issues_count(options[:current_user])
      end
768

F
Francesco Coda Zabetta 已提交
769 770 771
      expose :closed_issues_count do |label, options|
        label.closed_issues_count(options[:current_user])
      end
772

F
Francesco Coda Zabetta 已提交
773 774
      expose :open_merge_requests_count do |label, options|
        label.open_merge_requests_count(options[:current_user])
775 776
      end

777 778 779
      expose :priority do |label, options|
        label.priority(options[:project])
      end
780 781

      expose :subscribed do |label, options|
782
        label.subscribed?(options[:current_user], options[:project])
783
      end
784
    end
785

A
Andre Guedes 已提交
786 787 788 789 790 791 792 793
    class List < Grape::Entity
      expose :id
      expose :label, using: Entities::LabelBasic
      expose :position
    end

    class Board < Grape::Entity
      expose :id
F
Felipe Artur 已提交
794 795
      expose :project, using: Entities::BasicProjectDetails

A
Andre Guedes 已提交
796 797 798 799 800
      expose :lists, using: Entities::List do |board|
        board.lists.destroyable
      end
    end

801
    class Compare < Grape::Entity
802 803
      expose :commit, using: Entities::Commit do |compare, options|
        ::Commit.decorate(compare.commits, nil).last
804
      end
805

806 807
      expose :commits, using: Entities::Commit do |compare, options|
        ::Commit.decorate(compare.commits, nil)
808
      end
809

810
      expose :diffs, using: Entities::Diff do |compare, options|
D
Douwe Maan 已提交
811
        compare.diffs(limits: false).to_a
812
      end
813 814

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
815
        compare.diffs.overflow?
816 817 818
      end

      expose :same, as: :compare_same_ref
819
    end
820 821 822 823

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
824 825 826 827

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
828 829 830

    class ApplicationSetting < Grape::Entity
      expose :id
831
      expose(*::ApplicationSettingsHelper.visible_attributes)
832 833 834 835 836 837
      expose(:restricted_visibility_levels) do |setting, _options|
        setting.restricted_visibility_levels.map { |level| Gitlab::VisibilityLevel.string_level(level) }
      end
      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) }
838 839 840 841

      # support legacy names, can be removed in v5
      expose :password_authentication_enabled_for_web, as: :password_authentication_enabled
      expose :password_authentication_enabled_for_web, as: :signin_enabled
842
    end
D
Dmitriy Zaporozhets 已提交
843 844

    class Release < Grape::Entity
845 846
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
847
    end
848

849
    class Tag < Grape::Entity
850
      expose :name, :message
851

852
      expose :commit, using: Entities::Commit do |repo_tag, options|
853
        options[:project].repository.commit(repo_tag.dereferenced_target)
854 855
      end

856 857
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
858 859
      end
    end
K
Kamil Trzcinski 已提交
860

T
Tomasz Maczukin 已提交
861
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
862 863 864 865 866 867 868
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

869 870
    class RunnerDetails < Runner
      expose :tag_list
871
      expose :run_untagged
872
      expose :locked
S
Shinya Maeda 已提交
873
      expose :access_level
874
      expose :version, :revision, :platform, :architecture
875
      expose :contacted_at
B
blackst0ne 已提交
876
      expose :token, if: lambda { |runner, options| options[:current_user].admin? || !runner.is_shared? }
877
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
B
blackst0ne 已提交
878
        if options[:current_user].admin?
879 880
          runner.projects
        else
881
          options[:current_user].authorized_projects.where(id: runner.projects)
882 883
        end
      end
884 885
    end

886 887 888 889
    class RunnerRegistrationDetails < Grape::Entity
      expose :id, :token
    end

890
    class JobArtifactFile < Grape::Entity
891 892 893
      expose :filename, :size
    end

894 895 896 897
    class PipelineBasic < Grape::Entity
      expose :id, :sha, :ref, :status
    end

T
Tomasz Maczukin 已提交
898
    class JobBasic < Grape::Entity
T
Tomasz Maczukin 已提交
899
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
900
      expose :created_at, :started_at, :finished_at
M
Mehdi Lahmam 已提交
901
      expose :duration
T
Tomasz Maczukin 已提交
902
      expose :user, with: User
903
      expose :commit, with: Commit
904
      expose :pipeline, with: PipelineBasic
905
    end
906

T
Tomasz Maczukin 已提交
907 908 909 910 911 912
    class Job < JobBasic
      expose :artifacts_file, using: JobArtifactFile, if: -> (job, opts) { job.artifacts? }
      expose :runner, with: Runner
    end

    class JobBasicWithProject < JobBasic
T
Tomasz Maczukin 已提交
913 914 915
      expose :project, with: ProjectIdentity
    end

T
Tomasz Maczukin 已提交
916
    class Trigger < Grape::Entity
917
      expose :id
918 919 920
      expose :token, :description
      expose :created_at, :updated_at, :deleted_at, :last_used
      expose :owner, using: Entities::UserBasic
T
Tomasz Maczukin 已提交
921
    end
922

923
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
924
      expose :key, :value
S
Shinya Maeda 已提交
925
      expose :protected?, as: :protected, if: -> (entity, _) { entity.respond_to?(:protected?) }
926
    end
927

928 929
    class Pipeline < PipelineBasic
      expose :before_sha, :tag, :yaml_errors
Z
Z.J. van de Weg 已提交
930 931 932 933

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

937 938 939
    class PipelineSchedule < Grape::Entity
      expose :id
      expose :description, :ref, :cron, :cron_timezone, :next_run_at, :active
940
      expose :created_at, :updated_at
941 942 943
      expose :owner, using: Entities::UserBasic
    end

S
Shinya Maeda 已提交
944 945
    class PipelineScheduleDetails < PipelineSchedule
      expose :last_pipeline, using: Entities::PipelineBasic
946
      expose :variables, using: Entities::Variable
S
Shinya Maeda 已提交
947 948
    end

949
    class EnvironmentBasic < Grape::Entity
N
Nick Thomas 已提交
950
      expose :id, :name, :slug, :external_url
951 952
    end

953
    class Environment < EnvironmentBasic
954
      expose :project, using: Entities::BasicProjectDetails
Z
Z.J. van de Weg 已提交
955 956 957 958
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
959 960
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
961
      expose :deployable,  using: Entities::Job
962 963
    end

964
    class License < Grape::Entity
965 966
      expose :key, :name, :nickname
      expose :featured, as: :popular
967 968 969
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
970 971 972
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
973 974
      expose :content
    end
975

Z
ZJ van de Weg 已提交
976
    class TemplatesList < Grape::Entity
977 978 979
      expose :name
    end

Z
ZJ van de Weg 已提交
980
    class Template < Grape::Entity
981 982
      expose :name, :content
    end
983 984 985 986 987

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

989
    class PersonalAccessToken < Grape::Entity
990 991 992 993 994 995 996
      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

997
    class PersonalAccessTokenWithToken < PersonalAccessToken
998 999
      expose :token
    end
1000 1001 1002 1003

    class ImpersonationToken < PersonalAccessTokenWithToken
      expose :impersonation
    end
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    class FeatureGate < Grape::Entity
      expose :key
      expose :value
    end

    class Feature < Grape::Entity
      expose :name
      expose :state
      expose :gates, using: FeatureGate do |model|
        model.gates.map do |gate|
          value = model.gate_values[gate.key]

          # By default all gate values are populated. Only show relevant ones.
          if (value.is_a?(Integer) && value.zero?) || (value.is_a?(Set) && value.empty?)
            next
          end

          { key: gate.key, value: value }
        end.compact
      end
    end

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    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 已提交
1043

1044 1045 1046
      class RunnerInfo < Grape::Entity
        expose :timeout
      end
T
Tomasz Maczukin 已提交
1047

1048
      class Step < Grape::Entity
T
Tomasz Maczukin 已提交
1049
        expose :name, :script, :timeout, :when, :allow_failure
1050
      end
T
Tomasz Maczukin 已提交
1051

1052
      class Image < Grape::Entity
1053 1054 1055
        expose :name, :entrypoint
      end

1056
      class Service < Image
1057
        expose :alias, :command
1058
      end
T
Tomasz Maczukin 已提交
1059

1060 1061
      class Artifacts < Grape::Entity
        expose :name, :untracked, :paths, :when, :expire_in
T
Tomasz Maczukin 已提交
1062 1063
      end

1064
      class Cache < Grape::Entity
1065
        expose :key, :untracked, :paths, :policy
T
Tomasz Maczukin 已提交
1066 1067
      end

1068 1069 1070
      class Credentials < Grape::Entity
        expose :type, :url, :username, :password
      end
T
Tomasz Maczukin 已提交
1071

1072
      class Dependency < Grape::Entity
T
Tomasz Maczukin 已提交
1073
        expose :id, :name, :token
1074
        expose :artifacts_file, using: JobArtifactFile, if: ->(job, _) { job.artifacts? }
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
      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
1097
        expose :services, using: Service
1098 1099 1100
        expose :artifacts, using: Artifacts
        expose :cache, using: Cache
        expose :credentials, using: Credentials
T
Tomasz Maczukin 已提交
1101
        expose :dependencies, using: Dependency
1102
        expose :features
1103
      end
T
Tomasz Maczukin 已提交
1104
    end
1105 1106 1107 1108

    class UserAgentDetail < Grape::Entity
      expose :user_agent
      expose :ip_address
J
James Lopez 已提交
1109
      expose :submitted, as: :akismet_submitted
1110
    end
1111 1112 1113 1114 1115 1116

    class RepositoryStorageHealth < Grape::Entity
      expose :storage_name
      expose :failing_on_hosts
      expose :total_failures
    end
1117 1118 1119 1120 1121

    class CustomAttribute < Grape::Entity
      expose :key
      expose :value
    end
T
Travis Miller 已提交
1122

1123 1124 1125 1126 1127
    class PagesDomainCertificateExpiration < Grape::Entity
      expose :expired?, as: :expired
      expose :expiration
    end

T
Travis Miller 已提交
1128 1129 1130 1131 1132 1133 1134
    class PagesDomainCertificate < Grape::Entity
      expose :subject
      expose :expired?, as: :expired
      expose :certificate
      expose :certificate_text
    end

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    class PagesDomainBasic < Grape::Entity
      expose :domain
      expose :url
      expose :certificate,
        as: :certificate_expiration,
        if: ->(pages_domain, _) { pages_domain.certificate? },
        using: PagesDomainCertificateExpiration do |pages_domain|
        pages_domain
      end
    end

T
Travis Miller 已提交
1146 1147 1148 1149
    class PagesDomain < Grape::Entity
      expose :domain
      expose :url
      expose :certificate,
1150 1151
        if: ->(pages_domain, _) { pages_domain.certificate? },
        using: PagesDomainCertificate do |pages_domain|
T
Travis Miller 已提交
1152 1153 1154
        pages_domain
      end
    end
N
Nihad Abbasov 已提交
1155 1156
  end
end