Commit 378dc55d29a97447315c6181f678cf933afbbb78
1 parent
1d2c9818
Exists in
master
and in
4 other branches
add project snippets API
Showing
3 changed files
with
87 additions
and
0 deletions
Show diff stats
lib/api/entities.rb
| ... | ... | @@ -19,5 +19,11 @@ module Gitlab |
| 19 | 19 | class ProjectRepositoryTags < Grape::Entity |
| 20 | 20 | expose :name, :commit |
| 21 | 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 | 28 | end |
| 23 | 29 | end | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -45,6 +45,59 @@ module Gitlab |
| 45 | 45 | @project = current_user.projects.find_by_code(params[:id]) |
| 46 | 46 | present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags |
| 47 | 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 | |
| 48 | 101 | end |
| 49 | 102 | end |
| 50 | 103 | end | ... | ... |
spec/api/projects_spec.rb
| ... | ... | @@ -3,6 +3,7 @@ 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 | 7 | before { project.add_access(user, :read) } |
| 7 | 8 | |
| 8 | 9 | describe "GET /projects" do |
| ... | ... | @@ -52,4 +53,31 @@ describe Gitlab::API do |
| 52 | 53 | json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name |
| 53 | 54 | end |
| 54 | 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 | 83 | end | ... | ... |