api.rb 4.04 KB
Newer Older
1
module API
Nihad Abbasov's avatar
Nihad Abbasov committed
2
  class API < Grape::API
Valery Sizov's avatar
Valery Sizov committed
3
    include APIGuard
4

5 6
    allow_access_with_scope :api

7 8 9
    version %w(v3 v4), using: :path

    version 'v3', using: :path do
10
      helpers ::API::V3::Helpers
11
      helpers ::API::Helpers::CommonHelpers
12

Robert Schilling's avatar
Robert Schilling committed
13
      mount ::API::V3::AwardEmoji
Robert Schilling's avatar
Robert Schilling committed
14 15
      mount ::API::V3::Boards
      mount ::API::V3::Branches
Robert Schilling's avatar
Robert Schilling committed
16
      mount ::API::V3::BroadcastMessages
17
      mount ::API::V3::Builds
18
      mount ::API::V3::Commits
19
      mount ::API::V3::DeployKeys
Robert Schilling's avatar
Robert Schilling committed
20
      mount ::API::V3::Environments
21
      mount ::API::V3::Files
Robert Schilling's avatar
Robert Schilling committed
22
      mount ::API::V3::Groups
23
      mount ::API::V3::Issues
Robert Schilling's avatar
Robert Schilling committed
24
      mount ::API::V3::Labels
25
      mount ::API::V3::Members
Robert Schilling's avatar
Robert Schilling committed
26
      mount ::API::V3::MergeRequestDiffs
27
      mount ::API::V3::MergeRequests
28
      mount ::API::V3::Notes
29
      mount ::API::V3::Pipelines
Robert Schilling's avatar
Robert Schilling committed
30
      mount ::API::V3::ProjectHooks
31
      mount ::API::V3::Milestones
32
      mount ::API::V3::Projects
33
      mount ::API::V3::ProjectSnippets
Robert Schilling's avatar
Robert Schilling committed
34
      mount ::API::V3::Repositories
Robert Schilling's avatar
Robert Schilling committed
35 36
      mount ::API::V3::Runners
      mount ::API::V3::Services
37
      mount ::API::V3::Settings
38
      mount ::API::V3::Snippets
39
      mount ::API::V3::Subscriptions
Robert Schilling's avatar
Robert Schilling committed
40 41
      mount ::API::V3::SystemHooks
      mount ::API::V3::Tags
42
      mount ::API::V3::Templates
Robert Schilling's avatar
Robert Schilling committed
43 44
      mount ::API::V3::Todos
      mount ::API::V3::Triggers
Robert Schilling's avatar
Robert Schilling committed
45
      mount ::API::V3::Users
Robert Schilling's avatar
Robert Schilling committed
46
      mount ::API::V3::Variables
47
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
48

49
    before { header['X-Frame-Options'] = 'SAMEORIGIN' }
50
    before { Gitlab::I18n.locale = current_user&.preferred_language }
51

52
    after { Gitlab::I18n.use_default_locale }
53

54 55 56 57
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

58
    rescue_from ActiveRecord::RecordNotFound do
59
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
60 61
    end

Connor Shea's avatar
Connor Shea committed
62
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
63 64 65 66 67
    # 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

Connor Shea's avatar
Connor Shea committed
68 69
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
70 71
    end

72
    rescue_from Gitlab::Auth::TooManyIps do |e|
73
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
74 75
    end

76
    rescue_from :all do |exception|
Stan Hu's avatar
Stan Hu committed
77
      handle_api_exception(exception)
78 79
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
80
    format :json
81 82
    content_type :txt, "text/plain"

83
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
Stan Hu's avatar
Stan Hu committed
84
    helpers ::SentryHelper
85
    helpers ::API::Helpers
86
    helpers ::API::Helpers::CommonHelpers
87

88
    # Keep in alphabetical order
89
    mount ::API::AccessRequests
90
    mount ::API::AwardEmoji
Robert Schilling's avatar
Robert Schilling committed
91
    mount ::API::Boards
92
    mount ::API::Branches
93
    mount ::API::BroadcastMessages
94
    mount ::API::Commits
Robert Schilling's avatar
Robert Schilling committed
95
    mount ::API::CommitStatuses
96
    mount ::API::DeployKeys
Z.J. van de Weg's avatar
Z.J. van de Weg committed
97
    mount ::API::Deployments
98
    mount ::API::Environments
Mark Fletcher's avatar
Mark Fletcher committed
99
    mount ::API::Events
100
    mount ::API::Features
101 102 103
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
104
    mount ::API::Issues
105
    mount ::API::Jobs
106 107
    mount ::API::Keys
    mount ::API::Labels
108
    mount ::API::Lint
109
    mount ::API::Members
110
    mount ::API::MergeRequestDiffs
Robert Schilling's avatar
Robert Schilling committed
111
    mount ::API::MergeRequests
112 113
    mount ::API::ProjectMilestones
    mount ::API::GroupMilestones
114
    mount ::API::Namespaces
115
    mount ::API::Notes
116
    mount ::API::NotificationSettings
117
    mount ::API::Pipelines
118
    mount ::API::PipelineSchedules
119
    mount ::API::ProjectHooks
120
    mount ::API::Projects
Robert Schilling's avatar
Robert Schilling committed
121
    mount ::API::ProjectSnippets
122
    mount ::API::Repositories
123
    mount ::API::Runner
124
    mount ::API::Runners
125
    mount ::API::Services
126
    mount ::API::Session
127
    mount ::API::Settings
128
    mount ::API::SidekiqMetrics
129
    mount ::API::Snippets
130 131
    mount ::API::Subscriptions
    mount ::API::SystemHooks
132
    mount ::API::Tags
ZJ van de Weg's avatar
ZJ van de Weg committed
133
    mount ::API::Templates
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
134
    mount ::API::Todos
135
    mount ::API::Triggers
136
    mount ::API::Users
137
    mount ::API::Variables
Robert Schilling's avatar
Robert Schilling committed
138
    mount ::API::Version
139 140

    route :any, '*path' do
141
      error!('404 Not Found', 404)
142
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
143
  end
144
end