api.rb 3.7 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 7

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

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

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

46 47
    before { allow_access_with_scope :api }

48 49 50 51
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
52
    rescue_from ActiveRecord::RecordNotFound do
53
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
54 55
    end

C
Connor Shea 已提交
56
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
57 58 59 60 61
    # 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 已提交
62 63
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
64 65
    end

66
    rescue_from Gitlab::Auth::TooManyIps do |e|
67
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
68 69
    end

F
Felix Gilcher 已提交
70
    rescue_from :all do |exception|
S
Stan Hu 已提交
71
      handle_api_exception(exception)
72 73
    end

N
Nihad Abbasov 已提交
74
    format :json
75 76
    content_type :txt, "text/plain"

77
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
78
    helpers ::SentryHelper
79 80
    helpers ::API::Helpers

81
    # Keep in alphabetical order
82
    mount ::API::AccessRequests
83
    mount ::API::AwardEmoji
R
Robert Schilling 已提交
84
    mount ::API::Boards
85
    mount ::API::Branches
86
    mount ::API::BroadcastMessages
87
    mount ::API::Commits
R
Robert Schilling 已提交
88
    mount ::API::CommitStatuses
89
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
90
    mount ::API::Deployments
91
    mount ::API::Environments
92 93 94
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
95
    mount ::API::Issues
96
    mount ::API::Jobs
97 98
    mount ::API::Keys
    mount ::API::Labels
99
    mount ::API::Lint
100
    mount ::API::Members
101
    mount ::API::MergeRequestDiffs
R
Robert Schilling 已提交
102
    mount ::API::MergeRequests
103 104
    mount ::API::Milestones
    mount ::API::Namespaces
105
    mount ::API::Notes
106
    mount ::API::NotificationSettings
Z
Z.J. van de Weg 已提交
107
    mount ::API::Pipelines
108
    mount ::API::ProjectHooks
109
    mount ::API::Projects
R
Robert Schilling 已提交
110
    mount ::API::ProjectSnippets
111
    mount ::API::Repositories
T
Tomasz Maczukin 已提交
112
    mount ::API::Runner
113
    mount ::API::Runners
114
    mount ::API::Services
115
    mount ::API::Session
116
    mount ::API::Settings
117
    mount ::API::SidekiqMetrics
118
    mount ::API::Snippets
119 120
    mount ::API::Subscriptions
    mount ::API::SystemHooks
121
    mount ::API::Tags
Z
ZJ van de Weg 已提交
122
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
123
    mount ::API::Todos
124
    mount ::API::Triggers
125
    mount ::API::Users
126
    mount ::API::Variables
R
Robert Schilling 已提交
127
    mount ::API::Version
128 129

    route :any, '*path' do
130
      error!('404 Not Found', 404)
131
    end
N
Nihad Abbasov 已提交
132
  end
133
end