BigW Consortium Gitlab

entities.rb 17.3 KB
Newer Older
1
module API
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 committed
9 10

      expose :web_url do |user, options|
11
        Gitlab::Routing.url_helpers.user_url(user)
Douwe Maan committed
12
      end
Nihad Abbasov committed
13
    end
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 committed
45
    class Hook < Grape::Entity
46
      expose :id, :url, :created_at
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 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
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled
80
      expose :created_at, :last_activity_at
81
      expose :shared_runners_enabled
82
      expose :creator_id
83
      expose :namespace
84
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
85
      expose :avatar_url
86
      expose :star_count, :forks_count
Rubén Dávila committed
87
      expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
88
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
89
      expose :public_builds
90 91 92
      expose :shared_with_groups do |project, options|
        SharedGroup.represent(project.project_group_links.all, options)
      end
93
      expose :only_allow_merge_if_build_succeeds
Nihad Abbasov committed
94 95
    end

96
    class Member < UserBasic
97
      expose :access_level do |user, options|
98
        member = options[:member] || options[:members].find { |m| m.user_id == user.id }
99 100
        member.access_level
      end
101 102 103 104
      expose :expires_at do |user, options|
        member = options[:member] || options[:members].find { |m| m.user_id == user.id }
        member.expires_at
      end
105 106 107 108
    end

    class AccessRequester < UserBasic
      expose :requested_at do |user, options|
109
        access_requester = options[:access_requester] || options[:access_requesters].find { |m| m.user_id == user.id }
110
        access_requester.requested_at
111
      end
112 113
    end

114
    class Group < Grape::Entity
115
      expose :id, :name, :path, :description, :visibility_level
Douwe Maan committed
116
      expose :avatar_url
117
      expose :web_url
118
    end
Andrew8xx8 committed
119

120
    class GroupDetail < Group
121
      expose :projects, using: Entities::Project
122
      expose :shared_projects, using: Entities::Project
123 124
    end

125
    class RepoBranch < Grape::Entity
126 127
      expose :name

128 129
      expose :commit do |repo_branch, options|
        options[:project].repository.commit(repo_branch.target)
130 131
      end

132 133
      expose :protected do |repo_branch, options|
        options[:project].protected_branch? repo_branch.name
134 135
      end

136
      expose :developers_can_push do |repo_branch, options|
137
        project = options[:project]
138 139
        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 }
140
      end
141

142
      expose :developers_can_merge do |repo_branch, options|
143
        project = options[:project]
144 145
        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 }
146
      end
Nihad Abbasov committed
147
    end
Nihad Abbasov committed
148

149 150 151 152 153 154 155 156 157 158
    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

159 160
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
161
      expose :safe_message, as: :message
162 163
    end

164 165 166 167
    class RepoCommitStats < Grape::Entity
      expose :additions, :deletions, :total
    end

168 169
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
170
      expose :stats, using: Entities::RepoCommitStats
171
      expose :status
172 173
    end

Nihad Abbasov committed
174 175
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
176
      expose :author, using: Entities::UserBasic
177
      expose :updated_at, :created_at
178 179 180

      # TODO (rspeicher): Deprecated; remove in 9.0
      expose(:expires_at) { |snippet| nil }
181 182 183 184

      expose :web_url do |snippet, options|
        Gitlab::UrlBuilder.build(snippet)
      end
Nihad Abbasov committed
185
    end
Nihad Abbasov committed
186

187 188
    class ProjectEntity < Grape::Entity
      expose :id, :iid
189
      expose(:project_id) { |entity| entity.project.id }
190 191
      expose :title, :description
      expose :state, :created_at, :updated_at
192 193
    end

194 195 196 197 198
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

199
    class Milestone < ProjectEntity
200
      expose :due_date
Nihad Abbasov committed
201 202
    end

203
    class Issue < ProjectEntity
204
      expose :label_names, as: :labels
205 206
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
207 208 209 210

      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
211
      expose :user_notes_count
212
      expose :upvotes, :downvotes
213
      expose :due_date
214 215 216 217

      expose :web_url do |issue, options|
        Gitlab::UrlBuilder.build(issue)
      end
Nihad Abbasov committed
218
    end
Alex Denisov committed
219

220 221 222 223 224
    class ExternalIssue < Grape::Entity
      expose :title
      expose :id
    end

225
    class MergeRequest < ProjectEntity
Valery Sizov committed
226
      expose :target_branch, :source_branch
227
      expose :upvotes, :downvotes
228 229
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
230
      expose :label_names, as: :labels
231
      expose :work_in_progress?, as: :work_in_progress
232
      expose :milestone, using: Entities::Milestone
233
      expose :merge_when_build_succeeds
234
      expose :merge_status
235 236 237
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user])
      end
238
      expose :user_notes_count
239 240
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
241 242 243 244

      expose :web_url do |merge_request, options|
        Gitlab::UrlBuilder.build(merge_request)
      end
Alex Denisov committed
245
    end
246

247 248
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
249
        compare.raw_diffs(all_diffs: true).to_a
250 251 252
      end
    end

253 254
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
255
    end
256

257 258 259 260
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

261
    class Note < Grape::Entity
262 263
      expose :id
      expose :note, as: :body
264
      expose :attachment_identifier, as: :attachment
265
      expose :author, using: Entities::UserBasic
266
      expose :created_at, :updated_at
267
      expose :system?, as: :system
268
      expose :noteable_id, :noteable_type
269
      # upvote? and downvote? are deprecated, always return false
270 271
      expose(:upvote?)    { |note| false }
      expose(:downvote?)  { |note| false }
