summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-04-22 15:03:54 +0200
committerRémy Coutable <remy@rymai.me>2016-04-22 15:07:12 +0200
commit9e38a2ca628b5d03d6a82e1b09d21c915a47a3c5 (patch)
treec6d5286a9a0e2a7a19a1197777b169481627ade0
parentd5398e9656c1174d4d5be5a8cdad2a26d7587a27 (diff)
downloadgitlab-ce-15470-fix-unknown-license-not-shown.tar.gz
Fix license detection to detect all license files, not only known licenses15470-fix-unknown-license-not-shown
Fixes #15470. Signed-off-by: Rémy Coutable <remy@rymai.me>
-rw-r--r--CHANGELOG5
-rw-r--r--app/helpers/projects_helper.rb20
-rw-r--r--app/models/repository.rb10
-rw-r--r--spec/helpers/projects_helper_spec.rb26
-rw-r--r--spec/models/repository_spec.rb46
5 files changed, 60 insertions, 47 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 361e33eb6b3..1deab582211 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,7 +2,10 @@ Please view this file on the master branch, on stable branches it's out of date.
v 8.8.0 (unreleased)
-v 8.7.0 (unreleased)
+v 8.7.1 (unreleased)
+ - Fix license detection to detect all license files, not only known licenses
+
+v 8.7.0
- Gitlab::GitAccess and Gitlab::GitAccessWiki are now instrumented
- Fix vulnerability that made it possible to gain access to private labels and milestones
- The number of InfluxDB points stored per UDP packet can now be configured
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index ab85694da3f..3d5e61d2c18 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -123,6 +123,18 @@ module ProjectsHelper
end
end
+ def license_short_name(project)
+ no_license_key = project.repository.license_key.nil? ||
+ # Back-compat if cache contains 'no-license', can be removed in a few weeks
+ project.repository.license_key == 'no-license'
+
+ return 'LICENSE' if no_license_key
+
+ license = Licensee::License.new(project.repository.license_key)
+
+ license.nickname || license.name
+ end
+
private
def get_project_nav_tabs(project, current_user)
@@ -320,14 +332,6 @@ module ProjectsHelper
@ref || @repository.try(:root_ref)
end
- def license_short_name(project)
- license = Licensee::License.new(project.repository.license_key)
-
- license.nickname || license.name
- end
-
- private
-
def filename_path(project, filename)
if project && blob = project.repository.send(filename)
namespace_project_blob_path(
diff --git a/app/models/repository.rb b/app/models/repository.rb
index da751591103..61c8dce6060 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -466,8 +466,8 @@ class Repository
return nil if !exists? || empty?
cache.fetch(:license_blob) do
- if licensee_project.license
- blob_at_branch(root_ref, licensee_project.matched_file.filename)
+ tree(:head).blobs.find do |file|
+ file.name =~ /\A(licen[sc]e|copying)(\..+|\z)/i
end
end
end
@@ -476,7 +476,7 @@ class Repository
return nil if !exists? || empty?
cache.fetch(:license_key) do
- licensee_project.license.try(:key) || 'no-license'
+ Licensee.license(path).try(:key)
end
end
@@ -959,8 +959,4 @@ class Repository
def cache
@cache ||= RepositoryCache.new(path_with_namespace)
end
-
- def licensee_project
- @licensee_project ||= Licensee.project(path)
- end
end
diff --git a/spec/helpers/projects_helper_spec.rb b/spec/helpers/projects_helper_spec.rb
index c258cfebd73..f483e28f805 100644
--- a/spec/helpers/projects_helper_spec.rb
+++ b/spec/helpers/projects_helper_spec.rb
@@ -105,4 +105,30 @@ describe ProjectsHelper do
end
end
end
+
+ describe '#license_short_name' do
+ let(:project) { create(:project) }
+
+ context 'when project.repository has a license_key' do
+ it 'returns the nickname of the license if present' do
+ allow(project.repository).to receive(:license_key).and_return('agpl-3.0')
+
+ expect(helper.license_short_name(project)).to eq('GNU AGPLv3')
+ end
+
+ it 'returns the name of the license if nickname is not present' do
+ allow(project.repository).to receive(:license_key).and_return('mit')
+
+ expect(helper.license_short_name(project)).to eq('MIT License')
+ end
+ end
+
+ context 'when project.repository has a no license_key but a license_blob' do
+ it 'returns LICENSE' do
+ allow(project.repository).to receive(:license_key).and_return(nil)
+
+ expect(helper.license_short_name(project)).to eq('LICENSE')
+ end
+ end
+ end
end
diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb
index b561aa663d1..c19524a01f8 100644
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -132,7 +132,6 @@ describe Repository, models: true do
it { expect(subject.basename).to eq('a/b/c') }
end
end
-
end
describe '#license_blob' do
@@ -148,39 +147,18 @@ describe Repository, models: true do
expect(repository.license_blob).to be_nil
end
- it 'favors license file with no extension' do
- repository.commit_file(user, 'LICENSE', Licensee::License.new('mit').content, 'Add LICENSE', 'master', false)
- repository.commit_file(user, 'LICENSE.md', Licensee::License.new('mit').content, 'Add LICENSE.md', 'master', false)
-
- expect(repository.license_blob.name).to eq('LICENSE')
- end
-
- it 'favors .md file to .txt' do
- repository.commit_file(user, 'LICENSE.md', Licensee::License.new('mit').content, 'Add LICENSE.md', 'master', false)
- repository.commit_file(user, 'LICENSE.txt', Licensee::License.new('mit').content, 'Add LICENSE.txt', 'master', false)
-
- expect(repository.license_blob.name).to eq('LICENSE.md')
- end
-
- it 'favors LICENCE to LICENSE' do
- repository.commit_file(user, 'LICENSE', Licensee::License.new('mit').content, 'Add LICENSE', 'master', false)
- repository.commit_file(user, 'LICENCE', Licensee::License.new('mit').content, 'Add LICENCE', 'master', false)
-
- expect(repository.license_blob.name).to eq('LICENCE')
- end
-
- it 'favors LICENSE to COPYING' do
- repository.commit_file(user, 'LICENSE', Licensee::License.new('mit').content, 'Add LICENSE', 'master', false)
- repository.commit_file(user, 'COPYING', Licensee::License.new('mit').content, 'Add COPYING', 'master', false)
+ it 'detects license file with no recognizable open-source license content' do
+ repository.commit_file(user, 'LICENSE', 'Copyright!', 'Add LICENSE', 'master', false)
expect(repository.license_blob.name).to eq('LICENSE')
end
- it 'favors LICENCE to COPYING' do
- repository.commit_file(user, 'LICENCE', Licensee::License.new('mit').content, 'Add LICENCE', 'master', false)
- repository.commit_file(user, 'COPYING', Licensee::License.new('mit').content, 'Add COPYING', 'master', false)
+ %w[LICENSE LICENCE LiCensE LICENSE.md LICENSE.foo COPYING COPYING.md].each do |filename|
+ it "detects '#{filename}'" do
+ repository.commit_file(user, filename, Licensee::License.new('mit').content, "Add #{filename}", 'master', false)
- expect(repository.license_blob.name).to eq('LICENCE')
+ expect(repository.license_blob.name).to eq(filename)
+ end
end
end
@@ -190,8 +168,14 @@ describe Repository, models: true do
repository.remove_file(user, 'LICENSE', 'Remove LICENSE', 'master')
end
- it 'returns "no-license" when no license is detected' do
- expect(repository.license_key).to eq('no-license')
+ it 'returns nil when no license is detected' do
+ expect(repository.license_key).to be_nil
+ end
+
+ it 'detects license file with no recognizable open-source license content' do
+ repository.commit_file(user, 'LICENSE', 'Copyright!', 'Add LICENSE', 'master', false)
+
+ expect(repository.license_key).to be_nil
end
it 'returns the license key' do