entities.rb 5.5 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 46
      expose :id, :description, :default_branch
      expose :public?, as: :public
      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
50
      expose :issues_enabled, :merge_requests_enabled, :wall_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
56
      expose :project_access, as: :access_level do |user, options|
S
skv 已提交
57
        options[:project].users_projects.find_by(user_id: user.id).project_access
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 70
    class GroupMember < UserBasic
      expose :group_access, as: :access_level do |user, options|
S
skv 已提交
71
        options[:group].users_groups.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 105 106
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
    end

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

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

117 118 119 120 121 122
    class ProjectEntity < Grape::Entity
      expose :id, :iid
      expose (:project_id) { |entity| entity.project.id }
    end

    class Milestone < ProjectEntity
A
Andrew8xx8 已提交
123
      expose :title, :description, :due_date, :state, :updated_at, :created_at
N
Nihad Abbasov 已提交
124 125
    end

126
    class Issue < ProjectEntity
N
Nihad Abbasov 已提交
127
      expose :title, :description
128 129 130
      expose :label_list, as: :labels
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
A
Andrew8xx8 已提交
131
      expose :state, :updated_at, :created_at
N
Nihad Abbasov 已提交
132
    end
A
Alex Denisov 已提交
133

134
    class MergeRequest < ProjectEntity
D
Dmitriy Zaporozhets 已提交
135
      expose :target_branch, :source_branch, :title, :state, :upvotes, :downvotes, :description
136 137
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
A
Alex Denisov 已提交
138
    end
V
Valeriy Sizov 已提交
139

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

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

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

    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
    end
162 163 164 165

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

    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|
184 185 186
          if project.group
            project.group.users_groups.find_by(user_id: options[:user].id)
          end
187 188 189
        end
      end
    end
190 191 192 193

    class Label < Grape::Entity
      expose :name
    end
N
Nihad Abbasov 已提交
194 195
  end
end