entities.rb 6.2 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2 3
  module Entities
    class User < Grape::Entity
J
Jerome Dalbert 已提交
4
      expose :id, :username, :email, :name, :bio, :skype, :linkedin, :twitter, :website_url,
5
             :theme_id, :color_scheme_id, :state, :created_at, :extern_uid, :provider
6 7 8
      expose :is_admin?, as: :is_admin
      expose :can_create_group?, as: :can_create_group
      expose :can_create_project?, as: :can_create_project
9 10 11 12 13 14

      expose :avatar_url do |user, options|
        if user.avatar.present?
          user.avatar.url
        end
      end
N
Nihad Abbasov 已提交
15
    end
N
Nihad Abbasov 已提交
16

17
    class UserSafe < Grape::Entity
18
      expose :name, :username
19 20
    end

21
    class UserBasic < Grape::Entity
22
      expose :id, :username, :email, :name, :state, :created_at
23 24
    end

A
Alex Denisov 已提交
25
    class UserLogin < User
26
      expose :private_token
27 28
    end

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

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

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

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

N
Nihad Abbasov 已提交
56
    class ProjectMember < UserBasic
57
      expose :project_access, as: :access_level do |user, options|
S
skv 已提交
58
        options[:project].users_projects.find_by(user_id: user.id).project_access
N
Nihad Abbasov 已提交
59
      end
M
miks 已提交
60 61
    end

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

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

I
Izaak Alpert 已提交
70 71
    class GroupMember < UserBasic
      expose :group_access, as: :access_level do |user, options|
S
skv 已提交
72
        options[:group].users_groups.find_by(user_id: user.id).group_access
I
Izaak Alpert 已提交
73 74 75
      end
    end

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

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

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

104 105 106 107
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
    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 131 132
      expose :label_list, as: :labels
      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
D
Drew Blessing 已提交
139
      expose :label_list, as: :labels
A
Alex Denisov 已提交
140
    end
V
Valeriy Sizov 已提交
141

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

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

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

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

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    class ProjectAccess < Grape::Entity
      expose :project_access, as: :access_level
      expose :notification_level
    end

    class GroupAccess < Grape::Entity
      expose :group_access, as: :access_level
      expose :notification_level
    end

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

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

    class Label < Grape::Entity
      expose :name
    end
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    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|
        Commit.new compare.commit
      end
      expose :commits, using: Entities::RepoCommit do |compare, options|
        Commit.decorate compare.commits
      end
      expose :diffs, using: Entities::RepoDiff do |compare, options|
        compare.diffs
      end
213 214 215 216 217 218

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

      expose :same, as: :compare_same_ref
219
    end
N
Nihad Abbasov 已提交
220 221
  end
end