debian-mirror-gitlab/lib/api/project_snippets.rb

203 lines
7.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
2021-01-03 14:25:43 +05:30
class ProjectSnippets < ::API::Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2020-04-08 14:13:33 +05:30
before { check_snippets_enabled }
2014-09-02 18:07:02 +05:30
2021-01-29 00:20:46 +05:30
feature_category :snippets
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
2022-05-07 20:08:51 +05:30
helpers SpammableActions::CaptchaCheck::RestApiActionsSupport
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
2022-06-21 17:19:12 +05:30
get ":id/snippets", urgency: :low do
2021-01-03 14:25:43 +05:30
authenticate!
2020-07-28 23:09:34 +05:30
present paginate(snippets_for_current_user), with: Entities::ProjectSnippet, current_user: current_user
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])
2021-01-03 14:25:43 +05:30
not_found!('Snippet') unless snippet
2020-07-28 23:09:34 +05:30
present snippet, with: Entities::ProjectSnippet, current_user: current_user
2017-08-17 22:00:37 +05:30
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-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'
2020-10-24 23:57:45 +05:30
use :create_file_params
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
post ":id/snippets" do
2021-01-03 14:25:43 +05:30
authenticate!
2020-03-13 15:44:24 +05:30
authorize! :create_snippet, user_project
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
snippet_params = process_create_params(declared_params(include_missing: false))
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
spam_params = ::Spam::SpamParams.new_from_request(request: request)
service_response = ::Snippets::CreateService.new(project: user_project, current_user: current_user, params: snippet_params, spam_params: spam_params).execute
2020-03-13 15:44:24 +05:30
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?
2020-07-28 23:09:34 +05:30
present snippet, with: Entities::ProjectSnippet, current_user: current_user
2014-09-02 18:07:02 +05:30
else
2022-05-07 20:08:51 +05:30
with_captcha_check_rest_api(spammable: snippet) do
render_api_error!({ error: service_response.message }, service_response.http_status)
end
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'
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'
2020-11-24 15:15:51 +05:30
optional :file_name, type: String, desc: 'The file name of the snippet'
optional :title, type: String, allow_blank: false, desc: 'The title of the 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-11-24 15:15:51 +05:30
use :update_file_params
use :minimum_update_params
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
2021-01-03 14:25:43 +05:30
authenticate!
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-13 15:44:24 +05:30
authorize! :update_snippet, snippet
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
validate_params_for_multiple_files(snippet)
snippet_params = process_update_params(declared_params(include_missing: false))
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
spam_params = ::Spam::SpamParams.new_from_request(request: request)
service_response = ::Snippets::UpdateService.new(project: user_project, current_user: current_user, params: snippet_params, spam_params: spam_params).execute(snippet)
2020-03-13 15:44:24 +05:30
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?
2020-07-28 23:09:34 +05:30
present snippet, with: Entities::ProjectSnippet, current_user: current_user
2014-09-02 18:07:02 +05:30
else
2022-05-07 20:08:51 +05:30
with_captcha_check_rest_api(spammable: snippet) do
render_api_error!({ error: service_response.message }, service_response.http_status)
end
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
2021-01-03 14:25:43 +05:30
authenticate!
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-13 15:44:24 +05:30
authorize! :admin_snippet, snippet
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +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
2020-05-24 23:13:21 +05:30
present content_for(snippet)
2014-09-02 18:07:02 +05:30
end
2020-07-28 23:09:34 +05:30
desc 'Get raw project snippet file contents from the repository'
params do
use :raw_file_params
end
get ":id/snippets/:snippet_id/files/:ref/:file_path/raw", requirements: { file_path: API::NO_SLASH_URL_PART_REGEX } do
snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
not_found!('Snippet') unless snippet&.repo_exists?
present file_content_for(snippet)
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