2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module API
|
|
|
|
class ProjectSnippets < Grape::API
|
2017-08-17 22:00:37 +05:30
|
|
|
include PaginationParams
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
before { authenticate! }
|
2020-04-08 14:13:33 +05:30
|
|
|
before { check_snippets_enabled }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2020-05-24 23:13:21 +05:30
|
|
|
helpers Helpers::SnippetsHelpers
|
2014-09-02 18:07:02 +05:30
|
|
|
helpers do
|
2020-04-08 14:13:33 +05:30
|
|
|
def check_snippets_enabled
|
|
|
|
forbidden! unless user_project.feature_available?(:snippets, current_user)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def handle_project_member_errors(errors)
|
|
|
|
if errors[:project_access].any?
|
|
|
|
error!(errors[:project_access], 422)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
not_found!
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
def snippets_for_current_user
|
2017-08-17 22:00:37 +05:30
|
|
|
SnippetsFinder.new(current_user, project: user_project).execute
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Get all project snippets' do
|
|
|
|
success Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
get ":id/snippets" do
|
2016-06-02 11:05:42 +05:30
|
|
|
present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Get a single project snippet' do
|
|
|
|
success Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
get ":id/snippets/:snippet_id" do
|
2017-08-17 22:00:37 +05:30
|
|
|
snippet = snippets_for_current_user.find(params[:snippet_id])
|
|
|
|
present snippet, with: Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a new project snippet' do
|
|
|
|
success Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
params do
|
2020-05-24 23:13:21 +05:30
|
|
|
requires :title, type: String, allow_blank: false, desc: 'The title of the snippet'
|
2017-08-17 22:00:37 +05:30
|
|
|
requires :file_name, type: String, desc: 'The file name of the snippet'
|
2020-05-24 23:13:21 +05:30
|
|
|
requires :content, type: String, allow_blank: false, desc: 'The content of the snippet'
|
2017-09-10 17:25:29 +05:30
|
|
|
optional :description, type: String, desc: 'The description of a snippet'
|
2017-08-17 22:00:37 +05:30
|
|
|
requires :visibility, type: String,
|
|
|
|
values: Gitlab::VisibilityLevel.string_values,
|
|
|
|
desc: 'The visibility of the snippet'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
post ":id/snippets" do
|
2020-03-09 13:42:32 +05:30
|
|
|
authorize! :create_snippet, user_project
|
2019-12-04 20:38:33 +05:30
|
|
|
snippet_params = declared_params(include_missing: false).merge(request: request, api: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
service_response = ::Snippets::CreateService.new(user_project, current_user, snippet_params).execute
|
|
|
|
snippet = service_response.payload[:snippet]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
if service_response.success?
|
2017-08-17 22:00:37 +05:30
|
|
|
present snippet, with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2020-05-24 23:13:21 +05:30
|
|
|
render_spam_error! if snippet.spam?
|
|
|
|
|
|
|
|
render_api_error!({ error: service_response.message }, service_response.http_status)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Update an existing project snippet' do
|
|
|
|
success Entities::ProjectSnippet
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
|
2020-05-24 23:13:21 +05:30
|
|
|
optional :title, type: String, allow_blank: false, desc: 'The title of the snippet'
|
2017-08-17 22:00:37 +05:30
|
|
|
optional :file_name, type: String, desc: 'The file name of the snippet'
|
2019-12-04 20:38:33 +05:30
|
|
|
optional :content, type: String, allow_blank: false, desc: 'The content of the snippet'
|
2017-09-10 17:25:29 +05:30
|
|
|
optional :description, type: String, desc: 'The description of a snippet'
|
2017-08-17 22:00:37 +05:30
|
|
|
optional :visibility, type: String,
|
|
|
|
values: Gitlab::VisibilityLevel.string_values,
|
|
|
|
desc: 'The visibility of the snippet'
|
2020-05-24 23:13:21 +05:30
|
|
|
at_least_one_of :title, :file_name, :content, :visibility_level
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
put ":id/snippets/:snippet_id" do
|
2017-08-17 22:00:37 +05:30
|
|
|
snippet = snippets_for_current_user.find_by(id: params.delete(:snippet_id))
|
|
|
|
not_found!('Snippet') unless snippet
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
authorize! :update_snippet, snippet
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
snippet_params = declared_params(include_missing: false)
|
|
|
|
.merge(request: request, api: true)
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
service_response = ::Snippets::UpdateService.new(user_project, current_user, snippet_params).execute(snippet)
|
|
|
|
snippet = service_response.payload[:snippet]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
if service_response.success?
|
2017-08-17 22:00:37 +05:30
|
|
|
present snippet, with: Entities::ProjectSnippet
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2020-05-24 23:13:21 +05:30
|
|
|
render_spam_error! if snippet.spam?
|
|
|
|
|
|
|
|
render_api_error!({ error: service_response.message }, service_response.http_status)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Delete a project snippet'
|
|
|
|
params do
|
|
|
|
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
delete ":id/snippets/:snippet_id" do
|
2017-08-17 22:00:37 +05:30
|
|
|
snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
|
|
|
|
not_found!('Snippet') unless snippet
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
authorize! :admin_snippet, snippet
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
destroy_conditionally!(snippet) do |snippet|
|
|
|
|
service = ::Snippets::DestroyService.new(current_user, snippet)
|
|
|
|
response = service.execute
|
|
|
|
|
|
|
|
if response.error?
|
|
|
|
render_api_error!({ error: response.message }, response.http_status)
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
desc 'Get a raw project snippet'
|
|
|
|
params do
|
|
|
|
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
get ":id/snippets/:snippet_id/raw" do
|
2017-08-17 22:00:37 +05:30
|
|
|
snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
|
|
|
|
not_found!('Snippet') unless snippet
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
env['api.format'] = :txt
|
|
|
|
content_type 'text/plain'
|
2020-05-24 23:13:21 +05:30
|
|
|
present content_for(snippet)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
desc 'Get the user agent details for a project snippet' do
|
|
|
|
success Entities::UserAgentDetail
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
get ":id/snippets/:snippet_id/user_agent_detail" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
snippet = Snippet.find_by!(id: params[:snippet_id], project_id: params[:id])
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
break not_found!('UserAgentDetail') unless snippet.user_agent_detail
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
present snippet.user_agent_detail, with: Entities::UserAgentDetail
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|