summaryrefslogtreecommitdiff
path: root/lib/api/snippets.rb
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-03-05 16:12:27 +0000
committerNick Thomas <nick@gitlab.com>2019-03-06 09:05:03 +0000
commite05a86cecdf52a0ec1f0f4ce4f30287f881b8ea2 (patch)
treebf11e94a8cd21c43affadcd8fd00f9f5d23d0d6e /lib/api/snippets.rb
parent42d3117f9c3371e07e8b0aafab6f504e87251c2a (diff)
downloadgitlab-ce-e05a86cecdf52a0ec1f0f4ce4f30287f881b8ea2.tar.gz
Allow all personal snippets to be accessed by API
Previously, you could only access personal snippets in the API if you had authored them. The documentation doesn't state that this is the case, and it's quite surprising.
Diffstat (limited to 'lib/api/snippets.rb')
-rw-r--r--lib/api/snippets.rb25
1 files changed, 12 insertions, 13 deletions
diff --git a/lib/api/snippets.rb b/lib/api/snippets.rb
index 326d55afd0e..f8b37b33348 100644
--- a/lib/api/snippets.rb
+++ b/lib/api/snippets.rb
@@ -16,6 +16,10 @@ module API
def public_snippets
SnippetsFinder.new(current_user, scope: :are_public).execute
end
+
+ def snippets
+ SnippetsFinder.new(current_user).execute
+ end
end
desc 'Get a snippets list for authenticated user' do
@@ -48,7 +52,10 @@ module API
requires :id, type: Integer, desc: 'The ID of a snippet'
end
get ':id' do
- snippet = snippets_for_current_user.find(params[:id])
+ snippet = snippets.find_by_id(params[:id])
+
+ break not_found!('Snippet') unless snippet
+
present snippet, with: Entities::PersonalSnippet
end
@@ -94,9 +101,8 @@ module API
desc: 'The visibility of the snippet'
at_least_one_of :title, :file_name, :content, :visibility
end
- # rubocop: disable CodeReuse/ActiveRecord
put ':id' do
- snippet = snippets_for_current_user.find_by(id: params.delete(:id))
+ snippet = snippets_for_current_user.find_by_id(params.delete(:id))
break not_found!('Snippet') unless snippet
authorize! :update_personal_snippet, snippet
@@ -113,7 +119,6 @@ module API
render_validation_error!(snippet)
end
end
- # rubocop: enable CodeReuse/ActiveRecord
desc 'Remove snippet' do
detail 'This feature was introduced in GitLab 8.15.'
@@ -122,16 +127,14 @@ module API
params do
requires :id, type: Integer, desc: 'The ID of a snippet'
end
- # rubocop: disable CodeReuse/ActiveRecord
delete ':id' do
- snippet = snippets_for_current_user.find_by(id: params.delete(:id))
+ snippet = snippets_for_current_user.find_by_id(params.delete(:id))
break not_found!('Snippet') unless snippet
authorize! :destroy_personal_snippet, snippet
destroy_conditionally!(snippet)
end
- # rubocop: enable CodeReuse/ActiveRecord
desc 'Get a raw snippet' do
detail 'This feature was introduced in GitLab 8.15.'
@@ -139,9 +142,8 @@ module API
params do
requires :id, type: Integer, desc: 'The ID of a snippet'
end
- # rubocop: disable CodeReuse/ActiveRecord
get ":id/raw" do
- snippet = snippets_for_current_user.find_by(id: params.delete(:id))
+ snippet = snippets.find_by_id(params.delete(:id))
break not_found!('Snippet') unless snippet
env['api.format'] = :txt
@@ -149,7 +151,6 @@ module API
header['Content-Disposition'] = 'attachment'
present snippet.content
end
- # rubocop: enable CodeReuse/ActiveRecord
desc 'Get the user agent details for a snippet' do
success Entities::UserAgentDetail
@@ -157,17 +158,15 @@ module API
params do
requires :id, type: Integer, desc: 'The ID of a snippet'
end
- # rubocop: disable CodeReuse/ActiveRecord
get ":id/user_agent_detail" do
authenticated_as_admin!
- snippet = Snippet.find_by!(id: params[:id])
+ snippet = Snippet.find_by_id!(params[:id])
break not_found!('UserAgentDetail') unless snippet.user_agent_detail
present snippet.user_agent_detail, with: Entities::UserAgentDetail
end
- # rubocop: enable CodeReuse/ActiveRecord
end
end
end