entities.rb 5.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 18 19 20
    class UserSafe < Grape::Entity
      expose :name
    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
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

C
Christian Simon 已提交
61 62
    class TeamMember < UserBasic
      expose :permission, as: :access_level do |user, options|
S
skv 已提交
63
        options[:user_team].user_team_user_relationships.find_by(user_id: user.id).permission
C
Christian Simon 已提交
64 65 66 67 68
      end
    end

    class TeamProject < Project
      expose :greatest_access, as: :greatest_access_level do |project, options|
S
skv 已提交
69
        options[:user_team].user_team_project_relationships.find_by(project_id: project.id).greatest_access
C
Christian Simon 已提交
70 71 72
      end
    end

73
    class Group < Grape::Entity
74
      expose :id, :name, :path, :owner_id
75
    end
A
Andrew8xx8 已提交
76

77
    class GroupDetail < Group
78 79 80
      expose :projects, using: Entities::Project
    end

I
Izaak Alpert 已提交
81 82
    class GroupMember < UserBasic
      expose :group_access, as: :access_level do |user, options|
S
skv 已提交
83
        options[:group].users_groups.find_by(user_id: user.id).group_access
I
Izaak Alpert 已提交
84 85 86
      end
    end

N
Nihad Abbasov 已提交
87
    class RepoObject < Grape::Entity
88 89 90 91 92 93 94 95 96 97
      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

98 99 100 101 102
      expose :protected do |repo, options|
        if options[:project]
          options[:project].protected_branch? repo.name
        end
      end
N
Nihad Abbasov 已提交
103
    end
N
Nihad Abbasov 已提交
104

105 106 107 108 109 110 111 112 113 114
    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

115 116 117 118
    class RepoCommit < Grape::Entity
      expose :id, :short_id, :title, :author_name, :author_email, :created_at
    end

119 120 121 122
    class RepoCommitDetail < RepoCommit
      expose :parent_ids, :committed_date, :authored_date
    end

N
Nihad Abbasov 已提交
123 124
    class ProjectSnippet < Grape::Entity
      expose :id, :title, :file_name
125
      expose :author, using: Entities::UserBasic
N
Nihad Abbasov 已提交
126 127
      expose :expires_at, :updated_at, :created_at
    end
N
Nihad Abbasov 已提交
128

129 130 131 132 133 134
    class ProjectEntity < Grape::Entity
      expose :id, :iid
      expose (:project_id) { |entity| entity.project.id }
    end

    class Milestone < ProjectEntity
A
Andrew8xx8 已提交
135
      expose :title, :description, :due_date, :state, :updated_at, :created_at
N
Nihad Abbasov 已提交
136 137
    end

138
    class Issue < ProjectEntity
N
Nihad Abbasov 已提交
139
      expose :title, :description
140 141 142
      expose :label_list, as: :labels
      expose :milestone, using: Entities::Milestone
      expose :assignee, :author, using: Entities::UserBasic
A
Andrew8xx8 已提交
143
      expose :state, :updated_at, :created_at
N
Nihad Abbasov 已提交
144
    end
A
Alex Denisov 已提交
145

146 147 148 149
    class MergeRequest < ProjectEntity
      expose :target_branch, :source_branch, :title, :state, :upvotes, :downvotes
      expose :author, :assignee, using: Entities::UserBasic
      expose :source_project_id, :target_project_id
A
Alex Denisov 已提交
150
    end
V
Valeriy Sizov 已提交
151

152 153
    class SSHKey < Grape::Entity
      expose :id, :title, :key, :created_at
V
Valeriy Sizov 已提交
154
    end
155 156

    class Note < Grape::Entity
157 158
      expose :id
      expose :note, as: :body
159
      expose :attachment_identifier, as: :attachment
160
      expose :author, using: Entities::UserBasic
161
      expose :created_at
162
    end
163 164 165 166 167

    class MRNote < Grape::Entity
      expose :note
      expose :author, using: Entities::UserBasic
    end
D
Dmitriy Zaporozhets 已提交
168 169 170 171 172 173

    class Event < Grape::Entity
      expose :title, :project_id, :action_name
      expose :target_id, :target_type, :author_id
      expose :data, :target_title
    end
174 175 176 177

    class Namespace < Grape::Entity
      expose :id, :path, :kind
    end
N
Nihad Abbasov 已提交
178 179
  end
end