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

189 lines
5.9 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
class API < Grape::API
2015-04-26 12:48:37 +05:30
include APIGuard
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
LOG_FILENAME = Rails.root.join("log", "api_json.log")
2019-07-31 22:56:46 +05:30
NO_SLASH_URL_PART_REGEX = %r{[^/]+}.freeze
2019-02-15 15:39:39 +05:30
NAMESPACE_OR_PROJECT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
COMMIT_ENDPOINT_REQUIREMENTS = NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze
2019-03-02 22:35:43 +05:30
USER_REQUIREMENTS = { user_id: NO_SLASH_URL_PART_REGEX }.freeze
2019-09-30 21:07:59 +05:30
LOG_FILTERS = ::Rails.application.config.filter_parameters + [/^output$/]
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
insert_before Grape::Middleware::Error,
GrapeLogging::Middleware::RequestLogger,
logger: Logger.new(LOG_FILENAME),
formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
include: [
2019-09-30 21:07:59 +05:30
GrapeLogging::Loggers::FilterParameters.new(LOG_FILTERS),
2019-12-04 20:38:33 +05:30
Gitlab::GrapeLogging::Loggers::ClientEnvLogger.new,
2018-11-20 20:47:30 +05:30
Gitlab::GrapeLogging::Loggers::RouteLogger.new,
2018-11-08 19:23:39 +05:30
Gitlab::GrapeLogging::Loggers::UserLogger.new,
2019-12-26 22:10:19 +05:30
Gitlab::GrapeLogging::Loggers::ExceptionLogger.new,
2018-11-20 20:47:30 +05:30
Gitlab::GrapeLogging::Loggers::QueueDurationLogger.new,
2019-02-15 15:39:39 +05:30
Gitlab::GrapeLogging::Loggers::PerfLogger.new,
Gitlab::GrapeLogging::Loggers::CorrelationIdLogger.new
2018-11-08 19:23:39 +05:30
]
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
allow_access_with_scope :api
prefix :api
2017-08-17 22:00:37 +05:30
version 'v3', using: :path do
2018-11-08 19:23:39 +05:30
route :any, '*path' do
error!('API V3 is no longer supported. Use API V4 instead.', 410)
end
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
version 'v4', using: :path
2018-03-17 18:26:18 +05:30
before do
header['X-Frame-Options'] = 'SAMEORIGIN'
header['X-Content-Type-Options'] = 'nosniff'
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
# The locale is set to the current user's locale when `current_user` is loaded
after { Gitlab::I18n.use_default_locale }
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
rescue_from Gitlab::Access::AccessDeniedError do
rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
end
2014-09-02 18:07:02 +05:30
rescue_from ActiveRecord::RecordNotFound do
2015-04-26 12:48:37 +05:30
rack_response({ 'message' => '404 Not found' }.to_json, 404)
2014-09-02 18:07:02 +05:30
end
2019-09-30 21:07:59 +05:30
rescue_from(
::ActiveRecord::StaleObjectError,
::Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
) do
2019-02-15 15:39:39 +05:30
rack_response({ 'message' => '409 Conflict: Resource lock' }.to_json, 409)
end
2018-05-09 12:01:36 +05:30
rescue_from UploadedFile::InvalidPathError do |e|
rack_response({ 'message' => e.message }.to_json, 400)
end
rescue_from ObjectStorage::RemoteStoreError do |e|
rack_response({ 'message' => e.message }.to_json, 500)
end
2016-09-13 17:45:13 +05:30
# Retain 405 error rather than a 500 error for Grape 0.15.0+.
2017-08-17 22:00:37 +05:30
# https://github.com/ruby-grape/grape/blob/a3a28f5b5dfbb2797442e006dbffd750b27f2a76/UPGRADING.md#changes-to-method-not-allowed-routes
rescue_from Grape::Exceptions::MethodNotAllowed do |e|
error! e.message, e.status, e.headers
end
2016-09-13 17:45:13 +05:30
rescue_from Grape::Exceptions::Base do |e|
error! e.message, e.status, e.headers
end
2017-08-17 22:00:37 +05:30
rescue_from Gitlab::Auth::TooManyIps do |e|
rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
end
2014-09-02 18:07:02 +05:30
rescue_from :all do |exception|
2016-09-29 09:46:39 +05:30
handle_api_exception(exception)
2014-09-02 18:07:02 +05:30
end
format :json
content_type :txt, "text/plain"
2016-06-02 11:05:42 +05:30
# Ensure the namespace is right, otherwise we might load Grape::API::Helpers
helpers ::API::Helpers
2017-08-17 22:00:37 +05:30
helpers ::API::Helpers::CommonHelpers
2016-06-02 11:05:42 +05:30
2016-11-03 12:29:30 +05:30
# Keep in alphabetical order
2016-09-13 17:45:13 +05:30
mount ::API::AccessRequests
2018-03-17 18:26:18 +05:30
mount ::API::Applications
2018-11-08 19:23:39 +05:30
mount ::API::Avatar
2016-06-22 15:30:34 +05:30
mount ::API::AwardEmoji
2018-03-27 19:54:05 +05:30
mount ::API::Badges
2016-11-03 12:29:30 +05:30
mount ::API::Boards
2016-06-22 15:30:34 +05:30
mount ::API::Branches
2016-09-29 09:46:39 +05:30
mount ::API::BroadcastMessages
2016-06-22 15:30:34 +05:30
mount ::API::Commits
2016-11-03 12:29:30 +05:30
mount ::API::CommitStatuses
2016-06-22 15:30:34 +05:30
mount ::API::DeployKeys
2016-09-13 17:45:13 +05:30
mount ::API::Deployments
mount ::API::Environments
2017-09-10 17:25:29 +05:30
mount ::API::Events
mount ::API::Features
2016-06-22 15:30:34 +05:30
mount ::API::Files
2018-03-27 19:54:05 +05:30
mount ::API::GroupBoards
2019-09-30 21:07:59 +05:30
mount ::API::GroupClusters
2019-12-26 22:10:19 +05:30
mount ::API::GroupExport
2019-03-02 22:35:43 +05:30
mount ::API::GroupLabels
2018-03-17 18:26:18 +05:30
mount ::API::GroupMilestones
2018-11-20 20:47:30 +05:30
mount ::API::Groups
2019-10-12 21:52:04 +05:30
mount ::API::GroupContainerRepositories
2018-11-20 20:47:30 +05:30
mount ::API::GroupVariables
2019-03-02 22:35:43 +05:30
mount ::API::ImportGithub
2019-12-04 20:38:33 +05:30
mount ::API::Internal::Base
mount ::API::Internal::Pages
2016-06-02 11:05:42 +05:30
mount ::API::Issues
2018-03-17 18:26:18 +05:30
mount ::API::JobArtifacts
2018-11-20 20:47:30 +05:30
mount ::API::Jobs
2016-06-22 15:30:34 +05:30
mount ::API::Keys
mount ::API::Labels
2016-09-29 09:46:39 +05:30
mount ::API::Lint
2018-11-08 19:23:39 +05:30
mount ::API::Markdown
2016-09-13 17:45:13 +05:30
mount ::API::Members
2016-11-03 12:29:30 +05:30
mount ::API::MergeRequestDiffs
2016-06-02 11:05:42 +05:30
mount ::API::MergeRequests
2016-06-22 15:30:34 +05:30
mount ::API::Namespaces
2016-06-02 11:05:42 +05:30
mount ::API::Notes
2018-03-27 19:54:05 +05:30
mount ::API::Discussions
2018-11-20 20:47:30 +05:30
mount ::API::ResourceLabelEvents
2016-09-29 09:46:39 +05:30
mount ::API::NotificationSettings
2018-03-17 18:26:18 +05:30
mount ::API::PagesDomains
2016-09-13 17:45:13 +05:30
mount ::API::Pipelines
2017-09-10 17:25:29 +05:30
mount ::API::PipelineSchedules
2019-02-15 15:39:39 +05:30
mount ::API::ProjectClusters
2019-10-12 21:52:04 +05:30
mount ::API::ProjectContainerRepositories
2019-05-18 00:54:41 +05:30
mount ::API::ProjectEvents
2018-03-27 19:54:05 +05:30
mount ::API::ProjectExport
mount ::API::ProjectImport
2016-06-02 11:05:42 +05:30
mount ::API::ProjectHooks
2018-03-17 18:26:18 +05:30
mount ::API::ProjectMilestones
2018-11-20 20:47:30 +05:30
mount ::API::Projects
2018-05-09 12:01:36 +05:30
mount ::API::ProjectSnapshots
2016-11-03 12:29:30 +05:30
mount ::API::ProjectSnippets
2019-07-07 11:18:12 +05:30
mount ::API::ProjectStatistics
2018-12-05 23:21:45 +05:30
mount ::API::ProjectTemplates
2017-09-10 17:25:29 +05:30
mount ::API::ProtectedBranches
2018-11-20 20:47:30 +05:30
mount ::API::ProtectedTags
2019-02-15 15:39:39 +05:30
mount ::API::Releases
mount ::API::Release::Links
2016-06-22 15:30:34 +05:30
mount ::API::Repositories
2017-08-17 22:00:37 +05:30
mount ::API::Runner
2016-06-22 15:30:34 +05:30
mount ::API::Runners
2018-03-17 18:26:18 +05:30
mount ::API::Search
2016-06-02 11:05:42 +05:30
mount ::API::Services
mount ::API::Settings
2016-06-22 15:30:34 +05:30
mount ::API::SidekiqMetrics
2017-08-17 22:00:37 +05:30
mount ::API::Snippets
2019-12-04 20:38:33 +05:30
mount ::API::Statistics
2018-12-13 13:39:08 +05:30
mount ::API::Submodules
2016-06-22 15:30:34 +05:30
mount ::API::Subscriptions
2019-02-15 15:39:39 +05:30
mount ::API::Suggestions
2016-06-22 15:30:34 +05:30
mount ::API::SystemHooks
2016-06-02 11:05:42 +05:30
mount ::API::Tags
2016-06-22 15:30:34 +05:30
mount ::API::Templates
2016-08-24 12:49:21 +05:30
mount ::API::Todos
2016-06-02 11:05:42 +05:30
mount ::API::Triggers
2019-09-30 21:07:59 +05:30
mount ::API::UserCounts
2016-06-22 15:30:34 +05:30
mount ::API::Users
2016-06-02 11:05:42 +05:30
mount ::API::Variables
2016-11-03 12:29:30 +05:30
mount ::API::Version
2018-03-17 18:26:18 +05:30
mount ::API::Wikis
2016-11-03 12:29:30 +05:30
route :any, '*path' do
error!('404 Not Found', 404)
end
2014-09-02 18:07:02 +05:30
end
end
2019-12-04 20:38:33 +05:30
API::API.prepend_if_ee('::EE::API::API')