Commit 34fbade4 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Client implementation for Licensee#key

Part of the migration as tracked in: gitlab-org/gitaly#1026
parent e66e7767
......@@ -589,15 +589,7 @@ class Repository
def license_key
return unless exists?
# The licensee gem creates a Rugged object from the path:
# https://github.com/benbalter/licensee/blob/v8.7.0/lib/licensee/projects/git_project.rb
begin
Licensee.license(path).try(:key)
# Normally we would rescue Rugged::Error, but that is banned by lint-rugged
# and we need to migrate this endpoint to Gitaly:
# https://gitlab.com/gitlab-org/gitaly/issues/1026
rescue
end
raw_repository.license_short_name
end
cache_method :license_key
......
......@@ -1032,6 +1032,21 @@ module Gitlab
end
end
def license_short_name
gitaly_migrate(:license_short_name) do |is_enabled|
if is_enabled
gitaly_repository_client.license_short_name
else
begin
# The licensee gem creates a Rugged object from the path:
# https://github.com/benbalter/licensee/blob/v8.7.0/lib/licensee/projects/git_project.rb
Licensee.license(path).try(:key)
rescue Rugged::Error
end
end
end
end
def with_repo_branch_commit(start_repository, start_branch_name)
Gitlab::Git.check_namespace!(start_repository)
start_repository = RemoteRepository.new(start_repository) unless start_repository.is_a?(RemoteRepository)
......
......@@ -249,6 +249,14 @@ module Gitlab
raise Gitlab::Git::OSError.new(response.error) unless response.error.empty?
end
def license_short_name
request = Gitaly::FindLicenseRequest.new(repository: @gitaly_repo)
response = GitalyClient.call(@storage, :repository_service, :find_license, request, timeout: GitalyClient.fast_timeout)
response.license_short_name.presence
end
end
end
end
......@@ -1649,6 +1649,35 @@ describe Gitlab::Git::Repository, seed_helper: true do
end
end
describe '#license_short_name' do
shared_examples 'acquiring the Licensee license key' do
subject { repository.license_short_name }
context 'when no license file can be found' do
let(:project) { create(:project, :repository) }
let(:repository) { project.repository.raw_repository }
before do
project.repository.delete_file(project.owner, 'LICENSE', message: 'remove license', branch_name: 'master')
end
it { is_expected.to be_nil }
end
context 'when an mit license is found' do
it { is_expected.to eq('mit') }
end
end
context 'when gitaly is enabled' do
it_behaves_like 'acquiring the Licensee license key'
end
context 'when gitaly is disabled', :disable_gitaly do
it_behaves_like 'acquiring the Licensee license key'
end
end
describe '#with_repo_branch_commit' do
context 'when comparing with the same repository' do
let(:start_repository) { repository }
......
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