entities.rb 10.5 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 53 54 55 56 57
    class ForkedFromProject < Grape::Entity
      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
K
Kamil Trzcinski 已提交
70
      expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? }
S
sue445 已提交
71
      expose :avatar_url
72
      expose :star_count, :forks_count
N
Nihad Abbasov 已提交
73 74
    end

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

81
    class Group < Grape::Entity
82
      expose :id, :name, :path, :description
D
Douwe Maan 已提交
83 84 85
      expose :avatar_url

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

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

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

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

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

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

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

133 134
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
K
Kamil Trzcinski 已提交
135
      expose :status
136 137
    end

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

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

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

156
    class Milestone < ProjectEntity
157
      expose :due_date
N
Nihad Abbasov 已提交
158 159
    end

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

166
    class MergeRequest < ProjectEntity
V
Valery Sizov 已提交
167
      expose :target_branch, :source_branch
168 169
      # deprecated, always returns 0
      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
B
Ben Boeckel 已提交
174
      expose :work_in_progress?, as: :work_in_progress
175
      expose :milestone, using: Entities::Milestone
176
      expose :merge_when_build_succeeds
A
Alex Denisov 已提交
177
    end
V
Valeriy Sizov 已提交
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
V
Valeriy Sizov 已提交
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
D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
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

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

D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
239
    end
240 241 242 243

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

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
246
      expose :access_level
247 248 249 250
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
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
R
Robert Schilling 已提交
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
D
Douwe Maan 已提交
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
D
Dmitriy Zaporozhets 已提交
334 335

    class Release < Grape::Entity
336 337
      expose :tag, as: :tag_name
      expose :description
D
Dmitriy Zaporozhets 已提交
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
K
Kamil Trzcinski 已提交
364 365 366 367

    class TriggerRequest < Grape::Entity
      expose :id, :variables
    end
N
Nihad Abbasov 已提交
368 369
  end
end