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")
|
|
|
|
|
2017-09-23 09:21:32 -04:00
|
|
|
NO_SLASH_URL_PART_REGEX = %r{[^/]+}
|
|
|
|
PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
|
|
|
|
COMMIT_ENDPOINT_REQUIREMENTS = PROJECT_ENDPOINT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze
|
|
|
|
|
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: [
|
|
|
|
GrapeLogging::Loggers::FilterParameters.new,
|
|
|
|
GrapeLogging::Loggers::ClientEnv.new,
|
|
|
|
Gitlab::GrapeLogging::Loggers::UserLogger.new
|
|
|
|
]
|
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 %w(v3 v4), using: :path
|
|
|
|
|
|
|
|
version 'v3', using: :path do
|
2017-02-25 03:44:09 -05:00
|
|
|
helpers ::API::V3::Helpers
|
2017-05-04 08:11:15 -04:00
|
|
|
helpers ::API::Helpers::CommonHelpers
|
2017-02-25 03:44:09 -05:00
|
|
|
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::AwardEmoji
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::Boards
|
|
|
|
mount ::API::V3::Branches
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::BroadcastMessages
|
2017-02-22 05:13:59 -05:00
|
|
|
mount ::API::V3::Builds
|
2017-02-02 09:24:30 -05:00
|
|
|
mount ::API::V3::Commits
|
2017-02-06 08:39:22 -05:00
|
|
|
mount ::API::V3::DeployKeys
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::Environments
|
2017-02-02 09:24:30 -05:00
|
|
|
mount ::API::V3::Files
|
2017-02-24 03:29:53 -05:00
|
|
|
mount ::API::V3::Groups
|
2017-01-25 16:35:27 -05:00
|
|
|
mount ::API::V3::Issues
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::Labels
|
2017-02-09 07:39:39 -05:00
|
|
|
mount ::API::V3::Members
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::MergeRequestDiffs
|
2017-01-25 16:35:27 -05:00
|
|
|
mount ::API::V3::MergeRequests
|
2017-02-20 09:35:05 -05:00
|
|
|
mount ::API::V3::Notes
|
2017-01-30 06:11:58 -05:00
|
|
|
mount ::API::V3::Pipelines
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::ProjectHooks
|
2017-02-09 09:28:19 -05:00
|
|
|
mount ::API::V3::Milestones
|
2017-01-30 12:41:56 -05:00
|
|
|
mount ::API::V3::Projects
|
2017-02-06 08:25:49 -05:00
|
|
|
mount ::API::V3::ProjectSnippets
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::Repositories
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::Runners
|
|
|
|
mount ::API::V3::Services
|
2017-02-17 08:50:02 -05:00
|
|
|
mount ::API::V3::Settings
|
2017-02-16 10:42:17 -05:00
|
|
|
mount ::API::V3::Snippets
|
2017-02-17 04:45:05 -05:00
|
|
|
mount ::API::V3::Subscriptions
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::SystemHooks
|
|
|
|
mount ::API::V3::Tags
|
2017-01-27 13:53:27 -05:00
|
|
|
mount ::API::V3::Templates
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::Todos
|
|
|
|
mount ::API::V3::Triggers
|
2017-02-06 13:38:17 -05:00
|
|
|
mount ::API::V3::Users
|
2017-02-20 14:32:44 -05:00
|
|
|
mount ::API::V3::Variables
|
2017-01-30 12:41:56 -05:00
|
|
|
end
|
2012-07-04 03:48:00 -04:00
|
|
|
|
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
|
|
|
|
|
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
|
2016-08-18 20:06:33 -04:00
|
|
|
helpers ::SentryHelper
|
2016-04-15 11:35:40 -04:00
|
|
|
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
|
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
|
2017-05-17 12:17:15 -04:00
|
|
|
mount ::API::CircuitBreakers
|
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
|
2016-06-10 02:57:56 -04:00
|
|
|
mount ::API::Groups
|
2017-12-06 14:07:47 -05:00
|
|
|
mount ::API::GroupMilestones
|
2016-06-10 02:57:56 -04:00
|
|
|
mount ::API::Internal
|
2016-04-15 11:35:40 -04:00
|
|
|
mount ::API::Issues
|
2017-02-22 05:13:59 -05:00
|
|
|
mount ::API::Jobs
|
2017-09-06 05:20:12 -04:00
|
|
|
mount ::API::JobArtifacts
|
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
|
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
|
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
|
2016-06-10 02:57:56 -04:00
|
|
|
mount ::API::Projects
|
2017-12-06 14:07:47 -05:00
|
|
|
mount ::API::ProjectMilestones
|
2018-04-03 13:57:55 -04:00
|
|
|
mount ::API::ProjectSnapshots
|
2016-10-13 06:52:51 -04:00
|
|
|
mount ::API::ProjectSnippets
|
2017-08-02 06:16:17 -04:00
|
|
|
mount ::API::ProtectedBranches
|
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
|
2016-06-10 02:57:56 -04:00
|
|
|
mount ::API::Subscriptions
|
|
|
|
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
|
2016-06-10 02:57:56 -04:00
|
|
|
mount ::API::Users
|
2016-04-15 11:35:40 -04:00
|
|
|
mount ::API::Variables
|
2017-07-18 08:40:35 -04:00
|
|
|
mount ::API::GroupVariables
|
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
|