Commit 634cbd71380f595f6f44ce93399b92fbee37f98f

Authored by Dmitriy Zaporozhets
1 parent e6224942

Refactor API classes. So api classes like Gitlab::Issues become API::Issues

app/controllers/application_controller.rb
... ... @@ -152,7 +152,7 @@ class ApplicationController < ActionController::Base
152 152  
153 153 def add_gon_variables
154 154 gon.default_issues_tracker = Project.issues_tracker.default_value
155   - gon.api_version = Gitlab::API.version
  155 + gon.api_version = API::API.version
156 156 gon.api_token = current_user.private_token if current_user
157 157 gon.gravatar_url = request.ssl? ? Gitlab.config.gravatar.ssl_url : Gitlab.config.gravatar.plain_url
158 158 gon.relative_url_root = Gitlab.config.gitlab.relative_url_root
... ...
config/routes.rb
1 1 require 'sidekiq/web'
  2 +require 'api/api'
2 3  
3 4 Gitlab::Application.routes.draw do
4 5 #
... ... @@ -7,9 +8,8 @@ Gitlab::Application.routes.draw do
7 8 get 'search' => "search#show"
8 9  
9 10 # API
10   - require 'api'
11   - Gitlab::API.logger Rails.logger
12   - mount Gitlab::API => '/api'
  11 + API::API.logger Rails.logger
  12 + mount API::API => '/api'
13 13  
14 14 constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? }
15 15 constraints constraint do
... ...
lib/api.rb
... ... @@ -1,38 +0,0 @@
1   -Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file}
2   -
3   -module Gitlab
4   - class API < Grape::API
5   - version 'v3', using: :path
6   -
7   - rescue_from ActiveRecord::RecordNotFound do
8   - rack_response({'message' => '404 Not found'}.to_json, 404)
9   - end
10   -
11   - rescue_from :all do |exception|
12   - # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
13   - # why is this not wrapped in something reusable?
14   - trace = exception.backtrace
15   -
16   - message = "\n#{exception.class} (#{exception.message}):\n"
17   - message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
18   - message << " " << trace.join("\n ")
19   -
20   - API.logger.add Logger::FATAL, message
21   - rack_response({'message' => '500 Internal Server Error'}, 500)
22   - end
23   -
24   - format :json
25   - helpers APIHelpers
26   -
27   - mount Groups
28   - mount Users
29   - mount Projects
30   - mount Issues
31   - mount Milestones
32   - mount Session
33   - mount MergeRequests
34   - mount Notes
35   - mount Internal
36   - mount SystemHooks
37   - end
38   -end
lib/api/api.rb 0 → 100644
... ... @@ -0,0 +1,38 @@
  1 +Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file}
  2 +
  3 +module API
  4 + class API < Grape::API
  5 + version 'v3', using: :path
  6 +
  7 + rescue_from ActiveRecord::RecordNotFound do
  8 + rack_response({'message' => '404 Not found'}.to_json, 404)
  9 + end
  10 +
  11 + rescue_from :all do |exception|
  12 + # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
  13 + # why is this not wrapped in something reusable?
  14 + trace = exception.backtrace
  15 +
  16 + message = "\n#{exception.class} (#{exception.message}):\n"
  17 + message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
  18 + message << " " << trace.join("\n ")
  19 +
  20 + API.logger.add Logger::FATAL, message
  21 + rack_response({'message' => '500 Internal Server Error'}, 500)
  22 + end
  23 +
  24 + format :json
  25 + helpers APIHelpers
  26 +
  27 + mount Groups
  28 + mount Users
  29 + mount Projects
  30 + mount Issues
  31 + mount Milestones
  32 + mount Session
  33 + mount MergeRequests
  34 + mount Notes
  35 + mount Internal
  36 + mount SystemHooks
  37 + end
  38 +end
... ...
lib/api/entities.rb
1   -module Gitlab
  1 +module API
2 2 module Entities
3 3 class User < Grape::Entity
4 4 expose :id, :username, :email, :name, :bio, :skype, :linkedin, :twitter,
... ...
lib/api/groups.rb
1   -module Gitlab
  1 +module API
