entities.rb 15.9 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
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
D
Douwe Maan 已提交
9 10

      expose :web_url do |user, options|
11
        Gitlab::Routing.url_helpers.user_url(user)
D
Douwe Maan 已提交
12
      end
N
Nihad Abbasov 已提交
13
    end
N
Nihad Abbasov 已提交
14

15 16 17
    class User < UserBasic
      expose :created_at
      expose :is_admin?, as: :is_admin
R
Robert Schilling 已提交
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

M
miks 已提交
45
    class Hook < Grape::Entity
46
      expose :id, :url, :created_at
M
miks 已提交
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

N
Nihad Abbasov 已提交
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
K
Kamil Trzcinski 已提交
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? }
S
sue445 已提交
84
      expose :avatar_url
85
      expose :star_count, :forks_count
R
Rubén Dávila 已提交
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
N
Nihad Abbasov 已提交
92 93
    end

N
Nihad Abbasov 已提交
94
    class ProjectMember < UserBasic
D
Dmitriy Zaporozhets 已提交
95 96
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
N
Nihad Abbasov 已提交
97
      end
M
miks 已提交
98 99
    end

100
    class Group < Grape::Entity
101
      expose :id, :name, :path, :description, :visibility_level
D
Douwe Maan 已提交
102
      expose :avatar_url
R
Rémy Coutable 已提交
103
      expose :web_url
104
    end
A
Andrew8xx8 已提交
105

106
    class GroupDetail < Group
107
      expose :projects, using: Entities::Project
108
      expose :shared_projects, using: Entities::Project
109 110
    end

I
Izaak Alpert 已提交
111
    class GroupMember < UserBasic
D
Dmitriy Zaporozhets 已提交
112
      expose :access_level do |user, options|
113
        options[:group].group_members.find_by(user_id: user.id).access_level
I
Izaak Alpert 已提交
114 115 116
      end
    end

N
Nihad Abbasov 已提交
117
    class RepoObject < Grape::Entity
118 119 120 121 122 123 124 125 126 127
      expose :name

      expose :commit do |repo_obj, options|
        if repo_obj.respond_to?(:commit)
          repo_obj.commit
        elsif options[:project]
          options[:project].repository.commit(repo_obj.target)
        end
      end

128
      expose :protected do |repo_obj, options|
129
        if options[:project]
130 131 132 133 134 135 136
          options[:project].protected_branch? repo_obj.name
        end
      end

      expose :developers_can_push do |repo_obj, options|
        if options[:project]
          options[:project].developers_can_push_to_protected_branch? repo_obj.name
137 138
        end
      end
139 140 141 142 143 144

      expose :developers_can_merge do |repo_obj, options|
        if options[:project]
          options[:project].developers_can_merge_to_protected_branch? repo_obj.name
        end
      end
N
Nihad Abbasov 已提交
145
    end
N
Nihad Abbasov 已提交
146

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

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

162 163
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
K
Kamil Trzcinski 已提交
164
      expose :status
165 166
    end

N
Nihad Abbasov 已提交
167 168
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
169
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
170
      expose :updated_at, :created_at
171 172 173

      # TODO (rspeicher): Deprecated; remove in 9.0
      expose(:expires_at) { |snippet| nil }
N
Nihad Abbasov 已提交
174
    end
N
Nihad Abbasov 已提交
175

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

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

188
    class Milestone < ProjectEntity
189
      expose :due_date
N
Nihad Abbasov 已提交
190 191
    end

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

      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
Z
Z.J. van de Weg 已提交
200
      expose :user_notes_count
Z
Zeger-Jan van de Weg 已提交
201
      expose :upvotes, :downvotes
202
      expose :due_date
N
Nihad Abbasov 已提交
203
    end
A
Alex Denisov 已提交
204

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

210
    class MergeRequest < ProjectEntity
V
Valery Sizov 已提交
211
      expose :target_branch, :source_branch
212
      expose :upvotes, :downvotes
213 214
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
215
      expose :label_names, as: :labels
B
Ben Boeckel 已提交
216
      expose :work_in_progress?, as: :work_in_progress
217
      expose :milestone, using: Entities::Milestone
218
      expose :merge_when_build_succeeds
219
      expose :merge_status
220 221 222
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user])
      end
Z
Z.J. van de Weg 已提交
223
      expose :user_notes_count
224 225
      expose :should_remove_source_branch?, as: :should_remove_source_branch
      expose :force_remove_source_branch?, as: :force_remove_source_branch
A
Alex Denisov 已提交
226
    end
V
Valeriy Sizov 已提交
227

228 229
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
J
Jacob Vosmaer 已提交
230
        compare.diffs(all_diffs: true).to_a
231 232 233
      end
    end

234 235
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
236
    end
237

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

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

Z
Z.J. van de Weg 已提交
255 256 257 258 259 260 261 262
    class AwardEmoji < Grape::Entity
      expose :id
      expose :name
      expose :user, using: Entities::UserBasic
      expose :created_at, :updated_at
      expose :awardable_id, :awardable_type
    end

