entities.rb 19.1 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 8
    class UserBasic < UserSafe
      expose :id, :state, :avatar_url
Douwe Maan's avatar
Douwe Maan committed
9 10

      expose :web_url do |user, options|
11
        Gitlab::Routing.url_helpers.user_url(user)
Douwe Maan's avatar
Douwe Maan committed
12
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
13
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
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
19 20
    end

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

25
    class UserFull < User
26 27
      expose :last_sign_in_at
      expose :confirmed_at
28
      expose :email
29
      expose :theme_id, :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 UserLogin < UserFull
38
      expose :private_token
39 40
    end

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

miks's avatar
miks committed
45
    class Hook < Grape::Entity
46
      expose :id, :url, :created_at
miks's avatar
miks committed
47 48
    end

49
    class ProjectHook < Hook
50
      expose :project_id, :push_events
51
      expose :issues_events, :merge_requests_events, :tag_push_events
52
      expose :note_events, :build_events, :pipeline_events, :wiki_page_events
53
      expose :enable_ssl_verification
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

Nihad Abbasov's avatar
Nihad Abbasov committed
71
    class Project < Grape::Entity
72
      expose :id, :description, :default_branch, :tag_list
73
      expose :public?, as: :public
74
      expose :archived?, as: :archived
75
      expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
76
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
77
      expose :name, :name_with_namespace
78
      expose :path, :path_with_namespace
79 80 81 82 83 84 85 86 87
      expose :container_registry_enabled

      # Expose old field names with the new permissions methods to keep API compatible
      expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:user]) }
      expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:user]) }
      expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:user]) }
      expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:user]) }
      expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:user]) }

88
      expose :created_at, :last_activity_at
89 90
      expose :shared_runners_enabled
      expose :lfs_enabled?, as: :lfs_enabled
91
      expose :creator_id
92
      expose :namespace
93
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
sue445's avatar
sue445 committed
94
      expose :avatar_url
95
      expose :star_count, :forks_count
96
      expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:user]) && project.default_issues_tracker? }
97
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
98
      expose :public_builds
99 100 101
      expose :shared_with_groups do |project, options|
        SharedGroup.represent(project.project_group_links.all, options)
      end
102
      expose :only_allow_merge_if_build_succeeds
103
      expose :request_access_enabled
Nihad Abbasov's avatar
Nihad Abbasov committed
104 105
    end

106
    class Member < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
107
      expose :access_level do |user, options|
108
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
109 110
        member.access_level
      end
111
      expose :expires_at do |user, options|
112
        member = options[:member] || options[:source].members.find_by(user_id: user.id)
113 114
        member.expires_at
      end
115 116 117 118
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
119
        access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
120
        access_requester.requested_at
121
      end
miks's avatar
miks committed
122 123
    end

124
    class Group < Grape::Entity
125 126
      expose :id, :name, :path, :description, :visibility_level
      expose :lfs_enabled?, as: :lfs_enabled
Douwe Maan's avatar
Douwe Maan committed
127
      expose :avatar_url
128
      expose :web_url
129
      expose :request_access_enabled
130
    end
Andrew8xx8's avatar
Andrew8xx8 committed
131

132
    class GroupDetail < Group
133
      expose :projects, using: Entities::Project
134
      expose :shared_projects, using: Entities::Project
135 136
    end

137
    class RepoBranch < Grape::Entity
138 139
      expose :name

140 141
      expose :commit do |repo_branch, options|
        options[:project].repository.commit(repo_branch.target)
142 143
      end

144 145
      expose :protected do |repo_branch, options|
        options[:project].protected_branch? repo_branch.name
146 147
      end

148
      expose :developers_can_push do |repo_branch, options|
149
        project = options[:project]
150 151
        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 }
152
      end
153

154
      expose :developers_can_merge do |repo_branch, options|
155
        project = options[:project]
156 157
        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 }
158
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
159
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
160

161 162 163 164 165 166 167 168 169 170
    class RepoTreeObject < Grape::Entity
      expose :id, :name, :type

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

171 172
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
173
      expose :safe_message, as: :message
174 175
    end

176 177 178 179
    class RepoCommitStats < Grape::Entity
      expose :additions, :deletions, :total
    end

180 181
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
182
      expose :stats, using: Entities::RepoCommitStats
183
      expose :status
