Commit 3ce629fdedd3d283b9b899541330bb34022300c5
Exists in
master
and in
4 other branches
Merge pull request #1034 from NARKOZ/api
API version
Showing
6 changed files
with
96 additions
and
35 deletions
Show diff stats
lib/api.rb
@@ -2,6 +2,9 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} | @@ -2,6 +2,9 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} | ||
2 | 2 | ||
3 | module Gitlab | 3 | module Gitlab |
4 | class API < Grape::API | 4 | class API < Grape::API |
5 | + VERSION = 'v2' | ||
6 | + version VERSION, :using => :path | ||
7 | + | ||
5 | format :json | 8 | format :json |
6 | error_format :json | 9 | error_format :json |
7 | helpers APIHelpers | 10 | helpers APIHelpers |
lib/api/projects.rb
@@ -86,6 +86,34 @@ module Gitlab | @@ -86,6 +86,34 @@ module Gitlab | ||
86 | end | 86 | end |
87 | end | 87 | end |
88 | 88 | ||
89 | + # Update an existing project snippet | ||
90 | + # | ||
91 | + # Parameters: | ||
92 | + # id (required) - The code name of a project | ||
93 | + # snippet_id (required) - The ID of a project snippet | ||
94 | + # title (optional) - The title of a snippet | ||
95 | + # file_name (optional) - The name of a snippet file | ||
96 | + # lifetime (optional) - The expiration date of a snippet | ||
97 | + # code (optional) - The content of a snippet | ||
98 | + # Example Request: | ||
99 | + # PUT /projects/:id/snippets/:snippet_id | ||
100 | + put ":id/snippets/:snippet_id" do | ||
101 | + @project = current_user.projects.find_by_code(params[:id]) | ||
102 | + @snippet = @project.snippets.find(params[:snippet_id]) | ||
103 | + parameters = { | ||
104 | + :title => (params[:title] || @snippet.title), | ||
105 | + :file_name => (params[:file_name] || @snippet.file_name), | ||
106 | + :expires_at => (params[:lifetime] || @snippet.expires_at), | ||
107 | + :content => (params[:code] || @snippet.content) | ||
108 | + } | ||
109 | + | ||
110 | + if @snippet.update_attributes(parameters) | ||
111 | + present @snippet, :with => Entities::ProjectSnippet | ||
112 | + else | ||
113 | + error!({'message' => '404 Not found'}, 404) | ||
114 | + end | ||
115 | + end | ||
116 | + | ||
89 | # Delete a project snippet | 117 | # Delete a project snippet |
90 | # | 118 | # |
91 | # Parameters: | 119 | # Parameters: |
@@ -98,6 +126,19 @@ module Gitlab | @@ -98,6 +126,19 @@ module Gitlab | ||
98 | @snippet = @project.snippets.find(params[:snippet_id]) | 126 | @snippet = @project.snippets.find(params[:snippet_id]) |
99 | @snippet.destroy | 127 | @snippet.destroy |
100 | end | 128 | end |
129 | + | ||
130 | + # Get a raw project snippet | ||
131 | + # | ||
132 | + # Parameters: | ||
133 | + # id (required) - The code of a project | ||
134 | + # snippet_id (required) - The ID of a project snippet | ||
135 | + # Example Request: | ||
136 | + # GET /projects/:id/snippets/:snippet_id/raw | ||
137 | + get ":id/snippets/:snippet_id/raw" do | ||
138 | + @project = current_user.projects.find_by_code(params[:id]) | ||
139 | + @snippet = @project.snippets.find(params[:snippet_id]) | ||
140 | + present @snippet.content | ||
141 | + end | ||
101 | end | 142 | end |
102 | end | 143 | end |
103 | end | 144 | end |
spec/api/projects_spec.rb
@@ -3,81 +3,92 @@ require 'spec_helper' | @@ -3,81 +3,92 @@ 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 | + let!(:snippet) { Factory :snippet, :author => user, :project => project, :title => 'example' } |
7 | before { project.add_access(user, :read) } | 7 | before { project.add_access(user, :read) } |
8 | 8 | ||
9 | describe "GET /projects" do | 9 | describe "GET /projects" do |
10 | it "should return authentication error" do | 10 | it "should return authentication error" do |
11 | - get "/api/projects" | 11 | + get "#{api_prefix}/projects" |
12 | response.status.should == 401 | 12 | response.status.should == 401 |
13 | end | 13 | end |
14 | 14 | ||
15 | describe "authenticated GET /projects" do | 15 | describe "authenticated GET /projects" do |
16 | it "should return an array of projects" do | 16 | it "should return an array of projects" do |
17 | - get "/api/projects?private_token=#{user.private_token}" | 17 | + get "#{api_prefix}/projects?private_token=#{user.private_token}" |
18 | response.status.should == 200 | 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 | 19 | + json_response.should be_an Array |
20 | + json_response.first['name'].should == project.name | ||
21 | + json_response.first['owner']['email'].should == user.email | ||
23 | end | 22 | end |
24 | end | 23 | end |
25 | end | 24 | end |
26 | 25 | ||
27 | describe "GET /projects/:id" do | 26 | describe "GET /projects/:id" do |
28 | it "should return a project by id" do | 27 | it "should return a project by id" do |
29 | - get "/api/projects/#{project.code}?private_token=#{user.private_token}" | 28 | + get "#{api_prefix}/projects/#{project.code}?private_token=#{user.private_token}" |
30 | response.status.should == 200 | 29 | response.status.should == 200 |
31 | - json = JSON.parse(response.body) | ||
32 | - json['name'].should == project.name | ||
33 | - json['owner']['email'].should == user.email | 30 | + json_response['name'].should == project.name |
31 | + json_response['owner']['email'].should == user.email | ||
34 | end | 32 | end |
35 | end | 33 | end |
36 | 34 | ||
37 | describe "GET /projects/:id/repository/branches" do | 35 | describe "GET /projects/:id/repository/branches" do |
38 | it "should return an array of project branches" do | 36 | it "should return an array of project branches" do |
39 | - get "/api/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" | 37 | + get "#{api_prefix}/projects/#{project.code}/repository/branches?private_token=#{user.private_token}" |
40 | response.status.should == 200 | 38 | 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 | 39 | + json_response.should be_an Array |
40 | + json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name | ||
44 | end | 41 | end |
45 | end | 42 | end |
46 | 43 | ||
47 | describe "GET /projects/:id/repository/tags" do | 44 | describe "GET /projects/:id/repository/tags" do |
48 | it "should return an array of project tags" do | 45 | it "should return an array of project tags" do |
49 | - get "/api/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" | 46 | + get "#{api_prefix}/projects/#{project.code}/repository/tags?private_token=#{user.private_token}" |
50 | response.status.should == 200 | 47 | 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 | 48 | + json_response.should be_an Array |
49 | + json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name | ||
54 | end | 50 | end |
55 | end | 51 | end |
56 | 52 | ||
57 | describe "GET /projects/:id/snippets/:snippet_id" do | 53 | describe "GET /projects/:id/snippets/:snippet_id" do |
58 | it "should return a project snippet" do | 54 | it "should return a project snippet" do |
59 | - get "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | 55 | + get "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" |
60 | response.status.should == 200 | 56 | response.status.should == 200 |
61 | - json = JSON.parse(response.body) | ||
62 | - json['title'].should == snippet.title | 57 | + json_response['title'].should == snippet.title |
63 | end | 58 | end |
64 | end | 59 | end |
65 | 60 | ||
66 | describe "POST /projects/:id/snippets" do | 61 | describe "POST /projects/:id/snippets" do |
67 | it "should create a new project snippet" do | 62 | it "should create a new project snippet" do |
68 | - post "/api/projects/#{project.code}/snippets?private_token=#{user.private_token}", | 63 | + post "#{api_prefix}/projects/#{project.code}/snippets?private_token=#{user.private_token}", |
69 | :title => 'api test', :file_name => 'sample.rb', :code => 'test' | 64 | :title => 'api test', :file_name => 'sample.rb', :code => 'test' |
70 | response.status.should == 201 | 65 | response.status.should == 201 |
71 | - json = JSON.parse(response.body) | ||
72 | - json['title'].should == 'api test' | 66 | + json_response['title'].should == 'api test' |
67 | + end | ||
68 | + end | ||
69 | + | ||
70 | + describe "PUT /projects/:id/snippets" do | ||
71 | + it "should update an existing project snippet" do | ||
72 | + put "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}", | ||
73 | + :code => 'updated code' | ||
74 | + response.status.should == 200 | ||
75 | + json_response['title'].should == 'example' | ||
76 | + snippet.reload.content.should == 'updated code' | ||
73 | end | 77 | end |
74 | end | 78 | end |
75 | 79 | ||
76 | describe "DELETE /projects/:id/snippets/:snippet_id" do | 80 | describe "DELETE /projects/:id/snippets/:snippet_id" do |
77 | it "should create a new project snippet" do | 81 | it "should create a new project snippet" do |
78 | expect { | 82 | expect { |
79 | - delete "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" | 83 | + delete "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}" |
80 | }.should change { Snippet.count }.by(-1) | 84 | }.should change { Snippet.count }.by(-1) |
81 | end | 85 | end |
82 | end | 86 | end |
87 | + | ||
88 | + describe "GET /projects/:id/snippets/:snippet_id/raw" do | ||
89 | + it "should get a raw project snippet" do | ||
90 | + get "#{api_prefix}/projects/#{project.code}/snippets/#{snippet.id}/raw?private_token=#{user.private_token}" | ||
91 | + response.status.should == 200 | ||
92 | + end | ||
93 | + end | ||
83 | end | 94 | end |
spec/api/users_spec.rb
@@ -5,34 +5,33 @@ describe Gitlab::API do | @@ -5,34 +5,33 @@ describe Gitlab::API do | ||
5 | 5 | ||
6 | describe "GET /users" do | 6 | describe "GET /users" do |
7 | it "should return authentication error" do | 7 | it "should return authentication error" do |
8 | - get "/api/users" | 8 | + get "#{api_prefix}/users" |
9 | response.status.should == 401 | 9 | response.status.should == 401 |
10 | end | 10 | end |
11 | 11 | ||
12 | describe "authenticated GET /users" do | 12 | describe "authenticated GET /users" do |
13 | it "should return an array of users" do | 13 | it "should return an array of users" do |
14 | - get "/api/users?private_token=#{user.private_token}" | 14 | + get "#{api_prefix}/users?private_token=#{user.private_token}" |
15 | response.status.should == 200 | 15 | response.status.should == 200 |
16 | - json = JSON.parse(response.body) | ||
17 | - json.should be_an Array | ||
18 | - json.first['email'].should == user.email | 16 | + json_response.should be_an Array |
17 | + json_response.first['email'].should == user.email | ||
19 | end | 18 | end |
20 | end | 19 | end |
21 | end | 20 | end |
22 | 21 | ||
23 | describe "GET /users/:id" do | 22 | describe "GET /users/:id" do |
24 | it "should return a user by id" do | 23 | it "should return a user by id" do |
25 | - get "/api/users/#{user.id}?private_token=#{user.private_token}" | 24 | + get "#{api_prefix}/users/#{user.id}?private_token=#{user.private_token}" |
26 | response.status.should == 200 | 25 | response.status.should == 200 |
27 | - JSON.parse(response.body)['email'].should == user.email | 26 | + json_response['email'].should == user.email |
28 | end | 27 | end |
29 | end | 28 | end |
30 | 29 | ||
31 | describe "GET /user" do | 30 | describe "GET /user" do |
32 | it "should return current user" do | 31 | it "should return current user" do |
33 | - get "/api/user?private_token=#{user.private_token}" | 32 | + get "#{api_prefix}/user?private_token=#{user.private_token}" |
34 | response.status.should == 200 | 33 | response.status.should == 200 |
35 | - JSON.parse(response.body)['email'].should == user.email | 34 | + json_response['email'].should == user.email |
36 | end | 35 | end |
37 | end | 36 | end |
38 | end | 37 | end |
spec/support/security.rb