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 50
      expose :id, :description, :default_branch
      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? }
N
Nihad Abbasov 已提交
59 60
    end

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

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

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

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

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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 已提交
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 135 136
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    end

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

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

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

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

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

165
    class MergeRequest < ProjectEntity
166
      expose :target_branch, :source_branch, :upvotes, :downvotes
167 168
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
169
      expose :label_names, as: :labels
170 171
      expose :description
      expose :milestone, using: Entities::Milestone
A
Alex Denisov 已提交
172
    end
V
Valeriy Sizov 已提交
173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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