184 185
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
186 187
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
188
      expose :author, using: Entities::UserBasic
189
      expose :updated_at, :created_at
190 191 192

      # TODO (rspeicher): Deprecated; remove in 9.0
      expose(:expires_at) { |snippet| nil }
193 194 195 196

      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
197
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
198

199 200
    class ProjectEntity < Grape::Entity
      expose :id, :iid
201
      expose(:project_id) { |entity| entity.project.id }
202 203
      expose :title, :description
      expose :state, :created_at, :updated_at
204 205
    end

206 207 208 209 210
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

211
    class Milestone < ProjectEntity
212
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
213 214
    end

215
    class Issue < ProjectEntity
216
      expose :label_names, as: :labels
217 218
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
219 220 221 222

      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
223
      expose :user_notes_count
224
      expose :upvotes, :downvotes
225
      expose :due_date
226
      expose :confidential
227 228 229 230

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

233 234 235 236 237
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

238
    class MergeRequest < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
239
      expose :target_branch, :source_branch
240
      expose :upvotes, :downvotes
241 242
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
243
      expose :label_names, as: :labels
244
      expose :work_in_progress?, as: :work_in_progress
245
      expose :milestone, using: Entities::Milestone
246
      expose :merge_when_build_succeeds
247
      expose :merge_status
248 249
      expose :diff_head_sha, as: :sha
      expose :merge_commit_sha
250 251 252
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user])
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
253
      expose :user_notes_count
254 255
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
256 257 258 259

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
Alex Denisov's avatar
Alex Denisov committed
260
    end
261

262 263
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
264
        compare.raw_diffs(all_diffs: true).to_a
265 266 267
      end
    end

268 269 270
    class MergeRequestDiff < Grape::Entity
      expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
        :created_at, :merge_request_id, :state, :real_size
271
    end
272

273
    class MergeRequestDiffFull < MergeRequestDiff
274 275 276
      expose :commits, using: Entities::RepoCommit

      expose :diffs, using: Entities::RepoDiff do |compare, _|
277
        compare.raw_diffs(all_diffs: true).to_a
278 279 280
      end
    end

281 282
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
283
    end
284

285 286 287 288
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

289
    class Note < Grape::Entity
290 291
      expose :id
      expose :note, as: :body
292
      expose :attachment_identifier, as: :attachment
293
      expose :author, using: Entities::UserBasic
294
      expose :created_at, :updated_at
295
      expose :system?, as: :system
296
      expose :noteable_id, :noteable_type
297
      # upvote? and downvote? are deprecated, always return false
298 299
      expose(:upvote?)    { |note| false }
      expose(:downvote?)  { |note| false }
300
    end
301

302 303 304 305 306 307 308 309
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

310 311 312 313
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
314

315 316
    class CommitNote < Grape::Entity
      expose :note
317 318 319
      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? }
320
      expose :author, using: Entities::UserBasic
321
      expose :created_at
322 323
    end

324 325
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
326
             :created_at, :started_at, :finished_at, :allow_failure
Kamil Trzcinski's avatar
Kamil Trzcinski committed
327
      expose :author, using: Entities::UserBasic
328 329
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
330 331 332 333
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
334
      expose :created_at
335 336
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
337 338 339 340 341 342

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
343
    end
344

345 346 347 348
    class ProjectGroupLink < Grape::Entity
      expose :id, :project_id, :group_id, :group_access
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
349 350 351 352
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling's avatar
Robert Schilling committed
353
      expose :action_name
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
354
      expose :target_type
355 356 357

      expose :target do |todo, options|
        Entities.const_get(todo.target_type).represent(todo.target, options)
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
358 359 360 361 362
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
363
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
364 365 366 367 368 369 370 371 372 373

        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

374 375 376
    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
377

378
    class MemberAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
379
      expose :access_level
380 381
      expose :notification_level do |member, options|
        if member.notification_setting
382
          ::NotificationSetting.levels[member.notification_setting.level]
383 384
        end
      end
385 386
    end

387
    class ProjectAccess < MemberAccess
388 389
    end

390
    class GroupAccess < MemberAccess
391 392
    end

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    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

408 409
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
410 411
      expose :push_events, :issues_events, :merge_requests_events
      expose :tag_push_events, :note_events, :build_events, :pipeline_events
412 413 414 415 416 417 418 419 420
      # 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