263 264 265 266
    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
267

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

K
Kamil Trzcinski 已提交
277 278
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
279
             :created_at, :started_at, :finished_at, :allow_failure
K
Kamil Trzcinski 已提交
280
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
281 282
    end

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

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
D
Dmitriy Zaporozhets 已提交
296
    end
297

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

D
Douglas Barbosa Alexandre 已提交
302 303 304 305
    class Todo < Grape::Entity
      expose :id
      expose :project, using: Entities::BasicProjectDetails
      expose :author, using: Entities::UserBasic
R
Robert Schilling 已提交
306
      expose :action_name
D
Douglas Barbosa Alexandre 已提交
307
      expose :target_type
308 309 310

      expose :target do |todo, options|
        Entities.const_get(todo.target_type).represent(todo.target, options)
D
Douglas Barbosa Alexandre 已提交
311 312 313 314 315
      end

      expose :target_url do |todo, options|
        target_type   = todo.target_type.underscore
        target_url    = "namespace_project_#{target_type}_url"
316
        target_anchor = "note_#{todo.note_id}" if todo.note_id?
D
Douglas Barbosa Alexandre 已提交
317 318 319 320 321 322 323 324 325 326

        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

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

331
    class Member < Grape::Entity
D
Dmitriy Zaporozhets 已提交
332
      expose :access_level
333 334 335 336 337
      expose :notification_level do |member, options|
        if member.notification_setting
          NotificationSetting.levels[member.notification_setting.level]
        end
      end
338 339
    end

340 341 342 343
    class ProjectAccess < Member
    end

    class GroupAccess < Member
344 345
    end

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

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

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

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

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

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

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

390
      expose :diffs, using: Entities::RepoDiff do |compare, options|
J
Jacob Vosmaer 已提交
391
        compare.diffs(all_diffs: true).to_a
392
      end
393 394

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
395
        compare.diffs.overflow?
396 397 398
      end

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

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
404 405 406 407

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

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
416
      expose :after_sign_up_text
417 418 419 420 421 422 423 424 425
      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
426
      expose :default_group_visibility
427 428 429
      expose :restricted_signup_domains
      expose :user_oauth_applications
      expose :after_sign_out_path
430
      expose :container_registry_token_expire_delay
431
      expose :repository_storage
432
    end
D
Dmitriy Zaporozhets 已提交
433 434

    class Release < Grape::Entity
435 436
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
437
    end
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    class RepoTag < Grape::Entity
      expose :name
      expose :message do |repo_obj, _options|
        if repo_obj.respond_to?(:message)
          repo_obj.message
        else
          nil
        end
      end

      expose :commit do |repo_obj, options|
        if repo_obj.respond_to?(:commit)
          repo_obj.commit
        elsif options[:project]
          options[:project].repository.commit(repo_obj.target)
        end
      end

      expose :release, using: Entities::Release do |repo_obj, options|
        if options[:project]
          options[:project].releases.find_by(tag: repo_obj.name)
        end
      end
    end
K
Kamil Trzcinski 已提交
463 464 465 466

    class TriggerRequest < Grape::Entity
      expose :id, :variables
    end
T
Tomasz Maczukin 已提交
467

T
Tomasz Maczukin 已提交
468
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
469 470 471 472 473 474 475
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

476 477
    class RunnerDetails < Runner
      expose :tag_list
478
      expose :run_untagged
479
      expose :locked
480
      expose :version, :revision, :platform, :architecture
481
      expose :contacted_at
482
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
483
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
484
        if options[:current_user].is_admin?
485 486
          runner.projects
        else
487
          options[:current_user].authorized_projects.where(id: runner.projects)
488 489
        end
      end
490 491
    end

492 493 494 495
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

496
    class Build < Grape::Entity
T
Tomasz Maczukin 已提交
497
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
498
      expose :created_at, :started_at, :finished_at
T
Tomasz Maczukin 已提交
499
      expose :user, with: User
K
Kamil Trzcinski 已提交
500
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
501
      expose :commit, with: RepoCommit
T
Tomasz Maczukin 已提交
502
      expose :runner, with: Runner
503
    end
504

T
Tomasz Maczukin 已提交
505
    class Trigger < Grape::Entity
T
Tomasz Maczukin 已提交
506
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
T
Tomasz Maczukin 已提交
507
    end
508

509
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
510
      expose :key, :value
511
    end
512

513
    class RepoLicense < Grape::Entity
514 515
      expose :key, :name, :nickname
      expose :featured, as: :popular
516 517 518
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
519 520 521
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
522 523
      expose :content
    end
524

Z
ZJ van de Weg 已提交
525
    class TemplatesList < Grape::Entity
526 527 528
      expose :name
    end

Z
ZJ van de Weg 已提交
529
    class Template < Grape::Entity
530 531
      expose :name, :content
    end
N
Nihad Abbasov 已提交
532 533
  end
end