entities.rb 9.69 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
      expose :issues_events, :merge_requests_events, :tag_push_events, :note_events, :enable_ssl_verification
49 50
    end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
57
    class Project < Grape::Entity
58
      expose :id, :description, :default_branch, :tag_list
59
      expose :public?, as: :public
60
      expose :archived?, as: :archived
61
      expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
62
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
63
      expose :name, :name_with_namespace
64
      expose :path, :path_with_namespace
65
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
66
      expose :creator_id
67
      expose :namespace
Kamil Trzcinski's avatar
Kamil Trzcinski committed
68
      expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? }
sue445's avatar
sue445 committed
69
      expose :avatar_url
70
      expose :star_count, :forks_count
Nihad Abbasov's avatar
Nihad Abbasov committed
71 72
    end

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

79
    class Group < Grape::Entity
80
      expose :id, :name, :path, :description
Douwe Maan's avatar
Douwe Maan committed
81 82 83
      expose :avatar_url

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

88
    class GroupDetail < Group
89 90 91
      expose :projects, using: Entities::Project
    end

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

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    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
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
117
    class RepoObject < Grape::Entity
118 119 120 121 122 123 124 125 126 127
      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

128 129 130 131 132
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
133
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
134

135 136 137 138 139 140 141 142 143 144
    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

145 146
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
147
      expose :safe_message, as: :message
148 149
    end

150 151
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
152
      expose :status
153 154
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
155 156
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
157
      expose :author, using: Entities::UserBasic
Nihad Abbasov's avatar
Nihad Abbasov committed
158 159
      expose :expires_at, :updated_at, :created_at
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
160

161 162
    class ProjectEntity < Grape::Entity
      expose :id, :iid
163
      expose(:project_id) { |entity| entity.project.id }
164 165
      expose :title, :description
      expose :state, :created_at, :updated_at
166 167
    end

168 169 170 171 172
    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

173
    class Milestone < ProjectEntity
174
      expose :due_date
Nihad Abbasov's avatar
Nihad Abbasov committed
175 176
    end

177
    class Issue < ProjectEntity
178
      expose :label_names, as: :labels
179 180
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
Nihad Abbasov's avatar
Nihad Abbasov committed
181
    end
Alex Denisov's avatar
Alex Denisov committed
182

183
    class MergeRequest < ProjectEntity
184
      expose :target_branch, :source_branch, :upvotes, :downvotes
185 186
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
187
      expose :label_names, as: :labels
188
      expose :description
189
      expose :work_in_progress?, as: :work_in_progress
190
      expose :milestone, using: Entities::Milestone
Alex Denisov's avatar
Alex Denisov committed
191
    end
192

193 194 195 196 197 198
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
        compare.diffs
      end
    end

199 200
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
201
    end
202

203 204 205 206
    class SSHKeyWithUser < SSHKey
      expose :user, using: Entities::UserFull
    end

207
    class Note < Grape::Entity
208 209
      expose :id
      expose :note, as: :body
210
      expose :attachment_identifier, as: :attachment
211
      expose :author, using: Entities::UserBasic
212
      expose :created_at
213 214 215
      expose :system?, as: :system
      expose :upvote?, as: :upvote
      expose :downvote?, as: :downvote
216
    end
217 218 219 220 221

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

223 224 225 226 227 228
    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
229
      expose :created_at
230 231
    end

232 233 234
    class CommitStatus < Grape::Entity
      expose :id, :sha, :ref, :status, :name, :target_url, :description,
             :created_at, :started_at, :finished_at
Kamil Trzcinski's avatar
Kamil Trzcinski committed
235
      expose :author, using: Entities::UserBasic
236 237
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
238 239 240 241
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
242
      expose :created_at
243 244 245 246 247 248

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
249
    end
250 251 252 253

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
254 255

    class ProjectAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
256
      expose :access_level
257 258 259 260
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
261
      expose :access_level
262 263 264
      expose :notification_level
    end

265 266 267 268 269 270 271 272 273 274 275 276
    class ProjectService < Grape::Entity
      expose :id, :title, :created_at, :updated_at, :active
      expose :push_events, :issues_events, :merge_requests_events, :tag_push_events, :note_events
      # 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

277 278 279
    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
280
          project.project_members.find_by(user_id: options[:user].id)
281 282 283
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
284
          if project.group
285
            project.group.group_members.find_by(user_id: options[:user].id)
286
          end
287 288 289
        end
      end
    end
290 291

    class Label < Grape::Entity
292
      expose :name, :color
293
    end
294 295 296

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

300
      expose :commits, using: Entities::RepoCommit do |compare, options|
301
        Commit.decorate(compare.commits, nil)
302
      end
303

304 305 306
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
307 308 309 310 311 312

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

      expose :same, as: :compare_same_ref
313
    end
314 315 316 317

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
318 319 320 321

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

    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
Nihad Abbasov's avatar
Nihad Abbasov committed
344 345
  end
end