gitlab-org--gitlab-foss/lib/api/entities.rb

637 lines
20 KiB
Ruby
Raw Normal View History

module API
2012-06-27 11:32:56 +00:00
module Entities
class UserSafe < Grape::Entity
expose :name, :username
end
class UserBasic < UserSafe
expose :id, :state, :avatar_url
2015-07-30 09:56:15 +00:00
expose :web_url do |user, options|
Gitlab::Routing.url_helpers.user_url(user)
2015-07-30 09:56:15 +00:00
end
2012-06-27 11:32:56 +00:00
end
2012-06-27 12:51:39 +00:00
class User < UserBasic
expose :created_at
expose :is_admin?, as: :is_admin
expose :bio, :location, :skype, :linkedin, :twitter, :website_url, :organization
end
class Identity < Grape::Entity
expose :provider, :extern_uid
end
class UserFull < User
expose :last_sign_in_at
expose :confirmed_at
expose :email
2015-02-09 12:52:42 +00:00
expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
expose :identities, using: Entities::Identity
expose :can_create_group?, as: :can_create_group
expose :can_create_project?, as: :can_create_project
expose :two_factor_enabled?, as: :two_factor_enabled
2016-03-14 23:11:20 +00:00
expose :external
end
class UserLogin < UserFull
expose :private_token
end
class Email < Grape::Entity
expose :id, :email
end
2012-09-08 17:51:12 +00:00
class Hook < Grape::Entity
expose :id, :url, :created_at
2012-09-08 17:51:12 +00:00
end
class ProjectHook < Hook
expose :project_id, :push_events
expose :issues_events, :merge_requests_events, :tag_push_events
expose :note_events, :build_events, :pipeline_events, :wiki_page_events
expose :enable_ssl_verification
end
class BasicProjectDetails < Grape::Entity
expose :id
expose :http_url_to_repo, :web_url
expose :name, :name_with_namespace
expose :path, :path_with_namespace
end
2016-07-08 07:34:36 +00:00
class SharedGroup < Grape::Entity
expose :group_id
expose :group_name do |group_link, options|
group_link.group.name
end
expose :group_access, as: :group_access_level
end
2012-06-27 12:51:39 +00:00
class Project < Grape::Entity
expose :id, :description, :default_branch, :tag_list
expose :public?, as: :public
expose :archived?, as: :archived
expose :visibility_level, :ssh_url_to_repo, :http_url_to_repo, :web_url
expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
expose :name, :name_with_namespace
expose :path, :path_with_namespace
2016-08-01 22:31:21 +00:00
expose :container_registry_enabled
# Expose old field names with the new permissions methods to keep API compatible
expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:user]) }
expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:user]) }
expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:user]) }
expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:user]) }
expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:user]) }
2016-04-18 12:23:17 +00:00
expose :created_at, :last_activity_at
expose :shared_runners_enabled
expose :lfs_enabled?, as: :lfs_enabled
expose :creator_id
expose :namespace
expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
expose :avatar_url
expose :star_count, :forks_count
2016-08-01 22:31:21 +00:00
expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:user]) && project.default_issues_tracker? }
2016-01-11 14:27:20 +00:00
expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
expose :public_builds
2016-07-08 07:34:36 +00:00
expose :shared_with_groups do |project, options|
SharedGroup.represent(project.project_group_links.all, options)
end
expose :only_allow_merge_if_build_succeeds
expose :request_access_enabled
2012-06-27 12:51:39 +00:00
end
class Member < UserBasic
expose :access_level do |user, options|
member = options[:member] || options[:source].members.find_by(user_id: user.id)
member.access_level
end
expose :expires_at do |user, options|
member = options[:member] || options[:source].members.find_by(user_id: user.id)
member.expires_at
end
end
class AccessRequester < UserBasic
expose :requested_at do |user, options|
access_requester = options[:access_requester] || options[:source].requesters.find_by(user_id: user.id)
access_requester.requested_at
2012-09-21 10:23:17 +00:00
end
2012-09-06 20:49:29 +00:00
end
class Group < Grape::Entity
expose :id, :name, :path, :description, :visibility_level
expose :lfs_enabled?, as: :lfs_enabled
2015-07-30 09:56:15 +00:00
expose :avatar_url
expose :web_url
expose :request_access_enabled
end
2013-02-18 09:10:58 +00:00
class GroupDetail < Group
expose :projects, using: Entities::Project
2016-07-08 08:59:52 +00:00
expose :shared_projects, using: Entities::Project
end
class RepoBranch < Grape::Entity
expose :name
expose :commit do |repo_branch, options|
options[:project].repository.commit(repo_branch.target)
end
expose :protected do |repo_branch, options|
options[:project].protected_branch? repo_branch.name
end
expose :developers_can_push do |repo_branch, options|
project = options[:project]
access_levels = project.protected_branches.matching(repo_branch.name).map(&:push_access_levels).flatten
access_levels.any? { |access_level| access_level.access_level == Gitlab::Access::DEVELOPER }
end
expose :developers_can_merge do |repo_branch, options|
project = options[:project]
access_levels = project.protected_branches.matching(repo_branch.name).map(&:merge_access_levels).flatten
access_levels.any? { |access_level| access_level.access_level == Gitlab::Access::DEVELOPER }
end
2012-06-27 12:51:39 +00:00
end
2012-06-29 13:34:08 +00:00
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
class RepoCommit < Grape::Entity
expose :id, :short_id, :title, :author_name, :author_email, :created_at
expose :safe_message, as: :message
end
class RepoCommitStats < Grape::Entity
expose :additions, :deletions, :total
end
class RepoCommitDetail < RepoCommit
expose :parent_ids, :committed_date, :authored_date
expose :stats, using: Entities::RepoCommitStats
2015-10-06 10:01:16 +00:00
expose :status
end
2012-06-29 13:34:08 +00:00
class ProjectSnippet < Grape::Entity
expose :id, :title, :file_name
expose :author, using: Entities::UserBasic
expose :updated_at, :created_at
# TODO (rspeicher): Deprecated; remove in 9.0
expose(:expires_at) { |snippet| nil }
expose :web_url do |snippet, options|
Gitlab::UrlBuilder.build(snippet)
end
2012-06-29 13:34:08 +00:00
end
2012-07-24 12:19:51 +00:00
class ProjectEntity < Grape::Entity
expose :id, :iid
expose(:project_id) { |entity| entity.project.id }
expose :title, :description
expose :state, :created_at, :updated_at
end
class RepoDiff < Grape::Entity
expose :old_path, :new_path, :a_mode, :b_mode, :diff
expose :new_file, :renamed_file, :deleted_file
end
class Milestone < ProjectEntity
expose :due_date
2012-07-24 12:19:51 +00:00
end
class Issue < ProjectEntity
expose :label_names, as: :labels
expose :milestone, using: Entities::Milestone
expose :assignee, :author, using: Entities::UserBasic
2016-04-08 06:41:10 +00:00
expose :subscribed do |issue, options|
issue.subscribed?(options[:current_user])
end
2016-06-01 09:23:09 +00:00
expose :user_notes_count
2016-05-11 20:43:58 +00:00
expose :upvotes, :downvotes
2016-07-12 15:59:21 +00:00
expose :due_date
2016-07-15 14:21:53 +00:00
expose :confidential
expose :web_url do |issue, options|
Gitlab::UrlBuilder.build(issue)
end
2012-07-24 12:19:51 +00:00
end
2012-09-16 14:52:06 +00:00
class ExternalIssue < Grape::Entity
expose :title
expose :id
end
class MergeRequest < ProjectEntity
2015-11-11 13:12:51 +00:00
expose :target_branch, :source_branch
2016-04-16 19:09:08 +00:00
expose :upvotes, :downvotes
expose :author, :assignee, using: Entities::UserBasic
expose :source_project_id, :target_project_id
expose :label_names, as: :labels
2015-07-09 17:21:37 +00:00
expose :work_in_progress?, as: :work_in_progress
expose :milestone, using: Entities::Milestone
2015-11-24 13:59:02 +00:00
expose :merge_when_build_succeeds
expose :merge_status
expose :diff_head_sha, as: :sha
expose :merge_commit_sha
2016-04-08 06:41:10 +00:00
expose :subscribed do |merge_request, options|
merge_request.subscribed?(options[:current_user])
end
2016-06-01 09:23:09 +00:00
expose :user_notes_count
expose :should_remove_source_branch?, as: :should_remove_source_branch
expose :force_remove_source_branch?, as: :force_remove_source_branch
expose :web_url do |merge_request, options|
Gitlab::UrlBuilder.build(merge_request)
end
2012-09-16 14:52:06 +00:00
end
2012-10-21 11:00:27 +00:00
class MergeRequestChanges < MergeRequest
expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
compare.raw_diffs(all_diffs: true).to_a
end
end
class MergeRequestDiff < Grape::Entity
expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha,
:created_at, :merge_request_id, :state, :real_size
end
class MergeRequestDiffFull < MergeRequestDiff
expose :commits, using: Entities::RepoCommit
expose :diffs, using: Entities::RepoDiff do |compare, _|
compare.raw_diffs(all_diffs: true).to_a
end
end
class SSHKey < Grape::Entity
expose :id, :title, :key, :created_at
2012-10-21 11:00:27 +00:00
end
2012-10-21 16:48:56 +00:00
class SSHKeyWithUser < SSHKey
expose :user, using: Entities::UserFull
end
2012-10-21 16:48:56 +00:00
class Note < Grape::Entity
expose :id
expose :note, as: :body
2013-03-19 11:28:29 +00:00
expose :attachment_identifier, as: :attachment
2012-10-21 16:48:56 +00:00
expose :author, using: Entities::UserBasic
2016-04-12 10:32:34 +00:00
expose :created_at, :updated_at
expose :system?, as: :system
expose :noteable_id, :noteable_type
# upvote? and downvote? are deprecated, always return false
2016-04-16 19:09:08 +00:00
expose(:upvote?) { |note| false }
expose(:downvote?) { |note| false }
2012-10-21 16:48:56 +00:00
end
class AwardEmoji < Grape::Entity
expose :id
expose :name
expose :user, using: Entities::UserBasic
expose :created_at, :updated_at
expose :awardable_id, :awardable_type
end
class MRNote < Grape::Entity
expose :note
expose :author, using: Entities::UserBasic
end
2013-06-06 16:19:17 +00:00
2014-06-27 14:48:30 +00:00
class CommitNote < Grape::Entity
expose :note
expose(:path) { |note| note.diff_file.try(:file_path) if note.diff_note? }
expose(:line) { |note| note.diff_line.try(:new_line) if note.diff_note? }
expose(:line_type) { |note| note.diff_line.try(:type) if note.diff_note? }
2014-06-27 14:48:30 +00:00
expose :author, using: Entities::UserBasic
expose :created_at
2014-06-27 14:48:30 +00:00
end
2015-10-06 10:01:16 +00:00
class CommitStatus < Grape::Entity
expose :id, :sha, :ref, :status, :name, :target_url, :description,
:created_at, :started_at, :finished_at, :allow_failure
2015-10-12 10:15:48 +00:00
expose :author, using: Entities::UserBasic
2015-10-06 10:01:16 +00:00
end
2013-06-06 16:19:17 +00:00
class Event < Grape::Entity
expose :title, :project_id, :action_name
expose :target_id, :target_type, :author_id
expose :data, :target_title
expose :created_at
expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
expose :author_username do |event, options|
if event.author
event.author.username
end
end
2013-06-06 16:19:17 +00:00
end
class ProjectGroupLink < Grape::Entity
expose :id, :project_id, :group_id, :group_access, :expires_at
end
2016-03-11 19:04:42 +00:00
class Todo < Grape::Entity
expose :id
expose :project, using: Entities::BasicProjectDetails
expose :author, using: Entities::UserBasic
2016-06-15 11:20:30 +00:00
expose :action_name
2016-03-11 19:04:42 +00:00
expose :target_type
expose :target do |todo, options|
Entities.const_get(todo.target_type).represent(todo.target, options)
2016-03-11 19:04:42 +00:00
end
expose :target_url do |todo, options|
target_type = todo.target_type.underscore
target_url = "namespace_project_#{target_type}_url"
target_anchor = "note_#{todo.note_id}" if todo.note_id?
2016-03-11 19:04:42 +00:00
Gitlab::Application.routes.url_helpers.public_send(target_url,
todo.project.namespace, todo.project, todo.target, anchor: target_anchor)
end
expose :body
expose :state
expose :created_at
end
class Namespace < Grape::Entity
expose :id, :path, :kind
end
class MemberAccess < Grape::Entity
expose :access_level
expose :notification_level do |member, options|
if member.notification_setting
::NotificationSetting.levels[member.notification_setting.level]
end
end
end
class ProjectAccess < MemberAccess
end
class GroupAccess < MemberAccess
end
class NotificationSetting < Grape::Entity
expose :level
expose :events, if: ->(notification_setting, _) { notification_setting.custom? } do
::NotificationSetting::EMAIL_EVENTS.each do |event|
expose event
end
end
end
class GlobalNotificationSetting < NotificationSetting
expose :notification_email do |notification_setting, options|
notification_setting.user.notification_email
end
end
class ProjectService < Grape::Entity
expose :id, :title, :created_at, :updated_at, :active
expose :push_events, :issues_events, :merge_requests_events
expose :tag_push_events, :note_events, :build_events, :pipeline_events
# Expose serialized properties
expose :properties do |service, options|
field_names = service.fields.
select { |field| options[:include_passwords] || field[:type] != 'password' }.
map { |field| field[:name] }
service.properties.slice(*field_names)
end
end
class ProjectWithAccess < Project
expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options|
project.project_members.find_by(user_id: options[:user].id)
end
expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group
project.group.group_members.find_by(user_id: options[:user].id)
end
end
end
end
class LabelBasic < Grape::Entity
2016-03-24 18:38:37 +00:00
expose :name, :color, :description
end
class Label < LabelBasic
expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
expose :subscribed do |label, options|
label.subscribed?(options[:current_user])
end
end
class List < Grape::Entity
expose :id
expose :label, using: Entities::LabelBasic
expose :position
end
class Board < Grape::Entity
expose :id
expose :lists, using: Entities::List do |board|
board.lists.destroyable
end
end
class Compare < Grape::Entity
expose :commit, using: Entities::RepoCommit do |compare, options|
Commit.decorate(compare.commits, nil).last
end
expose :commits, using: Entities::RepoCommit do |compare, options|
Commit.decorate(compare.commits, nil)
end
expose :diffs, using: Entities::RepoDiff do |compare, options|
2016-03-03 17:38:44 +00:00
compare.diffs(all_diffs: true).to_a
end
expose :compare_timeout do |compare, options|
2016-03-03 17:38:44 +00:00
compare.diffs.overflow?
end
expose :same, as: :compare_same_ref
end
2014-07-02 12:09:06 +00:00
class Contributor < Grape::Entity
expose :name, :email, :commits, :additions, :deletions
end
2015-02-07 15:41:30 +00:00
class BroadcastMessage < Grape::Entity
expose :message, :starts_at, :ends_at, :color, :font
end
class ApplicationSetting < Grape::Entity
expose :id
expose :default_projects_limit
expose :signup_enabled
expose :signin_enabled
expose :gravatar_enabled
expose :sign_in_text
expose :after_sign_up_text
expose :created_at
expose :updated_at
expose :home_page_url
expose :default_branch_protection
expose :restricted_visibility_levels
expose :max_attachment_size
expose :session_expire_delay
expose :default_project_visibility
expose :default_snippet_visibility
expose :default_group_visibility
expose :domain_whitelist
expose :domain_blacklist_enabled
expose :domain_blacklist
expose :user_oauth_applications
expose :after_sign_out_path
expose :container_registry_token_expire_delay
expose :repository_storage
expose :koding_enabled
expose :koding_url
end
class Release < Grape::Entity
expose :tag, as: :tag_name
expose :description
end
class RepoTag < Grape::Entity
expose :name, :message
expose :commit do |repo_tag, options|
options[:project].repository.commit(repo_tag.target)
end
expose :release, using: Entities::Release do |repo_tag, options|
options[:project].releases.find_by(tag: repo_tag.name)
end
end
2015-12-10 17:04:40 +00:00
class TriggerRequest < Grape::Entity
expose :id, :variables
end
2016-01-04 15:38:32 +00:00
2016-01-08 21:57:42 +00:00
class Runner < Grape::Entity
2015-12-28 12:09:51 +00:00
expose :id
expose :description
expose :active
expose :is_shared
expose :name
end
class RunnerDetails < Runner
expose :tag_list
expose :run_untagged
expose :locked
expose :version, :revision, :platform, :architecture
expose :contacted_at
expose :token, if: lambda { |runner, options| options[:current_user].is_admin? || !runner.is_shared? }
expose :projects, with: Entities::BasicProjectDetails do |runner, options|
if options[:current_user].is_admin?
runner.projects
else
options[:current_user].authorized_projects.where(id: runner.projects)
end
end
end
class BuildArtifactFile < Grape::Entity
expose :filename, :size
end
class PipelineBasic < Grape::Entity
expose :id, :sha, :ref, :status
end
class Build < Grape::Entity
2016-01-11 10:15:04 +00:00
expose :id, :status, :stage, :name, :ref, :tag, :coverage
2016-01-08 21:57:42 +00:00
expose :created_at, :started_at, :finished_at
2016-01-12 17:32:18 +00:00
expose :user, with: User
2016-02-19 17:07:05 +00:00
expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
expose :commit, with: RepoCommit
2016-01-08 21:57:42 +00:00
expose :runner, with: Runner
expose :pipeline, with: PipelineBasic
end
2016-01-04 15:38:32 +00:00
class Trigger < Grape::Entity
2016-01-13 11:16:27 +00:00
expose :token, :created_at, :updated_at, :deleted_at, :last_used
2016-01-04 15:38:32 +00:00
end
class Variable < Grape::Entity
2016-01-07 13:10:49 +00:00
expose :key, :value
end
class Pipeline < PipelineBasic
expose :before_sha, :tag, :yaml_errors
2016-08-17 08:09:42 +00:00
expose :user, with: Entities::UserBasic
expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
expose :duration
end
class EnvironmentBasic < Grape::Entity
2016-07-26 07:37:02 +00:00
expose :id, :name, :external_url
end
class Environment < EnvironmentBasic
expose :project, using: Entities::Project
2016-08-16 06:45:23 +00:00
end
class Deployment < Grape::Entity
expose :id, :iid, :ref, :sha, :created_at
2016-08-18 19:10:20 +00:00
expose :user, using: Entities::UserBasic
expose :environment, using: Entities::EnvironmentBasic
expose :deployable, using: Entities::Build
2016-07-26 07:37:02 +00:00
end
class RepoLicense < Grape::Entity
expose :key, :name, :nickname
expose :featured, as: :popular
expose :url, as: :html_url
expose(:source_url) { |license| license.meta['source'] }
expose(:description) { |license| license.meta['description'] }
expose(:conditions) { |license| license.meta['conditions'] }
expose(:permissions) { |license| license.meta['permissions'] }
expose(:limitations) { |license| license.meta['limitations'] }
expose :content
end
2016-04-29 14:25:03 +00:00
2016-05-27 09:00:56 +00:00
class TemplatesList < Grape::Entity
2016-04-29 14:25:03 +00:00
expose :name
end
2016-05-27 09:00:56 +00:00
class Template < Grape::Entity
2016-04-29 14:25:03 +00:00
expose :name, :content
end
class BroadcastMessage < Grape::Entity
expose :id, :message, :starts_at, :ends_at, :color, :font
expose :active?, as: :active
end
2012-06-27 11:32:56 +00:00
end
end