2012-09-05 07:01:20 +02:00
|
|
|
# Includes methods for handling Git Push events
|
|
|
|
#
|
|
|
|
# Triggered by PostReceive job
|
2012-08-21 20:26:56 +02:00
|
|
|
module PushObserver
|
2012-11-19 19:44:05 +01:00
|
|
|
# This method will be called after each post receive and only if the provided
|
|
|
|
# user is present in GitLab.
|
|
|
|
#
|
|
|
|
# All callbacks for post receive should be placed here.
|
|
|
|
def trigger_post_receive(oldrev, newrev, ref, user)
|
2012-06-07 14:44:57 +02:00
|
|
|
data = post_receive_data(oldrev, newrev, ref, user)
|
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
# Create push event
|
|
|
|
self.observe_push(data)
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
if push_to_branch? ref, oldrev
|
|
|
|
# Close merged MR
|
|
|
|
self.update_merge_requests(oldrev, newrev, ref, user)
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
# Execute web hooks
|
|
|
|
self.execute_hooks(data.dup)
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
# Execute project services
|
|
|
|
self.execute_services(data.dup)
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
# Create satellite
|
|
|
|
self.satellite.create unless self.satellite.exists?
|
|
|
|
|
|
|
|
# Discover the default branch, but only if it hasn't already been set to
|
|
|
|
# something else
|
|
|
|
if default_branch.nil?
|
|
|
|
update_attributes(default_branch: discover_default_branch)
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
def push_to_branch? ref, oldrev
|
2012-06-07 14:44:57 +02:00
|
|
|
ref_parts = ref.split('/')
|
|
|
|
|
|
|
|
# Return if this is not a push to a branch (e.g. new commits)
|
2012-11-19 19:44:05 +01:00
|
|
|
!(ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000")
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
def observe_push(data)
|
|
|
|
Event.create(
|
|
|
|
project: self,
|
|
|
|
action: Event::Pushed,
|
|
|
|
data: data,
|
|
|
|
author_id: data[:user_id]
|
|
|
|
)
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
def execute_hooks(data)
|
2012-07-15 16:36:06 +02:00
|
|
|
hooks.each { |hook| hook.execute(data) }
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
def execute_services(data)
|
|
|
|
services.each do |service|
|
|
|
|
|
2012-11-19 20:34:05 +01:00
|
|
|
# Call service hook only if it is active
|
2012-11-20 15:46:22 +01:00
|
|
|
service.execute(data) if service.active
|
2012-11-19 19:44:05 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Produce a hash of post-receive data
|
|
|
|
#
|
|
|
|
# data = {
|
|
|
|
# before: String,
|
|
|
|
# after: String,
|
|
|
|
# ref: String,
|
|
|
|
# user_id: String,
|
|
|
|
# user_name: String,
|
|
|
|
# repository: {
|
|
|
|
# name: String,
|
|
|
|
# url: String,
|
|
|
|
# description: String,
|
|
|
|
# homepage: String,
|
|
|
|
# },
|
|
|
|
# commits: Array,
|
|
|
|
# total_commits_count: Fixnum
|
|
|
|
# }
|
|
|
|
#
|
2012-06-07 14:44:57 +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
|
|
|
|
data = {
|
|
|
|
before: oldrev,
|
|
|
|
after: newrev,
|
|
|
|
ref: ref,
|
|
|
|
user_id: user.id,
|
|
|
|
user_name: user.name,
|
|
|
|
repository: {
|
|
|
|
name: name,
|
|
|
|
url: web_url,
|
|
|
|
description: description,
|
|
|
|
homepage: web_url,
|
|
|
|
},
|
|
|
|
commits: [],
|
|
|
|
total_commits_count: push_commits_count
|
|
|
|
}
|
|
|
|
|
2012-07-02 10:44:45 +02:00
|
|
|
# For perfomance purposes maximum 20 latest commits
|
2012-06-07 14:44:57 +02:00
|
|
|
# will be passed as post receive hook data.
|
|
|
|
#
|
|
|
|
push_commits_limited.each do |commit|
|
|
|
|
data[:commits] << {
|
|
|
|
id: commit.id,
|
|
|
|
message: commit.safe_message,
|
|
|
|
timestamp: commit.date.xmlschema,
|
2012-07-02 20:51:48 +02:00
|
|
|
url: "#{Gitlab.config.url}/#{code}/commits/#{commit.id}",
|
2012-06-07 14:44:57 +02:00
|
|
|
author: {
|
|
|
|
name: commit.author_name,
|
|
|
|
email: commit.author_email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
def update_merge_requests(oldrev, newrev, ref, user)
|
|
|
|
return true unless ref =~ /heads/
|
|
|
|
branch_name = ref.gsub("refs/heads/", "")
|
|
|
|
c_ids = self.commits_between(oldrev, newrev).map(&:id)
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
# Update code for merge requests
|
|
|
|
mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
|
|
|
|
mrs.each { |merge_request| merge_request.reload_code; merge_request.mark_as_unchecked }
|
2012-06-07 14:44:57 +02:00
|
|
|
|
2012-11-19 19:44:05 +01: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) }
|
2012-09-05 07:01:20 +02:00
|
|
|
|
2012-11-19 19:44:05 +01:00
|
|
|
true
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
end
|