2012-03-28 09:44:51 +02:00
|
|
|
module Project::HooksTrait
|
|
|
|
as_trait do
|
2012-04-04 06:39:04 +02:00
|
|
|
def observe_push(oldrev, newrev, ref, user)
|
|
|
|
data = post_receive_data(oldrev, newrev, ref, user)
|
2012-03-28 09:44:51 +02:00
|
|
|
|
|
|
|
Event.create(
|
|
|
|
:project => self,
|
|
|
|
:action => Event::Pushed,
|
|
|
|
:data => data,
|
|
|
|
:author_id => data[:user_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-04-04 06:39:04 +02:00
|
|
|
def update_merge_requests(oldrev, newrev, ref, user)
|
2012-03-28 09:44:51 +02:00
|
|
|
return true unless ref =~ /heads/
|
|
|
|
branch_name = ref.gsub("refs/heads/", "")
|
|
|
|
c_ids = self.commits_between(oldrev, newrev).map(&:id)
|
|
|
|
|
|
|
|
# Update code for merge requests
|
|
|
|
mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
|
2012-03-30 07:15:04 +02:00
|
|
|
mrs.each { |merge_request| merge_request.reload_code; merge_request.mark_as_unchecked }
|
2012-03-28 09:44:51 +02:00
|
|
|
|
|
|
|
# Close merge requests
|
|
|
|
mrs = self.merge_requests.opened.where(:target_branch => branch_name).all
|
|
|
|
mrs = mrs.select(&:last_commit).select { |mr| c_ids.include?(mr.last_commit.id) }
|
|
|
|
mrs.each { |merge_request| merge_request.merge!(user.id) }
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-04-04 06:39:04 +02:00
|
|
|
def execute_web_hooks(oldrev, newrev, ref, user)
|
2012-03-28 09:44:51 +02:00
|
|
|
ref_parts = ref.split('/')
|
|
|
|
|
|
|
|
# Return if this is not a push to a branch (e.g. new commits)
|
|
|
|
return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
|
|
|
|
|
2012-04-04 06:39:04 +02:00
|
|
|
data = post_receive_data(oldrev, newrev, ref, user)
|
2012-03-28 09:44:51 +02:00
|
|
|
|
|
|
|
web_hooks.each { |web_hook| web_hook.execute(data) }
|
|
|
|
end
|
|
|
|
|
2012-04-04 06:39:04 +02:00
|
|
|
def post_receive_data(oldrev, newrev, ref, user)
|
|
|
|
|
|
|
|
push_commits = commits_between(oldrev, newrev)
|
|
|
|
|
|
|
|
# Total commits count
|
|
|
|
push_commits_count = push_commits.size
|
|
|
|
|
|
|
|
# Get latest 20 commits ASC
|
|
|
|
push_commits_limited = push_commits.last(20)
|
|
|
|
|
|
|
|
# Hash to be passed as post_receive_data
|
2012-03-28 09:44:51 +02:00
|
|
|
data = {
|
|
|
|
before: oldrev,
|
|
|
|
after: newrev,
|
|
|
|
ref: ref,
|
2012-04-04 06:39:04 +02:00
|
|
|
user_id: user.id,
|
|
|
|
user_name: user.name,
|
2012-03-28 09:44:51 +02:00
|
|
|
repository: {
|
|
|
|
name: name,
|
|
|
|
url: web_url,
|
|
|
|
description: description,
|
|
|
|
homepage: web_url,
|
|
|
|
},
|
2012-04-04 06:39:04 +02:00
|
|
|
commits: [],
|
|
|
|
total_commits_count: push_commits_count
|
2012-03-28 09:44:51 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 06:39:04 +02:00
|
|
|
# For perfomance purposes maximum 20 latest commits
|
|
|
|
# will be passed as post receive hook data.
|
|
|
|
#
|
|
|
|
push_commits_limited.each do |commit|
|
2012-03-28 09:44:51 +02:00
|
|
|
data[:commits] << {
|
|
|
|
id: commit.id,
|
|
|
|
message: commit.safe_message,
|
|
|
|
timestamp: commit.date.xmlschema,
|
|
|
|
url: "http://#{GIT_HOST['host']}/#{code}/commits/#{commit.id}",
|
|
|
|
author: {
|
|
|
|
name: commit.author_name,
|
|
|
|
email: commit.author_email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
2012-04-04 06:39:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
# This method will be called after each post receive
|
|
|
|
# and only if autor_key_id present in gitlab.
|
|
|
|
# All callbacks for post receive should be placed here
|
|
|
|
#
|
|
|
|
def trigger_post_receive(oldrev, newrev, ref, author_key_id)
|
|
|
|
user = Key.find_by_identifier(author_key_id).user
|
|
|
|
|
|
|
|
# Create push event
|
|
|
|
self.observe_push(oldrev, newrev, ref, user)
|
|
|
|
|
|
|
|
# Close merged MR
|
|
|
|
self.update_merge_requests(oldrev, newrev, ref, user)
|
|
|
|
|
|
|
|
# Execute web hooks
|
|
|
|
self.execute_web_hooks(oldrev, newrev, ref, user)
|
2012-04-22 14:19:09 +02:00
|
|
|
|
2012-04-22 15:14:01 +02:00
|
|
|
# Create satellite
|
|
|
|
self.satellite.create unless self.satellite.exists?
|
2012-04-04 06:39:04 +02:00
|
|
|
end
|
2012-03-28 09:44:51 +02:00
|
|
|
end
|
|
|
|
end
|