421 422 423
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
424
          project.project_members.find_by(user_id: options[:user].id)
425 426 427
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
428
          if project.group
429
            project.group.group_members.find_by(user_id: options[:user].id)
430
          end
431 432 433
        end
      end
    end
434 435

    class Label < Grape::Entity
436
      expose :name, :color, :description
437
      expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
438 439 440 441

      expose :subscribed do |label, options|
        label.subscribed?(options[:current_user])
      end
442
    end
443 444 445

    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
446
        Commit.decorate(compare.commits, nil).last
447
      end
448

449
      expose :commits, using: Entities::RepoCommit do |compare, options|
450
        Commit.decorate(compare.commits, nil)
451
      end
452

453
      expose :diffs, using: Entities::RepoDiff do |compare, options|
454
        compare.diffs(all_diffs: true).to_a
455
      end
456 457

      expose :compare_timeout do |compare, options|
458
        compare.diffs.overflow?
459 460 461
      end

      expose :same, as: :compare_same_ref
462
    end
463 464 465 466

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
467 468 469 470

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
471 472 473 474 475 476 477 478

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
479
      expose :after_sign_up_text
480 481 482 483 484 485 486 487 488
      expose :created_at
      expose :updated_at
      expose :home_page_url
      expose :default_branch_protection
      expose :restricted_visibility_levels
      expose :max_attachment_size
      expose :session_expire_delay
      expose :default_project_visibility
      expose :default_snippet_visibility
489
      expose :default_group_visibility
490
      expose :domain_whitelist
491 492
      expose :domain_blacklist_enabled
      expose :domain_blacklist
493 494
      expose :user_oauth_applications
      expose :after_sign_out_path
495
      expose :container_registry_token_expire_delay
496
      expose :repository_storage
497
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
498 499

    class Release < Grape::Entity
500 501
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
502
    end
503 504

    class RepoTag < Grape::Entity
505
      expose :name, :message
506

507 508
      expose :commit do |repo_tag, options|
        options[:project].repository.commit(repo_tag.target)
509 510
      end

511 512
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
513 514
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
515 516 517 518

    class TriggerRequest < Grape::Entity
      expose :id, :variables
    end
519

520
    class Runner < Grape::Entity
521 522 523 524 525 526 527
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

528 529
    class RunnerDetails < Runner
      expose :tag_list
530
      expose :run_untagged
531
      expose :locked
532
      expose :version, :revision, :platform, :architecture
533
      expose :contacted_at
534
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
535
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
536
        if options[:current_user].is_admin?
537 538
          runner.projects
        else
539
          options[:current_user].authorized_projects.where(id: runner.projects)
540 541
        end
      end
542 543
    end

544 545 546 547
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

548
    class Build < Grape::Entity
549
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
550
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
551
      expose :user, with: User
Kamil Trzcinski's avatar
Kamil Trzcinski committed
552
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
553
      expose :commit, with: RepoCommit
554
      expose :runner, with: Runner
555
    end
556

557
    class Trigger < Grape::Entity
558
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
559
    end
560

561
    class Variable < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
562
      expose :key, :value
563
    end
564

565
    class Pipeline < Grape::Entity
566
      expose :id, :status, :ref, :sha, :before_sha, :tag, :yaml_errors
567 568 569 570 571 572

      expose :user, with: Entities::UserBasic
      expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
      expose :duration
    end

573
    class EnvironmentBasic < Grape::Entity
574 575 576
      expose :id, :name, :external_url
    end

577 578
    class Environment < EnvironmentBasic
      expose :project, using: Entities::Project
Z.J. van de Weg's avatar
Z.J. van de Weg committed
579 580 581 582
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
583 584 585
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
      expose :deployable,  using: Entities::Build
586 587
    end

588
    class RepoLicense < Grape::Entity
589 590
      expose :key, :name, :nickname
      expose :featured, as: :popular
591 592 593
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
594 595 596
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
597 598
      expose :content
    end
599

600
    class TemplatesList < Grape::Entity
601 602 603
      expose :name
    end

604
    class Template < Grape::Entity
605 606
      expose :name, :content
    end
607 608 609 610 611

    class BroadcastMessage < Grape::Entity
      expose :id, :message, :starts_at, :ends_at, :color, :font
      expose :active?, as: :active
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
612 613
  end
end