entities.rb 13.9 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
Stan Hu's avatar
Stan Hu committed
33
      expose :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 57 58 59 60
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

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

82
    class ProjectMember < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83 84
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
85
      end
miks's avatar
miks committed
86 87
    end

88
    class Group < Grape::Entity
89
      expose :id, :name, :path, :description, :visibility_level
Douwe Maan's avatar
Douwe Maan committed
90 91 92
      expose :avatar_url

      expose :web_url do |group, options|
93
        Gitlab::Routing.url_helpers.group_url(group)
Douwe Maan's avatar
Douwe Maan committed
94
      end
95
    end
Andrew8xx8's avatar
Andrew8xx8 committed
96

97
    class GroupDetail < Group
98 99 100
      expose :projects, using: Entities::Project
    end

Izaak Alpert's avatar
Izaak Alpert committed
101
    class GroupMember < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
102
      expose :access_level do |user, options|
103
        options[:group].group_members.find_by(user_id: user.id).access_level
Izaak Alpert's avatar
Izaak Alpert committed
104 105 106
      end
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
107
    class RepoObject < Grape::Entity
108 109 110 111 112 113 114 115 116 117
      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

118 119 120 121 122
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
123
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
124

125 126 127 128 129 130 131 132 133 134
    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

135 136
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
137
      expose :safe_message, as: :message
138 139
    end

140 141
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
142
      expose :status
143 144
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
145 146
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
147
      expose :author, using: Entities::UserBasic
148
      expose :updated_at, :created_at
149 150 151

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

154 155
    class ProjectEntity < Grape::Entity
      expose :id, :iid
156
      expose(:project_id) { |entity| entity.project.id }
157 158
      expose :title, :description
      expose :state, :created_at, :updated_at
159 160
    end

161 162 163 164 165
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

166
    class Milestone < ProjectEntity
167
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
168 169
    end

170
    class Issue < ProjectEntity
171
      expose :label_names, as: :labels
172 173
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
174 175 176
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
177
      expose :user_notes_count
Nihad Abbasov's avatar
Nihad Abbasov committed
178
    end
Alex Denisov's avatar
Alex Denisov committed
179

180
    class MergeRequest < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
181
      expose :target_branch, :source_branch
182
      expose :upvotes,  :downvotes
183 184
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
185
      expose :label_names, as: :labels
186
      expose :description
187
      expose :work_in_progress?, as: :work_in_progress
188
      expose :milestone, using: Entities::Milestone
189
      expose :merge_when_build_succeeds
190
      expose :merge_status
191 192 193
      expose :subscribed do |merge_request, options|
        merge_request.subscribed?(options[:current_user])
      end
194
      expose :user_notes_count
Alex Denisov's avatar
Alex Denisov committed
195
    end
196

197 198
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
199
        compare.diffs(all_diffs: true).to_a
200 201 202
      end
    end

203 204
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
205
    end
206

207 208 209 210
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

211
    class Note < Grape::Entity
212 213
      expose :id
      expose :note, as: :body
214
      expose :attachment_identifier, as: :attachment
215
      expose :author, using: Entities::UserBasic
216
      expose :created_at, :updated_at
217
      expose :system?, as: :system
218
      expose :noteable_id, :noteable_type
219
      # upvote? and downvote? are deprecated, always return false
220 221
      expose :upvote?, as: :upvote
      expose :downvote?, as: :downvote
222
    end
223 224 225 226 227

    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
228

229 230
    class CommitNote < Grape::Entity
      expose :note
231
      expose(:path) { |note| note.diff_file_path if note.legacy_diff_note? }
232 233
      expose(:line) { |note| note.diff_new_line if note.legacy_diff_note? }
      expose(:line_type) { |note| note.diff_line_type if note.legacy_diff_note? }
234
      expose :author, using: Entities::UserBasic
235
      expose :created_at
236 237
    end

