diff options
author | Stan Hu <stanhu@gmail.com> | 2018-02-14 12:53:53 -0800 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-02-14 12:53:53 -0800 |
commit | 35b3a0b967399d0b4340325d067592c166f208e5 (patch) | |
tree | f15bdfa166b0ea22cdf48063b1a2468d9b74c6c5 | |
parent | dd633bc1888453a07474d045eca91a9e66302ce0 (diff) | |
download | gitlab-ce-35b3a0b967399d0b4340325d067592c166f208e5.tar.gz |
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
-rw-r--r-- | app/models/repository.rb | 8 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 13 |
2 files changed, 20 insertions, 1 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 1cf55fd4332..eef187b623b 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -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 diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index a6d48e369ac..1b5b746b340 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -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, |