272
    end
273

274 275 276 277 278 279 280 281
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

282 283 284 285
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
286

287 288
    class CommitNote < Grape::Entity
      expose :note
289 290 291
      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? }
292
      expose :author, using: Entities::UserBasic
293
      expose :created_at
294 295
    end

296 297
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
298
             :created_at, :started_at, :finished_at, :allow_failure
299
      expose :author, using: Entities::UserBasic
300 301
    end

302 303 304 305
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
306
      expose :created_at
307 308
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
309 310 311 312 313 314

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
315
    end
316

317 318 319 320
    class ProjectGroupLink < Grape::Entity
      expose :id, :project_id, :group_id, :group_access
    end

321 322 323 324
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
Robert Schilling committed
325
      expose :action_name
326
      expose :target_type
327 328 329

      expose :target do |todo, options|
        Entities.const_get(todo.target_type).represent(todo.target, options)
330 331 332 333 334
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
335
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
336 337 338 339 340 341 342 343 344 345

        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

346 347 348
    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
349

350
    class MemberAccess < Grape::Entity
351
      expose :access_level
352 353 354 355 356
      expose :notification_level do |member, options|
        if member.notification_setting
          NotificationSetting.levels[member.notification_setting.level]
        end
      end
357 358
    end

359
    class ProjectAccess < MemberAccess
360 361
    end

362
    class GroupAccess < MemberAccess
363 364
    end

365 366
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
367 368
      expose :push_events, :issues_events, :merge_requests_events
      expose :tag_push_events, :note_events, :build_events, :pipeline_events
369 370 371 372 373 374 375 376 377
      # 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

378 379 380
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
381
          project.project_members.find_by(user_id: options[:user].id)
382 383 384
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
385
          if project.group
386
            project.group.group_members.find_by(user_id: options[:user].id)
387
          end
388 389 390
        end
      end
    end
391 392

    class Label < Grape::Entity
393
      expose :name, :color, :description
394
      expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
395 396 397 398

      expose :subscribed do |label, options|
        label.subscribed?(options[:current_user])
      end
399
    end
400 401 402

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

406
      expose :commits, using: Entities::RepoCommit do |compare, options|
407
        Commit.decorate(compare.commits, nil)
408
      end
409

410
      expose :diffs, using: Entities::RepoDiff do |compare, options|
411
        compare.diffs(all_diffs: true).to_a
412
      end
413 414

      expose :compare_timeout do |compare, options|
415
        compare.diffs.overflow?
416 417 418
      end

      expose :same, as: :compare_same_ref
419
    end
420 421 422 423

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
424 425 426 427

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
428 429 430 431 432 433 434 435

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
436
      expose :after_sign_up_text
437 438 439 440 441 442 443 444 445
      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
446
      expose :default_group_visibility
447
      expose :domain_whitelist
448 449
      expose :domain_blacklist_enabled
      expose :domain_blacklist
450 451
      expose :user_oauth_applications
      expose :after_sign_out_path
452
      expose :container_registry_token_expire_delay
453
      expose :repository_storage
454
    end
Dmitriy Zaporozhets committed
455 456

    class Release < Grape::Entity
457 458
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets committed
459
    end
460 461

    class RepoTag < Grape::Entity
462
      expose :name, :message
463

464 465
      expose :commit do |repo_tag, options|
        options[:project].repository.commit(repo_tag.target)
466 467
      end

468 469
      expose :release, using: Entities::Release do |repo_tag, options|
        options[:project].releases.find_by(tag: repo_tag.name)
470 471
      end
    end
472 473 474 475

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

477
    class Runner < Grape::Entity
478 479 480 481 482 483 484
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

485 486
    class RunnerDetails < Runner
      expose :tag_list
487
      expose :run_untagged
488
      expose :locked
489
      expose :version, :revision, :platform, :architecture
490
      expose :contacted_at
491
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
492
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
493
        if options[:current_user].is_admin?
494 495
          runner.projects
        else
496
          options[:current_user].authorized_projects.where(id: runner.projects)
497 498
        end
      end
499 500
    end

501 502 503 504
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

505
    class Build < Grape::Entity
506
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
507
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin committed
508
      expose :user, with: User
509
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
510
      expose :commit, with: RepoCommit
511
      expose :runner, with: Runner
512
    end
513

514
    class Trigger < Grape::Entity
515
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
516
    end
517

518
    class Variable < Grape::Entity
Tomasz Maczukin committed
519
      expose :key, :value
520
    end
521

522
    class Pipeline < Grape::Entity
523
      expose :id, :status, :ref, :sha, :before_sha, :tag, :yaml_errors
524 525 526 527 528 529

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

530
    class EnvironmentBasic < Grape::Entity
531 532 533
      expose :id, :name, :external_url
    end

534 535
    class Environment < EnvironmentBasic
      expose :project, using: Entities::Project
536 537 538 539
    end

    class Deployment < Grape::Entity
      expose :id, :iid, :ref, :sha, :created_at
540 541 542
      expose :user,        using: Entities::UserBasic
      expose :environment, using: Entities::EnvironmentBasic
      expose :deployable,  using: Entities::Build
543 544
    end

545
    class RepoLicense < Grape::Entity
546 547
      expose :key, :name, :nickname
      expose :featured, as: :popular
548 549 550
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
551 552 553
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
554 555
      expose :content
    end
556

557
    class TemplatesList < Grape::Entity
558 559 560
      expose :name
    end

561
    class Template < Grape::Entity
562 563
      expose :name, :content
    end
Nihad Abbasov committed
564 565
  end
end