Commit f6f62950 authored by Nick Thomas's avatar Nick Thomas

Convert the license template API to use the new LicenseTemplateFinder

parent 40d1fc1c
...@@ -1159,7 +1159,7 @@ module API ...@@ -1159,7 +1159,7 @@ module API
class License < Grape::Entity class License < Grape::Entity
expose :key, :name, :nickname expose :key, :name, :nickname
expose :featured, as: :popular expose :popular?, as: :popular
expose :url, as: :html_url expose :url, as: :html_url
expose(:source_url) { |license| license.meta['source'] } expose(:source_url) { |license| license.meta['source'] }
expose(:description) { |license| license.meta['description'] } expose(:description) { |license| license.meta['description'] }
......
...@@ -16,31 +16,8 @@ module API ...@@ -16,31 +16,8 @@ module API
gitlab_version: 8.15 gitlab_version: 8.15
} }
}.freeze }.freeze
PROJECT_TEMPLATE_REGEX =
%r{[\<\{\[]
(project|description|
one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here
[\>\}\]]}xi.freeze
YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i.freeze
FULLNAME_TEMPLATE_REGEX =
%r{[\<\{\[]
(fullname|name\sof\s(author|copyright\sowner))
[\>\}\]]}xi.freeze
helpers do helpers do
def parsed_license_template
# We create a fresh Licensee::License object since we'll modify its
# content in place below.
template = Licensee::License.new(params[:name])
template.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
template.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present?
fullname = params[:fullname].presence || current_user.try(:name)
template.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname
template
end
def render_response(template_type, template) def render_response(template_type, template)
not_found!(template_type.to_s.singularize) unless template not_found!(template_type.to_s.singularize) unless template
present template, with: Entities::Template present template, with: Entities::Template
...@@ -56,11 +33,12 @@ module API ...@@ -56,11 +33,12 @@ module API
use :pagination use :pagination
end end
get "templates/licenses" do get "templates/licenses" do
options = { popular = declared(params)[:popular]
featured: declared(params)[:popular].present? ? true : nil popular = to_boolean(popular) if popular.present?
}
licences = ::Kaminari.paginate_array(Licensee::License.all(options)) templates = LicenseTemplateFinder.new(popular: popular).execute
present paginate(licences), with: Entities::License
present paginate(::Kaminari.paginate_array(templates)), with: ::API::Entities::License
end end
desc 'Get the text for a specific license' do desc 'Get the text for a specific license' do
...@@ -71,9 +49,15 @@ module API ...@@ -71,9 +49,15 @@ module API
requires :name, type: String, desc: 'The name of the template' requires :name, type: String, desc: 'The name of the template'
end end
get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ } do get "templates/licenses/:name", requirements: { name: /[\w\.-]+/ } do
not_found!('License') unless Licensee::License.find(declared(params)[:name]) templates = LicenseTemplateFinder.new.execute
template = templates.find { |template| template.key == params[:name] }
not_found!('License') unless template.present?
template = parsed_license_template template.resolve!(
project_name: params[:project].presence,
fullname: params[:fullname].presence || current_user&.name
)
present template, with: ::API::Entities::License present template, with: ::API::Entities::License
end end
......
...@@ -56,6 +56,8 @@ describe API::Templates do ...@@ -56,6 +56,8 @@ describe API::Templates do
end end
it 'returns a license template' do it 'returns a license template' do
expect(response).to have_gitlab_http_status(200)
expect(json_response['key']).to eq('mit') expect(json_response['key']).to eq('mit')
expect(json_response['name']).to eq('MIT License') expect(json_response['name']).to eq('MIT License')
expect(json_response['nickname']).to be_nil expect(json_response['nickname']).to be_nil
...@@ -181,6 +183,7 @@ describe API::Templates do ...@@ -181,6 +183,7 @@ describe API::Templates do
it 'replaces the copyright owner placeholder with the name of the current user' do it 'replaces the copyright owner placeholder with the name of the current user' do
get api('/templates/licenses/mit', user) get api('/templates/licenses/mit', user)
expect(response).to have_gitlab_http_status(200)
expect(json_response['content']).to include("Copyright (c) #{Time.now.year} #{user.name}") expect(json_response['content']).to include("Copyright (c) #{Time.now.year} #{user.name}")
end end
end 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