Commit 7b5c3cc8be40ee161ae89a06bba6229da1032a0c
1 parent
4ad91d3c
Exists in
master
and in
4 other branches
add projects API
Showing
3 changed files
with
101 additions
and
0 deletions
Show diff stats
lib/api.rb
| ... | ... | @@ -6,6 +6,7 @@ module Gitlab |
| 6 | 6 | format :json |
| 7 | 7 | helpers APIHelpers |
| 8 | 8 | |
| 9 | + # Users API | |
| 9 | 10 | resource :users do |
| 10 | 11 | before { authenticate! } |
| 11 | 12 | |
| ... | ... | @@ -27,5 +28,34 @@ module Gitlab |
| 27 | 28 | authenticate! |
| 28 | 29 | present @current_user, :with => Entities::User |
| 29 | 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 = Project.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 = Project.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 = Project.find_by_code(params[:id]) | |
| 57 | + present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | |
| 58 | + end | |
| 59 | + end | |
| 30 | 60 | end |
| 31 | 61 | end | ... | ... |
lib/api/entities.rb
| ... | ... | @@ -4,5 +4,20 @@ module Gitlab |
| 4 | 4 | expose :id, :email, :name, :bio, :skype, :linkedin, :twitter, |
| 5 | 5 | :dark_scheme, :theme_id, :blocked, :created_at |
| 6 | 6 | end |
| 7 | + | |
| 8 | + class Project < Grape::Entity | |
| 9 | + expose :id, :code, :name, :description, :path, :default_branch | |
| 10 | + expose :owner, :using => Entities::User | |
| 11 | + expose :private_flag, :as => :private | |
| 12 | + expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at | |
| 13 | + end | |
| 14 | + | |
| 15 | + class ProjectRepositoryBranches < Grape::Entity | |
| 16 | + expose :name, :commit | |
| 17 | + end | |
| 18 | + | |
| 19 | + class ProjectRepositoryTags < Grape::Entity | |
| 20 | + expose :name, :commit | |
| 21 | + end | |
| 7 | 22 | end |
| 8 | 23 | end | ... | ... |
| ... | ... | @@ -0,0 +1,56 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Gitlab::API do | |
| 4 | + let(:user) { Factory :user } | |
| 5 | + let!(:project) { Factory :project, :owner => user } | |
| 6 | + | |
| 7 | + describe "GET /projects" do | |
| 8 | + before { project.add_access(user, :read) } | |
| 9 | + | |
| 10 | + it "should return authentication error" do | |
| 11 | + get "/api/projects" | |
| 12 | + response.status.should == 401 | |
| 13 | + end | |
| 14 | + | |
| 15 | + describe "authenticated GET /projects" do | |
| 16 | + it "should return an array of projects" do | |
| 17 | + get "/api/projects?private_token=#{user.private_token}" | |
| 18 | + response.status.should == 200 | |
| 19 | + json = JSON.parse(response.body) | |
| 20 | + json.should be_an Array | |
| 21 | + json.first['name'].should == project.name | |
| 22 | + json.first['owner']['email'].should == user.email | |
| 23 | + end | |
| 24 | + end | |
| 25 | + end | |
| 26 | + | |
| 27 | + describe "GET /projects/:id" do | |
| 28 | + it "should return a project by id" do | |
| 29 | + get "/api/projects/#{project.code}?private_token=#{user.private_token}" | |
| 30 | + response.status.should == 200 | |
| 31 | + json = JSON.parse(response.body) | |
| 32 | + json['name'].should == project.name | |
| 33 | + json['owner']['email'].should == user.email | |
| 34 | + end | |
| 35 | + end | |
| 36 | + | |
| 37 | + describe "GET /projects/:id/repository/branches" do | |
| 38 | + it "should return an array of project branches" do | |
| 39 | + get "/api/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" | |
| 40 | + response.status.should == 200 | |
| 41 | + json = JSON.parse(response.body) | |
| 42 | + json.should be_an Array | |
| 43 | + json.first['name'].should == project.repo.heads.sort_by(&:name).first.name | |
| 44 | + end | |
| 45 | + end | |
| 46 | + | |
| 47 | + describe "GET /projects/:id/repository/tags" do | |
| 48 | + it "should return an array of project tags" do | |
| 49 | + get "/api/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" | |
| 50 | + response.status.should == 200 | |
| 51 | + json = JSON.parse(response.body) | |
| 52 | + json.should be_an Array | |
| 53 | + json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name | |
| 54 | + end | |
| 55 | + end | |
| 56 | +end | ... | ... |