entities.rb 11.3 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::Application.routes.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 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
Stan Hu's avatar
Stan Hu committed
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

miks's avatar
miks committed
42
    class Hook < Grape::Entity
43
      expose :id, :url, :created_at
miks's avatar
miks committed
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 53 54 55 56 57
    class ForkedFromProject < Grape::Entity
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
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
67
      expose :shared_runners_enabled
68
      expose :creator_id
69
      expose :namespace
Gabriel Mazetto's avatar
Gabriel Mazetto committed
70
      expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ |project, options| project.forked? }
sue445's avatar
sue445 committed
71
      expose :avatar_url
72
      expose :star_count, :forks_count
Rubén Dávila's avatar
Rubén Dávila committed
73
      expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
Nihad Abbasov's avatar
Nihad Abbasov committed
74 75
    end

76
    class ProjectMember < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
77 78
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
79
      end
miks's avatar
miks committed
80 81
    end

82
    class Group < Grape::Entity
83
      expose :id, :name, :path, :description
Douwe Maan's avatar
Douwe Maan committed
84 85 86
      expose :avatar_url

      expose :web_url do |group, options|
87
        Gitlab::Application.routes.url_helpers.group_url(group)
Douwe Maan's avatar
Douwe Maan committed
88
      end
89
    end
Andrew8xx8's avatar
Andrew8xx8 committed
90

91
    class GroupDetail < Group
92 93 94
      expose :projects, using: Entities::Project
    end

Izaak Alpert's avatar
Izaak Alpert committed
95
    class GroupMember < UserBasic
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
96
      expose :access_level do |user, options|
97
        options[:group].group_members.find_by(user_id: user.id).access_level
Izaak Alpert's avatar
Izaak Alpert committed
98 99 100
      end
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
101
    class RepoObject < Grape::Entity
102 103 104 105 106 107 108 109 110 111
      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

112 113 114 115 116
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
117
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
118

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

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

134 135
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
136
      expose :status
137 138
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
139 140
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
141
      expose :author, using: Entities::UserBasic
Nihad Abbasov's avatar
Nihad Abbasov committed
142 143
      expose :expires_at, :updated_at, :created_at
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
144

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

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

157
    class Milestone < ProjectEntity
158
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
159 160
    end

161
    class Issue < ProjectEntity
162
      expose :label_names, as: :labels
163 164
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
Nihad Abbasov's avatar
Nihad Abbasov committed
165
    end
Alex Denisov's avatar
Alex Denisov committed
166

167
    class MergeRequest < ProjectEntity
Valery Sizov's avatar
Valery Sizov committed
168
      expose :target_branch, :source_branch
169
      expose :upvotes,  :downvotes
170 171
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
172
      expose :label_names, as: :labels
173
      expose :description
174
      expose :work_in_progress?, as: :work_in_progress
175
      expose :milestone, using: Entities::Milestone
176
      expose :merge_when_build_succeeds
Alex Denisov's avatar
Alex Denisov committed
177
    end
178

179 180 181 182 183 184
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
        compare.diffs
      end
    end

185 186
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
187
    end
188

189 190 191 192
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

193
    class Note < Grape::Entity
194 195
      expose :id
      expose :note, as: :body
196
      expose :attachment_identifier, as: :attachment
197
      expose :author, using: Entities::UserBasic
198
      expose :created_at
199
      expose :system?, as: :system
200
      expose :noteable_id, :noteable_type
201
      # upvote? and downvote? are deprecated, always return false
202 203
      expose :upvote?, as: :upvote
      expose :downvote?, as: :downvote
204
    end
205 206 207 208 209

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

211 212 213 214 215 216
    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
217
      expose :created_at
218 219
    end

220 221
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
222
             :created_at, :started_at, :finished_at, :allow_failure
Kamil Trzcinski's avatar
Kamil Trzcinski committed
223
      expose :author, using: Entities::UserBasic
224 225
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
226 227 228 229
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
230
      expose :created_at
231 232
      expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
      expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
233 234 235 236 237 238

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
239
    end
240 241 242 243

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
244 245

    class ProjectAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
246
      expose :access_level
247 248 249 250
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
251
      expose :access_level
252 253 254
      expose :notification_level
    end

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

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

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

    class Label < Grape::Entity
282
      expose :name, :color
283
    end
284 285 286

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

290
      expose :commits, using: Entities::RepoCommit do |compare, options|
291
        Commit.decorate(compare.commits, nil)
292
      end
293

294 295 296
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
297 298 299 300 301 302

      expose :compare_timeout do |compare, options|
        compare.timeout
      end

      expose :same, as: :compare_same_ref
303
    end
304 305 306 307

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
308 309 310 311

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

    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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
334 335

    class Release < Grape::Entity
336 337
      expose :tag, as: :tag_name
      expose :description
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
338
    end
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

    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
364 365 366 367

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

369
    class Runner < Grape::Entity
370 371 372 373 374 375 376
      expose :id
      expose :description
      expose :active
      expose :is_shared
      expose :name
    end

377
    class Build < Grape::Entity
378
      expose :id, :status, :stage, :name, :ref, :tag, :coverage
379
      expose :created_at, :started_at, :finished_at
Tomasz Maczukin's avatar
Tomasz Maczukin committed
380
      expose :user, with: User
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
      expose :download_url do |repo_obj, options|
        if options[:user_can_download_artifacts]
          repo_obj.download_url
        else
          nil
        end
      end
      expose :commit, with: RepoCommit do |repo_obj, _options|
        if repo_obj.respond_to?(:commit)
          repo_obj.commit.commit_data
        else
          nil
        end
      end
      expose :runner, with: Runner
396
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
397 398
  end
end