entities.rb 12.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|
D
Douwe Maan 已提交
11
        Gitlab::Application.routes.url_helpers.user_url(user)
D
Douwe Maan 已提交
12
      end
N
Nihad Abbasov 已提交
13
    end
N
Nihad Abbasov 已提交
14

15 16 17 18
    class User < UserBasic
      expose :created_at
      expose :is_admin?, as: :is_admin
      expose :bio, :skype, :linkedin, :twitter, :website_url
19 20
    end

21 22 23 24
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

25 26
    class UserFull < User
      expose :email
27
      expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
28
      expose :identities, using: Entities::Identity
29 30
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
S
Stan Hu 已提交
31
      expose :two_factor_enabled
32 33
    end

34
    class UserLogin < UserFull
35
      expose :private_token
36 37
    end

38 39 40 41
    class Email < Grape::Entity
      expose :id, :email
    end

M
miks 已提交
42
    class Hook < Grape::Entity
43
      expose :id, :url, :created_at
M
miks 已提交
44 45
    end

46
    class ProjectHook < Hook
47
      expose :project_id, :push_events
48 49
      expose :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
      expose :enable_ssl_verification
50 51
    end

52
    class BasicProjectDetails < Grape::Entity
53 54 55 56 57
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

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

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

84
    class Group < Grape::Entity
85
      expose :id, :name, :path, :description
D
Douwe Maan 已提交
86 87 88
      expose :avatar_url

      expose :web_url do |group, options|
D
Douwe Maan 已提交
89
        Gitlab::Application.routes.url_helpers.group_url(group)
D
Douwe Maan 已提交
90
      end
91
    end
A
Andrew8xx8 已提交
92

93
    class GroupDetail < Group
94 95 96
      expose :projects, using: Entities::Project
    end

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

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

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

121 122 123 124 125 126 127 128 129 130
    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

131 132
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
133
      expose :safe_message, as: :message
134 135
    end

136 137
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
K
Kamil Trzcinski 已提交
138
      expose :status
139 140
    end

N
Nihad Abbasov 已提交
141 142
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
143
      expose :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
144 145
      expose :expires_at, :updated_at, :created_at
    end
N
Nihad Abbasov 已提交
146

147 148
    class ProjectEntity < Grape::Entity
      expose :id, :iid
149
      expose(:project_id) { |entity| entity.project.id }
150 151
      expose :title, :description
      expose :state, :created_at, :updated_at
152 153
    end

154 155 156 157 158
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

159
    class Milestone < ProjectEntity
160
      expose :due_date
N
Nihad Abbasov 已提交
161 162
    end

163
    class Issue < ProjectEntity
164
      expose :label_names, as: :labels
165 166
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
167
    end
A
Alex Denisov 已提交
168

169
    class MergeRequest < ProjectEntity
V
Valery Sizov 已提交
170
      expose :target_branch, :source_branch
171
      expose :upvotes,  :downvotes
172 173
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
174
      expose :label_names, as: :labels
175
      expose :description
B
Ben Boeckel 已提交
176
      expose :work_in_progress?, as: :work_in_progress
177
      expose :milestone, using: Entities::Milestone
178
      expose :merge_when_build_succeeds
179
      expose :merge_status
A
Alex Denisov 已提交
180
    end
V
Valeriy Sizov 已提交
181

182 183
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
J
Jacob Vosmaer 已提交
184
        compare.diffs(all_diffs: true).to_a
185 186 187
      end
    end

188 189
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
190
    end
191

192 193 194 195
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

196
    class Note < Grape::Entity
197 198
      expose :id
      expose :note, as: :body
199
      expose :attachment_identifier, as: :attachment
200
      expose :author, using: Entities::UserBasic
201
      expose :created_at
202
      expose :system?, as: :system
D
Dmitriy Zaporozhets 已提交
203
      expose :noteable_id, :noteable_type
204
      # upvote? and downvote? are deprecated, always return false
205 206
      expose :upvote?, as: :upvote
      expose :downvote?, as: :downvote
207
    end
208 209 210 211 212

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

214 215 216 217 218 219
    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
220
      expose :created_at
221 222
    end

K
Kamil Trzcinski 已提交
223 224
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
225
             :created_at, :started_at, :finished_at, :allow_failure
