Commit f8ee07d9 authored by Tiago Botelho's avatar Tiago Botelho

User not defined in PostReceive#process_project_changes

When Gitlab::GitPostReceive#changes_refs is empty
user would not get defined and nil would be passed
to PostReceive#after_project_changes_hooks which would
then throw an error.
parent f5d71ad8
...@@ -29,14 +29,11 @@ class PostReceive ...@@ -29,14 +29,11 @@ class PostReceive
def process_project_changes(post_received) def process_project_changes(post_received)
changes = [] changes = []
refs = Set.new refs = Set.new
@user = post_received.identify
post_received.changes_refs do |oldrev, newrev, ref| post_received.changes_refs do |oldrev, newrev, ref|
@user ||= post_received.identify(newrev) @user ||= post_received.identify(newrev)
break unless @user
unless @user
log("Triggered hook for non-existing user \"#{post_received.identifier}\"")
return false # rubocop:disable Cop/AvoidReturnFromBlocks
end
if Gitlab::Git.tag_ref?(ref) if Gitlab::Git.tag_ref?(ref)
GitTagPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute GitTagPushService.new(post_received.project, @user, oldrev: oldrev, newrev: newrev, ref: ref).execute
...@@ -48,6 +45,11 @@ class PostReceive ...@@ -48,6 +45,11 @@ class PostReceive
refs << ref refs << ref
end 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) after_project_changes_hooks(post_received, @user, refs.to_a, changes)
end end
......
---
title: If user was not found, service hooks won't run on post receive background job
merge_request: 22519
author:
type: fixed
...@@ -11,7 +11,7 @@ module Gitlab ...@@ -11,7 +11,7 @@ module Gitlab
@changes = deserialize_changes(changes) @changes = deserialize_changes(changes)
end end
def identify(revision) def identify(revision = nil)
super(identifier, project, revision) super(identifier, project, revision)
end end
......
...@@ -6,7 +6,7 @@ describe PostReceive do ...@@ -6,7 +6,7 @@ describe PostReceive do
let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) } let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) }
let(:gl_repository) { "project-#{project.id}" } let(:gl_repository) { "project-#{project.id}" }
let(:key) { create(:key, user: project.owner) } let(:key) { create(:key, user: project.owner) }
let(:key_id) { key.shell_id } let!(:key_id) { key.shell_id }
let(:project) do let(:project) do
create(:project, :repository, auto_cancel_pending_pipelines: 'disabled') create(:project, :repository, auto_cancel_pending_pipelines: 'disabled')
...@@ -31,6 +31,17 @@ describe PostReceive do ...@@ -31,6 +31,17 @@ describe PostReceive do
end end
describe "#process_project_changes" do describe "#process_project_changes" do
context 'empty changes' do
it "does not call any PushService but runs after project hooks" do
expect(GitPushService).not_to receive(:new)
expect(GitTagPushService).not_to receive(:new)
expect_next_instance_of(SystemHooksService) { |service| expect(service).to receive(:execute_hooks) }
described_class.new.perform(gl_repository, key_id, "")
end
end
context 'with changes' do
before do before do
allow_any_instance_of(Gitlab::GitPostReceive).to receive(:identify).and_return(project.owner) allow_any_instance_of(Gitlab::GitPostReceive).to receive(:identify).and_return(project.owner)
end end
...@@ -38,7 +49,7 @@ describe PostReceive do ...@@ -38,7 +49,7 @@ describe PostReceive do
context "branches" do context "branches" do
let(:changes) { "123456 789012 refs/heads/tést" } let(:changes) { "123456 789012 refs/heads/tést" }
it "calls GitTagPushService" do it "calls GitPushService" do
expect_any_instance_of(GitPushService).to receive(:execute).and_return(true) expect_any_instance_of(GitPushService).to receive(:execute).and_return(true)
expect_any_instance_of(GitTagPushService).not_to receive(:execute) expect_any_instance_of(GitTagPushService).not_to receive(:execute)
described_class.new.perform(gl_repository, key_id, base64_changes) described_class.new.perform(gl_repository, key_id, base64_changes)
...@@ -113,6 +124,7 @@ describe PostReceive do ...@@ -113,6 +124,7 @@ describe PostReceive do
end end
end end
end end
end
describe '#process_wiki_changes' do describe '#process_wiki_changes' do
let(:gl_repository) { "wiki-#{project.id}" } let(:gl_repository) { "wiki-#{project.id}" }
......
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