entities.rb 7.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
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 24
      expose :theme_id, :color_scheme_id, :projects_limit
      expose :identities, using: Entities::Identity
25 26
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
27 28
    end

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

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

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

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

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

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

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

72
    class GroupDetail < Group
73 74 75
      expose :projects, using: Entities::Project
    end

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

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

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

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

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

134 135 136 137
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    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
167
      expose :target_branch, :source_branch, :upvotes, :downvotes
168 169
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
170
      expose :label_names, as: :labels
171 172
      expose :description
      expose :milestone, using: Entities::Milestone
A
Alex Denisov 已提交
173
    end
V
Valeriy Sizov 已提交
174

175 176 177 178 179 180
    class MergeRequestChanges < MergeRequest
      expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
        compare.diffs
      end
    end

181 182
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
183
    end
184 185

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

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

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

      expose :author_username do |event, options|
        if event.author
          event.author.username
        end
      end
D
Dmitriy Zaporozhets 已提交
217
    end
218 219 220 221

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
222 223

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
224
      expose :access_level
225 226 227 228
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
229
      expose :access_level
230 231 232 233 234 235
      expose :notification_level
    end

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

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

    class Label < Grape::Entity
R
Robert Schilling 已提交
248
      expose :name, :color
249
    end
250 251 252

    class Compare < Grape::Entity
      expose :commit, using: Entities::RepoCommit do |compare, options|
253
        Commit.decorate(compare.commits).last
254
      end
255

256
      expose :commits, using: Entities::RepoCommit do |compare, options|
257
        Commit.decorate(compare.commits)
258
      end
259

260 261 262
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
263 264 265 266 267 268

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

      expose :same, as: :compare_same_ref
269
    end
270 271 272 273

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
D
Douwe Maan 已提交
274 275 276 277

    class BroadcastMessage < Grape::Entity
      expose :message, :starts_at, :ends_at, :color, :font
    end
N
Nihad Abbasov 已提交
278 279
  end
end