entities.rb 16.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 52
      expose :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
      expose :enable_ssl_verification
53 54
    end

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

62 63 64 65 66 67 68 69
    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
70
    class Project < Grape::Entity
71
      expose :id, :description, :default_branch, :tag_list
72
      expose :public?, as: :public
73
      expose :archived?, as: :archived
74
      expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
75
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
76
      expose :name, :name_with_namespace
77
      expose :path, :path_with_namespace
78
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled
79
      expose :created_at, :last_activity_at
80
      expose :shared_runners_enabled
81
      expose :creator_id
82
      expose :namespace
83
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
sue445's avatar
sue445 committed
84
      expose :avatar_url
85
      expose :star_count, :forks_count
Rubén Dávila's avatar
Rubén Dávila committed
86
      expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
87
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
88
      expose :public_builds
89 90 91
      expose :shared_with_groups do |project, options|
        SharedGroup.represent(project.project_group_links.all, options)
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
92 93
    end

94
    class Member < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
95
      expose :access_level do |user, options|
96
        member = options[:member] || options[:members].find { |m| m.user_id == user.id }
97 98 99 100 101 102
        member.access_level
      end
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
103
        access_requester = options[:access_requester] || options[:access_requesters].find { |m| m.user_id == user.id }
104
        access_requester.requested_at
105
      end
miks's avatar
miks committed
106 107
    end

108
    class Group < Grape::Entity
109
      expose :id, :name, :path, :description, :visibility_level
Douwe Maan's avatar
Douwe Maan committed
110
      expose :avatar_url
111
      expose :web_url
112
    end
Andrew8xx8's avatar
Andrew8xx8 committed
113

114
    class GroupDetail < Group
115
      expose :projects, using: Entities::Project
116
      expose :shared_projects, using: Entities::Project
117 118
    end

119
    class RepoBranch < Grape::Entity
120 121
      expose :name

122 123
      expose :commit do |repo_branch, options|
        options[:project].repository.commit(repo_branch.target)
124 125
      end

126 127
      expose :protected do |repo_branch, options|
        options[:project].protected_branch? repo_branch.name
128 129
      end

130
      expose :developers_can_push do |repo_branch, options|
131
        project = options[:project]
132 133
        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 }
134
      end
135

136
      expose :developers_can_merge do |repo_branch, options|
137
        project = options[:project]
138 139
        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 }
140
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
141
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
142

143 144 145 146 147 148 149 150 151 152
    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

153 154
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
155
      expose :safe_message, as: :message
156 157
    end

158 159 160 161
    class RepoCommitStats < Grape::Entity
      expose :additions, :deletions, :total
    end

162 163
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
164
      expose :stats, using: Entities::RepoCommitStats
165
      expose :status
166 167
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
168 169
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
170
      expose :author, using: Entities::UserBasic
171
      expose :updated_at, :created_at
172 173 174

      # TODO (rspeicher): Deprecated; remove in 9.0
      expose(:expires_at) { |snippet| nil }
Nihad Abbasov's avatar
Nihad Abbasov committed
175
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
176

177 178
    class ProjectEntity < Grape::Entity
      expose :id, :iid
179
      expose(:project_id) { |entity| entity.project.id }
180 181
      expose :title, :description
      expose :state, :created_at, :updated_at
182 183
    end

184 185 186 187 188
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

189
    class Milestone < ProjectEntity
190
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
191 192
    end

193
    class Issue < ProjectEntity
194
      expose :label_names, as: :labels
195 196
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
197 198 199 200

      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
201
      expose :user_notes_count
202
      expose :upvotes, :downvotes
203
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
204
    end
Alex Denisov's avatar
Alex Denisov committed
205

206 207 208 209 210
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

211
    class MergeRequest < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
212
      expose :target_branch, :source_branch
213
      expose :upvotes, :downvotes
214 215
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
216
      expose :label_names, as: :labels
217
      expose :work_in_progress?, as: :work_in_progress
218
      expose :milestone, using: Entities::Milestone
219
      expose :merge_when_build_succeeds
220
      expose :merge_status
221 222 223
      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
224
      expose :user_notes_count
225 226
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
Alex Denisov's avatar
Alex Denisov committed
227
    end
228

229 230
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
231
        compare.raw_diffs(all_diffs: true).to_a
232 233 234
      end
    end

235 236
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
237
    end
238

239 240 241 242
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

243
    class Note < Grape::Entity
244 245
      expose :id
      expose :note, as: :body
246
      expose :attachment_identifier, as: :attachment
247
      expose :author, using: Entities::UserBasic
248
      expose :created_at, :updated_at
249
      expose :system?, as: :system
250
      expose :noteable_id, :noteable_type
251
      # upvote? and downvote? are deprecated, always return false
