api.rb 4.8 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
    LOG_FILENAME = Rails.root.join("log", "api_json.log")

7 8 9 10
    NO_SLASH_URL_PART_REGEX = %r{[^/]+}
    PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
    COMMIT_ENDPOINT_REQUIREMENTS = PROJECT_ENDPOINT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze

11
    use GrapeLogging::Middleware::RequestLogger,
12 13
        logger: Logger.new(LOG_FILENAME),
        formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
S
Stan Hu 已提交
14 15 16 17
        include: [
          GrapeLogging::Loggers::FilterParameters.new,
          GrapeLogging::Loggers::ClientEnv.new
        ]
18

19
    allow_access_with_scope :api
20
    prefix :api
21

O
Oswaldo Ferreira 已提交
22 23 24
    version %w(v3 v4), using: :path

    version 'v3', using: :path do
25
      helpers ::API::V3::Helpers
26
      helpers ::API::Helpers::CommonHelpers
27

R
Robert Schilling 已提交
28
      mount ::API::V3::AwardEmoji
R
Robert Schilling 已提交
29 30
      mount ::API::V3::Boards
      mount ::API::V3::Branches
R
Robert Schilling 已提交
31
      mount ::API::V3::BroadcastMessages
32
      mount ::API::V3::Builds
33
      mount ::API::V3::Commits
34
      mount ::API::V3::DeployKeys
R
Robert Schilling 已提交
35
      mount ::API::V3::Environments
36
      mount ::API::V3::Files
R
Robert Schilling 已提交
37
      mount ::API::V3::Groups
38
      mount ::API::V3::Issues
R
Robert Schilling 已提交
39
      mount ::API::V3::Labels
40
      mount ::API::V3::Members
R
Robert Schilling 已提交
41
      mount ::API::V3::MergeRequestDiffs
42
      mount ::API::V3::MergeRequests
43
      mount ::API::V3::Notes
44
      mount ::API::V3::Pipelines
R
Robert Schilling 已提交
45
      mount ::API::V3::ProjectHooks
J
Jarka Kadlecova 已提交
46
      mount ::API::V3::Milestones
O
Oswaldo Ferreira 已提交
47
      mount ::API::V3::Projects
48
      mount ::API::V3::ProjectSnippets
R
Robert Schilling 已提交
49
      mount ::API::V3::Repositories
R
Robert Schilling 已提交
50 51
      mount ::API::V3::Runners
      mount ::API::V3::Services
52
      mount ::API::V3::Settings
53
      mount ::API::V3::Snippets
54
      mount ::API::V3::Subscriptions
R
Robert Schilling 已提交
55 56
      mount ::API::V3::SystemHooks
      mount ::API::V3::Tags
57
      mount ::API::V3::Templates
R
Robert Schilling 已提交
58 59
      mount ::API::V3::Todos
      mount ::API::V3::Triggers
R
Robert Schilling 已提交
60
      mount ::API::V3::Users
R
Robert Schilling 已提交
61
      mount ::API::V3::Variables
O
Oswaldo Ferreira 已提交
62
    end
N
Nihad Abbasov 已提交
63

64
    before { header['X-Frame-Options'] = 'SAMEORIGIN' }
65

66
    # The locale is set to the current user's locale when `current_user` is loaded
67
    after { Gitlab::I18n.use_default_locale }
68

69 70 71 72
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
73
    rescue_from ActiveRecord::RecordNotFound do
74
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
75 76
    end

C
Connor Shea 已提交
77
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
78 79 80 81 82
    # 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 已提交
83 84
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
85 86
    end

87
    rescue_from Gitlab::Auth::TooManyIps do |e|
88
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
89 90
    end

F
Felix Gilcher 已提交
91
    rescue_from :all do |exception|
S
Stan Hu 已提交
92
      handle_api_exception(exception)
93 94
    end

N
Nihad Abbasov 已提交
95
    format :json
96 97
    content_type :txt, "text/plain"

98
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
99
    helpers ::SentryHelper
100
    helpers ::API::Helpers
101
    helpers ::API::Helpers::CommonHelpers
102

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

    route :any, '*path' do
161
      error!('404 Not Found', 404)
162
    end
N
Nihad Abbasov 已提交
163
  end
164
end