2015-01-12 02:51:31 -05:00
|
|
|
module Gitlab
|
|
|
|
class PushDataBuilder
|
2015-01-15 13:26:33 -05:00
|
|
|
class << self
|
|
|
|
# Produce a hash of post-receive data
|
|
|
|
#
|
|
|
|
# data = {
|
|
|
|
# before: String,
|
|
|
|
# after: String,
|
|
|
|
# ref: String,
|
|
|
|
# user_id: String,
|
|
|
|
# user_name: String,
|
2015-03-04 13:24:34 -05:00
|
|
|
# user_email: String
|
2015-01-15 13:26:33 -05:00
|
|
|
# project_id: String,
|
|
|
|
# repository: {
|
|
|
|
# name: String,
|
|
|
|
# url: String,
|
|
|
|
# description: String,
|
|
|
|
# homepage: String,
|
|
|
|
# },
|
|
|
|
# commits: Array,
|
|
|
|
# total_commits_count: Fixnum
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
def build(project, user, oldrev, newrev, ref, commits = [])
|
|
|
|
# Total commits count
|
|
|
|
commits_count = commits.size
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# Get latest 20 commits ASC
|
|
|
|
commits_limited = commits.last(20)
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# Hash to be passed as post_receive_data
|
|
|
|
data = {
|
2015-02-19 00:02:57 -05:00
|
|
|
object_kind: "push",
|
2015-01-15 13:26:33 -05:00
|
|
|
before: oldrev,
|
|
|
|
after: newrev,
|
|
|
|
ref: ref,
|
|
|
|
checkout_sha: checkout_sha(project.repository, newrev, ref),
|
|
|
|
user_id: user.id,
|
|
|
|
user_name: user.name,
|
2015-03-04 13:24:34 -05:00
|
|
|
user_email: user.email,
|
2015-01-15 13:26:33 -05:00
|
|
|
project_id: project.id,
|
|
|
|
repository: {
|
|
|
|
name: project.name,
|
|
|
|
url: project.url_to_repo,
|
|
|
|
description: project.description,
|
|
|
|
homepage: project.web_url,
|
2015-02-20 13:27:37 -05:00
|
|
|
git_http_url: project.http_url_to_repo,
|
|
|
|
git_ssh_url: project.ssh_url_to_repo,
|
|
|
|
visibility_level: project.visibility_level
|
2015-01-15 13:26:33 -05:00
|
|
|
},
|
|
|
|
commits: [],
|
|
|
|
total_commits_count: commits_count
|
|
|
|
}
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# For performance purposes maximum 20 latest commits
|
|
|
|
# will be passed as post receive hook data.
|
|
|
|
commits_limited.each do |commit|
|
|
|
|
data[:commits] << commit.hook_attrs(project)
|
|
|
|
end
|
|
|
|
|
2014-09-03 16:28:04 -04:00
|
|
|
data[:commits] = "" if data[:commits].count == 0
|
2015-01-15 13:26:33 -05:00
|
|
|
data
|
2015-01-12 02:51:31 -05:00
|
|
|
end
|
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# This method provide a sample data generated with
|
|
|
|
# existing project and commits to test web hooks
|
|
|
|
def build_sample(project, user)
|
|
|
|
commits = project.repository.commits(project.default_branch, nil, 3)
|
2015-03-10 06:51:36 -04:00
|
|
|
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
|
|
|
|
build(project, user, commits.last.id, commits.first.id, ref, commits)
|
2015-01-15 13:26:33 -05:00
|
|
|
end
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
def checkout_sha(repository, newrev, ref)
|
2015-03-10 06:51:36 -04:00
|
|
|
if newrev != Gitlab::Git::BLANK_SHA && Gitlab::Git.tag_ref?(ref)
|
|
|
|
tag_name = Gitlab::Git.ref_name(ref)
|
2015-01-15 13:26:33 -05:00
|
|
|
tag = repository.find_tag(tag_name)
|
2015-01-15 14:17:47 -05:00
|
|
|
|
|
|
|
if tag
|
|
|
|
commit = repository.commit(tag.target)
|
|
|
|
commit.try(:sha)
|
|
|
|
end
|
2015-01-15 13:26:33 -05:00
|
|
|
else
|
|
|
|
newrev
|
|
|
|
end
|
|
|
|
end
|
2015-01-12 02:51:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|