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