api.rb 4.0 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 6
    allow_access_with_scope :api

O
Oswaldo Ferreira 已提交
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

R
Robert Schilling 已提交
13
      mount ::API::V3::AwardEmoji
R
Robert Schilling 已提交
14 15
      mount ::API::V3::Boards
      mount ::API::V3::Branches
R
Robert Schilling 已提交
16
      mount ::API::V3::BroadcastMessages
17
      mount ::API::V3::Builds
18
      mount ::API::V3::Commits
19
      mount ::API::V3::DeployKeys
R
Robert Schilling 已提交
20
      mount ::API::V3::Environments
21
      mount ::API::V3::Files
R
Robert Schilling 已提交
22
      mount ::API::V3::Groups
23
      mount ::API::V3::Issues
R
Robert Schilling 已提交
24
      mount ::API::V3::Labels
25
      mount ::API::V3::Members
R
Robert Schilling 已提交
26
      mount ::API::V3::MergeRequestDiffs
27
      mount ::API::V3::MergeRequests
28
      mount ::API::V3::Notes
29
      mount ::API::V3::Pipelines
R
Robert Schilling 已提交
30
      mount ::API::V3::ProjectHooks
J
Jarka Kadlecova 已提交
31
      mount ::API::V3::Milestones
O
Oswaldo Ferreira 已提交
32
      mount ::API::V3::Projects
33
      mount ::API::V3::ProjectSnippets
R
Robert Schilling 已提交
34
      mount ::API::V3::Repositories
R
Robert Schilling 已提交
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
R
Robert Schilling 已提交
40 41
      mount ::API::V3::SystemHooks
      mount ::API::V3::Tags
42
      mount ::API::V3::Templates
R
Robert Schilling 已提交
43 44
      mount ::API::V3::Todos
      mount ::API::V3::Triggers
R
Robert Schilling 已提交
45
      mount ::API::V3::Users
R
Robert Schilling 已提交
46
      mount ::API::V3::Variables
O
Oswaldo Ferreira 已提交
47
    end
N
Nihad Abbasov 已提交
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

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

C
Connor Shea 已提交
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

C
Connor Shea 已提交
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

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

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

83
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
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
R
Robert Schilling 已提交
91
    mount ::API::Boards
92
    mount ::API::Branches
93
    mount ::API::BroadcastMessages
94
    mount ::API::Commits
R
Robert Schilling 已提交
95
    mount ::API::CommitStatuses
96
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
97
    mount ::API::Deployments
98
    mount ::API::Environments
M
Mark Fletcher 已提交
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
R
Robert Schilling 已提交
111
    mount ::API::MergeRequests
112 113
    mount ::API::Milestones
    mount ::API::Namespaces
114
    mount ::API::Notes
115
    mount ::API::NotificationSettings
Z
Z.J. van de Weg 已提交
116
    mount ::API::Pipelines
117
    mount ::API::PipelineSchedules
118
    mount ::API::ProjectHooks
119
    mount ::API::Projects
R
Robert Schilling 已提交
120
    mount ::API::ProjectSnippets
121
    mount ::API::Repositories
T
Tomasz Maczukin 已提交
122
    mount ::API::Runner
123
    mount ::API::Runners
124
    mount ::API::Services
125
    mount ::API::Session
126
    mount ::API::Settings
127
    mount ::API::SidekiqMetrics
128
    mount ::API::Snippets
129 130
    mount ::API::Subscriptions
    mount ::API::SystemHooks
131
    mount ::API::Tags
Z
ZJ van de Weg 已提交
132
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
133
    mount ::API::Todos
134
    mount ::API::Triggers
135
    mount ::API::Users
136
    mount ::API::Variables
S
Ini  
Shinya Maeda 已提交
137
    mount ::API::GroupVariables
R
Robert Schilling 已提交
138
    mount ::API::Version
139 140

    route :any, '*path' do
141
      error!('404 Not Found', 404)
142
    end
N
Nihad Abbasov 已提交
143
  end
144
end