API: fixes a few return codes for project snippets
When using project snippets via API the functions now provide status codes for different situations other then only returning 404 error. If required parameters are missing, e.g. `title` when creating a project snippet a 400 (Bad request) error is returned. The snippet delete function now is idempotent and returns a 200 (Ok) regardless if the snippet with the given id is available or not. Changing return codes of these functions has the advantage that the 404 error is used only for resources, which are not available. Tests added to check these status codes when handling project snippets.
This commit is contained in:
parent
375caeefcf
commit
fd01f3aacd
2 changed files with 40 additions and 4 deletions
|
@ -368,6 +368,10 @@ module Gitlab
|
|||
post ":id/snippets" do
|
||||
authorize! :write_snippet, user_project
|
||||
|
||||
error!("Title not given", 400) if !params[:title].present?
|
||||
error!("Filename not given", 400) if !params[:file_name].present?
|
||||
error!("Code not given", 400) if !params[:code].present?
|
||||
|
||||
attrs = attributes_for_keys [:title, :file_name]
|
||||
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
|
||||
attrs[:content] = params[:code] if params[:code].present?
|
||||
|
@ -415,10 +419,12 @@ module Gitlab
|
|||
# Example Request:
|
||||
# DELETE /projects/:id/snippets/:snippet_id
|
||||
delete ":id/snippets/:snippet_id" do
|
||||
@snippet = user_project.snippets.find(params[:snippet_id])
|
||||
authorize! :modify_snippet, @snippet
|
||||
|
||||
@snippet.destroy
|
||||
begin
|
||||
@snippet = user_project.snippets.find(params[:snippet_id])
|
||||
authorize! :modify_snippet, user_project
|
||||
@snippet.destroy
|
||||
rescue
|
||||
end
|
||||
end
|
||||
|
||||
# Get a raw project snippet
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue