Commit cfcf24dc authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Execute project hooks when issue or merge request created

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 25951b91
class IssueObserver < BaseObserver class IssueObserver < BaseObserver
def after_create(issue) def after_create(issue)
notification.new_issue(issue, current_user) notification.new_issue(issue, current_user)
issue.create_cross_references!(issue.project, current_user) issue.create_cross_references!(issue.project, current_user)
execute_hooks(issue)
end end
def after_close(issue, transition) def after_close(issue, transition)
notification.close_issue(issue, current_user) notification.close_issue(issue, current_user)
create_note(issue) create_note(issue)
execute_hooks(issue)
end end
def after_reopen(issue, transition) def after_reopen(issue, transition)
...@@ -29,4 +29,8 @@ class IssueObserver < BaseObserver ...@@ -29,4 +29,8 @@ class IssueObserver < BaseObserver
def create_note(issue) def create_note(issue)
Note.create_status_change_note(issue, issue.project, current_user, issue.state, current_commit) Note.create_status_change_note(issue, issue.project, current_user, issue.state, current_commit)
end end
def execute_hooks(issue)
issue.project.execute_hooks(issue.to_hook_data, :issue_hooks)
end
end end
...@@ -7,15 +7,15 @@ class MergeRequestObserver < ActivityObserver ...@@ -7,15 +7,15 @@ class MergeRequestObserver < ActivityObserver
end end
notification.new_merge_request(merge_request, current_user) notification.new_merge_request(merge_request, current_user)
merge_request.create_cross_references!(merge_request.project, current_user) merge_request.create_cross_references!(merge_request.project, current_user)
execute_hooks(merge_request)
end end
def after_close(merge_request, transition) def after_close(merge_request, transition)
create_event(merge_request, Event::CLOSED) create_event(merge_request, Event::CLOSED)
Note.create_status_change_note(merge_request, merge_request.target_project, current_user, merge_request.state, nil)
notification.close_mr(merge_request, current_user) notification.close_mr(merge_request, current_user)
create_note(merge_request)
execute_hooks(merge_request)
end end
def after_merge(merge_request, transition) def after_merge(merge_request, transition)
...@@ -31,11 +31,13 @@ class MergeRequestObserver < ActivityObserver ...@@ -31,11 +31,13 @@ class MergeRequestObserver < ActivityObserver
action: Event::MERGED, action: Event::MERGED,
author_id: merge_request.author_id_of_changes author_id: merge_request.author_id_of_changes
) )
execute_hooks(merge_request)
end end
def after_reopen(merge_request, transition) def after_reopen(merge_request, transition)
create_event(merge_request, Event::REOPENED) create_event(merge_request, Event::REOPENED)
Note.create_status_change_note(merge_request, merge_request.target_project, current_user, merge_request.state, nil) create_note(merge_request)
end end
def after_update(merge_request) def after_update(merge_request)
...@@ -53,4 +55,15 @@ class MergeRequestObserver < ActivityObserver ...@@ -53,4 +55,15 @@ class MergeRequestObserver < ActivityObserver
author_id: current_user.id author_id: current_user.id
) )
end end
private
# Create merge request note with service comment like 'Status changed to closed'
def create_note(merge_request)
Note.create_status_change_note(merge_request, merge_request.target_project, current_user, merge_request.state, nil)
end
def execute_hooks(merge_request)
merge_request.project.execute_hooks(merge_request.to_hook_data, :merge_request_hooks)
end
end end
...@@ -32,7 +32,7 @@ class GitPushService ...@@ -32,7 +32,7 @@ class GitPushService
end end
if push_to_branch?(ref) if push_to_branch?(ref)
project.execute_hooks(@push_data.dup) project.execute_hooks(@push_data.dup, :push_hooks)
project.execute_services(@push_data.dup) project.execute_services(@push_data.dup)
end end
......
...@@ -74,38 +74,19 @@ describe GitPushService do ...@@ -74,38 +74,19 @@ describe GitPushService do
end end
describe "Web Hooks" do describe "Web Hooks" do
context "with web hooks" do
before do
@project_hook = create(:project_hook)
@project_hook_2 = create(:project_hook)
project.hooks << [@project_hook, @project_hook_2]
stub_request(:post, @project_hook.url)
stub_request(:post, @project_hook_2.url)
end
it "executes multiple web hook" do
@project_hook.should_receive(:async_execute).once
@project_hook_2.should_receive(:async_execute).once
service.execute(project, user, @oldrev, @newrev, @ref)
end
end
context "execute web hooks" do context "execute web hooks" do
before do
@project_hook = create(:project_hook)
project.hooks << [@project_hook]
stub_request(:post, @project_hook.url)
end
it "when pushing a branch for the first time" do it "when pushing a branch for the first time" do
@project_hook.should_receive(:async_execute) project.should_receive(:execute_hooks)
service.execute(project, user, @blankrev, 'newrev', 'refs/heads/master') service.execute(project, user, @blankrev, 'newrev', 'refs/heads/master')
end end
it "when pushing new commits to existing branch" do
project.should_receive(:execute_hooks)
service.execute(project, user, 'oldrev', 'newrev', 'refs/heads/master')
end
it "when pushing tags" do it "when pushing tags" do
@project_hook.should_not_receive(:async_execute) project.should_not_receive(:execute_hooks)
service.execute(project, user, 'newrev', 'newrev', 'refs/tags/v1.0.0') service.execute(project, user, 'newrev', 'newrev', 'refs/tags/v1.0.0')
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