2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-12 02:51:31 -05:00
|
|
|
module Gitlab
|
2016-08-04 11:44:27 -04:00
|
|
|
module DataBuilder
|
2016-08-12 04:09:29 -04:00
|
|
|
module Push
|
2016-08-12 03:33:28 -04:00
|
|
|
extend self
|
2016-08-04 11:44:27 -04:00
|
|
|
|
2017-08-27 17:43:42 -04:00
|
|
|
SAMPLE_DATA =
|
|
|
|
{
|
|
|
|
object_kind: "push",
|
|
|
|
event_name: "push",
|
|
|
|
before: "95790bf891e76fee5e1747ab589903a6a1f80f22",
|
|
|
|
after: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
|
|
|
ref: "refs/heads/master",
|
|
|
|
checkout_sha: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
|
|
|
message: "Hello World",
|
|
|
|
user_id: 4,
|
|
|
|
user_name: "John Smith",
|
|
|
|
user_email: "john@example.com",
|
|
|
|
user_avatar: "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
|
|
|
|
project_id: 15,
|
2019-10-22 05:06:14 -04:00
|
|
|
project: {
|
|
|
|
id: 15,
|
|
|
|
name: "gitlab",
|
|
|
|
description: "",
|
|
|
|
web_url: "http://test.example.com/gitlab/gitlab",
|
|
|
|
avatar_url: "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
|
|
|
|
git_ssh_url: "git@test.example.com:gitlab/gitlab.git",
|
|
|
|
git_http_url: "http://test.example.com/gitlab/gitlab.git",
|
|
|
|
namespace: "gitlab",
|
|
|
|
visibility_level: 0,
|
|
|
|
path_with_namespace: "gitlab/gitlab",
|
|
|
|
default_branch: "master"
|
|
|
|
},
|
2017-08-27 17:43:42 -04:00
|
|
|
commits: [
|
|
|
|
{
|
|
|
|
id: "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
|
2020-02-21 10:09:05 -05:00
|
|
|
message: "Add simple search to projects in public area\n\ncommit message body",
|
|
|
|
title: "Add simple search to projects in public area",
|
2017-08-27 17:43:42 -04:00
|
|
|
timestamp: "2013-05-13T18:18:08+00:00",
|
2020-02-05 16:09:02 -05:00
|
|
|
url: "https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
|
2017-08-27 17:43:42 -04:00
|
|
|
author: {
|
|
|
|
name: "Test User",
|
|
|
|
email: "test@example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2017-11-28 22:50:29 -05:00
|
|
|
total_commits_count: 1,
|
2019-04-05 00:19:30 -04:00
|
|
|
push_options: { ci: { skip: true } }
|
2017-08-27 17:43:42 -04:00
|
|
|
}.freeze
|
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# Produce a hash of post-receive data
|
|
|
|
#
|
|
|
|
# data = {
|
|
|
|
# before: String,
|
|
|
|
# after: String,
|
|
|
|
# ref: String,
|
|
|
|
# user_id: String,
|
|
|
|
# user_name: String,
|
2017-05-10 13:31:12 -04:00
|
|
|
# user_username: String,
|
2015-03-04 13:24:34 -05:00
|
|
|
# user_email: String
|
2019-10-22 05:06:14 -04:00
|
|
|
# project_id: Fixnum,
|
|
|
|
# project: {
|
|
|
|
# id: Fixnum,
|
|
|
|
# name: String,
|
|
|
|
# description: String,
|
|
|
|
# web_url: String,
|
|
|
|
# avatar_url: String,
|
|
|
|
# git_ssh_url: String,
|
|
|
|
# git_http_url: String,
|
|
|
|
# namespace: String,
|
|
|
|
# visibility_level: Fixnum,
|
|
|
|
# path_with_namespace: String,
|
|
|
|
# default_branch: String
|
|
|
|
# }
|
2015-01-15 13:26:33 -05:00
|
|
|
# repository: {
|
|
|
|
# name: String,
|
|
|
|
# url: String,
|
|
|
|
# description: String,
|
|
|
|
# homepage: String,
|
|
|
|
# },
|
|
|
|
# commits: Array,
|
2017-11-28 22:50:29 -05:00
|
|
|
# total_commits_count: Fixnum,
|
2019-04-05 00:19:30 -04:00
|
|
|
# push_options: Hash
|
2015-01-15 13:26:33 -05:00
|
|
|
# }
|
|
|
|
#
|
2017-11-28 22:50:29 -05:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
2018-12-30 22:31:35 -05:00
|
|
|
def build(
|
2020-09-02 08:10:35 -04:00
|
|
|
project:, user:, ref:, oldrev: nil, newrev: nil,
|
2019-08-12 18:31:58 -04:00
|
|
|
commits: [], commits_count: nil, message: nil, push_options: {},
|
|
|
|
with_changed_files: true)
|
2018-12-30 22:31:35 -05:00
|
|
|
|
2016-02-15 16:06:45 -05:00
|
|
|
commits = Array(commits)
|
|
|
|
|
2015-01-15 13:26:33 -05:00
|
|
|
# Total commits count
|
2017-07-27 08:58:02 -04:00
|
|
|
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.
|
2019-09-18 10:02:45 -04:00
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/38259
|
2017-09-22 14:54:35 -04:00
|
|
|
commit_attrs = Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
commits_limited.map do |commit|
|
2019-08-12 18:31:58 -04:00
|
|
|
commit.hook_attrs(with_changed_files: with_changed_files)
|
2017-09-22 14:54:35 -04:00
|
|
|
end
|
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
|
2017-04-24 11:23:51 -04:00
|
|
|
{
|
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,
|
2017-05-10 13:31:12 -04:00
|
|
|
user_username: user.username,
|
2019-01-14 21:11:04 -05:00
|
|
|
user_email: user.public_email,
|
2017-10-05 08:54:56 -04:00
|
|
|
user_avatar: user.avatar_url(only_path: false),
|
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,
|
2017-11-28 22:50:29 -05:00
|
|
|
push_options: push_options,
|
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
|
|
|
# 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
|
|
|
end
|
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
def build_bulk(action:, ref_type:, changes:)
|
|
|
|
{
|
|
|
|
action: action,
|
|
|
|
ref_count: changes.count,
|
|
|
|
ref_type: ref_type
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-30 06:00:18 -04:00
|
|
|
# This method provides 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)
|
2018-08-30 06:00:18 -04:00
|
|
|
# Use sample data if repo has no commit
|
|
|
|
# (expect the case of test service configuration settings)
|
|
|
|
return sample_data if project.empty_repo?
|
|
|
|
|
2015-03-10 06:51:36 -04:00
|
|
|
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
|
2018-08-30 06:00:18 -04:00
|
|
|
commits = project.repository.commits(project.default_branch.to_s, limit: 3)
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-12-30 22:31:35 -05:00
|
|
|
build(project: project,
|
|
|
|
user: user,
|
|
|
|
oldrev: commits.last&.id,
|
|
|
|
newrev: commits.first&.id,
|
|
|
|
ref: ref,
|
|
|
|
commits: commits)
|
2015-01-15 13:26:33 -05:00
|
|
|
end
|
2015-01-12 02:51:31 -05:00
|
|
|
|
2017-08-27 17:43:42 -04:00
|
|
|
def sample_data
|
|
|
|
SAMPLE_DATA
|
|
|
|
end
|
|
|
|
|
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
|
2016-09-26 23:07:31 -04:00
|
|
|
commit = repository.commit(tag.dereferenced_target)
|
2015-01-15 14:17:47 -05:00
|
|
|
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
|