debian-mirror-gitlab/lib/api/entities.rb

554 lines
17 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module API
module Entities
class UserSafe < Grape::Entity
expose :name, :username
end
class UserBasic < UserSafe
expose :id, :state, :avatar_url
2015-09-11 14:41:01 +05:30
expose :web_url do |user, options|
2016-06-02 11:05:42 +05:30
Gitlab::Routing.url_helpers.user_url(user)
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
end
class User < UserBasic
expose :created_at
expose :is_admin?, as: :is_admin
2016-06-02 11:05:42 +05:30
expose :bio, :location, :skype, :linkedin, :twitter, :website_url
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
class Identity < Grape::Entity
expose :provider, :extern_uid
end
2014-09-02 18:07:02 +05:30
class UserFull < User
2016-06-02 11:05:42 +05:30
expose :last_sign_in_at
expose :confirmed_at
2014-09-02 18:07:02 +05:30
expose :email
2015-09-11 14:41:01 +05:30
expose :theme_id, :color_scheme_id, :projects_limit, :current_sign_in_at
2015-04-26 12:48:37 +05:30
expose :identities, using: Entities::Identity
2014-09-02 18:07:02 +05:30
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-06-02 11:05:42 +05:30
expose :external
2014-09-02 18:07:02 +05:30
end
class UserLogin < UserFull
expose :private_token
end
2015-09-11 14:41:01 +05:30
class Email < Grape::Entity
expose :id, :email
end
2014-09-02 18:07:02 +05:30
class Hook < Grape::Entity
expose :id, :url, :created_at
end
class ProjectHook < Hook
2015-04-26 12:48:37 +05:30
expose :project_id, :push_events
2016-09-13 17:45:13 +05:30
expose :issues_events, :merge_requests_events, :tag_push_events
expose :note_events, :build_events, :pipeline_events
2015-12-23 02:04:40 +05:30
expose :enable_ssl_verification
2014-09-02 18:07:02 +05:30
end
2016-04-02 18:10:28 +05:30
class BasicProjectDetails < Grape::Entity
2014-09-02 18:07:02 +05:30
expose :id
2016-08-24 12:49:21 +05:30
expose :http_url_to_repo, :web_url
2014-09-02 18:07:02 +05:30
expose :name, :name_with_namespace
expose :path, :path_with_namespace
end
2016-08-24 12:49:21 +05:30
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
2014-09-02 18:07:02 +05:30
class Project < Grape::Entity
2015-04-26 12:48:37 +05:30
expose :id, :description, :default_branch, :tag_list
2014-09-02 18:07:02 +05:30
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-06-02 11:05:42 +05:30
expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled
expose :created_at, :last_activity_at
2015-12-23 02:04:40 +05:30
expose :shared_runners_enabled
2015-04-26 12:48:37 +05:30
expose :creator_id
2014-09-02 18:07:02 +05:30
expose :namespace
2016-04-02 18:10:28 +05:30
expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? }
2015-04-26 12:48:37 +05:30
expose :avatar_url
2015-09-11 14:41:01 +05:30
expose :star_count, :forks_count
2015-12-23 02:04:40 +05:30
expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? }
expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] }
2016-04-02 18:10:28 +05:30
expose :public_builds
2016-08-24 12:49:21 +05:30
expose :shared_with_groups do |project, options|
SharedGroup.represent(project.project_group_links.all, options)
end
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
class Member < UserBasic
2015-04-26 12:48:37 +05:30
expose :access_level do |user, options|
2016-09-13 17:45:13 +05:30
member = options[:member] || options[:members].find { |m| m.user_id == user.id }
member.access_level
end
expose :expires_at do |user, options|
member = options[:member] || options[:members].find { |m| m.user_id == user.id }
member.expires_at
end
end
class AccessRequester < UserBasic
expose :requested_at do |user, options|
access_requester = options[:access_requester] || options[:access_requesters].find { |m| m.user_id == user.id }
access_requester.requested_at
2014-09-02 18:07:02 +05:30
end
end
class Group < Grape::Entity
2016-06-02 11:05:42 +05:30
expose :id, :name, :path, :description, :visibility_level
2015-09-11 14:41:01 +05:30
expose :avatar_url
expose :web_url
2014-09-02 18:07:02 +05:30
end
class GroupDetail < Group
expose :projects, using: Entities::Project
2016-08-24 12:49:21 +05:30
expose :shared_projects, using: Entities::Project
2014-09-02 18:07:02 +05:30
end
2016-08-24 12:49:21 +05:30
class RepoBranch < Grape::Entity
2014-09-02 18:07:02 +05:30
expose :name
2016-08-24 12:49:21 +05:30
expose :commit do |repo_branch, options|
options[:project].repository.commit(repo_branch.target)
2014-09-02 18:07:02 +05:30
end
2016-08-24 12:49:21 +05:30
expose :protected do |repo_branch, options|
options[:project].protected_branch? repo_branch.name
end
expose :developers_can_push do |repo_branch, options|
2016-09-13 17:45:13 +05:30
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 }
2016-08-24 12:49:21 +05:30
end
expose :developers_can_merge do |repo_branch, options|
2016-09-13 17:45:13 +05:30
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 }
2014-09-02 18:07:02 +05:30
end
end
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
2016-09-13 17:45:13 +05:30
class RepoCommitStats < Grape::Entity
expose :additions, :deletions, :total
end
2014-09-02 18:07:02 +05:30
class RepoCommitDetail < RepoCommit
expose :parent_ids, :committed_date, :authored_date
2016-09-13 17:45:13 +05:30
expose :stats, using: Entities::RepoCommitStats
2015-10-24 18:46:33 +05:30
expose :status
2014-09-02 18:07:02 +05:30
end
class ProjectSnippet < Grape::Entity
expose :id, :title, :file_name
expose :author, using: Entities::UserBasic
2016-06-02 11:05:42 +05:30
expose :updated_at, :created_at
# TODO (rspeicher): Deprecated; remove in 9.0
expose(:expires_at) { |snippet| nil }
2014-09-02 18:07:02 +05:30
end
class ProjectEntity < Grape::Entity
expose :id, :iid
2015-04-26 12:48:37 +05:30
expose(:project_id) { |entity| entity.project.id }
2014-09-02 18:07:02 +05:30
expose :title, :description
expose :state, :created_at, :updated_at
end
2015-04-26 12:48:37 +05:30
class RepoDiff < Grape::Entity
expose :old_path, :new_path, :a_mode, :b_mode, :diff
expose :new_file, :renamed_file, :deleted_file
end
2014-09-02 18:07:02 +05:30
class Milestone < ProjectEntity
expose :due_date
end
class Issue < ProjectEntity
expose :label_names, as: :labels
expose :milestone, using: Entities::Milestone
expose :assignee, :author, using: Entities::UserBasic
2016-06-02 11:05:42 +05:30
expose :subscribed do |issue, options|
issue.subscribed?(options[:current_user])
end
expose :user_notes_count
expose :upvotes, :downvotes
2016-08-24 12:49:21 +05:30
expose :due_date
end
class ExternalIssue < Grape::Entity
expose :title
expose :id
2014-09-02 18:07:02 +05:30
end
class MergeRequest < ProjectEntity
2015-11-26 14:37:03 +05:30
expose :target_branch, :source_branch
expose :upvotes, :downvotes
2014-09-02 18:07:02 +05:30
expose :author, :assignee, using: Entities::UserBasic
expose :source_project_id, :target_project_id
expose :label_names, as: :labels
2015-09-11 14:41:01 +05:30
expose :work_in_progress?, as: :work_in_progress
2014-09-02 18:07:02 +05:30
expose :milestone, using: Entities::Milestone
2015-12-23 02:04:40 +05:30
expose :merge_when_build_succeeds
2016-04-02 18:10:28 +05:30
expose :merge_status
2016-06-02 11:05:42 +05:30
expose :subscribed do |merge_request, options|
merge_request.subscribed?(options[:current_user])
end
expose :user_notes_count
2016-08-24 12:49:21 +05:30
expose :should_remove_source_branch?, as: :should_remove_source_branch
expose :force_remove_source_branch?, as: :force_remove_source_branch
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
class MergeRequestChanges < MergeRequest
expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _|
2016-09-13 17:45:13 +05:30
compare.raw_diffs(all_diffs: true).to_a
2015-04-26 12:48:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
class SSHKey < Grape::Entity
expose :id, :title, :key, :created_at
end
2015-09-25 12:07:36 +05:30
class SSHKeyWithUser < SSHKey
expose :user, using: Entities::UserFull
end
2014-09-02 18:07:02 +05:30
class Note < Grape::Entity
expose :id
expose :note, as: :body
expose :attachment_identifier, as: :attachment
expose :author, using: Entities::UserBasic
2016-06-02 11:05:42 +05:30
expose :created_at, :updated_at
2015-09-11 14:41:01 +05:30
expose :system?, as: :system
2015-12-23 02:04:40 +05:30
expose :noteable_id, :noteable_type
2015-11-26 14:37:03 +05:30
# upvote? and downvote? are deprecated, always return false
expose(:upvote?) { |note| false }
expose(:downvote?) { |note| false }
2014-09-02 18:07:02 +05:30
end
2016-06-22 15:30:34 +05:30
class AwardEmoji < Grape::Entity
expose :id
expose :name
expose :user, using: Entities::UserBasic
expose :created_at, :updated_at
expose :awardable_id, :awardable_type
end
2014-09-02 18:07:02 +05:30
class MRNote < Grape::Entity
expose :note
expose :author, using: Entities::UserBasic
end
2015-04-26 12:48:37 +05:30
class CommitNote < Grape::Entity
expose :note
2016-08-24 12:49:21 +05:30
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? }
2015-04-26 12:48:37 +05:30
expose :author, using: Entities::UserBasic
2015-09-25 12:07:36 +05:30
expose :created_at
2015-04-26 12:48:37 +05:30
end
2015-10-24 18:46:33 +05:30
class CommitStatus < Grape::Entity
expose :id, :sha, :ref, :status, :name, :target_url, :description,
2015-11-26 14:37:03 +05:30
:created_at, :started_at, :finished_at, :allow_failure
2015-10-24 18:46:33 +05:30
expose :author, using: Entities::UserBasic
end
2014-09-02 18:07:02 +05:30
class Event < Grape::Entity
expose :title, :project_id, :action_name
expose :target_id, :target_type, :author_id
expose :data, :target_title
expose :created_at
2015-12-23 02:04:40 +05:30
expose :note, using: Entities::Note, if: ->(event, options) { event.note? }
expose :author, using: Entities::UserBasic, if: ->(event, options) { event.author }
2015-04-26 12:48:37 +05:30
expose :author_username do |event, options|
if event.author
event.author.username
end
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
class ProjectGroupLink < Grape::Entity
expose :id, :project_id, :group_id, :group_access
end
2016-08-24 12:49:21 +05:30
class Todo < Grape::Entity
expose :id
expose :project, using: Entities::BasicProjectDetails
expose :author, using: Entities::UserBasic
expose :action_name
expose :target_type
expose :target do |todo, options|
Entities.const_get(todo.target_type).represent(todo.target, options)
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?
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
2014-09-02 18:07:02 +05:30
class Namespace < Grape::Entity
expose :id, :path, :kind
end
2016-09-13 17:45:13 +05:30
class MemberAccess < Grape::Entity
2015-04-26 12:48:37 +05:30
expose :access_level
2016-06-02 11:05:42 +05:30
expose :notification_level do |member, options|
if member.notification_setting
NotificationSetting.levels[member.notification_setting.level]
end
end
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
class ProjectAccess < MemberAccess
2016-06-02 11:05:42 +05:30
end
2016-09-13 17:45:13 +05:30
class GroupAccess < MemberAccess
2014-09-02 18:07:02 +05:30
end
2015-10-24 18:46:33 +05:30
class ProjectService < Grape::Entity
expose :id, :title, :created_at, :updated_at, :active
2016-09-13 17:45:13 +05:30
expose :push_events, :issues_events, :merge_requests_events
expose :tag_push_events, :note_events, :build_events, :pipeline_events
2015-10-24 18:46:33 +05:30
# 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
2014-09-02 18:07:02 +05:30
class ProjectWithAccess < Project
expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options|
2015-04-26 12:48:37 +05:30
project.project_members.find_by(user_id: options[:user].id)
2014-09-02 18:07:02 +05:30
end
expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group
2015-04-26 12:48:37 +05:30
project.group.group_members.find_by(user_id: options[:user].id)
2014-09-02 18:07:02 +05:30
end
end
end
end
class Label < Grape::Entity
2016-06-02 11:05:42 +05:30
expose :name, :color, :description
expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
expose :subscribed do |label, options|
label.subscribed?(options[:current_user])
end
2014-09-02 18:07:02 +05:30
end
class Compare < Grape::Entity
expose :commit, using: Entities::RepoCommit do |compare, options|
2015-09-11 14:41:01 +05:30
Commit.decorate(compare.commits, nil).last
2014-09-02 18:07:02 +05:30
end
expose :commits, using: Entities::RepoCommit do |compare, options|
2015-09-11 14:41:01 +05:30
Commit.decorate(compare.commits, nil)
2014-09-02 18:07:02 +05:30
end
expose :diffs, using: Entities::RepoDiff do |compare, options|
2016-06-02 11:05:42 +05:30
compare.diffs(all_diffs: true).to_a
2014-09-02 18:07:02 +05:30
end
expose :compare_timeout do |compare, options|
2016-06-02 11:05:42 +05:30
compare.diffs.overflow?
2014-09-02 18:07:02 +05:30
end
expose :same, as: :compare_same_ref
end
class Contributor < Grape::Entity
expose :name, :email, :commits, :additions, :deletions
end
2015-04-26 12:48:37 +05:30
class BroadcastMessage < Grape::Entity
expose :message, :starts_at, :ends_at, :color, :font
end
2015-09-11 14:41:01 +05:30
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
2015-09-11 14:41:01 +05:30
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
2016-06-02 11:05:42 +05:30
expose :default_group_visibility
2016-08-24 12:49:21 +05:30
expose :domain_whitelist
expose :domain_blacklist_enabled
expose :domain_blacklist
2015-09-11 14:41:01 +05:30
expose :user_oauth_applications
expose :after_sign_out_path
expose :container_registry_token_expire_delay
2016-08-24 12:49:21 +05:30
expose :repository_storage
2015-09-11 14:41:01 +05:30
end
2015-11-26 14:37:03 +05:30
class Release < Grape::Entity
expose :tag, as: :tag_name
expose :description
end
class RepoTag < Grape::Entity
2016-08-24 12:49:21 +05:30
expose :name, :message
2015-11-26 14:37:03 +05:30
2016-08-24 12:49:21 +05:30
expose :commit do |repo_tag, options|
options[:project].repository.commit(repo_tag.target)
2015-11-26 14:37:03 +05:30
end
2016-08-24 12:49:21 +05:30
expose :release, using: Entities::Release do |repo_tag, options|
options[:project].releases.find_by(tag: repo_tag.name)
2015-11-26 14:37:03 +05:30
end
end
2015-12-23 02:04:40 +05:30
class TriggerRequest < Grape::Entity
expose :id, :variables
end
class Runner < Grape::Entity
expose :id
expose :description
expose :active
expose :is_shared
expose :name
end
2016-04-02 18:10:28 +05:30
class RunnerDetails < Runner
expose :tag_list
2016-06-02 11:05:42 +05:30
expose :run_untagged
2016-06-22 15:30:34 +05:30
expose :locked
2016-04-02 18:10:28 +05:30
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 Build < Grape::Entity
expose :id, :status, :stage, :name, :ref, :tag, :coverage
expose :created_at, :started_at, :finished_at
expose :user, with: User
2016-04-02 18:10:28 +05:30
expose :artifacts_file, using: BuildArtifactFile, if: -> (build, opts) { build.artifacts? }
2016-06-22 15:30:34 +05:30
expose :commit, with: RepoCommit
expose :runner, with: Runner
end
class Trigger < Grape::Entity
expose :token, :created_at, :updated_at, :deleted_at, :last_used
end
class Variable < Grape::Entity
expose :key, :value
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
class Pipeline < Grape::Entity
expose :id, :status, :ref, :sha, :before_sha, :tag, :yaml_errors
expose :user, with: Entities::UserBasic
expose :created_at, :updated_at, :started_at, :finished_at, :committed_at
expose :duration
end
class Environment < Grape::Entity
expose :id, :name, :external_url
expose :project, using: Entities::Project
end
class EnvironmentBasic < Grape::Entity
expose :id, :name, :external_url
end
class Deployment < Grape::Entity
expose :id, :iid, :ref, :sha, :created_at
expose :user, using: Entities::UserBasic
expose :environment, using: Entities::EnvironmentBasic
expose :deployable, using: Entities::Build
end
2016-06-02 11:05:42 +05:30
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-06-22 15:30:34 +05:30
class TemplatesList < Grape::Entity
2016-06-02 11:05:42 +05:30
expose :name
end
2016-06-22 15:30:34 +05:30
class Template < Grape::Entity
2016-06-02 11:05:42 +05:30
expose :name, :content
end
2014-09-02 18:07:02 +05:30
end
end