diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-02-23 21:24:11 +0100 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-03-06 12:53:13 +0100 |
commit | 870109833d1038631c18cf341e254d43b5a7f748 (patch) | |
tree | a39ee5231654b80f86304058caf9df21b7649f61 | |
parent | d4d0740e24315e7071f90b3744377b3ca3b5816a (diff) | |
download | gitlab-ce-870109833d1038631c18cf341e254d43b5a7f748.tar.gz |
Treat special cases accordingly.
* Event without project
* Fail early on unexpectedly missing author
-rw-r--r-- | app/models/user_contributed_projects.rb | 16 | ||||
-rw-r--r-- | spec/models/user_contributed_projects_spec.rb | 8 |
2 files changed, 23 insertions, 1 deletions
diff --git a/app/models/user_contributed_projects.rb b/app/models/user_contributed_projects.rb index e051e75af1d..bc63f6be1b7 100644 --- a/app/models/user_contributed_projects.rb +++ b/app/models/user_contributed_projects.rb @@ -8,7 +8,21 @@ class UserContributedProjects < ActiveRecord::Base CACHE_EXPIRY_TIME = 1.day def self.track(event) - attributes = {project_id: event.project_id, user_id: event.author_id} + # For events without a project, we simply don't care. + # An example of this is the creation of a snippet (which + # is not related to any project). + return unless event.project + + # This is a precaution because the cache lookup + # will work just fine without an author. + # + # However, this should never happen (tm). + raise 'event#author not present unexpectedly' unless event.author + + attributes = { + project_id: event.project_id, + user_id: event.author_id + } cached_exists?(attributes) do begin diff --git a/spec/models/user_contributed_projects_spec.rb b/spec/models/user_contributed_projects_spec.rb index b0a10ca59ab..776f36675d8 100644 --- a/spec/models/user_contributed_projects_spec.rb +++ b/spec/models/user_contributed_projects_spec.rb @@ -31,6 +31,14 @@ describe UserContributedProjects do described_class.track(event) end.to change { UserContributedProjects.count }.from(0).to(1) end + + describe 'with an event without a project' do + let(:event) { build(:event, project: nil) } + + it 'ignores the event' do + expect { subject }.not_to change { UserContributedProjects.count } + end + end end it { is_expected.to validate_presence_of(:project) } |