gitlab-org--gitlab-foss/lib/gitlab/data_builder/push.rb

99 lines
2.9 KiB
Ruby
Raw Normal View History

module Gitlab
module DataBuilder
module Push
extend self
# Produce a hash of post-receive data
#
# data = {
# before: String,
# after: String,
# ref: String,
# user_id: String,
# user_name: String,
# user_username: String,
2015-03-04 13:24:34 -05:00
# user_email: String
# 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 = [], message = nil, commits_count: nil)
commits = Array(commits)
# Total commits count
commits_count ||= commits.size
# Get latest 20 commits ASC
commits_limited = commits.last(20)
# For performance purposes maximum 20 latest commits
# will be passed as post receive hook data.
2015-12-07 07:11:15 -05:00
commit_attrs = commits_limited.map do |commit|
2015-12-07 08:13:06 -05:00
commit.hook_attrs(with_changed_files: true)
2015-12-07 07:11:15 -05:00
end
type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push'
# Hash to be passed as post_receive_data
{
object_kind: type,
event_name: type,
before: oldrev,
after: newrev,
ref: ref,
checkout_sha: checkout_sha(project.repository, newrev, ref),
message: message,
user_id: user.id,
user_name: user.name,
user_username: user.username,
2015-03-04 13:24:34 -05:00
user_email: user.email,
user_avatar: user.avatar_url,
project_id: project.id,
project: project.hook_attrs,
commits: commit_attrs,
total_commits_count: commits_count,
# DEPRECATED
repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
:git_http_url, :git_ssh_url, :visibility_level)
}
end
# This method provide a sample data generated with
2016-03-10 14:48:29 -05:00
# existing project and commits to test webhooks
def build_sample(project, user)
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
commits = project.repository.commits(project.default_branch.to_s, limit: 3) rescue []
build(project, user, commits.last&.id, commits.first&.id, ref, commits)
end
2017-07-20 11:12:06 -04:00
private
def checkout_sha(repository, newrev, ref)
# Checkout sha is nil when we remove branch or tag
return if Gitlab::Git.blank_ref?(newrev)
2015-03-19 05:34:04 -04:00
# Find sha for tag, except when it was deleted.
if Gitlab::Git.tag_ref?(ref)
tag_name = Gitlab::Git.ref_name(ref)
tag = repository.find_tag(tag_name)
2015-01-15 14:17:47 -05:00
if tag
commit = repository.commit(tag.dereferenced_target)
2015-01-15 14:17:47 -05:00
commit.try(:sha)
end
else
newrev
end
end
end
end
end