entities.rb 10.6 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 170
      # deprecated, always returns 0
      expose :upvotes,  :downvotes
171 172
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
173
      expose :label_names, as: :labels
174
      expose :description
175
      expose :work_in_progress?, as: :work_in_progress
176
      expose :milestone, using: Entities::Milestone
177
      expose :merge_when_build_succeeds
Alex Denisov's avatar
Alex Denisov committed
178
    end
179

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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
335 336

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

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

    class TriggerRequest < Grape::Entity
      expose :id, :variables
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
369 370
  end
end