Commit b110c6bc869d49089dc00abef53849765653d49b
Exists in
master
and in
4 other branches
Merge pull request #1019 from NARKOZ/api
API
Showing
6 changed files
with
178 additions
and
54 deletions
Show diff stats
lib/api.rb
| 1 | -require 'api/entities' | ||
| 2 | -require 'api/helpers' | 1 | +Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} |
| 3 | 2 | ||
| 4 | module Gitlab | 3 | module Gitlab |
| 5 | class API < Grape::API | 4 | class API < Grape::API |
| 6 | format :json | 5 | format :json |
| 6 | + error_format :json | ||
| 7 | helpers APIHelpers | 7 | helpers APIHelpers |
| 8 | 8 | ||
| 9 | - # Users API | ||
| 10 | - resource :users do | ||
| 11 | - before { authenticate! } | ||
| 12 | - | ||
| 13 | - # GET /users | ||
| 14 | - get do | ||
| 15 | - @users = User.all | ||
| 16 | - present @users, :with => Entities::User | ||
| 17 | - end | ||
| 18 | - | ||
| 19 | - # GET /users/:id | ||
| 20 | - get ":id" do | ||
| 21 | - @user = User.find(params[:id]) | ||
| 22 | - present @user, :with => Entities::User | ||
| 23 | - end | ||
| 24 | - end | ||
| 25 | - | ||
| 26 | - # GET /user | ||
| 27 | - get "/user" do | ||
| 28 | - authenticate! | ||
| 29 | - present @current_user, :with => Entities::User | ||
| 30 | - end | ||
| 31 | - | ||
| 32 | - # Projects API | ||
| 33 | - resource :projects do | ||
| 34 | - before { authenticate! } | ||
| 35 | - | ||
| 36 | - # GET /projects | ||
| 37 | - get do | ||
| 38 | - @projects = current_user.projects | ||
| 39 | - present @projects, :with => Entities::Project | ||
| 40 | - end | ||
| 41 | - | ||
| 42 | - # GET /projects/:id | ||
| 43 | - get ":id" do | ||
| 44 | - @project = current_user.projects.find_by_code(params[:id]) | ||
| 45 | - present @project, :with => Entities::Project | ||
| 46 | - end | ||
| 47 | - | ||
| 48 | - # GET /projects/:id/repository/branches | ||
| 49 | - get ":id/repository/branches" do | ||
| 50 | - @project = current_user.projects.find_by_code(params[:id]) | ||
| 51 | - present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches | ||
| 52 | - end | ||
| 53 | - | ||
| 54 | - # GET /projects/:id/repository/tags | ||
| 55 | - get ":id/repository/tags" do | ||
| 56 | - @project = current_user.projects.find_by_code(params[:id]) | ||
| 57 | - present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | ||
| 58 | - end | ||
| 59 | - end | 9 | + mount Users |
| 10 | + mount Projects | ||
| 60 | end | 11 | end |
| 61 | end | 12 | end |
lib/api/entities.rb
| @@ -19,5 +19,11 @@ module Gitlab | @@ -19,5 +19,11 @@ module Gitlab | ||
| 19 | class ProjectRepositoryTags < Grape::Entity | 19 | class ProjectRepositoryTags < Grape::Entity |
| 20 | expose :name, :commit | 20 | expose :name, :commit |
| 21 | end | 21 | end |
| 22 | + | ||
| 23 | + class ProjectSnippet < Grape::Entity | ||
| 24 | + expose :id, :title, :file_name | ||
| 25 | + expose :author, :using => Entities::User | ||
| 26 | + expose :expires_at, :updated_at, :created_at | ||
| 27 | + end | ||
| 22 | end | 28 | end |
| 23 | end | 29 | end |
lib/api/helpers.rb
| @@ -0,0 +1,103 @@ | @@ -0,0 +1,103 @@ | ||
| 1 | +module Gitlab | ||
| 2 | + # Projects API | ||
| 3 | + class Projects < Grape::API | ||
| 4 | + before { authenticate! } | ||
| 5 | + | ||
| 6 | + resource :projects do | ||
| 7 | + # Get a projects list for authenticated user | ||
| 8 | + # | ||
| 9 | + # Example Request: | ||
| 10 | + # GET /projects | ||
| 11 | + get do | ||
| 12 | + @projects = current_user.projects | ||
| 13 | + present @projects, :with => Entities::Project | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + # Get a single project | ||
| 17 | + # | ||
| 18 | + # Parameters: | ||
| 19 | + # id (required) - The code of a project | ||
| 20 | + # Example Request: | ||
| 21 | + # GET /projects/:id | ||
| 22 | + get ":id" do | ||
| 23 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 24 | + present @project, :with => Entities::Project | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + # Get a project repository branches | ||
| 28 | + # | ||
| 29 | + # Parameters: | ||
| 30 | + # id (required) - The code of a project | ||
| 31 | + # Example Request: | ||
| 32 | + # GET /projects/:id/repository/branches | ||
| 33 | + get ":id/repository/branches" do | ||
| 34 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 35 | + present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + # Get a project repository tags | ||
| 39 | + # | ||
| 40 | + # Parameters: | ||
| 41 | + # id (required) - The code of a project | ||
| 42 | + # Example Request: | ||
| 43 | + # GET /projects/:id/repository/tags | ||
| 44 | + get ":id/repository/tags" do | ||
| 45 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 46 | + present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | ||
| 47 | + end | ||
| 48 | + | ||
| 49 | + # Get a project snippet | ||
| 50 | + # | ||
| 51 | + # Parameters: | ||
| 52 | + # id (required) - The code of a project | ||
| 53 | + # snippet_id (required) - The ID of a project snippet | ||
| 54 | + # Example Request: | ||
| 55 | + # GET /projects/:id/snippets/:snippet_id | ||
| 56 | + get ":id/snippets/:snippet_id" do | ||
| 57 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 58 | + @snippet = @project.snippets.find(params[:snippet_id]) | ||
| 59 | + present @snippet, :with => Entities::ProjectSnippet | ||
| 60 | + end | ||
| 61 | + | ||
| 62 | + # Create a new project snippet | ||
| 63 | + # | ||
| 64 | + # Parameters: | ||
| 65 | + # id (required) - The code name of a project | ||
| 66 | + # title (required) - The title of a snippet | ||
| 67 | + # file_name (required) - The name of a snippet file | ||
| 68 | + # lifetime (optional) - The expiration date of a snippet | ||
| 69 | + # code (required) - The content of a snippet | ||
| 70 | + # Example Request: | ||
| 71 | + # POST /projects/:id/snippets | ||
| 72 | + post ":id/snippets" do | ||
| 73 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 74 | + @snippet = @project.snippets.new( | ||
| 75 | + :title => params[:title], | ||
| 76 | + :file_name => params[:file_name], | ||
| 77 | + :expires_at => params[:lifetime], | ||
| 78 | + :content => params[:code] | ||
| 79 | + ) | ||
| 80 | + @snippet.author = current_user | ||
| 81 | + | ||
| 82 | + if @snippet.save | ||
| 83 | + present @snippet, :with => Entities::ProjectSnippet | ||
| 84 | + else | ||
| 85 | + error!({'message' => '404 Not found'}, 404) | ||
| 86 | + end | ||
| 87 | + end | ||
| 88 | + | ||
| 89 | + # Delete a project snippet | ||
| 90 | + # | ||
| 91 | + # Parameters: | ||
| 92 | + # id (required) - The code of a project | ||
| 93 | + # snippet_id (required) - The ID of a project snippet | ||
| 94 | + # Example Request: | ||
| 95 | + # DELETE /projects/:id/snippets/:snippet_id | ||
| 96 | + delete ":id/snippets/:snippet_id" do | ||
| 97 | + @project = current_user.projects.find_by_code(params[:id]) | ||
| 98 | + @snippet = @project.snippets.find(params[:snippet_id]) | ||
| 99 | + @snippet.destroy | ||
| 100 | + end | ||
| 101 | + end | ||
| 102 | + end | ||
| 103 | +end |
| @@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
| 1 | +module Gitlab | ||
| 2 | + # Users API | ||
| 3 | + class Users < Grape::API | ||
| 4 | + before { authenticate! } | ||
| 5 | + | ||
| 6 | + resource :users do | ||
| 7 | + # Get a users list | ||
| 8 | + # | ||
| 9 | + # Example Request: | ||
| 10 | + # GET /users | ||
| 11 | + get do | ||
| 12 | + @users = User.all | ||
| 13 | + present @users, :with => Entities::User | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + # Get a single user | ||
| 17 | + # | ||
| 18 | + # Parameters: | ||
| 19 | + # id (required) - The ID of a user | ||
| 20 | + # Example Request: | ||
| 21 | + # GET /users/:id | ||
| 22 | + get ":id" do | ||
| 23 | + @user = User.find(params[:id]) | ||
| 24 | + present @user, :with => Entities::User | ||
| 25 | + end | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + # Get currently authenticated user | ||
| 29 | + # | ||
| 30 | + # Example Request: | ||
| 31 | + # GET /user | ||
| 32 | + get "/user" do | ||
| 33 | + present @current_user, :with => Entities::User | ||
| 34 | + end | ||
| 35 | + end | ||
| 36 | +end |
spec/api/projects_spec.rb
| @@ -3,6 +3,7 @@ require 'spec_helper' | @@ -3,6 +3,7 @@ require 'spec_helper' | ||
| 3 | describe Gitlab::API do | 3 | describe Gitlab::API do |
| 4 | let(:user) { Factory :user } | 4 | let(:user) { Factory :user } |
| 5 | let!(:project) { Factory :project, :owner => user } | 5 | let!(:project) { Factory :project, :owner => user } |
| 6 | + let!(:snippet) { Factory :snippet, :author => user, :project => project } | ||
| 6 | before { project.add_access(user, :read) } | 7 | before { project.add_access(user, :read) } |
| 7 | 8 | ||
| 8 | describe "GET /projects" do | 9 | describe "GET /projects" do |
| @@ -52,4 +53,31 @@ describe Gitlab::API do | @@ -52,4 +53,31 @@ describe Gitlab::API do | ||
| 52 | json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name | 53 | json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name |
| 53 | end | 54 | end |
| 54 | end | 55 | end |
| 56 | + | ||
| 57 | + describe "GET /projects/:id/snippets/:snippet_id" do | ||
| 58 | + it "should return a project snippet" do | ||
| 59 | + get "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | ||
| 60 | + response.status.should == 200 | ||
| 61 | + json = JSON.parse(response.body) | ||
| 62 | + json['title'].should == snippet.title | ||
| 63 | + end | ||
| 64 | + end | ||
| 65 | + | ||
| 66 | + describe "POST /projects/:id/snippets" do | ||
| 67 | + it "should create a new project snippet" do | ||
| 68 | + post "/api/projects/#{project.code}/snippets?private_token=#{user.private_token}", | ||
| 69 | + :title => 'api test', :file_name => 'sample.rb', :code => 'test' | ||
| 70 | + response.status.should == 201 | ||
| 71 | + json = JSON.parse(response.body) | ||
| 72 | + json['title'].should == 'api test' | ||
| 73 | + end | ||
| 74 | + end | ||
| 75 | + | ||
| 76 | + describe "DELETE /projects/:id/snippets/:snippet_id" do | ||
| 77 | + it "should create a new project snippet" do | ||
| 78 | + expect { | ||
| 79 | + delete "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | ||
| 80 | + }.should change { Snippet.count }.by(-1) | ||
| 81 | + end | ||
| 82 | + end | ||
| 55 | end | 83 | end |