entities.rb 8.3 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
N
Nihad Abbasov 已提交
9
    end
N
Nihad Abbasov 已提交
10

11 12 13 14
    class User < UserBasic
      expose :created_at
      expose :is_admin?, as: :is_admin
      expose :bio, :skype, :linkedin, :twitter, :website_url
15 16
    end

17 18 19 20
    class Identity < Grape::Entity
      expose :provider, :extern_uid
    end

21 22
    class UserFull < User
      expose :email
23
      expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
24
      expose :identities, using: Entities::Identity
25 26
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
S
Stan Hu 已提交
27
      expose :two_factor_enabled
28 29
    end

30
    class UserLogin < UserFull
31
      expose :private_token
32 33
    end

M
miks 已提交
34
    class Hook < Grape::Entity
35
      expose :id, :url, :created_at
M
miks 已提交
36 37
    end

38
    class ProjectHook < Hook
39 40
      expose :project_id, :push_events
      expose :issues_events, :merge_requests_events, :tag_push_events
41 42
    end

43 44 45 46 47 48
    class ForkedFromProject < Grape::Entity
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

N
Nihad Abbasov 已提交
49
    class Project < Grape::Entity
50
      expose :id, :description, :default_branch, :tag_list
51
      expose :public?, as: :public
52
      expose :archived?, as: :archived
53
      expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
54
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
55
      expose :name, :name_with_namespace
56
      expose :path, :path_with_namespace
D
Dmitriy Zaporozhets 已提交
57
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
58
      expose :creator_id
59
      expose :namespace
60
      expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? }
S
sue445 已提交
61
      expose :avatar_url
N
Nihad Abbasov 已提交
62 63
    end

N
Nihad Abbasov 已提交
64
    class ProjectMember < UserBasic
D
Dmitriy Zaporozhets 已提交
65 66
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
N
Nihad Abbasov 已提交
67
      end
M
miks 已提交
68 69
    end

70
    class Group < Grape::Entity
71
      expose :id, :name, :path, :description
72
    end
A
Andrew8xx8 已提交
73

74
    class GroupDetail < Group
75 76 77
      expose :projects, using: Entities::Project
    end

I
Izaak Alpert 已提交
78
    class GroupMember < UserBasic
D
Dmitriy Zaporozhets 已提交
79
      expose :access_level do |user, options|
80
        options[:group].group_members.find_by(user_id: user.id).access_level
I
Izaak Alpert 已提交
81 82 83
      end
    end

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    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 已提交
103
    class RepoObject < Grape::Entity
104 105 106 107 108 109 110 111 112 113
      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

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

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

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

136 137 138 139
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    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
169
      expose :target_branch, :source_branch, :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
A
Alex Denisov 已提交
176
    end
V
Valeriy Sizov 已提交
177

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

184 185
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
186
    end
187 188

    class Note < Grape::Entity
189 190
      expose :id
      expose :note, as: :body
191
      expose :attachment_identifier, as: :attachment
192
      expose :author, using: Entities::UserBasic
193
      expose :created_at
194
    end
195 196 197 198 199

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

201 202 203 204 205 206 207 208
    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 已提交
209 210 211 212
    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
213
      expose :created_at
214 215 216 217 218 219

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
D
Dmitriy Zaporozhets 已提交
220
    end
221 222 223 224

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
225 226

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
227
      expose :access_level
228 229 230 231
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
232
      expose :access_level
233 234 235 236 237 238
      expose :notification_level
    end

    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
239
          project.project_members.find_by(user_id: options[:user].id)
240 241 242
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
243
          if project.group
244
            project.group.group_members.find_by(user_id: options[:user].id)
245
          end
246 247 248
        end
      end
    end
249 250

    class Label < Grape::Entity
R
Robert Schilling 已提交
251
      expose :name, :color
252
    end
253 254 255

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

259
      expose :commits, using: Entities::RepoCommit do |compare, options|
260
        Commit.decorate(compare.commits, nil)
261
      end
262

263 264 265
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
266 267 268 269 270 271

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

      expose :same, as: :compare_same_ref
272
    end
273 274 275 276

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
277 278 279 280

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

    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 已提交
303 304
  end
end