entities.rb 10.7 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
G
Gabriel Mazetto 已提交
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
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] }
N
Nihad Abbasov 已提交
75 76
    end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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