Commit 0b92a595 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Remove instances of deprecated Snippet code calls

parent 69304a32
---
title: Remove deprecated Snippet `code` attribute from Project Snippets API
merge_request: 30739
author:
type: other
......@@ -57,18 +57,15 @@ module API
params do
requires :title, type: String, desc: 'The title of the snippet'
requires :file_name, type: String, desc: 'The file name of the snippet'
optional :code, type: String, allow_blank: false, desc: 'The content of the snippet (deprecated in favor of "content")'
optional :content, type: String, allow_blank: false, desc: 'The content of the snippet'
optional :description, type: String, desc: 'The description of a snippet'
requires :visibility, type: String,
values: Gitlab::VisibilityLevel.string_values,
desc: 'The visibility of the snippet'
mutually_exclusive :code, :content
end
post ":id/snippets" do
authorize! :create_snippet, user_project
snippet_params = declared_params(include_missing: false).merge(request: request, api: true)
snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
service_response = ::Snippets::CreateService.new(user_project, current_user, snippet_params).execute
snippet = service_response.payload[:snippet]
......@@ -89,14 +86,12 @@ module API
requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
optional :title, type: String, desc: 'The title of the snippet'
optional :file_name, type: String, desc: 'The file name of the snippet'
optional :code, type: String, allow_blank: false, desc: 'The content of the snippet (deprecated in favor of "content")'
optional :content, type: String, allow_blank: false, desc: 'The content of the snippet'
optional :description, type: String, desc: 'The description of a snippet'
optional :visibility, type: String,
values: Gitlab::VisibilityLevel.string_values,
desc: 'The visibility of the snippet'
at_least_one_of :title, :file_name, :code, :content, :visibility_level
mutually_exclusive :code, :content
at_least_one_of :title, :file_name, :content, :visibility_level
end
# rubocop: disable CodeReuse/ActiveRecord
put ":id/snippets/:snippet_id" do
......@@ -108,8 +103,6 @@ module API
snippet_params = declared_params(include_missing: false)
.merge(request: request, api: true)
snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
service_response = ::Snippets::UpdateService.new(user_project, current_user, snippet_params).execute(snippet)
snippet = service_response.payload[:snippet]
......
......@@ -119,7 +119,7 @@ describe API::ProjectSnippets do
title: 'Test Title',
file_name: 'test.rb',
description: 'test description',
code: 'puts "hello world"',
content: 'puts "hello world"',
visibility: 'public'
}
end
......@@ -138,7 +138,7 @@ describe API::ProjectSnippets do
blob = snippet.repository.blob_at('master', params[:file_name])
expect(blob.data).to eq params[:code]
expect(blob.data).to eq params[:content]
end
end
......@@ -180,7 +180,7 @@ describe API::ProjectSnippets do
expect(response).to have_gitlab_http_status(:created)
snippet = ProjectSnippet.find(json_response['id'])
expect(snippet.content).to eq(params[:code])
expect(snippet.content).to eq(params[:content])
expect(snippet.description).to eq(params[:description])
expect(snippet.title).to eq(params[:title])
expect(snippet.file_name).to eq(params[:file_name])
......@@ -197,7 +197,7 @@ describe API::ProjectSnippets do
expect(response).to have_gitlab_http_status(:created)
snippet = ProjectSnippet.find(json_response['id'])
expect(snippet.content).to eq(params[:code])
expect(snippet.content).to eq(params[:content])
expect(snippet.description).to eq(params[:description])
expect(snippet.title).to eq(params[:title])
expect(snippet.file_name).to eq(params[:file_name])
......@@ -208,29 +208,6 @@ describe API::ProjectSnippets do
subject { post api("/projects/#{project.id}/snippets/", admin), params: params }
end
it 'creates a new snippet with content parameter' do
params[:content] = params.delete(:code)
post api("/projects/#{project.id}/snippets/", admin), params: params
expect(response).to have_gitlab_http_status(:created)
snippet = ProjectSnippet.find(json_response['id'])
expect(snippet.content).to eq(params[:content])
expect(snippet.description).to eq(params[:description])
expect(snippet.title).to eq(params[:title])
expect(snippet.file_name).to eq(params[:file_name])
expect(snippet.visibility_level).to eq(Snippet::PUBLIC)
end
it 'returns 400 when both code and content parameters specified' do
params[:content] = params[:code]
post api("/projects/#{project.id}/snippets/", admin), params: params
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq('code, content are mutually exclusive')
end
it 'returns 400 for missing parameters' do
params.delete(:title)
......@@ -239,8 +216,8 @@ describe API::ProjectSnippets do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns 400 for empty code field' do
params[:code] = ''
it 'returns 400 for empty content field' do
params[:content] = ''
post api("/projects/#{project.id}/snippets/", admin), params: params
......@@ -298,7 +275,7 @@ describe API::ProjectSnippets do
new_content = 'New content'
new_description = 'New description'
update_snippet(params: { code: new_content, description: new_description, visibility: 'private' })
update_snippet(params: { content: new_content, description: new_description, visibility: 'private' })
expect(response).to have_gitlab_http_status(:ok)
snippet.reload
......@@ -319,13 +296,6 @@ describe API::ProjectSnippets do
expect(snippet.description).to eq(new_description)
end
it 'returns 400 when both code and content parameters specified' do
update_snippet(params: { code: 'some content', content: 'other content' })
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eq('code, content are mutually exclusive')
end
it 'returns 404 for invalid snippet id' do
update_snippet(snippet_id: non_existing_record_id, params: { title: 'foo' })
......@@ -339,10 +309,8 @@ describe API::ProjectSnippets do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns 400 for empty code field' do
new_content = ''
update_snippet(params: { code: new_content })
it 'returns 400 for empty content field' do
update_snippet(params: { content: '' })
expect(response).to have_gitlab_http_status(:bad_request)
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