Commit 5ca9300b authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'grapify-project-snippet-api' into 'master'

Grapify the projects snippet API

Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/22928

See merge request !7621
parents 55ca2da7 b7bf1f3a
...@@ -3,6 +3,9 @@ module API ...@@ -3,6 +3,9 @@ module API
class ProjectSnippets < Grape::API class ProjectSnippets < Grape::API
before { authenticate! } before { authenticate! }
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do resource :projects do
helpers do helpers do
def handle_project_member_errors(errors) def handle_project_member_errors(errors)
...@@ -18,111 +21,108 @@ module API ...@@ -18,111 +21,108 @@ module API
end end
end end
# Get a project snippets desc 'Get all project snippets' do
# success Entities::ProjectSnippet
# Parameters: end
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id/snippets
get ":id/snippets" do get ":id/snippets" do
present paginate(snippets_for_current_user), with: Entities::ProjectSnippet present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
end end
# Get a project snippet desc 'Get a single project snippet' do
# success Entities::ProjectSnippet
# Parameters: end
# id (required) - The ID of a project params do
# snippet_id (required) - The ID of a project snippet requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
# Example Request: end
# GET /projects/:id/snippets/:snippet_id
get ":id/snippets/:snippet_id" do get ":id/snippets/:snippet_id" do
@snippet = snippets_for_current_user.find(params[:snippet_id]) snippet = snippets_for_current_user.find(params[:snippet_id])
present @snippet, with: Entities::ProjectSnippet present snippet, with: Entities::ProjectSnippet
end end
# Create a new project snippet desc 'Create a new project snippet' do
# success Entities::ProjectSnippet
# Parameters: end
# id (required) - The ID of a project params do
# title (required) - The title of a snippet requires :title, type: String, desc: 'The title of the snippet'
# file_name (required) - The name of a snippet file requires :file_name, type: String, desc: 'The file name of the snippet'
# code (required) - The content of a snippet requires :code, type: String, desc: 'The content of the snippet'
# visibility_level (required) - The snippet's visibility requires :visibility_level, type: Integer,
# Example Request: values: [Gitlab::VisibilityLevel::PRIVATE,
# POST /projects/:id/snippets Gitlab::VisibilityLevel::INTERNAL,
Gitlab::VisibilityLevel::PUBLIC],
desc: 'The visibility level of the snippet'
end
post ":id/snippets" do post ":id/snippets" do
authorize! :create_project_snippet, user_project authorize! :create_project_snippet, user_project
required_attributes! [:title, :file_name, :code, :visibility_level] snippet_params = declared_params
snippet_params[:content] = snippet_params.delete(:code)
attrs = attributes_for_keys [:title, :file_name, :visibility_level] snippet = CreateSnippetService.new(user_project, current_user, snippet_params).execute
attrs[:content] = params[:code] if params[:code].present?
@snippet = CreateSnippetService.new(user_project, current_user,
attrs).execute
if @snippet.errors.any? if snippet.persisted?
render_validation_error!(@snippet) present snippet, with: Entities::ProjectSnippet
else else
present @snippet, with: Entities::ProjectSnippet render_validation_error!(snippet)
end end
end end
# Update an existing project snippet desc 'Update an existing project snippet' do
# success Entities::ProjectSnippet
# Parameters: end
# id (required) - The ID of a project params do
# snippet_id (required) - The ID of a project snippet requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
# title (optional) - The title of a snippet optional :title, type: String, desc: 'The title of the snippet'
# file_name (optional) - The name of a snippet file optional :file_name, type: String, desc: 'The file name of the snippet'
# code (optional) - The content of a snippet optional :code, type: String, desc: 'The content of the snippet'
# visibility_level (optional) - The snippet's visibility optional :visibility_level, type: Integer,
# Example Request: values: [Gitlab::VisibilityLevel::PRIVATE,
# PUT /projects/:id/snippets/:snippet_id Gitlab::VisibilityLevel::INTERNAL,
Gitlab::VisibilityLevel::PUBLIC],
desc: 'The visibility level of the snippet'
at_least_one_of :title, :file_name, :code, :visibility_level
end
put ":id/snippets/:snippet_id" do put ":id/snippets/:snippet_id" do
@snippet = snippets_for_current_user.find(params[:snippet_id]) snippet = snippets_for_current_user.find_by(id: params.delete(:snippet_id))
authorize! :update_project_snippet, @snippet not_found!('Snippet') unless snippet
authorize! :update_project_snippet, snippet
attrs = attributes_for_keys [:title, :file_name, :visibility_level] snippet_params = declared_params(include_missing: false)
attrs[:content] = params[:code] if params[:code].present? snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
UpdateSnippetService.new(user_project, current_user, @snippet, UpdateSnippetService.new(user_project, current_user, snippet,
attrs).execute snippet_params).execute
if @snippet.errors.any?
render_validation_error!(@snippet) if snippet.persisted?
present snippet, with: Entities::ProjectSnippet
else else
present @snippet, with: Entities::ProjectSnippet render_validation_error!(snippet)
end end
end end
# Delete a project snippet desc 'Delete a project snippet'
# params do
# Parameters: requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
# id (required) - The ID of a project end
# snippet_id (required) - The ID of a project snippet
# Example Request:
# DELETE /projects/:id/snippets/:snippet_id
delete ":id/snippets/:snippet_id" do delete ":id/snippets/:snippet_id" do
begin snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
@snippet = snippets_for_current_user.find(params[:snippet_id]) not_found!('Snippet') unless snippet
authorize! :update_project_snippet, @snippet
@snippet.destroy authorize! :admin_project_snippet, snippet
rescue snippet.destroy
not_found!('Snippet') end
end
end desc 'Get a raw project snippet'
params do
# Get a raw project snippet requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
# end
# Parameters:
# id (required) - The ID of a project
# snippet_id (required) - The ID of a project snippet
# Example Request:
# GET /projects/:id/snippets/:snippet_id/raw
get ":id/snippets/:snippet_id/raw" do get ":id/snippets/:snippet_id/raw" do
@snippet = snippets_for_current_user.find(params[:snippet_id]) snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
not_found!('Snippet') unless snippet
env['api.format'] = :txt env['api.format'] = :txt
content_type 'text/plain' content_type 'text/plain'
present @snippet.content present snippet.content
end end
end end
end end
......
...@@ -3,10 +3,12 @@ require 'rails_helper' ...@@ -3,10 +3,12 @@ require 'rails_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
let(:project) { create(:empty_project, :public) }
let(:admin) { create(:admin) }
describe 'GET /projects/:project_id/snippets/:id' do describe 'GET /projects/:project_id/snippets/:id' do
# TODO (rspeicher): Deprecated; remove in 9.0 # TODO (rspeicher): Deprecated; remove in 9.0
it 'always exposes expires_at as nil' do it 'always exposes expires_at as nil' do
admin = create(:admin)
snippet = create(:project_snippet, author: admin) snippet = create(:project_snippet, author: admin)
get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}", admin) get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}", admin)
...@@ -17,9 +19,9 @@ describe API::API, api: true do ...@@ -17,9 +19,9 @@ describe API::API, api: true do
end end
describe 'GET /projects/:project_id/snippets/' do describe 'GET /projects/:project_id/snippets/' do
let(:user) { create(:user) }
it 'returns all snippets available to team member' do it 'returns all snippets available to team member' do
project = create(:project, :public)
user = create(:user)
project.team << [user, :developer] project.team << [user, :developer]
public_snippet = create(:project_snippet, :public, project: project) public_snippet = create(:project_snippet, :public, project: project)
internal_snippet = create(:project_snippet, :internal, project: project) internal_snippet = create(:project_snippet, :internal, project: project)
...@@ -34,8 +36,6 @@ describe API::API, api: true do ...@@ -34,8 +36,6 @@ describe API::API, api: true do
end end
it 'hides private snippets from regular user' do it 'hides private snippets from regular user' do
project = create(:project, :public)
user = create(:user)
create(:project_snippet, :private, project: project) create(:project_snippet, :private, project: project)
get api("/projects/#{project.id}/snippets/", user) get api("/projects/#{project.id}/snippets/", user)
...@@ -45,16 +45,16 @@ describe API::API, api: true do ...@@ -45,16 +45,16 @@ describe API::API, api: true do
end end
describe 'POST /projects/:project_id/snippets/' do describe 'POST /projects/:project_id/snippets/' do
it 'creates a new snippet' do let(:params) do
admin = create(:admin) {
project = create(:project)
params = {
title: 'Test Title', title: 'Test Title',
file_name: 'test.rb', file_name: 'test.rb',
code: 'puts "hello world"', code: 'puts "hello world"',
visibility_level: Gitlab::VisibilityLevel::PUBLIC visibility_level: Gitlab::VisibilityLevel::PUBLIC
} }
end
it 'creates a new snippet' do
post api("/projects/#{project.id}/snippets/", admin), params post api("/projects/#{project.id}/snippets/", admin), params
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
...@@ -64,12 +64,20 @@ describe API::API, api: true do ...@@ -64,12 +64,20 @@ describe API::API, api: true do
expect(snippet.file_name).to eq(params[:file_name]) expect(snippet.file_name).to eq(params[:file_name])
expect(snippet.visibility_level).to eq(params[:visibility_level]) expect(snippet.visibility_level).to eq(params[:visibility_level])
end end
it 'returns 400 for missing parameters' do
params.delete(:title)
post api("/projects/#{project.id}/snippets/", admin), params
expect(response).to have_http_status(400)
end
end end
describe 'PUT /projects/:project_id/snippets/:id/' do describe 'PUT /projects/:project_id/snippets/:id/' do
let(:snippet) { create(:project_snippet, author: admin) }
it 'updates snippet' do it 'updates snippet' do
admin = create(:admin)
snippet = create(:project_snippet, author: admin)
new_content = 'New content' new_content = 'New content'
put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin), code: new_content put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin), code: new_content
...@@ -78,9 +86,24 @@ describe API::API, api: true do ...@@ -78,9 +86,24 @@ describe API::API, api: true do
snippet.reload snippet.reload
expect(snippet.content).to eq(new_content) expect(snippet.content).to eq(new_content)
end end
it 'returns 404 for invalid snippet id' do
put api("/projects/#{snippet.project.id}/snippets/1234", admin), title: 'foo'
expect(response).to have_http_status(404)
expect(json_response['message']).to eq('404 Snippet Not Found')
end
it 'returns 400 for missing parameters' do
put api("/projects/#{project.id}/snippets/1234", admin)
expect(response).to have_http_status(400)
end
end end
describe 'DELETE /projects/:project_id/snippets/:id/' do describe 'DELETE /projects/:project_id/snippets/:id/' do
let(:snippet) { create(:project_snippet, author: admin) }
it 'deletes snippet' do it 'deletes snippet' do
admin = create(:admin) admin = create(:admin)
snippet = create(:project_snippet, author: admin) snippet = create(:project_snippet, author: admin)
...@@ -89,18 +112,31 @@ describe API::API, api: true do ...@@ -89,18 +112,31 @@ describe API::API, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'returns 404 for invalid snippet id' do
delete api("/projects/#{snippet.project.id}/snippets/1234", admin)
expect(response).to have_http_status(404)
expect(json_response['message']).to eq('404 Snippet Not Found')
end
end end
describe 'GET /projects/:project_id/snippets/:id/raw' do describe 'GET /projects/:project_id/snippets/:id/raw' do
it 'returns raw text' do let(:snippet) { create(:project_snippet, author: admin) }
admin = create(:admin)
snippet = create(:project_snippet, author: admin)
it 'returns raw text' do
get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", admin) get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", admin)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response.content_type).to eq 'text/plain' expect(response.content_type).to eq 'text/plain'
expect(response.body).to eq(snippet.content) expect(response.body).to eq(snippet.content)
end end
it 'returns 404 for invalid snippet id' do
delete api("/projects/#{snippet.project.id}/snippets/1234", admin)
expect(response).to have_http_status(404)
expect(json_response['message']).to eq('404 Snippet Not Found')
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment