Commit b8f5e23f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'tag-branch-hooks' into 'master'

Execute hooks and services when branch or tag is created or deleted through web interface.

Fixes #2095.

Split up into commits to make it easier to see why what was changed :)

See merge request !1692
parents d37d7ffc b160db14
......@@ -64,6 +64,7 @@ v 7.9.0 (unreleased)
- Block user if he/she was blocked in Active Directory
- Fix import pages not working after first load.
- Use custom LDAP label in LDAP signin form.
- Execute hooks and services when branch or tag is created or deleted through web interface.
v 7.8.4
- Fix issue_tracker_id substitution in custom issue trackers
......
......@@ -24,14 +24,13 @@ class Projects::TagsController < Projects::ApplicationController
end
def destroy
tag = @repository.find_tag(params[:id])
if tag && @repository.rm_tag(tag.name)
EventCreateService.new.push_ref(@project, current_user, tag, 'rm', Gitlab::Git::TAG_REF_PREFIX)
end
DeleteTagService.new(project, current_user).execute(params[:id])
respond_to do |format|
format.html { redirect_to namespace_project_tags_path }
format.html do
redirect_to namespace_project_tags_path(@project.namespace,
@project)
end
format.js
end
end
......
......@@ -17,10 +17,15 @@ class CreateBranchService < BaseService
new_branch = repository.find_branch(branch_name)
if new_branch
EventCreateService.new.push_ref(project, current_user, new_branch, 'add')
return success(new_branch)
push_data = build_push_data(project, current_user, new_branch)
EventCreateService.new.push(project, current_user, push_data)
project.execute_hooks(push_data.dup, :push_hooks)
project.execute_services(push_data.dup, :push_hooks)
success(new_branch)
else
return error('Invalid reference name')
error('Invalid reference name')
end
end
......@@ -29,4 +34,9 @@ class CreateBranchService < BaseService
out[:branch] = branch
out
end
def build_push_data(project, user, branch)
Gitlab::PushDataBuilder.
build(project, user, Gitlab::Git::BLANK_SHA, branch.target, "#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}", [])
end
end
......@@ -21,9 +21,9 @@ class CreateTagService < BaseService
new_tag = repository.find_tag(tag_name)
if new_tag
EventCreateService.new.push_ref(project, current_user, new_tag, 'add', Gitlab::Git::TAG_REF_PREFIX)
push_data = create_push_data(project, current_user, new_tag)
EventCreateService.new.push(project, current_user, push_data)
project.execute_hooks(push_data.dup, :tag_push_hooks)
project.execute_services(push_data.dup, :tag_push_hooks)
......@@ -40,9 +40,7 @@ class CreateTagService < BaseService
end
def create_push_data(project, user, tag)
data = Gitlab::PushDataBuilder.
Gitlab::PushDataBuilder.
build(project, user, Gitlab::Git::BLANK_SHA, tag.target, "#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}", [])
data[:object_kind] = "tag_push"
data
end
end
......@@ -25,10 +25,15 @@ class DeleteBranchService < BaseService
end
if repository.rm_branch(branch_name)
EventCreateService.new.push_ref(project, current_user, branch, 'rm')
push_data = build_push_data(branch)
EventCreateService.new.push(project, current_user, push_data)
project.execute_hooks(push_data.dup, :push_hooks)
project.execute_services(push_data.dup, :push_hooks)
success('Branch was removed')
else
return error('Failed to remove branch')
error('Failed to remove branch')
end
end
......@@ -43,4 +48,9 @@ class DeleteBranchService < BaseService
out[:message] = message
out
end
def build_push_data(branch)
Gitlab::PushDataBuilder
.build(project, current_user, branch.target, Gitlab::Git::BLANK_SHA, "#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch.name}", [])
end
end
require_relative 'base_service'
class DeleteTagService < BaseService
def execute(tag_name)
repository = project.repository
tag = repository.find_tag(tag_name)
# No such tag
unless tag
return error('No such tag', 404)
end
if repository.rm_tag(tag_name)
push_data = build_push_data(tag)
EventCreateService.new.push(project, current_user, push_data)
project.execute_hooks(push_data.dup, :tag_push_hooks)
project.execute_services(push_data.dup, :tag_push_hooks)
success('Tag was removed')
else
error('Failed to remove tag')
end
end
def error(message, return_code = 400)
out = super(message)
out[:return_code] = return_code
out
end
def success(message)
out = super()
out[:message] = message
out
end
def build_push_data(tag)
Gitlab::PushDataBuilder
.build(project, current_user, tag.target, Gitlab::Git::BLANK_SHA, "#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}", [])
end
end
......@@ -62,26 +62,6 @@ class EventCreateService
create_event(project, current_user, Event::CREATED)
end
def push_ref(project, current_user, ref, action = 'add', prefix = Gitlab::Git::BRANCH_REF_PREFIX)
commit = project.repository.commit(ref.target)
if action.to_s == 'add'
before = Gitlab::Git::BLANK_SHA
after = commit.id
else
before = commit.id
after = Gitlab::Git::BLANK_SHA
end
data = {
ref: "#{prefix}#{ref.name}",
before: before,
after: after
}
push(project, current_user, data)
end
def push(project, current_user, push_data)
create_event(project, current_user, Event::PUSHED, data: push_data)
end
......
......@@ -53,7 +53,8 @@ class GitPushService
process_commit_messages(ref)
end
@push_data = post_receive_data(oldrev, newrev, ref)
@push_data = build_push_data(oldrev, newrev, ref)
EventCreateService.new.push(project, user, @push_data)
project.execute_hooks(@push_data.dup, :push_hooks)
project.execute_services(@push_data.dup, :push_hooks)
......@@ -101,7 +102,7 @@ class GitPushService
end
end
def post_receive_data(oldrev, newrev, ref)
def build_push_data(oldrev, newrev, ref)
Gitlab::PushDataBuilder.
build(project, user, oldrev, newrev, ref, push_commits)
end
......
......@@ -3,21 +3,21 @@ class GitTagPushService
def execute(project, user, oldrev, newrev, ref)
@project, @user = project, user
@push_data = create_push_data(oldrev, newrev, ref)
@push_data = build_push_data(oldrev, newrev, ref)
EventCreateService.new.push(project, user, @push_data)
project.repository.expire_cache
project.execute_hooks(@push_data.dup, :tag_push_hooks)
project.execute_services(@push_data.dup, :tag_push_hooks)
project.repository.expire_cache
true
end
private
def create_push_data(oldrev, newrev, ref)
data = Gitlab::PushDataBuilder.build(project, user, oldrev, newrev, ref, [])
data[:object_kind] = "tag_push"
data
def build_push_data(oldrev, newrev, ref)
Gitlab::PushDataBuilder.build(project, user, oldrev, newrev, ref, [])
end
end
......@@ -28,9 +28,10 @@ module Gitlab
# Get latest 20 commits ASC
commits_limited = commits.last(20)
type = Gitlab::Git.tag_ref?(ref) ? "tag_push" : "push"
# Hash to be passed as post_receive_data
data = {
object_kind: "push",
object_kind: type,
before: oldrev,
after: newrev,
ref: ref,
......
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