entities.rb 8.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 11 12

      expose :web_url do |user, options|
        Rails.application.routes.url_helpers.user_url(user)
      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 48
      expose :project_id, :push_events
      expose :issues_events, :merge_requests_events, :tag_push_events
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

N
Nihad Abbasov 已提交
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
D
Dmitriy Zaporozhets 已提交
65
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
66
      expose :creator_id
67
      expose :namespace
68
      expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? }
S
sue445 已提交
69
      expose :avatar_url
70
      expose :star_count, :forks_count
N
Nihad Abbasov 已提交
71 72
    end

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

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

      expose :web_url do |group, options|
        Rails.application.routes.url_helpers.group_url(group)
      end
86
    end
A
Andrew8xx8 已提交
87

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

I
Izaak Alpert 已提交
92
    class GroupMember < UserBasic
D
Dmitriy Zaporozhets 已提交
93
      expose :access_level do |user, options|
94
        options[:group].group_members.find_by(user_id: user.id).access_level
I
Izaak Alpert 已提交
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

N
Nihad Abbasov 已提交
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
N
Nihad Abbasov 已提交
133
    end
N
Nihad Abbasov 已提交
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 152 153
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    end

N
Nihad Abbasov 已提交
154 155
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
156
      expose :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
157 158
      expose :expires_at, :updated_at, :created_at
    end
N
Nihad Abbasov 已提交
159

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

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

172
    class Milestone < ProjectEntity
173
      expose :due_date
N
Nihad Abbasov 已提交
174 175
    end

176
    class Issue < ProjectEntity
177
      expose :label_names, as: :labels
178 179
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
180
    end
A
Alex Denisov 已提交
181

182
    class MergeRequest < ProjectEntity
183
      expose :target_branch, :source_branch, :upvotes, :downvotes
184 185
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
186
      expose :label_names, as: :labels
187
      expose :description
B
Ben Boeckel 已提交
188
      expose :work_in_progress?, as: :work_in_progress
189
      expose :milestone, using: Entities::Milestone
A
Alex Denisov 已提交
190
    end
V
Valeriy Sizov 已提交
191

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

198 199
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
200
    end
201 202

    class Note < Grape::Entity
203 204
      expose :id
      expose :note, as: :body
205
      expose :attachment_identifier, as: :attachment
206
      expose :author, using: Entities::UserBasic
207
      expose :created_at
208
    end
209 210 211 212 213

    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
214

215 216 217 218 219 220 221 222
    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
    end

D
Dmitriy Zaporozhets 已提交
223 224 225 226
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
227
      expose :created_at
228 229 230 231 232 233

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
D
Dmitriy Zaporozhets 已提交
234
    end
235 236 237 238

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
239 240

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
241
      expose :access_level
242 243 244 245
      expose :notification_level
    end

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

    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
253
          project.project_members.find_by(user_id: options[:user].id)
254 255 256
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
257
          if project.group
258
            project.group.group_members.find_by(user_id: options[:user].id)
259
          end
260 261 262
        end
      end
    end
263 264

    class Label < Grape::Entity
R
Robert Schilling 已提交
265
      expose :name, :color
266
    end
267 268 269

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

273
      expose :commits, using: Entities::RepoCommit do |compare, options|
274
        Commit.decorate(compare.commits, nil)
275
      end
276

277 278 279
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
280 281 282 283 284 285

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

      expose :same, as: :compare_same_ref
286
    end
287 288 289 290

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
291 292 293 294

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

    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
N
Nihad Abbasov 已提交
317 318
  end
end