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,
|
2015-12-04 09:23:21 -05:00
|
|
|
# total_commits_count: Fixnum
|
2015-01-15 13:26:33 -05:00
|
|
|
# }
|
|
|
|
#
|
2014-12-05 16:56:43 -05:00
|
|
|
def build(project, user, oldrev, newrev, ref, commits = [], message = nil)
|
2016-02-15 16:06:45 -05:00
|
|
|
commits = Array(commits)
|
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# 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-06-15 11:54:22 -04:00
|
|
|
|
2015-03-17 10:51:14 -04:00
|
|
|
# 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
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2016-04-15 07:08:22 -04:00
|
|
|
type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push'
|
2015-11-02 09:56:44 -05:00
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# Hash to be passed as post_receive_data
|
|
|
|
data = {
|
2015-03-13 09:51:48 -04:00
|
|
|
object_kind: type,
|
2016-04-18 20:52:43 -04:00
|
|
|
event_name: type,
|
2015-01-15 13:26:33 -05:00
|
|
|
before: oldrev,
|
|
|
|
after: newrev,
|
|
|
|
ref: ref,
|
|
|
|
checkout_sha: checkout_sha(project.repository, newrev, ref),
|
2014-12-05 16:56:43 -05:00
|
|
|
message: message,
|
2015-01-15 13:26:33 -05:00
|
|
|
user_id: user.id,
|
|
|
|
user_name: user.name,
|
2015-03-04 13:24:34 -05:00
|
|
|
user_email: user.email,
|
Add new data to project in push, issue, merge-request and note webhooks data
- Add `avatar_url`, `description`, `git_ssh_url`, `git_http_url`,
`path_with_namespace` and `default_branch` in `project` in push, issue,
merge-request and note webhooks data
- Deprecate the `ssh_url` in favor of `git_ssh_url` and `http_url` in
favor of `git_http_url` in `project` for push, issue, merge-request and
note webhooks data
- Deprecate the `repository` key in push, issue, merge-request and
note webhooks data, use `project` instead
2016-02-06 09:20:21 -05:00
|
|
|
user_avatar: user.avatar_url,
|
2015-01-15 13:26:33 -05:00
|
|
|
project_id: project.id,
|
Add new data to project in push, issue, merge-request and note webhooks data
- Add `avatar_url`, `description`, `git_ssh_url`, `git_http_url`,
`path_with_namespace` and `default_branch` in `project` in push, issue,
merge-request and note webhooks data
- Deprecate the `ssh_url` in favor of `git_ssh_url` and `http_url` in
favor of `git_http_url` in `project` for push, issue, merge-request and
note webhooks data
- Deprecate the `repository` key in push, issue, merge-request and
note webhooks data, use `project` instead
2016-02-06 09:20:21 -05:00
|
|
|
project: project.hook_attrs,
|
2015-03-17 10:51:14 -04:00
|
|
|
commits: commit_attrs,
|
Add new data to project in push, issue, merge-request and note webhooks data
- Add `avatar_url`, `description`, `git_ssh_url`, `git_http_url`,
`path_with_namespace` and `default_branch` in `project` in push, issue,
merge-request and note webhooks data
- Deprecate the `ssh_url` in favor of `git_ssh_url` and `http_url` in
favor of `git_http_url` in `project` for push, issue, merge-request and
note webhooks data
- Deprecate the `repository` key in push, issue, merge-request and
note webhooks data, use `project` instead
2016-02-06 09:20:21 -05:00
|
|
|
total_commits_count: commits_count,
|
|
|
|
# DEPRECATED
|
|
|
|
repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
|
|
|
|
:git_http_url, :git_ssh_url, :visibility_level)
|
2015-01-15 13:26:33 -05:00
|
|
|
}
|
2015-01-12 02:51:31 -05:00
|
|
|
|
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
|
2016-03-10 14:48:29 -05:00
|
|
|
# existing project and commits to test webhooks
|
2015-01-15 13:26:33 -05:00
|
|
|
def build_sample(project, user)
|
2016-04-22 08:07:25 -04:00
|
|
|
commits = project.repository.commits(project.default_branch, limit: 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-06-15 11:54:22 -04:00
|
|
|
# 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.
|
2015-06-15 11:54:22 -04:00
|
|
|
if Gitlab::Git.tag_ref?(ref)
|
2015-03-10 06:51:36 -04:00
|
|
|
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
|