Commit 35b3a0b9 authored by Stan Hu's avatar Stan Hu

Fix Error 500s loading repositories with no master branch

We removed the exception handling for Rugged errors in !16770, which
revealed that the licensee gem attempts to retrieve a license file
via Rugged in `refs/heads/master` by default. If that branch
did not exist, a Rugged::ReferenceError would be thrown.

There were two issues:

1. Not every project uses `master` as the default branch. This
change uses the head commit to identify the license.

2. Removing the exception handling caused repositories to fail
loading. We can safely catch and ignore any Rugged error because
this means we weren't able to load a license file.

Closes #43268
parent dd633bc1
......@@ -592,8 +592,14 @@ class Repository
def license_key
return unless exists?
return unless head_commit
Licensee.license(path).try(:key)
# 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.project(path, revision: head_commit.sha).license.try(:key)
rescue Rugged::Error
end
end
cache_method :license_key
......
......@@ -873,6 +873,19 @@ describe Repository do
expect(repository.license_key).to be_nil
end
it 'returns nil when the commit SHA does not exist' do
allow(repository.head_commit).to receive(:sha).and_return('1' * 40)
expect(repository.license_key).to be_nil
end
it 'returns the license key even when master does not exist' do
repository.rm_branch(user, 'master')
project.change_head('test')
expect(repository.license_key).to eq('mit')
end
it 'returns the license key' do
repository.create_file(user, 'LICENSE',
Licensee::License.new('mit').content,
......
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