api.rb 4.3 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  class API < Grape::API
V
Valery Sizov 已提交
3
    include APIGuard
O
Oswaldo Ferreira 已提交
4

5
    allow_access_with_scope :api
6
    prefix :api
7

O
Oswaldo Ferreira 已提交
8 9 10
    version %w(v3 v4), using: :path

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

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

50
    before { header['X-Frame-Options'] = 'SAMEORIGIN' }
51

52
    # The locale is set to the current user's locale when `current_user` is loaded
53
    after { Gitlab::I18n.use_default_locale }
54

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

N
Nihad Abbasov 已提交
59
    rescue_from ActiveRecord::RecordNotFound do
60
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
61 62
    end

C
Connor Shea 已提交
63
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
64 65 66 67 68
    # 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

C
Connor Shea 已提交
69 70
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
71 72
    end

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

F
Felix Gilcher 已提交
77
    rescue_from :all do |exception|
S
Stan Hu 已提交
78
      handle_api_exception(exception)
79 80
    end

N
Nihad Abbasov 已提交
81
    format :json
82 83
    content_type :txt, "text/plain"

84
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
85
    helpers ::SentryHelper
86
    helpers ::API::Helpers
87
    helpers ::API::Helpers::CommonHelpers
88

89 90 91
    NO_SLASH_URL_PART_REGEX = %r{[^/]+}
    PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze

92
    # Keep in alphabetical order
93
    mount ::API::AccessRequests
94
    mount ::API::AwardEmoji
R
Robert Schilling 已提交
95
    mount ::API::Boards
96
    mount ::API::Branches
97
    mount ::API::BroadcastMessages
98
    mount ::API::CircuitBreakers
99
    mount ::API::Commits
R
Robert Schilling 已提交
100
    mount ::API::CommitStatuses
101
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
102
    mount ::API::Deployments
103
    mount ::API::Environments
M
Mark Fletcher 已提交
104
    mount ::API::Events
105
    mount ::API::Features
106 107 108
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
109
    mount ::API::Issues
110
    mount ::API::Jobs
111
    mount ::API::JobArtifacts
112 113
    mount ::API::Keys
    mount ::API::Labels
114
    mount ::API::Lint
115
    mount ::API::Members
116
    mount ::API::MergeRequestDiffs
R
Robert Schilling 已提交
117
    mount ::API::MergeRequests
F
Felipe Artur 已提交
118 119
    mount ::API::ProjectMilestones
    mount ::API::GroupMilestones
120
    mount ::API::Namespaces
121
    mount ::API::Notes
122
    mount ::API::NotificationSettings
Z
Z.J. van de Weg 已提交
123
    mount ::API::Pipelines
124
    mount ::API::PipelineSchedules
125
    mount ::API::ProjectHooks
126
    mount ::API::Projects
R
Robert Schilling 已提交
127
    mount ::API::ProjectSnippets
E
Eric 已提交
128
    mount ::API::ProtectedBranches
129
    mount ::API::Repositories
T
Tomasz Maczukin 已提交
130
    mount ::API::Runner
131
    mount ::API::Runners
132
    mount ::API::Services
133
    mount ::API::Session
134
    mount ::API::Settings
135
    mount ::API::SidekiqMetrics
136
    mount ::API::Snippets
137 138
    mount ::API::Subscriptions
    mount ::API::SystemHooks
139
    mount ::API::Tags
Z
ZJ van de Weg 已提交
140
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
141
    mount ::API::Todos
142
    mount ::API::Triggers
143
    mount ::API::Users
144
    mount ::API::Variables
S
Ini  
Shinya Maeda 已提交
145
    mount ::API::GroupVariables
R
Robert Schilling 已提交
146
    mount ::API::Version
147 148

    route :any, '*path' do
149
      error!('404 Not Found', 404)
150
    end
N
Nihad Abbasov 已提交
151
  end
152
end