diff options
| author | Rémy Coutable <remy@rymai.me> | 2016-04-11 15:49:25 +0200 |
|---|---|---|
| committer | Rémy Coutable <remy@rymai.me> | 2016-04-18 14:47:50 +0200 |
| commit | 13804aba867d19009ca94d820aa7ec650a509f5a (patch) | |
| tree | 64c0d9415d81bb3104ba84af3834115f3a8cc5e1 /lib | |
| parent | 073c3d15c71a0f877b62c7d3d7417a9721da1dba (diff) | |
| download | gitlab-ce-13804aba867d19009ca94d820aa7ec650a509f5a.tar.gz | |
Continue implementation of the license template selector and /licenses API endpoint
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/api/entities.rb | 11 | ||||
| -rw-r--r-- | lib/api/licenses.rb | 57 |
2 files changed, 55 insertions, 13 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 60b9f5e0ece..61df948df60 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -439,5 +439,16 @@ module API class Variable < Grape::Entity expose :key, :value end + + class License < Grape::Entity + expose :key, :name, :nickname, :featured + expose :url, as: :html_url + expose(:source_url) { |license| license.meta['source'] } + expose(:description) { |license| license.meta['description'] } + expose(:conditions) { |license| license.meta['required'] } + expose(:permissions) { |license| license.meta['permitted'] } + expose(:limitations) { |license| license.meta['forbidden'] } + expose :content + end end end diff --git a/lib/api/licenses.rb b/lib/api/licenses.rb index 7fffbef84e4..dac29df30f9 100644 --- a/lib/api/licenses.rb +++ b/lib/api/licenses.rb @@ -1,27 +1,58 @@ module API # Licenses API class Licenses < Grape::API - YEAR_TEMPLATE_REGEX = /(\[|<|{)(year|yyyy)(\]|>|})/ - FULLNAME_TEMPLATE_REGEX = /\[fullname\]/ + PROJECT_TEMPLATE_REGEX = + /[\<\{\[] + (project|description| + one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here + [\>\}\]]/xi + YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i + FULLNAME_TEMPLATE_REGEX = + /[\<\{\[] + (fullname|name\sof\s(author|copyright\sowner)) + [\>\}\]]/xi + + # Get the list of the available license templates + # + # Parameters: + # popular - Filter licenses to only the popular ones + # + # Example Request: + # GET /licenses + # GET /licenses?popular=1 + get 'licenses' do + options = { + featured: params[:popular].present? ? true : nil + } + present Licensee::License.all(options), with: Entities::License + end # Get text for specific license # # Parameters: # key (required) - The key of a license - # fullname - Reository owner fullname + # project - Copyrighted project name + # fullname - Full name of copyright holder + # # Example Request: # GET /licenses/mit - get 'licenses/:key', requirements: { key: /[\w.-]*/ } do - env['api.format'] = :txt - license = Licensee::License.find(params[:key]).try(:text) + # + get 'licenses/:key', requirements: { key: /[\w\.-]+/ } do + required_attributes! [:key] + + not_found!('License') unless Licensee::License.find(params[:key]) + + # We create a fresh Licensee::License object since we'll modify its + # content in place below. + license = Licensee::License.new(params[:key]) + + license.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s) + license.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present? + + fullname = params[:fullname].presence || current_user.try(:name) + license.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname - if license - license - .gsub(YEAR_TEMPLATE_REGEX, Time.now.year.to_s) - .gsub(FULLNAME_TEMPLATE_REGEX, params[:fullname]) - else - error!('License not found', 404) - end + present license, with: Entities::License end end end |