238 239
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
240
             :created_at, :started_at, :finished_at, :allow_failure
Kamil Trzcinski's avatar
Kamil Trzcinski committed
241
      expose :author, using: Entities::UserBasic
242 243
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
244 245 246 247
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
248
      expose :created_at
249 250
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
251 252 253 254 255 256

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

259 260 261 262
    class ProjectGroupLink < Grape::Entity
      expose :id, :project_id, :group_id, :group_access
    end

263 264 265
    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
266

267
    class Member < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
268
      expose :access_level
269 270 271 272 273
      expose :notification_level do |member, options|
        if member.notification_setting
          NotificationSetting.levels[member.notification_setting.level]
        end
      end
274 275
    end

276 277 278 279
    class ProjectAccess < Member
    end

    class GroupAccess < Member
280 281
    end

282 283
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
284
      expose :push_events, :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
285 286 287 288 289 290 291 292 293
      # 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

294 295 296
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
297
          project.project_members.find_by(user_id: options[:user].id)
298 299 300
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
301
          if project.group
302
            project.group.group_members.find_by(user_id: options[:user].id)
303
          end
304 305 306
        end
      end
    end
307 308

    class Label < Grape::Entity
309
      expose :name, :color, :description
310
      expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
311 312 313 314

      expose :subscribed do |label, options|
        label.subscribed?(options[:current_user])
      end
315
    end
316 317 318

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

322
      expose :commits, using: Entities::RepoCommit do |compare, options|
323
        Commit.decorate(compare.commits, nil)
324
      end
325

326
      expose :diffs, using: Entities::RepoDiff do |compare, options|
327
        compare.diffs(all_diffs: true).to_a
328
      end
329 330

      expose :compare_timeout do |compare, options|
331
        compare.diffs.overflow?
332 333 334
      end

      expose :same, as: :compare_same_ref
335
    end
336 337 338 339

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
340 341 342 343

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

    class ApplicationSetting < Grape::Entity
      expose :id
      expose :default_projects_limit
      expose :signup_enabled
      expose :signin_enabled
      expose :gravatar_enabled
      expose :sign_in_text
      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
361
      expose :default_group_visibility
362 363 364 365
      expose :restricted_signup_domains
      expose :user_oauth_applications
      expose :after_sign_out_path
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
366 367

    class Release < Grape::Entity
368 369
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
370
    end
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

    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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
396 397 398 399

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

401
    class Runner < Grape::Entity
402 403 404 405 406 407 408
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

409 410
    class RunnerDetails < Runner
      expose :tag_list
411
      expose :run_untagged
412
      expose :version, :revision, :platform, :architecture
413
      expose :contacted_at
414
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
415
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
416
        if options[:current_user].is_admin?
417 418
          runner.projects
        else
419
          options[:current_user].authorized_projects.where(id: runner.projects)
420 421
        end
      end
422 423
    end

424 425 426 427
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

428
    class Build < Grape::Entity
429
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
430
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
431
      expose :user, with: User
Kamil Trzcinski's avatar
Kamil Trzcinski committed
432
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
433 434 435 436 437 438
      expose :commit, with: RepoCommit do |repo_obj, _options|
        if repo_obj.respond_to?(:commit)
          repo_obj.commit.commit_data
        end
      end
      expose :runner, with: Runner
439
    end
440

441
    class Trigger < Grape::Entity
442
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
443
    end
444

445
    class Variable < Grape::Entity
Tomasz Maczukin's avatar
Tomasz Maczukin committed
446
      expose :key, :value
447
    end
448

449
    class RepoLicense < Grape::Entity
450 451
      expose :key, :name, :nickname
      expose :featured, as: :popular
452 453 454
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
455 456 457
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
458 459
      expose :content
    end
460 461 462 463 464 465 466 467

    class GitignoresList < Grape::Entity
      expose :name
    end

    class Gitignore < Grape::Entity
      expose :name, :content
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
468 469
  end
end