Commit f4e6aba1 authored by Alejandro Rodríguez's avatar Alejandro Rodríguez

Set the GL_REPOSITORY env variable on Gitlab::Git::Hook

parent 6205e457
...@@ -3,8 +3,8 @@ class GitHooksService ...@@ -3,8 +3,8 @@ class GitHooksService
attr_accessor :oldrev, :newrev, :ref attr_accessor :oldrev, :newrev, :ref
def execute(user, repo_path, oldrev, newrev, ref) def execute(user, project, oldrev, newrev, ref)
@repo_path = repo_path @project = project
@user = Gitlab::GlId.gl_id(user) @user = Gitlab::GlId.gl_id(user)
@oldrev = oldrev @oldrev = oldrev
@newrev = newrev @newrev = newrev
...@@ -26,7 +26,7 @@ class GitHooksService ...@@ -26,7 +26,7 @@ class GitHooksService
private private
def run_hook(name) def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repo_path) hook = Gitlab::Git::Hook.new(name, @project)
hook.trigger(@user, oldrev, newrev, ref) hook.trigger(@user, oldrev, newrev, ref)
end end
end end
...@@ -120,7 +120,7 @@ class GitOperationService ...@@ -120,7 +120,7 @@ class GitOperationService
def with_hooks(ref, newrev, oldrev) def with_hooks(ref, newrev, oldrev)
GitHooksService.new.execute( GitHooksService.new.execute(
user, user,
repository.path_to_repo, repository.project,
oldrev, oldrev,
newrev, newrev,
ref) do |service| ref) do |service|
......
...@@ -4,9 +4,10 @@ module Gitlab ...@@ -4,9 +4,10 @@ module Gitlab
GL_PROTOCOL = 'web'.freeze GL_PROTOCOL = 'web'.freeze
attr_reader :name, :repo_path, :path attr_reader :name, :repo_path, :path
def initialize(name, repo_path) def initialize(name, project)
@name = name @name = name
@repo_path = repo_path @project = project
@repo_path = project.repository.path
@path = File.join(repo_path.strip, 'hooks', name) @path = File.join(repo_path.strip, 'hooks', name)
end end
...@@ -38,7 +39,8 @@ module Gitlab ...@@ -38,7 +39,8 @@ module Gitlab
vars = { vars = {
'GL_ID' => gl_id, 'GL_ID' => gl_id,
'PWD' => repo_path, 'PWD' => repo_path,
'GL_PROTOCOL' => GL_PROTOCOL 'GL_PROTOCOL' => GL_PROTOCOL,
'GL_REPOSITORY' => Gitlab::GlRepository.gl_repository(@project, false)
} }
options = { options = {
......
...@@ -98,6 +98,6 @@ feature 'Import/Export - project import integration test', feature: true, js: tr ...@@ -98,6 +98,6 @@ feature 'Import/Export - project import integration test', feature: true, js: tr
end end
def project_hook_exists?(project) def project_hook_exists?(project)
Gitlab::Git::Hook.new('post-receive', project.repository.path).exists? Gitlab::Git::Hook.new('post-receive', project).exists?
end end
end end
...@@ -4,18 +4,20 @@ require 'fileutils' ...@@ -4,18 +4,20 @@ require 'fileutils'
describe Gitlab::Git::Hook, lib: true do describe Gitlab::Git::Hook, lib: true do
describe "#trigger" do describe "#trigger" do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:repo_path) { project.repository.path }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:gl_id) { Gitlab::GlId.gl_id(user) }
def create_hook(name) def create_hook(name)
FileUtils.mkdir_p(File.join(project.repository.path, 'hooks')) FileUtils.mkdir_p(File.join(repo_path, 'hooks'))
File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f| File.open(File.join(repo_path, 'hooks', name), 'w', 0755) do |f|
f.write('exit 0') f.write('exit 0')
end end
end end
def create_failing_hook(name) def create_failing_hook(name)
FileUtils.mkdir_p(File.join(project.repository.path, 'hooks')) FileUtils.mkdir_p(File.join(repo_path, 'hooks'))
File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f| File.open(File.join(repo_path, 'hooks', name), 'w', 0755) do |f|
f.write(<<-HOOK) f.write(<<-HOOK)
echo 'regular message from the hook' echo 'regular message from the hook'
echo 'error message from the hook' 1>&2 echo 'error message from the hook' 1>&2
...@@ -27,13 +29,29 @@ describe Gitlab::Git::Hook, lib: true do ...@@ -27,13 +29,29 @@ describe Gitlab::Git::Hook, lib: true do
['pre-receive', 'post-receive', 'update'].each do |hook_name| ['pre-receive', 'post-receive', 'update'].each do |hook_name|
context "when triggering a #{hook_name} hook" do context "when triggering a #{hook_name} hook" do
context "when the hook is successful" do context "when the hook is successful" do
let(:hook_path) { File.join(repo_path, 'hooks', hook_name) }
let(:gl_repository) { Gitlab::GlRepository.gl_repository(project, false) }
let(:env) do
{
'GL_ID' => gl_id,
'PWD' => repo_path,
'GL_PROTOCOL' => 'web',
'GL_REPOSITORY' => gl_repository
}
end
it "returns success with no errors" do it "returns success with no errors" do
create_hook(hook_name) create_hook(hook_name)
hook = Gitlab::Git::Hook.new(hook_name, project.repository.path) hook = Gitlab::Git::Hook.new(hook_name, project)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref) if hook_name != 'update'
expect(Open3).to receive(:popen3)
.with(env, hook_path, chdir: repo_path).and_call_original
end
status, errors = hook.trigger(gl_id, blank, blank, ref)
expect(status).to be true expect(status).to be true
expect(errors).to be_blank expect(errors).to be_blank
end end
...@@ -42,11 +60,11 @@ describe Gitlab::Git::Hook, lib: true do ...@@ -42,11 +60,11 @@ describe Gitlab::Git::Hook, lib: true do
context "when the hook is unsuccessful" do context "when the hook is unsuccessful" do
it "returns failure with errors" do it "returns failure with errors" do
create_failing_hook(hook_name) create_failing_hook(hook_name)
hook = Gitlab::Git::Hook.new(hook_name, project.repository.path) hook = Gitlab::Git::Hook.new(hook_name, project)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref) status, errors = hook.trigger(gl_id, blank, blank, ref)
expect(status).to be false expect(status).to be false
expect(errors).to eq("error message from the hook\n") expect(errors).to eq("error message from the hook\n")
end end
...@@ -56,11 +74,11 @@ describe Gitlab::Git::Hook, lib: true do ...@@ -56,11 +74,11 @@ describe Gitlab::Git::Hook, lib: true do
context "when the hook doesn't exist" do context "when the hook doesn't exist" do
it "returns success with no errors" do it "returns success with no errors" do
hook = Gitlab::Git::Hook.new('unknown_hook', project.repository.path) hook = Gitlab::Git::Hook.new('unknown_hook', project)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref) status, errors = hook.trigger(gl_id, blank, blank, ref)
expect(status).to be true expect(status).to be true
expect(errors).to be_nil expect(errors).to be_nil
end end
......
...@@ -34,7 +34,7 @@ describe Gitlab::ImportExport::RepoRestorer, services: true do ...@@ -34,7 +34,7 @@ describe Gitlab::ImportExport::RepoRestorer, services: true do
it 'has the webhooks' do it 'has the webhooks' do
restorer.restore restorer.restore
expect(Gitlab::Git::Hook.new('post-receive', project.repository.path_to_repo)).to exist expect(Gitlab::Git::Hook.new('post-receive', project)).to exist
end end
end end
end end
...@@ -780,7 +780,7 @@ describe Repository, models: true do ...@@ -780,7 +780,7 @@ describe Repository, models: true do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
it 'runs without errors' do it 'runs without errors' do
expect_any_instance_of(GitHooksService).to receive(:execute) expect_any_instance_of(GitHooksService).to receive(:execute)
.with(user, project.repository.path_to_repo, old_rev, blank_sha, 'refs/heads/feature') .with(user, project, old_rev, blank_sha, 'refs/heads/feature')
expect { repository.rm_branch(user, 'feature') }.not_to raise_error expect { repository.rm_branch(user, 'feature') }.not_to raise_error
end end
...@@ -823,12 +823,7 @@ describe Repository, models: true do ...@@ -823,12 +823,7 @@ describe Repository, models: true do
service = GitHooksService.new service = GitHooksService.new
expect(GitHooksService).to receive(:new).and_return(service) expect(GitHooksService).to receive(:new).and_return(service)
expect(service).to receive(:execute) expect(service).to receive(:execute)
.with( .with(user, project, old_rev, new_rev, 'refs/heads/feature')
user,
repository.path_to_repo,
old_rev,
new_rev,
'refs/heads/feature')
.and_yield(service).and_return(true) .and_yield(service).and_return(true)
end end
...@@ -1474,9 +1469,9 @@ describe Repository, models: true do ...@@ -1474,9 +1469,9 @@ describe Repository, models: true do
it 'passes commit SHA to pre-receive and update hooks,\ it 'passes commit SHA to pre-receive and update hooks,\
and tag SHA to post-receive hook' do and tag SHA to post-receive hook' do
pre_receive_hook = Gitlab::Git::Hook.new('pre-receive', repository.path_to_repo) pre_receive_hook = Gitlab::Git::Hook.new('pre-receive', project)
update_hook = Gitlab::Git::Hook.new('update', repository.path_to_repo) update_hook = Gitlab::Git::Hook.new('update', project)
post_receive_hook = Gitlab::Git::Hook.new('post-receive', repository.path_to_repo) post_receive_hook = Gitlab::Git::Hook.new('post-receive', project)
allow(Gitlab::Git::Hook).to receive(:new) allow(Gitlab::Git::Hook).to receive(:new)
.and_return(pre_receive_hook, update_hook, post_receive_hook) .and_return(pre_receive_hook, update_hook, post_receive_hook)
......
...@@ -12,7 +12,6 @@ describe GitHooksService, services: true do ...@@ -12,7 +12,6 @@ describe GitHooksService, services: true do
@oldrev = sample_commit.parent_id @oldrev = sample_commit.parent_id
@newrev = sample_commit.id @newrev = sample_commit.id
@ref = 'refs/heads/feature' @ref = 'refs/heads/feature'
@repo_path = project.repository.path_to_repo
end end
describe '#execute' do describe '#execute' do
...@@ -21,7 +20,7 @@ describe GitHooksService, services: true do ...@@ -21,7 +20,7 @@ describe GitHooksService, services: true do
hook = double(trigger: [true, nil]) hook = double(trigger: [true, nil])
expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook) expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook)
service.execute(user, @repo_path, @blankrev, @newrev, @ref) { } service.execute(user, project, @blankrev, @newrev, @ref) { }
end end
end end
...@@ -31,7 +30,7 @@ describe GitHooksService, services: true do ...@@ -31,7 +30,7 @@ describe GitHooksService, services: true do
expect(service).not_to receive(:run_hook).with('post-receive') expect(service).not_to receive(:run_hook).with('post-receive')
expect do expect do
service.execute(user, @repo_path, @blankrev, @newrev, @ref) service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(GitHooksService::PreReceiveError)
end end
end end
...@@ -43,7 +42,7 @@ describe GitHooksService, services: true do ...@@ -43,7 +42,7 @@ describe GitHooksService, services: true do
expect(service).not_to receive(:run_hook).with('post-receive') expect(service).not_to receive(:run_hook).with('post-receive')
expect do expect do
service.execute(user, @repo_path, @blankrev, @newrev, @ref) service.execute(user, project, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(GitHooksService::PreReceiveError)
end end
end end
......
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