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

171 lines
5 KiB
Ruby
Raw Normal View History

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")
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
use 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-10 17:25:29 +05:30
allow_access_with_scope :api
prefix :api
2017-08-17 22:00:37 +05:30
version %w(v3 v4), using: :path
version 'v3', using: :path do
helpers ::API::V3::Helpers
helpers ::API::Helpers::CommonHelpers
mount ::API::V3::AwardEmoji
mount ::API::V3::Boards
mount ::API::V3::Branches
mount ::API::V3::BroadcastMessages
mount ::API::V3::Builds
mount ::API::V3::Commits
mount ::API::V3::DeployKeys
mount ::API::V3::Environments
mount ::API::V3::Files
mount ::API::V3::Groups
mount ::API::V3::Issues
mount ::API::V3::Labels
mount ::API::V3::Members
mount ::API::V3::MergeRequestDiffs
mount ::API::V3::MergeRequests
mount ::API::V3::Notes
mount ::API::V3::Pipelines
mount ::API::V3::ProjectHooks
mount ::API::V3::Milestones
mount ::API::V3::Projects
mount ::API::V3::ProjectSnippets
mount ::API::V3::Repositories
mount ::API::V3::Runners
mount ::API::V3::Services
mount ::API::V3::Settings
mount ::API::V3::Snippets
mount ::API::V3::Subscriptions
mount ::API::V3::SystemHooks
mount ::API::V3::Tags
mount ::API::V3::Templates
mount ::API::V3::Todos
mount ::API::V3::Triggers
mount ::API::V3::Users
mount ::API::V3::Variables
end
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
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
2016-09-29 09:46:39 +05:30
helpers ::SentryHelper
2016-06-02 11:05:42 +05:30
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
2016-06-22 15:30:34 +05:30
mount ::API::AwardEmoji
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
2017-09-10 17:25:29 +05:30
mount ::API::CircuitBreakers
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
mount ::API::Groups
2018-03-17 18:26:18 +05:30
mount ::API::GroupMilestones
2016-06-22 15:30:34 +05:30
mount ::API::Internal
2016-06-02 11:05:42 +05:30
mount ::API::Issues
2017-08-17 22:00:37 +05:30
mount ::API::Jobs
2018-03-17 18:26:18 +05:30
mount ::API::JobArtifacts
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
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
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
2016-06-02 11:05:42 +05:30
mount ::API::ProjectHooks
2016-06-22 15:30:34 +05:30
mount ::API::Projects
2018-03-17 18:26:18 +05:30
mount ::API::ProjectMilestones
2016-11-03 12:29:30 +05:30
mount ::API::ProjectSnippets
2017-09-10 17:25:29 +05:30
mount ::API::ProtectedBranches
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
2016-06-22 15:30:34 +05:30
mount ::API::Subscriptions
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
2016-06-22 15:30:34 +05:30
mount ::API::Users
2016-06-02 11:05:42 +05:30
mount ::API::Variables
2017-09-10 17:25:29 +05:30
mount ::API::GroupVariables
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