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
lib/api/projects.rb
| ... | ... | @@ -86,6 +86,34 @@ module Gitlab |
| 86 | 86 | end |
| 87 | 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 | 117 | # Delete a project snippet |
| 90 | 118 | # |
| 91 | 119 | # Parameters: |
| ... | ... | @@ -98,6 +126,19 @@ module Gitlab |
| 98 | 126 | @snippet = @project.snippets.find(params[:snippet_id]) |
| 99 | 127 | @snippet.destroy |
| 100 | 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 | 142 | end |
| 102 | 143 | end |
| 103 | 144 | end | ... | ... |
spec/api/projects_spec.rb
| ... | ... | @@ -3,81 +3,92 @@ require 'spec_helper' |
| 3 | 3 | describe Gitlab::API do |
| 4 | 4 | let(:user) { Factory :user } |
| 5 | 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 | 7 | before { project.add_access(user, :read) } |
| 8 | 8 | |
| 9 | 9 | describe "GET /projects" do |
| 10 | 10 | it "should return authentication error" do |
| 11 | - get "/api/projects" | |
| 11 | + get "#{api_prefix}/projects" | |
| 12 | 12 | response.status.should == 401 |
| 13 | 13 | end |
| 14 | 14 | |
| 15 | 15 | describe "authenticated GET /projects" do |
| 16 | 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 | 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 | 22 | end |
| 24 | 23 | end |
| 25 | 24 | end |
| 26 | 25 | |
| 27 | 26 | describe "GET /projects/:id" do |
| 28 | 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 | 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 | 32 | end |
| 35 | 33 | end |
| 36 | 34 | |
| 37 | 35 | describe "GET /projects/:id/repository/branches" do |
| 38 | 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 | 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 | 41 | end |
| 45 | 42 | end |
| 46 | 43 | |
| 47 | 44 | describe "GET /projects/:id/repository/tags" do |
| 48 | 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 | 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 | 50 | end |
| 55 | 51 | end |
| 56 | 52 | |
| 57 | 53 | describe "GET /projects/:id/snippets/:snippet_id" do |
| 58 | 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 | 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 | 58 | end |
| 64 | 59 | end |
| 65 | 60 | |
| 66 | 61 | describe "POST /projects/:id/snippets" do |
| 67 | 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 | 64 | :title => 'api test', :file_name => 'sample.rb', :code => 'test' |
| 70 | 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 | 77 | end |
| 74 | 78 | end |
| 75 | 79 | |
| 76 | 80 | describe "DELETE /projects/:id/snippets/:snippet_id" do |
| 77 | 81 | it "should create a new project snippet" do |
| 78 | 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 | 84 | }.should change { Snippet.count }.by(-1) |
| 81 | 85 | end |
| 82 | 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 | 94 | end | ... | ... |
spec/api/users_spec.rb
| ... | ... | @@ -5,34 +5,33 @@ describe Gitlab::API do |
| 5 | 5 | |
| 6 | 6 | describe "GET /users" do |
| 7 | 7 | it "should return authentication error" do |
| 8 | - get "/api/users" | |
| 8 | + get "#{api_prefix}/users" | |
| 9 | 9 | response.status.should == 401 |
| 10 | 10 | end |
| 11 | 11 | |
| 12 | 12 | describe "authenticated GET /users" do |
| 13 | 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 | 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 | 18 | end |
| 20 | 19 | end |
| 21 | 20 | end |
| 22 | 21 | |
| 23 | 22 | describe "GET /users/:id" do |
| 24 | 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 | 25 | response.status.should == 200 |
| 27 | - JSON.parse(response.body)['email'].should == user.email | |
| 26 | + json_response['email'].should == user.email | |
| 28 | 27 | end |
| 29 | 28 | end |
| 30 | 29 | |
| 31 | 30 | describe "GET /user" do |
| 32 | 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 | 33 | response.status.should == 200 |
| 35 | - JSON.parse(response.body)['email'].should == user.email | |
| 34 | + json_response['email'].should == user.email | |
| 36 | 35 | end |
| 37 | 36 | end |
| 38 | 37 | end | ... | ... |
spec/support/security.rb