entities.rb 6.2 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 21
    class UserFull < User
      expose :email
      expose :theme_id, :color_scheme_id, :extern_uid, :provider
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
22 23
    end

24
    class UserLogin < UserFull
25
      expose :private_token
26 27
    end

M
miks 已提交
28
    class Hook < Grape::Entity
29
      expose :id, :url, :created_at
M
miks 已提交
30 31
    end

32 33 34 35
    class ProjectHook < Hook
      expose :project_id, :push_events, :issues_events, :merge_requests_events
    end

36 37 38 39 40 41
    class ForkedFromProject < Grape::Entity
      expose :id
      expose :name, :name_with_namespace
      expose :path, :path_with_namespace
    end

N
Nihad Abbasov 已提交
42
    class Project < Grape::Entity
43 44
      expose :id, :description, :default_branch
      expose :public?, as: :public
45
      expose :archived?, as: :archived
46
      expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
47
      expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
48
      expose :name, :name_with_namespace
49
      expose :path, :path_with_namespace
D
Dmitriy Zaporozhets 已提交
50
      expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
51
      expose :namespace
52
      expose :forked_from_project, using: Entities::ForkedFromProject, :if => lambda{ | project, options | project.forked? }
N
Nihad Abbasov 已提交
53 54
    end

N
Nihad Abbasov 已提交
55
    class ProjectMember < UserBasic
D
Dmitriy Zaporozhets 已提交
56 57
      expose :access_level do |user, options|
        options[:project].project_members.find_by(user_id: user.id).access_level
N
Nihad Abbasov 已提交
58
      end
M
miks 已提交
59 60
    end

61
    class Group < Grape::Entity
62
      expose :id, :name, :path, :owner_id
63
    end
A
Andrew8xx8 已提交
64

65
    class GroupDetail < Group
66 67 68
      expose :projects, using: Entities::Project
    end

I
Izaak Alpert 已提交
69
    class GroupMember < UserBasic
D
Dmitriy Zaporozhets 已提交
70
      expose :access_level do |user, options|
71
        options[:group].group_members.find_by(user_id: user.id).group_access
I
Izaak Alpert 已提交
72 73 74
      end
    end

N
Nihad Abbasov 已提交
75
    class RepoObject < Grape::Entity
76 77 78 79 80 81 82 83 84 85
      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

86 87 88 89 90
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
N
Nihad Abbasov 已提交
91
    end
N
Nihad Abbasov 已提交
92

93 94 95 96 97 98 99 100 101 102
    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

103 104
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
105
      expose :safe_message, as: :message
106 107
    end

108 109 110 111
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    end

N
Nihad Abbasov 已提交
112 113
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
114
      expose :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
115 116
      expose :expires_at, :updated_at, :created_at
    end
N
Nihad Abbasov 已提交
117

118 119 120
    class ProjectEntity < Grape::Entity
      expose :id, :iid
      expose (:project_id) { |entity| entity.project.id }
121 122
      expose :title, :description
      expose :state, :created_at, :updated_at
123 124 125
    end

    class Milestone < ProjectEntity
126
      expose :due_date
N
Nihad Abbasov 已提交
127 128
    end

129
    class Issue < ProjectEntity
130
      expose :label_names, as: :labels
131 132
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
133
    end
A
Alex Denisov 已提交
134

135
    class MergeRequest < ProjectEntity
136
      expose :target_branch, :source_branch, :upvotes, :downvotes
137 138
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
139
      expose :label_names, as: :labels
140 141
      expose :description
      expose :milestone, using: Entities::Milestone
A
Alex Denisov 已提交
142
    end
V
Valeriy Sizov 已提交
143

144 145
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
146
    end
147 148

    class Note < Grape::Entity
149 150
      expose :id
      expose :note, as: :body
151
      expose :attachment_identifier, as: :attachment
152
      expose :author, using: Entities::UserBasic
153
      expose :created_at
154
    end
155 156 157 158 159

    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
160 161 162 163 164

    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
165
      expose :created_at
D
Dmitriy Zaporozhets 已提交
166
    end
167 168 169 170

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
171 172

    class ProjectAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
173
      expose :access_level
174 175 176 177
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
D
Dmitriy Zaporozhets 已提交
178
      expose :access_level
179 180 181 182 183 184
      expose :notification_level
    end

    class ProjectWithAccess < Project
      expose :permissions do
        expose :project_access, using: Entities::ProjectAccess do |project, options|
185
          project.project_members.find_by(user_id: options[:user].id)
186 187 188
        end

        expose :group_access, using: Entities::GroupAccess do |project, options|
189
          if project.group
190
            project.group.group_members.find_by(user_id: options[:user].id)
191
          end
192 193 194
        end
      end
    end
195 196

    class Label < Grape::Entity
R
Robert Schilling 已提交
197
      expose :name, :color
198
    end
199 200 201 202 203 204 205 206

    class RepoDiff < Grape::Entity
      expose :old_path, :new_path, :a_mode, :b_mode, :diff
      expose :new_file, :renamed_file, :deleted_file
    end

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

210
      expose :commits, using: Entities::RepoCommit do |compare, options|
211
        Commit.decorate(compare.commits)
212
      end
213

214 215 216
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
217 218 219 220 221 222

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

      expose :same, as: :compare_same_ref
223
    end
224 225 226 227

    class Contributor < Grape::Entity
      expose :name, :email, :commits, :additions, :deletions
    end
N
Nihad Abbasov 已提交
228 229
  end
end