252 253
      expose(:upvote?)    { |note| false }
      expose(:downvote?)  { |note| false }
254
    end
255

256 257 258 259 260 261 262 263
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

264 265 266 267
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
268

269 270
    class CommitNote < Grape::Entity
      expose :note
271 272 273
      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? }
274
      expose :author, using: Entities::UserBasic
275
      expose :created_at
276 277
    end

278 279
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
280
             :created_at, :started_at, :finished_at, :allow_failure
Kamil Trzcinski's avatar
Kamil Trzcinski committed
281
      expose :author, using: Entities::UserBasic
282 283
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
284 285 286 287
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
288
      expose :created_at
289 290
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
291 292 293 294 295 296

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

299 300 301 302
    class ProjectGroupLink < Grape::Entity
      expose :id, :project_id, :group_id, :group_access
    end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
303 304 305 306
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling's avatar
Robert Schilling committed
307
      expose :action_name
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
308
      expose :target_type
309 310 311

      expose :target do |todo, options|
        Entities.const_get(todo.target_type).represent(todo.target, options)
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
312 313 314 315 316
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
317
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
318 319 320 321 322 323 324 325 326 327

        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

328 329 330
    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
331

332
    class MemberAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
333
      expose :access_level
334 335 336 337 338
      expose :notification_level do |member, options|
        if member.notification_setting
          NotificationSetting.levels[member.notification_setting.level]
        end
      end
339 340
    end

341
    class ProjectAccess < MemberAccess
342 343
    end

344
    class GroupAccess < MemberAccess
345 346
    end

347 348
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
349
      expose :push_events, :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
350 351 352 353 354 355 356 357 358
      # 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

359 360 361
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
362
          project.project_members.find_by(user_id: options[:user].id)
363 364 365
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
366
          if project.group
367
            project.group.group_members.find_by(user_id: options[:user].id)
368
          end
369 370 371
        end
      end
    end
372 373

    class Label < Grape::Entity
374
      expose :name, :color, :description
375
      expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
376 377 378 379

      expose :subscribed do |label, options|
        label.subscribed?(options[:current_user])
      end
380
    end
381 382 383

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

387
      expose :commits, using: Entities::RepoCommit do |compare, options|
388
        Commit.decorate(compare.commits, nil)
389
      end
390

391
      expose :diffs, using: Entities::RepoDiff do |compare, options|
392
        compare.diffs(all_diffs: true).to_a
393
      end
394 395

      expose :compare_timeout do |compare, options|
396
        compare.diffs.overflow?
397 398 399
      end

      expose :same, as: :compare_same_ref
400
    end
401 402 403 404

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
405 406 407 408

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
409 410 411 412 413 414 415 416

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
417
      expose :after_sign_up_text
418 419 420 421 422 423 424 425 426
      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
427
      expose :default_group_visibility
428
      expose :domain_whitelist
429 430
      expose :domain_blacklist_enabled
      expose :domain_blacklist
431 432
      expose :user_oauth_applications
      expose :after_sign_out_path
433
      expose :container_registry_token_expire_delay
434
      expose :repository_storage
435
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
436 437

    class Release < Grape::Entity
438 439
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
440
    end
441 442

    class RepoTag < Grape::Entity
443
      expose :name, :message
444

445 446
      expose :commit do |repo_tag, options|
        options[:project].repository.commit(repo_tag.target)
447 448
      end

449 450
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
451 452
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
453 454 455 456

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

458
    class Runner < Grape::Entity
459 460 461 462 463 464 465
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

466 467
    class RunnerDetails < Runner
      expose :tag_list
468
      expose :run_untagged
469
      expose :locked
470
      expose :version, :revision, :platform, :architecture
471
      expose :contacted_at
472
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
473
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
474
        if options[:current_user].is_admin?
475 476
          runner.projects
        else
477
          options[:current_user].authorized_projects.where(id: runner.projects)
478 479
        end
      end
480 481
    end

482 483 484 485
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

486
    class Build < Grape::Entity
487
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
488
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
489
      expose :user, with: User
Kamil Trzcinski's avatar
Kamil Trzcinski committed
490
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
491
      expose :commit, with: RepoCommit
492
      expose :runner, with: Runner
493
    end
494

495
    class Trigger < Grape::Entity
496
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
497
    end
498

499
    class Variable < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
500
      expose :key, :value
501
    end
502

503 504 505 506
    class Environment < Grape::Entity
      expose :id, :name, :external_url
    end

507
    class RepoLicense < Grape::Entity
508 509
      expose :key, :name, :nickname
      expose :featured, as: :popular
510 511 512
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
513 514 515
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
516 517
      expose :content
    end
518

519
    class TemplatesList < Grape::Entity
520 521 522
      expose :name
    end

523
    class Template < Grape::Entity
524 525
      expose :name, :content
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
526 527
  end
end