From 4aca61e8a60cae56a7cceec7d66fd7aa4138c274 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 27 Jun 2012 02:26:16 -0700 Subject: [PATCH 1/4] install grape and mount Gitlab::API --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ config/routes.rb | 46 +++++++++++++++++++++++++--------------------- lib/api.rb | 2 ++ 4 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 lib/api.rb diff --git a/Gemfile b/Gemfile index 44bdb392..3e5626e6 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem "omniauth-ldap", :git => "https://github.com/gitlabhq/omniauth-ldap.git", gem 'yaml_db', :git => "https://github.com/gitlabhq/yaml_db.git" gem "linguist", "~> 1.0.0", :git => "https://github.com/gitlabhq/linguist.git" +gem "grape" gem "stamp" gem "kaminari" gem "haml-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 8265641f..acc53495 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -162,6 +162,12 @@ GEM gherkin (2.11.0) json (>= 1.4.6) git (1.2.5) + grape (0.2.0) + hashie (~> 1.2) + multi_json + multi_xml + rack + rack-mount haml (3.1.6) haml-rails (0.3.4) actionpack (~> 3.0) @@ -223,6 +229,8 @@ GEM rack (1.4.1) rack-cache (1.2) rack (>= 0.4) + rack-mount (0.8.3) + rack (>= 1.0.0) rack-protection (1.2.0) rack rack-ssl (1.3.2) @@ -373,6 +381,7 @@ DEPENDENCIES foreman git gitolite! + grape grit! haml-rails httparty diff --git a/config/routes.rb b/config/routes.rb index d1dd03ab..35133d03 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,10 @@ Gitlab::Application.routes.draw do # get 'search' => "search#show" + # API + require 'api' + mount Gitlab::API => '/api' + # Optionally, enable Resque here require 'resque/server' mount Resque::Server.new, at: '/info/resque' @@ -20,15 +24,15 @@ Gitlab::Application.routes.draw do # Admin Area # namespace :admin do - resources :users do - member do + resources :users do + member do put :team_update put :block put :unblock end end - resources :projects, :constraints => { :id => /[^\/]+/ } do - member do + resources :projects, :constraints => { :id => /[^\/]+/ } do + member do get :team put :team_update end @@ -79,12 +83,12 @@ Gitlab::Application.routes.draw do resources :wikis, :only => [:show, :edit, :destroy, :create] do member do - get "history" + get "history" end end - resource :repository do - member do + resource :repository do + member do get "branches" get "tags" get "archive" @@ -94,14 +98,14 @@ Gitlab::Application.routes.draw do resources :deploy_keys resources :protected_branches, :only => [:index, :create, :destroy] - resources :refs, :only => [], :path => "/" do - collection do + resources :refs, :only => [], :path => "/" do + collection do get "switch" end - member do + member do get "tree", :constraints => { :id => /[a-zA-Z.\/0-9_\-]+/ } - get "blob", + get "blob", :constraints => { :id => /[a-zA-Z.0-9\/_\-]+/, :path => /.*/ @@ -126,32 +130,32 @@ Gitlab::Application.routes.draw do end end - resources :merge_requests do - member do + resources :merge_requests do + member do get :diffs get :automerge get :automerge_check end - collection do + collection do get :branch_from get :branch_to end end - - resources :snippets do - member do + + resources :snippets do + member do get "raw" end end - resources :hooks, :only => [:index, :create, :destroy] do - member do + resources :hooks, :only => [:index, :create, :destroy] do + member do get :test end end - resources :commits do - collection do + resources :commits do + collection do get :compare end end diff --git a/lib/api.rb b/lib/api.rb new file mode 100644 index 00000000..a3e9e57b --- /dev/null +++ b/lib/api.rb @@ -0,0 +1,2 @@ +class Gitlab::API < Grape::API +end From 4ad91d3c1144c406e50c7b33bae684bd6837faf8 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 27 Jun 2012 04:32:56 -0700 Subject: [PATCH 2/4] add users API --- lib/api.rb | 31 ++++++++++++++++++++++++++++++- lib/api/entities.rb | 8 ++++++++ lib/api/helpers.rb | 11 +++++++++++ spec/api/users_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/api/entities.rb create mode 100644 lib/api/helpers.rb create mode 100644 spec/api/users_spec.rb diff --git a/lib/api.rb b/lib/api.rb index a3e9e57b..9e38bc49 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,2 +1,31 @@ -class Gitlab::API < Grape::API +require 'api/entities' +require 'api/helpers' + +module Gitlab + class API < Grape::API + format :json + helpers APIHelpers + + resource :users do + before { authenticate! } + + # GET /users + get do + @users = User.all + present @users, :with => Entities::User + end + + # GET /users/:id + get ":id" do + @user = User.find(params[:id]) + present @user, :with => Entities::User + end + end + + # GET /user + get "/user" do + authenticate! + present @current_user, :with => Entities::User + end + end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb new file mode 100644 index 00000000..3548e8cc --- /dev/null +++ b/lib/api/entities.rb @@ -0,0 +1,8 @@ +module Gitlab + module Entities + class User < Grape::Entity + expose :id, :email, :name, :bio, :skype, :linkedin, :twitter, + :dark_scheme, :theme_id, :blocked, :created_at + end + end +end diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb new file mode 100644 index 00000000..cd2e39bf --- /dev/null +++ b/lib/api/helpers.rb @@ -0,0 +1,11 @@ +module Gitlab + module APIHelpers + def current_user + @current_user ||= User.find_by_authentication_token(params[:private_token]) + end + + def authenticate! + error!('401 Unauthorized', 401) unless current_user + end + end +end diff --git a/spec/api/users_spec.rb b/spec/api/users_spec.rb new file mode 100644 index 00000000..f142ac63 --- /dev/null +++ b/spec/api/users_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe Gitlab::API do + let(:user) { Factory :user } + + describe "GET /users" do + it "should return authentication error" do + get "/api/users" + response.status.should == 401 + end + + describe "authenticated GET /users" do + it "should return an array of users" do + get "/api/users?private_token=#{user.private_token}" + response.status.should == 200 + json = JSON.parse(response.body) + json.should be_an Array + json.first['email'].should == user.email + end + end + end + + describe "GET /users/:id" do + it "should return a user by id" do + get "/api/users/#{user.id}?private_token=#{user.private_token}" + response.status.should == 200 + JSON.parse(response.body)['email'].should == user.email + end + end + + describe "GET /user" do + it "should return current user" do + get "/api/user?private_token=#{user.private_token}" + response.status.should == 200 + JSON.parse(response.body)['email'].should == user.email + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5556798f..65e7e529 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -58,4 +58,8 @@ RSpec.configure do |config| config.after do DatabaseCleaner.clean end + + config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => { + :file_path => /spec\/api/ + } end From 7b5c3cc8be40ee161ae89a06bba6229da1032a0c Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 27 Jun 2012 05:51:39 -0700 Subject: [PATCH 3/4] add projects API --- lib/api.rb | 30 +++++++++++++++++++++ lib/api/entities.rb | 15 +++++++++++ spec/api/projects_spec.rb | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 spec/api/projects_spec.rb diff --git a/lib/api.rb b/lib/api.rb index 9e38bc49..ab5b02e0 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -6,6 +6,7 @@ module Gitlab format :json helpers APIHelpers + # Users API resource :users do before { authenticate! } @@ -27,5 +28,34 @@ module Gitlab authenticate! present @current_user, :with => Entities::User end + + # Projects API + resource :projects do + before { authenticate! } + + # GET /projects + get do + @projects = current_user.projects + present @projects, :with => Entities::Project + end + + # GET /projects/:id + get ":id" do + @project = Project.find_by_code(params[:id]) + present @project, :with => Entities::Project + end + + # GET /projects/:id/repository/branches + get ":id/repository/branches" do + @project = Project.find_by_code(params[:id]) + present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches + end + + # GET /projects/:id/repository/tags + get ":id/repository/tags" do + @project = Project.find_by_code(params[:id]) + present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags + end + end end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 3548e8cc..44a43985 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -4,5 +4,20 @@ module Gitlab expose :id, :email, :name, :bio, :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :blocked, :created_at end + + class Project < Grape::Entity + expose :id, :code, :name, :description, :path, :default_branch + expose :owner, :using => Entities::User + expose :private_flag, :as => :private + expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at + end + + class ProjectRepositoryBranches < Grape::Entity + expose :name, :commit + end + + class ProjectRepositoryTags < Grape::Entity + expose :name, :commit + end end end diff --git a/spec/api/projects_spec.rb b/spec/api/projects_spec.rb new file mode 100644 index 00000000..2d1043f9 --- /dev/null +++ b/spec/api/projects_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +describe Gitlab::API do + let(:user) { Factory :user } + let!(:project) { Factory :project, :owner => user } + + describe "GET /projects" do + before { project.add_access(user, :read) } + + it "should return authentication error" do + get "/api/projects" + response.status.should == 401 + end + + describe "authenticated GET /projects" do + it "should return an array of projects" do + get "/api/projects?private_token=#{user.private_token}" + response.status.should == 200 + json = JSON.parse(response.body) + json.should be_an Array + json.first['name'].should == project.name + json.first['owner']['email'].should == user.email + end + end + end + + describe "GET /projects/:id" do + it "should return a project by id" do + get "/api/projects/#{project.code}?private_token=#{user.private_token}" + response.status.should == 200 + json = JSON.parse(response.body) + json['name'].should == project.name + json['owner']['email'].should == user.email + end + end + + describe "GET /projects/:id/repository/branches" do + it "should return an array of project branches" do + get "/api/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" + response.status.should == 200 + json = JSON.parse(response.body) + json.should be_an Array + json.first['name'].should == project.repo.heads.sort_by(&:name).first.name + end + end + + describe "GET /projects/:id/repository/tags" do + it "should return an array of project tags" do + get "/api/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" + response.status.should == 200 + json = JSON.parse(response.body) + json.should be_an Array + json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name + end + end +end From 84a3f8fca4e57422267bdd7e07d4f1b90717fbcc Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Thu, 28 Jun 2012 07:02:20 -0700 Subject: [PATCH 4/4] show only current user projects --- lib/api.rb | 6 +++--- spec/api/projects_spec.rb | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index ab5b02e0..4fdc3273 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -41,19 +41,19 @@ module Gitlab # GET /projects/:id get ":id" do - @project = Project.find_by_code(params[:id]) + @project = current_user.projects.find_by_code(params[:id]) present @project, :with => Entities::Project end # GET /projects/:id/repository/branches get ":id/repository/branches" do - @project = Project.find_by_code(params[:id]) + @project = current_user.projects.find_by_code(params[:id]) present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches end # GET /projects/:id/repository/tags get ":id/repository/tags" do - @project = Project.find_by_code(params[:id]) + @project = current_user.projects.find_by_code(params[:id]) present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags end end diff --git a/spec/api/projects_spec.rb b/spec/api/projects_spec.rb index 2d1043f9..e4835736 100644 --- a/spec/api/projects_spec.rb +++ b/spec/api/projects_spec.rb @@ -3,10 +3,9 @@ require 'spec_helper' describe Gitlab::API do let(:user) { Factory :user } let!(:project) { Factory :project, :owner => user } + before { project.add_access(user, :read) } describe "GET /projects" do - before { project.add_access(user, :read) } - it "should return authentication error" do get "/api/projects" response.status.should == 401