K
Kamil Trzcinski 已提交
226
      expose :author, using: Entities::UserBasic
K
Kamil Trzcinski 已提交
227 228
    end

D
Dmitriy Zaporozhets 已提交
229 230 231 232
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
233
      expose :created_at
D
Dmitriy Zaporozhets 已提交
234 235
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
236 237 238 239 240 241

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
D
Dmitriy Zaporozhets 已提交
242
    end
243 244 245 246

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
247 248

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
249
      expose :access_level
250 251 252 253
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
254
      expose :access_level
255 256 257
      expose :notification_level
    end

258 259
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
260
      expose :push_events, :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
261 262 263 264 265 266 267 268 269
      # 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

270 271 272
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
273
          project.project_members.find_by(user_id: options[:user].id)
274 275 276
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
277
          if project.group
278
            project.group.group_members.find_by(user_id: options[:user].id)
279
          end
280 281 282
        end
      end
    end
283 284

    class Label < Grape::Entity
R
Robert Schilling 已提交
285
      expose :name, :color
286
    end
287 288 289

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

293
      expose :commits, using: Entities::RepoCommit do |compare, options|
294
        Commit.decorate(compare.commits, nil)
295
      end
296

297
      expose :diffs, using: Entities::RepoDiff do |compare, options|
J
Jacob Vosmaer 已提交
298
        compare.diffs(all_diffs: true).to_a
299
      end
300 301

      expose :compare_timeout do |compare, options|
J
Jacob Vosmaer 已提交
302
        compare.diffs.overflow?
303 304 305
      end

      expose :same, as: :compare_same_ref
306
    end
307 308 309 310

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
311 312 313 314

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

    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 :twitter_sharing_enabled
      expose :restricted_visibility_levels
      expose :max_attachment_size
      expose :session_expire_delay
      expose :default_project_visibility
      expose :default_snippet_visibility
      expose :restricted_signup_domains
      expose :user_oauth_applications
      expose :after_sign_out_path
    end
D
Dmitriy Zaporozhets 已提交
337 338

    class Release < Grape::Entity
339 340
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
341
    end
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

    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 已提交
367 368 369 370

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

T
Tomasz Maczukin 已提交
372
    class Runner < Grape::Entity
T
Tomasz Maczukin 已提交
373 374 375 376 377 378 379
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

380 381 382
    class RunnerDetails < Runner
      expose :tag_list
      expose :version, :revision, :platform, :architecture
383
      expose :contacted_at
384
      expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
385
      expose :projects, with: Entities::BasicProjectDetails do |runner, options|
386
        if options[:current_user].is_admin?
387 388
          runner.projects
        else
389
          options[:current_user].authorized_projects.where(id: runner.projects)
390 391
        end
      end
392 393
    end

394 395 396 397
    class BuildArtifactFile < Grape::Entity
      expose :filename, :size
    end

398
    class Build < Grape::Entity
T
Tomasz Maczukin 已提交
399
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
T
Tomasz Maczukin 已提交
400
      expose :created_at, :started_at, :finished_at
T
Tomasz Maczukin 已提交
401
      expose :user, with: User
T
Tomasz Maczukin 已提交
402 403
      # TODO: download_url in Ci:Build model is an GitLab Web Interface URL, not API URL. We should think on some API
      #       for downloading of artifacts (see: https://gitlab.com/gitlab-org/gitlab-ce/issues/4255)
T
Tomasz Maczukin 已提交
404 405
      expose :download_url do |repo_obj, options|
        if options[:user_can_download_artifacts]
406
          repo_obj.artifacts_download_url
T
Tomasz Maczukin 已提交
407 408
        end
      end
K
Kamil Trzcinski 已提交
409
      expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
T
Tomasz Maczukin 已提交
410 411 412 413 414 415
      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
416
    end
417

T
Tomasz Maczukin 已提交
418
    class Trigger < Grape::Entity
T
Tomasz Maczukin 已提交
419
      expose :token, :created_at, :updated_at, :deleted_at, :last_used
T
Tomasz Maczukin 已提交
420
    end
421

422
    class Variable < Grape::Entity
T
Tomasz Maczukin 已提交
423
      expose :key, :value
424
    end
N
Nihad Abbasov 已提交
425 426
  end
end