Commit 9de6041e authored by Sean McGivern's avatar Sean McGivern Committed by Alejandro Rodríguez

Merge branch '24813-project-members-with-developer-access-can-no-longer-create-tags' into 'master'

Create tag after running pre-hooks and pass updated SHA to post-hooks

Closes #24813

See merge request !7700
parent d700cd78
...@@ -196,18 +196,12 @@ class Repository ...@@ -196,18 +196,12 @@ class Repository
options = { message: message, tagger: user_to_committer(user) } if message options = { message: message, tagger: user_to_committer(user) } if message
rugged.tags.create(tag_name, target, options) GitHooksService.new.execute(user, path_to_repo, oldrev, target, ref) do |service|
tag = find_tag(tag_name) raw_tag = rugged.tags.create(tag_name, target, options)
service.newrev = raw_tag.target_id
GitHooksService.new.execute(user, path_to_repo, oldrev, tag.target, ref) do
# we already created a tag, because we need tag SHA to pass correct
# values to hooks
end end
tag find_tag(tag_name)
rescue GitHooksService::PreReceiveError
rugged.tags.delete(tag_name)
raise
end end
def rm_branch(user, branch_name) def rm_branch(user, branch_name)
......
class GitHooksService class GitHooksService
PreReceiveError = Class.new(StandardError) PreReceiveError = Class.new(StandardError)
attr_accessor :oldrev, :newrev, :ref
def execute(user, repo_path, oldrev, newrev, ref) def execute(user, repo_path, oldrev, newrev, ref)
@repo_path = repo_path @repo_path = repo_path
@user = Gitlab::GlId.gl_id(user) @user = Gitlab::GlId.gl_id(user)
...@@ -16,7 +18,7 @@ class GitHooksService ...@@ -16,7 +18,7 @@ class GitHooksService
end end
end end
yield yield self
run_hook('post-receive') run_hook('post-receive')
end end
...@@ -25,6 +27,6 @@ class GitHooksService ...@@ -25,6 +27,6 @@ class GitHooksService
def run_hook(name) def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repo_path) hook = Gitlab::Git::Hook.new(name, @repo_path)
hook.trigger(@user, @oldrev, @newrev, @ref) hook.trigger(@user, oldrev, newrev, ref)
end end
end end
---
title: Pass tag SHA to post-receive hook when tag is created via UI
merge_request: 7700
author:
...@@ -1303,32 +1303,36 @@ describe Repository, models: true do ...@@ -1303,32 +1303,36 @@ describe Repository, models: true do
repository.add_tag(user, '8.5', 'master', 'foo') repository.add_tag(user, '8.5', 'master', 'foo')
end end
it 'does not create a tag when a pre-hook fails' do it 'returns a Gitlab::Git::Tag object' do
allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([false, '']) tag = repository.add_tag(user, '8.5', 'master', 'foo')
expect do
repository.add_tag(user, '8.5', 'master', 'foo')
end.to raise_error(GitHooksService::PreReceiveError)
repository.expire_tags_cache expect(tag).to be_a(Gitlab::Git::Tag)
expect(repository.find_tag('8.5')).to be_nil
end end
it 'passes tag SHA to hooks' do it 'passes commit SHA to pre-receive and update hooks,\
spy = GitHooksService.new and tag SHA to post-receive hook' do
allow(GitHooksService).to receive(:new).and_return(spy) pre_receive_hook = Gitlab::Git::Hook.new('pre-receive', repository.path_to_repo)
allow(spy).to receive(:execute).and_call_original update_hook = Gitlab::Git::Hook.new('update', repository.path_to_repo)
post_receive_hook = Gitlab::Git::Hook.new('post-receive', repository.path_to_repo)
tag = repository.add_tag(user, '8.5', 'master', 'foo') allow(Gitlab::Git::Hook).to receive(:new).
and_return(pre_receive_hook, update_hook, post_receive_hook)
expect(spy).to have_received(:execute). allow(pre_receive_hook).to receive(:trigger).and_call_original
with(anything, anything, anything, tag.target, anything) allow(update_hook).to receive(:trigger).and_call_original
end allow(post_receive_hook).to receive(:trigger).and_call_original
it 'returns a Gitlab::Git::Tag object' do
tag = repository.add_tag(user, '8.5', 'master', 'foo') tag = repository.add_tag(user, '8.5', 'master', 'foo')
expect(tag).to be_a(Gitlab::Git::Tag) commit_sha = repository.commit('master').id
tag_sha = tag.target
expect(pre_receive_hook).to have_received(:trigger).
with(anything, anything, commit_sha, anything)
expect(update_hook).to have_received(:trigger).
with(anything, anything, commit_sha, anything)
expect(post_receive_hook).to have_received(:trigger).
with(anything, anything, tag_sha, anything)
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