BigW Consortium Gitlab

api.rb 4.01 KB
Newer Older
1
module API
Nihad Abbasov committed
2
  class API < Grape::API
Valery Sizov committed
3
    include APIGuard
4 5 6 7

    version %w(v3 v4), using: :path

    version 'v3', using: :path do
8
      helpers ::API::V3::Helpers
9
      helpers ::API::Helpers::CommonHelpers
10

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

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

51
    after { Gitlab::I18n.use_default_locale }
52

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

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

Connor Shea committed
61
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
62 63 64 65 66
    # 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 committed
67 68
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
69 70
    end

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

75
    rescue_from :all do |exception|
76
      handle_api_exception(exception)
77 78
    end

Nihad Abbasov committed
79
    format :json
80 81
    content_type :txt, "text/plain"

82
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
83
    helpers ::SentryHelper
84
    helpers ::API::Helpers
85
    helpers ::API::Helpers::CommonHelpers
86

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

    route :any, '*path' do
139
      error!('404 Not Found', 404)
140
    end
Nihad Abbasov committed
141
  end
142
end