Commit a030dc41 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'remote_user' into 'master'

Add GL_USERNAME environment variable for hooks

See merge request gitlab-org/gitlab-ce!13264
parents 8b0417aa 01ce58bd
---
title: Add username as GL_USERNAME in hooks
merge_request:
author:
...@@ -31,6 +31,12 @@ module API ...@@ -31,6 +31,12 @@ module API
protocol = params[:protocol] protocol = params[:protocol]
actor.update_last_used_at if actor.is_a?(Key) actor.update_last_used_at if actor.is_a?(Key)
user =
if actor.is_a?(Key)
actor.user
else
actor
end
access_checker_klass = wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess access_checker_klass = wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess
access_checker = access_checker_klass access_checker = access_checker_klass
...@@ -47,6 +53,7 @@ module API ...@@ -47,6 +53,7 @@ module API
{ {
status: true, status: true,
gl_repository: gl_repository, gl_repository: gl_repository,
gl_username: user&.username,
repository_path: repository_path, repository_path: repository_path,
gitaly: gitaly_payload(params[:action]) gitaly: gitaly_payload(params[:action])
} }
......
...@@ -22,22 +22,22 @@ module Gitlab ...@@ -22,22 +22,22 @@ module Gitlab
File.exist?(path) File.exist?(path)
end end
def trigger(gl_id, oldrev, newrev, ref) def trigger(gl_id, gl_username, oldrev, newrev, ref)
return [true, nil] unless exists? return [true, nil] unless exists?
Bundler.with_clean_env do Bundler.with_clean_env do
case name case name
when "pre-receive", "post-receive" when "pre-receive", "post-receive"
call_receive_hook(gl_id, oldrev, newrev, ref) call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
when "update" when "update"
call_update_hook(gl_id, oldrev, newrev, ref) call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
end end
end end
end end
private private
def call_receive_hook(gl_id, oldrev, newrev, ref) def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref)
changes = [oldrev, newrev, ref].join(" ") changes = [oldrev, newrev, ref].join(" ")
exit_status = false exit_status = false
...@@ -45,6 +45,7 @@ module Gitlab ...@@ -45,6 +45,7 @@ module Gitlab
vars = { vars = {
'GL_ID' => gl_id, 'GL_ID' => gl_id,
'GL_USERNAME' => gl_username,
'PWD' => repo_path, 'PWD' => repo_path,
'GL_PROTOCOL' => GL_PROTOCOL, 'GL_PROTOCOL' => GL_PROTOCOL,
'GL_REPOSITORY' => repository.gl_repository 'GL_REPOSITORY' => repository.gl_repository
...@@ -80,9 +81,13 @@ module Gitlab ...@@ -80,9 +81,13 @@ module Gitlab
[exit_status, exit_message] [exit_status, exit_message]
end end
def call_update_hook(gl_id, oldrev, newrev, ref) def call_update_hook(gl_id, gl_username, oldrev, newrev, ref)
Dir.chdir(repo_path) do Dir.chdir(repo_path) do
stdout, stderr, status = Open3.capture3({ 'GL_ID' => gl_id }, path, ref, oldrev, newrev) env = {
'GL_ID' => gl_id,
'GL_USERNAME' => gl_username
}
stdout, stderr, status = Open3.capture3(env, path, ref, oldrev, newrev)
[status.success?, (stderr.presence || stdout).gsub(/\R/, "<br>").html_safe] [status.success?, (stderr.presence || stdout).gsub(/\R/, "<br>").html_safe]
end end
end end
......
...@@ -5,12 +5,13 @@ module Gitlab ...@@ -5,12 +5,13 @@ module Gitlab
attr_accessor :oldrev, :newrev, :ref attr_accessor :oldrev, :newrev, :ref
def execute(committer, repository, oldrev, newrev, ref) def execute(pusher, repository, oldrev, newrev, ref)
@repository = repository @repository = repository
@gl_id = committer.gl_id @gl_id = pusher.gl_id
@oldrev = oldrev @gl_username = pusher.name
@newrev = newrev @oldrev = oldrev
@ref = ref @newrev = newrev
@ref = ref
%w(pre-receive update).each do |hook_name| %w(pre-receive update).each do |hook_name|
status, message = run_hook(hook_name) status, message = run_hook(hook_name)
...@@ -29,7 +30,7 @@ module Gitlab ...@@ -29,7 +30,7 @@ module Gitlab
def run_hook(name) def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repository) hook = Gitlab::Git::Hook.new(name, @repository)
hook.trigger(@gl_id, oldrev, newrev, ref) hook.trigger(@gl_id, @gl_username, oldrev, newrev, ref)
end end
end end
end end
......
module Gitlab module Gitlab
module Git module Git
class User class User
attr_reader :name, :email, :gl_id attr_reader :username, :name, :email, :gl_id
def self.from_gitlab(gitlab_user) def self.from_gitlab(gitlab_user)
new(gitlab_user.name, gitlab_user.email, Gitlab::GlId.gl_id(gitlab_user)) new(gitlab_user.username, gitlab_user.name, gitlab_user.email, Gitlab::GlId.gl_id(gitlab_user))
end end
def self.from_gitaly(gitaly_user) def initialize(username, name, email, gl_id)
new(gitaly_user.name, gitaly_user.email, gitaly_user.gl_id) @username = username
end
def initialize(name, email, gl_id)
@name = name @name = name
@email = email @email = email
@gl_id = gl_id @gl_id = gl_id
end end
def ==(other) def ==(other)
[name, email, gl_id] == [other.name, other.email, other.gl_id] [username, name, email, gl_id] == [other.username, other.name, other.email, other.gl_id]
end end
end end
end end
......
...@@ -22,9 +22,9 @@ module Gitlab ...@@ -22,9 +22,9 @@ module Gitlab
params = { params = {
GL_ID: Gitlab::GlId.gl_id(user), GL_ID: Gitlab::GlId.gl_id(user),
GL_REPOSITORY: Gitlab::GlRepository.gl_repository(project, is_wiki), GL_REPOSITORY: Gitlab::GlRepository.gl_repository(project, is_wiki),
GL_USERNAME: user&.username,
RepoPath: repo_path RepoPath: repo_path
} }
server = { server = {
address: Gitlab::GitalyClient.address(project.repository_storage), address: Gitlab::GitalyClient.address(project.repository_storage),
token: Gitlab::GitalyClient.token(project.repository_storage) token: Gitlab::GitalyClient.token(project.repository_storage)
......
...@@ -14,6 +14,7 @@ describe Gitlab::Git::Hook do ...@@ -14,6 +14,7 @@ describe Gitlab::Git::Hook do
let(:repo_path) { repository.path } let(:repo_path) { repository.path }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:gl_id) { Gitlab::GlId.gl_id(user) } let(:gl_id) { Gitlab::GlId.gl_id(user) }
let(:gl_username) { user.username }
def create_hook(name) def create_hook(name)
FileUtils.mkdir_p(File.join(repo_path, 'hooks')) FileUtils.mkdir_p(File.join(repo_path, 'hooks'))
...@@ -42,6 +43,7 @@ describe Gitlab::Git::Hook do ...@@ -42,6 +43,7 @@ describe Gitlab::Git::Hook do
let(:env) do let(:env) do
{ {
'GL_ID' => gl_id, 'GL_ID' => gl_id,
'GL_USERNAME' => gl_username,
'PWD' => repo_path, 'PWD' => repo_path,
'GL_PROTOCOL' => 'web', 'GL_PROTOCOL' => 'web',
'GL_REPOSITORY' => gl_repository 'GL_REPOSITORY' => gl_repository
...@@ -59,7 +61,7 @@ describe Gitlab::Git::Hook do ...@@ -59,7 +61,7 @@ describe Gitlab::Git::Hook do
.with(env, hook_path, chdir: repo_path).and_call_original .with(env, hook_path, chdir: repo_path).and_call_original
end end
status, errors = hook.trigger(gl_id, blank, blank, ref) status, errors = hook.trigger(gl_id, gl_username, 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
...@@ -72,7 +74,7 @@ describe Gitlab::Git::Hook do ...@@ -72,7 +74,7 @@ describe Gitlab::Git::Hook do
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(gl_id, blank, blank, ref) status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
expect(status).to be false expect(status).to be false
expect(errors).to eq("error message from the hook<br>error message from the hook line 2<br>") expect(errors).to eq("error message from the hook<br>error message from the hook line 2<br>")
end end
...@@ -86,7 +88,7 @@ describe Gitlab::Git::Hook do ...@@ -86,7 +88,7 @@ describe Gitlab::Git::Hook do
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(gl_id, blank, blank, ref) status, errors = hook.trigger(gl_id, gl_username, 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
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Git::HooksService, seed_helper: true do describe Gitlab::Git::HooksService, seed_helper: true do
let(:user) { Gitlab::Git::User.new('Jane Doe', 'janedoe@example.com', 'user-456') } let(:user) { Gitlab::Git::User.new('janedoe', 'Jane Doe', 'janedoe@example.com', 'user-456') }
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, 'project-123') } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, 'project-123') }
let(:service) { described_class.new } let(:service) { described_class.new }
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Git::User do describe Gitlab::Git::User do
let(:username) { 'janedo' }
let(:name) { 'Jane Doe' } let(:name) { 'Jane Doe' }
let(:email) { 'janedoe@example.com' } let(:email) { 'janedoe@example.com' }
let(:gl_id) { 'user-123' } let(:gl_id) { 'user-123' }
subject { described_class.new(name, email, gl_id) } subject { described_class.new(username, name, email, gl_id) }
describe '#==' do describe '#==' do
def eq_other(name, email, gl_id) def eq_other(username, name, email, gl_id)
eq(described_class.new(name, email, gl_id)) eq(described_class.new(username, name, email, gl_id))
end end
it { expect(subject).to eq_other(name, email, gl_id) } it { expect(subject).to eq_other(username, name, email, gl_id) }
it { expect(subject).not_to eq_other(nil, nil, nil) } it { expect(subject).not_to eq_other(nil, nil, nil, nil) }
it { expect(subject).not_to eq_other(name + 'x', email, gl_id) } it { expect(subject).not_to eq_other(username + 'x', name, email, gl_id) }
it { expect(subject).not_to eq_other(name, email + 'x', gl_id) } it { expect(subject).not_to eq_other(username, name + 'x', email, gl_id) }
it { expect(subject).not_to eq_other(name, email, gl_id + 'x') } it { expect(subject).not_to eq_other(username, name, email + 'x', gl_id) }
it { expect(subject).not_to eq_other(username, name, email, gl_id + 'x') }
end end
end end
...@@ -182,7 +182,12 @@ describe Gitlab::Workhorse do ...@@ -182,7 +182,12 @@ describe Gitlab::Workhorse do
let(:repo_path) { repository.path_to_repo } let(:repo_path) { repository.path_to_repo }
let(:action) { 'info_refs' } let(:action) { 'info_refs' }
let(:params) do let(:params) do
{ GL_ID: "user-#{user.id}", GL_REPOSITORY: "project-#{project.id}", RepoPath: repo_path } {
GL_ID: "user-#{user.id}",
GL_USERNAME: user.username,
GL_REPOSITORY: "project-#{project.id}",
RepoPath: repo_path
}
end end
subject { described_class.git_http_ok(repository, false, user, action) } subject { described_class.git_http_ok(repository, false, user, action) }
...@@ -191,7 +196,12 @@ describe Gitlab::Workhorse do ...@@ -191,7 +196,12 @@ describe Gitlab::Workhorse do
context 'when is_wiki' do context 'when is_wiki' do
let(:params) do let(:params) do
{ GL_ID: "user-#{user.id}", GL_REPOSITORY: "wiki-#{project.id}", RepoPath: repo_path } {
GL_ID: "user-#{user.id}",
GL_USERNAME: user.username,
GL_REPOSITORY: "wiki-#{project.id}",
RepoPath: repo_path
}
end end
subject { described_class.git_http_ok(repository, true, user, action) } subject { described_class.git_http_ok(repository, true, user, action) }
......
...@@ -1681,11 +1681,11 @@ describe Repository do ...@@ -1681,11 +1681,11 @@ describe Repository do
tag_sha = tag.target tag_sha = tag.target
expect(pre_receive_hook).to have_received(:trigger) expect(pre_receive_hook).to have_received(:trigger)
.with(anything, anything, commit_sha, anything) .with(anything, anything, anything, commit_sha, anything)
expect(update_hook).to have_received(:trigger) expect(update_hook).to have_received(:trigger)
.with(anything, anything, commit_sha, anything) .with(anything, anything, anything, commit_sha, anything)
expect(post_receive_hook).to have_received(:trigger) expect(post_receive_hook).to have_received(:trigger)
.with(anything, anything, tag_sha, anything) .with(anything, anything, anything, tag_sha, anything)
end end
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