PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/api/entities.rb

https://gitlab.com/leftathome/gitlab-ee
Ruby | 490 lines | 411 code | 76 blank | 3 comment | 18 complexity | fe88ccb03289d259a8c1300a823b0675 MD5 | raw file
  1. module API
  2. module Entities
  3. class UserSafe < Grape::Entity
  4. expose :name, :username
  5. end
  6. class UserBasic < UserSafe
  7. expose :id, :state, :avatar_url
  8. expose :web_url do |user, options|
  9. Gitlab::Routing.url_helpers.user_url(user)
  10. end
  11. end
  12. class User < UserBasic
  13. expose :created_at
  14. expose :is_admin?, as: :is_admin
  15. expose :bio, :location, :skype, :linkedin, :twitter, :website_url
  16. end
  17. class Identity < Grape::Entity
  18. expose :provider, :extern_uid
  19. end
  20. class UserFull < User
  21. expose :last_sign_in_at
  22. expose :confirmed_at
  23. expose :email
  24. expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
  25. expose :identities, using: Entities::Identity
  26. expose :can_create_group?, as: :can_create_group
  27. expose :can_create_project?, as: :can_create_project
  28. expose :two_factor_enabled
  29. expose :external
  30. end
  31. class UserLogin < UserFull
  32. expose :private_token
  33. end
  34. class Email < Grape::Entity
  35. expose :id, :email
  36. end
  37. class Hook < Grape::Entity
  38. expose :id, :url, :created_at
  39. end
  40. class ProjectHook < Hook
  41. expose :project_id, :push_events
  42. expose :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
  43. expose :enable_ssl_verification
  44. end
  45. class ProjectGitHook < Grape::Entity
  46. expose :id, :project_id, :created_at
  47. expose :commit_message_regex, :deny_delete_tag
  48. end
  49. class BasicProjectDetails < Grape::Entity
  50. expose :id
  51. expose :name, :name_with_namespace
  52. expose :path, :path_with_namespace
  53. end
  54. class Project < Grape::Entity
  55. expose :id, :description, :default_branch, :tag_list
  56. expose :public?, as: :public
  57. expose :archived?, as: :archived
  58. expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
  59. expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
  60. expose :name, :name_with_namespace
  61. expose :path, :path_with_namespace
  62. expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled
  63. expose :created_at, :last_activity_at
  64. expose :shared_runners_enabled
  65. expose :creator_id
  66. expose :namespace
  67. expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
  68. expose :avatar_url
  69. expose :star_count, :forks_count
  70. expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
  71. expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
  72. expose :public_builds
  73. end
  74. class ProjectMember < UserBasic
  75. expose :access_level do |user, options|
  76. options[:project].project_members.find_by(user_id: user.id).access_level
  77. end
  78. end
  79. class LdapGroupLink < Grape::Entity
  80. expose :cn, :group_access, :provider
  81. end
  82. class Group < Grape::Entity
  83. expose :id, :name, :path, :description, :visibility_level
  84. expose :ldap_cn, :ldap_access
  85. expose :ldap_group_links,
  86. using: Entities::LdapGroupLink,
  87. if: lambda { |group, options| group.ldap_group_links.any? }
  88. expose :avatar_url
  89. expose :web_url do |group, options|
  90. Gitlab::Routing.url_helpers.group_url(group)
  91. end
  92. end
  93. class GroupDetail < Group
  94. expose :projects, using: Entities::Project
  95. end
  96. class GroupMember < UserBasic
  97. expose :access_level do |user, options|
  98. options[:group].group_members.find_by(user_id: user.id).access_level
  99. end
  100. end
  101. class RepoObject < Grape::Entity
  102. expose :name
  103. expose :commit do |repo_obj, options|
  104. if repo_obj.respond_to?(:commit)
  105. repo_obj.commit
  106. elsif options[:project]
  107. options[:project].repository.commit(repo_obj.target)
  108. end
  109. end
  110. expose :protected do |repo, options|
  111. if options[:project]
  112. options[:project].protected_branch? repo.name
  113. end
  114. end
  115. end
  116. class RepoTreeObject < Grape::Entity
  117. expose :id, :name, :type
  118. expose :mode do |obj, options|
  119. filemode = obj.mode.to_s(8)
  120. filemode = "0" + filemode if filemode.length < 6
  121. filemode
  122. end
  123. end
  124. class RepoCommit < Grape::Entity
  125. expose :id, :short_id, :title, :author_name, :author_email, :created_at
  126. expose :safe_message, as: :message
  127. end
  128. class RepoCommitDetail < RepoCommit
  129. expose :parent_ids, :committed_date, :authored_date
  130. expose :status
  131. end
  132. class ProjectSnippet < Grape::Entity
  133. expose :id, :title, :file_name
  134. expose :author, using: Entities::UserBasic
  135. expose :updated_at, :created_at
  136. # TODO (rspeicher): Deprecated; remove in 9.0
  137. expose(:expires_at) { |snippet| nil }
  138. end
  139. class ProjectEntity < Grape::Entity
  140. expose :id, :iid
  141. expose(:project_id) { |entity| entity.project.id }
  142. expose :title, :description
  143. expose :state, :created_at, :updated_at
  144. end
  145. class RepoDiff < Grape::Entity
  146. expose :old_path, :new_path, :a_mode, :b_mode, :diff
  147. expose :new_file, :renamed_file, :deleted_file
  148. end
  149. class Milestone < ProjectEntity
  150. expose :due_date
  151. end
  152. class Issue < ProjectEntity
  153. expose :label_names, as: :labels
  154. expose :milestone, using: Entities::Milestone
  155. expose :assignee, :author, using: Entities::UserBasic
  156. expose :subscribed do |issue, options|
  157. issue.subscribed?(options[:current_user])
  158. end
  159. expose :user_notes_count
  160. end
  161. class MergeRequest < ProjectEntity
  162. expose :target_branch, :source_branch
  163. expose :upvotes, :downvotes
  164. expose :author, :assignee, using: Entities::UserBasic
  165. expose :source_project_id, :target_project_id
  166. expose :label_names, as: :labels
  167. expose :description
  168. expose :work_in_progress?, as: :work_in_progress
  169. expose :milestone, using: Entities::Milestone
  170. expose :merge_when_build_succeeds
  171. expose :merge_status
  172. expose :subscribed do |merge_request, options|
  173. merge_request.subscribed?(options[:current_user])
  174. end
  175. expose :user_notes_count
  176. end
  177. class MergeRequestChanges < MergeRequest
  178. expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
  179. compare.diffs(all_diffs: true).to_a
  180. end
  181. end
  182. class SSHKey < Grape::Entity
  183. expose :id, :title, :key, :created_at
  184. end
  185. class SSHKeyWithUser < SSHKey
  186. expose :user, using: Entities::UserFull
  187. end
  188. class Note < Grape::Entity
  189. expose :id
  190. expose :note, as: :body
  191. expose :attachment_identifier, as: :attachment
  192. expose :author, using: Entities::UserBasic
  193. expose :created_at, :updated_at
  194. expose :system?, as: :system
  195. expose :noteable_id, :noteable_type
  196. # upvote? and downvote? are deprecated, always return false
  197. expose :upvote?, as: :upvote
  198. expose :downvote?, as: :downvote
  199. end
  200. class MRNote < Grape::Entity
  201. expose :note
  202. expose :author, using: Entities::UserBasic
  203. end
  204. class CommitNote < Grape::Entity
  205. expose :note
  206. expose(:path) { |note| note.diff_file_path if note.legacy_diff_note? }
  207. expose(:line) { |note| note.diff_new_line if note.legacy_diff_note? }
  208. expose(:line_type) { |note| note.diff_line_type if note.legacy_diff_note? }
  209. expose :author, using: Entities::UserBasic
  210. expose :created_at
  211. end
  212. class CommitStatus < Grape::Entity
  213. expose :id, :sha, :ref, :status, :name, :target_url, :description,
  214. :created_at, :started_at, :finished_at, :allow_failure
  215. expose :author, using: Entities::UserBasic
  216. end
  217. class Event < Grape::Entity
  218. expose :title, :project_id, :action_name
  219. expose :target_id, :target_type, :author_id
  220. expose :data, :target_title
  221. expose :created_at
  222. expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
  223. expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
  224. expose :author_username do |event, options|
  225. if event.author
  226. event.author.username
  227. end
  228. end
  229. end
  230. class LdapGroup < Grape::Entity
  231. expose :cn
  232. end
  233. class ProjectGroupLink < Grape::Entity
  234. expose :id, :project_id, :group_id, :group_access
  235. end
  236. class Namespace < Grape::Entity
  237. expose :id, :path, :kind
  238. end
  239. class Member < Grape::Entity
  240. expose :access_level
  241. expose :notification_level do |member, options|
  242. if member.notification_setting
  243. NotificationSetting.levels[member.notification_setting.level]
  244. end
  245. end
  246. end
  247. class ProjectAccess < Member
  248. end
  249. class GroupAccess < Member
  250. end
  251. class ProjectService < Grape::Entity
  252. expose :id, :title, :created_at, :updated_at, :active
  253. expose :push_events, :issues_events, :merge_requests_events, :tag_push_events, :note_events, :build_events
  254. # Expose serialized properties
  255. expose :properties do |service, options|
  256. field_names = service.fields.
  257. select { |field| options[:include_passwords] || field[:type] != 'password' }.
  258. map { |field| field[:name] }
  259. service.properties.slice(*field_names)
  260. end
  261. end
  262. class ProjectWithAccess < Project
  263. expose :permissions do
  264. expose :project_access, using: Entities::ProjectAccess do |project, options|
  265. project.project_members.find_by(user_id: options[:user].id)
  266. end
  267. expose :group_access, using: Entities::GroupAccess do |project, options|
  268. if project.group
  269. project.group.group_members.find_by(user_id: options[:user].id)
  270. end
  271. end
  272. end
  273. end
  274. class Label < Grape::Entity
  275. expose :name, :color, :description
  276. expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
  277. expose :subscribed do |label, options|
  278. label.subscribed?(options[:current_user])
  279. end
  280. end
  281. class Compare < Grape::Entity
  282. expose :commit, using: Entities::RepoCommit do |compare, options|
  283. Commit.decorate(compare.commits, nil).last
  284. end
  285. expose :commits, using: Entities::RepoCommit do |compare, options|
  286. Commit.decorate(compare.commits, nil)
  287. end
  288. expose :diffs, using: Entities::RepoDiff do |compare, options|
  289. compare.diffs(all_diffs: true).to_a
  290. end
  291. expose :compare_timeout do |compare, options|
  292. compare.diffs.overflow?
  293. end
  294. expose :same, as: :compare_same_ref
  295. end
  296. class Contributor < Grape::Entity
  297. expose :name, :email, :commits, :additions, :deletions
  298. end
  299. class BroadcastMessage < Grape::Entity
  300. expose :message, :starts_at, :ends_at, :color, :font
  301. end
  302. class ApplicationSetting < Grape::Entity
  303. expose :id
  304. expose :default_projects_limit
  305. expose :signup_enabled
  306. expose :signin_enabled
  307. expose :gravatar_enabled
  308. expose :sign_in_text
  309. expose :created_at
  310. expose :updated_at
  311. expose :home_page_url
  312. expose :default_branch_protection
  313. expose :restricted_visibility_levels
  314. expose :max_attachment_size
  315. expose :session_expire_delay
  316. expose :default_project_visibility
  317. expose :default_snippet_visibility
  318. expose :default_group_visibility
  319. expose :restricted_signup_domains
  320. expose :user_oauth_applications
  321. expose :after_sign_out_path
  322. end
  323. class Release < Grape::Entity
  324. expose :tag, as: :tag_name
  325. expose :description
  326. end
  327. class RepoTag < Grape::Entity
  328. expose :name
  329. expose :message do |repo_obj, _options|
  330. if repo_obj.respond_to?(:message)
  331. repo_obj.message
  332. else
  333. nil
  334. end
  335. end
  336. expose :commit do |repo_obj, options|
  337. if repo_obj.respond_to?(:commit)
  338. repo_obj.commit
  339. elsif options[:project]
  340. options[:project].repository.commit(repo_obj.target)
  341. end
  342. end
  343. expose :release, using: Entities::Release do |repo_obj, options|
  344. if options[:project]
  345. options[:project].releases.find_by(tag: repo_obj.name)
  346. end
  347. end
  348. end
  349. class License < Grape::Entity
  350. expose :starts_at, :expires_at, :licensee
  351. expose :user_limit do |license, options|
  352. license.restricted?(:active_user_count) ? license.restrictions[:active_user_count] : 0
  353. end
  354. expose :active_users do |license, options|
  355. ::User.active.count
  356. end
  357. end
  358. class TriggerRequest < Grape::Entity
  359. expose :id, :variables
  360. end
  361. class Runner < Grape::Entity
  362. expose :id
  363. expose :description
  364. expose :active
  365. expose :is_shared
  366. expose :name
  367. end
  368. class RunnerDetails < Runner
  369. expose :tag_list
  370. expose :version, :revision, :platform, :architecture
  371. expose :contacted_at
  372. expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
  373. expose :projects, with: Entities::BasicProjectDetails do |runner, options|
  374. if options[:current_user].is_admin?
  375. runner.projects
  376. else
  377. options[:current_user].authorized_projects.where(id: runner.projects)
  378. end
  379. end
  380. end
  381. class BuildArtifactFile < Grape::Entity
  382. expose :filename, :size
  383. end
  384. class Build < Grape::Entity
  385. expose :id, :status, :stage, :name, :ref, :tag, :coverage
  386. expose :created_at, :started_at, :finished_at
  387. expose :user, with: User
  388. expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
  389. expose :commit, with: RepoCommit do |repo_obj, _options|
  390. if repo_obj.respond_to?(:commit)
  391. repo_obj.commit.commit_data
  392. end
  393. end
  394. expose :runner, with: Runner
  395. end
  396. class Trigger < Grape::Entity
  397. expose :token, :created_at, :updated_at, :deleted_at, :last_used
  398. end
  399. class Variable < Grape::Entity
  400. expose :key, :value
  401. end
  402. class RepoLicense < Grape::Entity
  403. expose :key, :name, :nickname
  404. expose :featured, as: :popular
  405. expose :url, as: :html_url
  406. expose(:source_url) { |license| license.meta['source'] }
  407. expose(:description) { |license| license.meta['description'] }
  408. expose(:conditions) { |license| license.meta['conditions'] }
  409. expose(:permissions) { |license| license.meta['permissions'] }
  410. expose(:limitations) { |license| license.meta['limitations'] }
  411. expose :content
  412. end
  413. end
  414. end