Commit 679d9b21 authored by Tiago Botelho's avatar Tiago Botelho

Removes idenfitication by commit from Gitlab::Identifier

Before we would need to identify a user when pushing
through the GitLab UI. Since this is no longer the case
we can remove the identification by commit and instead,
use the identify_using_user
parent f8ee07d9
......@@ -31,10 +31,12 @@ class PostReceive
refs = Set.new
@user = post_received.identify
post_received.changes_refs do |oldrev, newrev, ref|
@user ||= post_received.identify(newrev)
break unless @user
unless @user
log("Triggered hook for non-existing user \"#{post_received.identifier}\"")
return false
end
post_received.changes_refs do |oldrev, newrev, ref|
if Gitlab::Git.tag_ref?(ref)
GitTagPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
elsif Gitlab::Git.branch_ref?(ref)
......@@ -45,11 +47,6 @@ class PostReceive
refs << ref
end
unless @user
log("Triggered hook for non-existing user \"#{post_received.identifier}\"")
return false
end
after_project_changes_hooks(post_received, @user, refs.to_a, changes)
end
......
......@@ -11,8 +11,8 @@ module Gitlab
@changes = deserialize_changes(changes)
end
def identify(revision = nil)
super(identifier, project, revision)
def identify
super(identifier)
end
def changes_refs
......
# frozen_string_literal: true
# Detect user based on identifier like
# key-13 or user-36 or last commit
# key-13 or user-36
module Gitlab
module Identifier
def identify(identifier, project = nil, newrev = nil)
if identifier.blank?
identify_using_commit(project, newrev)
elsif identifier =~ /\Auser-\d+\Z/
def identify(identifier)
if identifier =~ /\Auser-\d+\Z/
# git push over http
identify_using_user(identifier)
elsif identifier =~ /\Akey-\d+\Z/
......@@ -16,19 +14,6 @@ module Gitlab
end
end
# Tries to identify a user based on a commit SHA.
def identify_using_commit(project, ref)
return if project.nil? && ref.nil?
commit = project.commit(ref)
return if !commit || !commit.author_email
identify_with_cache(:email, commit.author_email) do
commit.author
end
end
# Tries to identify a user based on a user identifier (e.g. "user-123").
# rubocop: disable CodeReuse/ActiveRecord
def identify_using_user(identifier)
......
......@@ -11,11 +11,8 @@ describe Gitlab::Identifier do
describe '#identify' do
context 'without an identifier' do
it 'identifies the user using a commit' do
expect(identifier).to receive(:identify_using_commit)
.with(project, '123')
identifier.identify('', project, '123')
it 'returns nil' do
expect(identifier.identify('')).to be nil
end
end
......@@ -24,7 +21,7 @@ describe Gitlab::Identifier do
expect(identifier).to receive(:identify_using_user)
.with("user-#{user.id}")
identifier.identify("user-#{user.id}", project, '123')
identifier.identify("user-#{user.id}")
end
end
......@@ -33,49 +30,11 @@ describe Gitlab::Identifier do
expect(identifier).to receive(:identify_using_ssh_key)
.with("key-#{key.id}")
identifier.identify("key-#{key.id}", project, '123')
identifier.identify("key-#{key.id}")
end
end
end
describe '#identify_using_commit' do
it "returns the User for an existing commit author's Email address" do
commit = double(:commit, author: user, author_email: user.email)
expect(project).to receive(:commit).with('123').and_return(commit)
expect(identifier.identify_using_commit(project, '123')).to eq(user)
end
it 'returns nil when no user could be found' do
allow(project).to receive(:commit).with('123').and_return(nil)
expect(identifier.identify_using_commit(project, '123')).to be_nil
end
it 'returns nil when the commit does not have an author Email' do
commit = double(:commit, author_email: nil)
expect(project).to receive(:commit).with('123').and_return(commit)
expect(identifier.identify_using_commit(project, '123')).to be_nil
end
it 'caches the found users per Email' do
commit = double(:commit, author: user, author_email: user.email)
expect(project).to receive(:commit).with('123').twice.and_return(commit)
2.times do
expect(identifier.identify_using_commit(project, '123')).to eq(user)
end
end
it 'returns nil if the project & ref are not present' do
expect(identifier.identify_using_commit(nil, nil)).to be_nil
end
end
describe '#identify_using_user' do
it 'returns the User for an existing ID in the identifier' do
found = identifier.identify_using_user("user-#{user.id}")
......
......@@ -41,6 +41,17 @@ describe PostReceive do
end
end
context 'unidentified user' do
let!(:key_id) { "" }
it 'returns false' do
expect(GitPushService).not_to receive(:new)
expect(GitTagPushService).not_to receive(:new)
expect(described_class.new.perform(gl_repository, key_id, base64_changes)).to be false
end
end
context 'with changes' do
before do
allow_any_instance_of(Gitlab::GitPostReceive).to receive(:identify).and_return(project.owner)
......
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