entities.rb 13.6 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
S
Stan Hu 已提交
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

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 57 58 59 60
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

N
Nihad Abbasov 已提交
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, :created_at, :last_activity_at
K
Kamil Trzcinski 已提交
70
      expose :shared_runners_enabled
71
      expose :creator_id
72
      expose :namespace
73
      expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
S
sue445 已提交
74
      expose :avatar_url
75
      expose :star_count, :forks_count
R
Rubén Dávila 已提交
76
      expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
77
      expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
78
      expose :public_builds
N
Nihad Abbasov 已提交
79 80
    end

N
Nihad Abbasov 已提交
81
    class ProjectMember < UserBasic
D
Dmitriy Zaporozhets 已提交
82 83
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
N
Nihad Abbasov 已提交
84
      end
M
miks 已提交
85 86
    end

87
    class Group < Grape::Entity
88
      expose :id, :name, :path, :description, :visibility_level
D
Douwe Maan 已提交
89 90 91
      expose :avatar_url

      expose :web_url do |group, options|
92
        Gitlab::Routing.url_helpers.group_url(group)
D
Douwe Maan 已提交
93
      end
94
    end
A
Andrew8xx8 已提交
95

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

I
Izaak Alpert 已提交
100
    class GroupMember < UserBasic
D
Dmitriy Zaporozhets 已提交
101
      expose :access_level do |user, options|
102
        options[:group].group_members.find_by(user_id: user.id).access_level
I
Izaak Alpert 已提交
103 104 105
      end
    end

N
Nihad Abbasov 已提交
106
    class RepoObject < Grape::Entity
107 108 109 110 111 112 113 114 115 116
      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

117 118 119 120 121
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
N
Nihad Abbasov 已提交
122
    end
N
Nihad Abbasov 已提交
123

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

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

139 140
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
K
Kamil Trzcinski 已提交
141
      expose :status
142 143
    end

N
Nihad Abbasov 已提交
144 145
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
146
      expose :author, using: Entities::UserBasic
R
Robert Speicher 已提交
147
      expose :updated_at, :created_at
148 149 150

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

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

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

165
    class Milestone < ProjectEntity
166
      expose :due_date
N
Nihad Abbasov 已提交
167 168
    end

169
    class Issue < ProjectEntity
170
      expose :label_names, as: :labels
171 172
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
173 174 175
      expose :subscribed do |issue, options|
        issue.subscribed?(options[:current_user])
      end
C
cnam-dep 已提交
176
      expose :user_notes_count
N
Nihad Abbasov 已提交
177
    end
A
Alex Denisov 已提交
178

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

196 197
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
J
Jacob Vosmaer 已提交
198
        compare.diffs(all_diffs: true).to_a
199 200 201
      end
    end

202 203
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
204
    end
205

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

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

    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
227

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

K
Kamil Trzcinski 已提交
237 238
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
239
             :created_at, :started_at, :finished_at, :allow_failure
K
Kamil Trzcinski 已提交
240
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
241 242
    end

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

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

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

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

266
    class Member < Grape::Entity
D
Dmitriy Zaporozhets 已提交
267
      expose :access_level
268 269 270 271 272
      expose :notification_level do |member, options|
        if member.notification_setting
          NotificationSetting.levels[member.notification_setting.level]
        end
      end
273 274
    end

275 276 277 278
    class ProjectAccess < Member
    end

    class GroupAccess < Member
279 280
    end

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

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

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

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

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

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

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

325
      expose :diffs, using: Entities::RepoDiff do |compare, options|
J
Jacob Vosmaer 已提交
326
        compare.diffs(all_diffs: true).to_a
327
      end
328 329

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
330
        compare.diffs.overflow?
331 332 333
      end

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

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
339 340 341 342

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

    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
360
      expose :default_group_visibility
361 362 363 364
      expose :restricted_signup_domains
      expose :user_oauth_applications
      expose :after_sign_out_path
    end
D
Dmitriy Zaporozhets 已提交
365 366

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

    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 已提交
395 396 397 398

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

T
Tomasz Maczukin 已提交
400
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
401 402 403 404 405 406 407
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

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

422 423 424 425
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

426
    class Build < Grape::Entity
T
Tomasz Maczukin 已提交
427
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
428
      expose :created_at, :started_at, :finished_at
T
Tomasz Maczukin 已提交
429
      expose :user, with: User
K
Kamil Trzcinski 已提交
430
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
T
Tomasz Maczukin 已提交
431 432 433 434 435 436
      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
437
    end
438

T
Tomasz Maczukin 已提交
439
    class Trigger < Grape::Entity
T
Tomasz Maczukin 已提交
440
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
T
Tomasz Maczukin 已提交
441
    end
442

443
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
444
      expose :key, :value
445
    end
446

447
    class RepoLicense < Grape::Entity
448 449
      expose :key, :name, :nickname
      expose :featured, as: :popular
450 451 452
      expose :url, as: :html_url
      expose(:source_url) { |license| license.meta['source'] }
      expose(:description) { |license| license.meta['description'] }
453 454 455
      expose(:conditions) { |license| license.meta['conditions'] }
      expose(:permissions) { |license| license.meta['permissions'] }
      expose(:limitations) { |license| license.meta['limitations'] }
456 457
      expose :content
    end
N
Nihad Abbasov 已提交
458 459
  end
end