2 2 # groups API
3 3 class Groups < Grape::API
4 4 before { authenticate! }
... ...
lib/api/helpers.rb
1   -module Gitlab
  1 +module API
2 2 module APIHelpers
3 3 def current_user
4 4 @current_user ||= User.find_by_authentication_token(params[:private_token] || env["HTTP_PRIVATE_TOKEN"])
... ...
lib/api/internal.rb
1   -module Gitlab
  1 +module API
2 2 # Internal access API
3 3 class Internal < Grape::API
4 4 namespace 'internal' do
... ... @@ -58,7 +58,7 @@ module Gitlab
58 58  
59 59 get "/check" do
60 60 {
61   - api_version: Gitlab::API.version,
  61 + api_version: API.version,
62 62 gitlab_version: Gitlab::VERSION,
63 63 gitlab_rev: Gitlab::REVISION,
64 64 }
... ...
lib/api/issues.rb
1   -module Gitlab
  1 +module API
2 2 # Issues API
3 3 class Issues < Grape::API
4 4 before { authenticate! }
... ...
lib/api/merge_requests.rb
1   -module Gitlab
  1 +module API
2 2 # MergeRequest API
3 3 class MergeRequests < Grape::API
4 4 before { authenticate! }
... ...
lib/api/milestones.rb
1   -module Gitlab
  1 +module API
2 2 # Milestones API
3 3 class Milestones < Grape::API
4 4 before { authenticate! }
... ...
lib/api/notes.rb
1   -module Gitlab
  1 +module API
2 2 # Notes API
3 3 class Notes < Grape::API
4 4 before { authenticate! }
... ...
lib/api/projects.rb
1   -module Gitlab
  1 +module API
2 2 # Projects API
3 3 class Projects < Grape::API
4 4 before { authenticate! }
... ...
lib/api/session.rb
1   -module Gitlab
  1 +module API
2 2 # Users API
3 3 class Session < Grape::API
4 4 # Login to get token
... ...
lib/api/system_hooks.rb
1   -module Gitlab
  1 +module API
2 2 # Hooks API
3 3 class SystemHooks < Grape::API
4 4 before {
... ...
lib/api/users.rb
1   -module Gitlab
  1 +module API
2 2 # Users API
3 3 class Users < Grape::API
4 4 before { authenticate! }
... ...
spec/requests/api/groups_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user1) { create(:user) }
... ...
spec/requests/api/internal_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ... @@ -12,7 +12,7 @@ describe Gitlab::API do
12 12 get api("/internal/check")
13 13  
14 14 response.status.should == 200
15   - json_response['api_version'].should == Gitlab::API.version
  15 + json_response['api_version'].should == API::API.version
16 16 end
17 17 end
18 18  
... ...
spec/requests/api/issues_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ...
spec/requests/api/merge_requests_spec.rb
1 1 require "spec_helper"
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user ) }
... ...
spec/requests/api/milestones_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5 before(:each) { enable_observers }
6 6  
... ...
spec/requests/api/notes_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ...
spec/requests/api/projects_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5 before(:each) { enable_observers }
6 6  
... ...
spec/requests/api/session_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ...
spec/requests/api/system_hooks_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ...
spec/requests/api/users_spec.rb
1 1 require 'spec_helper'
2 2  
3   -describe Gitlab::API do
  3 +describe API::API do
4 4 include ApiHelpers
5 5  
6 6 let(:user) { create(:user) }
... ...
spec/routing/routing_spec.rb
... ... @@ -7,7 +7,7 @@ describe SearchController, &quot;routing&quot; do
7 7 end
8 8 end
9 9  
10   -# gitlab_api /api Gitlab::API
  10 +# gitlab_api /api API::API
11 11 # resque /info/resque Resque::Server
12 12 # /:path Grack
13 13 describe "Mounted Apps", "routing" do
... ...
spec/support/api_helpers.rb
... ... @@ -18,7 +18,7 @@ module ApiHelpers
18 18 #
19 19 # Returns the relative path to the requested API resource
20 20 def api(path, user = nil)
21   - "/api/#{Gitlab::API.version}#{path}" +
  21 + "/api/#{API::API.version}#{path}" +
22 22  
23 23 # Normalize query string
24 24 (path.index('?') ? '' : '?') +
... ...