Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-01-13 00:06:35 +0300
committerDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-01-13 00:06:35 +0300
commit6774a701a6b7a8c2508b5e36bd292a63f3d9d7d6 (patch)
treea1bf4b61a2490145c8eb23724a2877ac3a267fce /lib
parent1c6a1253899219b84cc9cb8b090a3bfbbd19605e (diff)
parentbba8e59a044f34a02000b752a70198fb74236b1d (diff)
Merge branch 'refactor_push_data_builder' into 'master'
Refactor push data builder See merge request !1384
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/push_data_builder.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb
new file mode 100644
index 00000000000..72c42a6a254
--- /dev/null
+++ b/lib/gitlab/push_data_builder.rb
@@ -0,0 +1,63 @@
+module Gitlab
+ class PushDataBuilder
+ # Produce a hash of post-receive data
+ #
+ # data = {
+ # before: String,
+ # after: String,
+ # ref: String,
+ # user_id: String,
+ # user_name: String,
+ # project_id: String,
+ # repository: {
+ # name: String,
+ # url: String,
+ # description: String,
+ # homepage: String,
+ # },
+ # commits: Array,
+ # total_commits_count: Fixnum
+ # }
+ #
+ def self.build(project, user, oldrev, newrev, ref, commits = [])
+ # Total commits count
+ commits_count = commits.size
+
+ # Get latest 20 commits ASC
+ commits_limited = commits.last(20)
+
+ # Hash to be passed as post_receive_data
+ data = {
+ before: oldrev,
+ after: newrev,
+ ref: ref,
+ user_id: user.id,
+ user_name: user.name,
+ project_id: project.id,
+ repository: {
+ name: project.name,
+ url: project.url_to_repo,
+ description: project.description,
+ homepage: project.web_url,
+ },
+ commits: [],
+ total_commits_count: commits_count
+ }
+
+ # 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
+
+ data
+ end
+
+ # This method provide a sample data generated with
+ # existing project and commits to test web hooks
+ def self.build_sample(project, user)
+ commits = project.repository.commits(project.default_branch, nil, 3)
+ build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits)
+ end
+ end
+end