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