debian-mirror-gitlab/lib/gitlab/data_builder/push.rb

95 lines
2.8 KiB
Ruby
Raw Normal View History

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