From a0d4235c04e8f47e8625a6f46d64b65df599b370 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 15 Jan 2015 10:26:33 -0800 Subject: Send checkout sha for web hooks and services --- lib/gitlab/git.rb | 4 ++ lib/gitlab/push_data_builder.rb | 118 ++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 52 deletions(-) diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index 67aca5e36e9..4a712c6345f 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -1,5 +1,9 @@ module Gitlab module Git BLANK_SHA = '0' * 40 + + def self.extract_ref_name(ref) + ref.gsub(/\Arefs\/(tags|heads)\//, '') + end end end diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb index 72c42a6a254..7f5d71376f1 100644 --- a/lib/gitlab/push_data_builder.rb +++ b/lib/gitlab/push_data_builder.rb @@ -1,63 +1,77 @@ 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 + class << self + # 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 build(project, user, oldrev, newrev, ref, commits = []) + # Total commits count + commits_count = commits.size - # Get latest 20 commits ASC - commits_limited = commits.last(20) + # 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 - } + # Hash to be passed as post_receive_data + data = { + before: oldrev, + after: newrev, + ref: ref, + checkout_sha: checkout_sha(project.repository, newrev, 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) + # 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 - data - end + # This method provide a sample data generated with + # existing project and commits to test web hooks + def 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 - # 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) + def checkout_sha(repository, newrev, ref) + if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') + tag_name = Gitlab::Git.extract_ref_name(ref) + tag = repository.find_tag(tag_name) + commit = repository.commit(tag.target) + commit.try(:sha) + else + newrev + end + end end end end -- cgit v1.2.1 From de27375d6cb2772b91459f5e706aed5b03b35a54 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 15 Jan 2015 11:17:47 -0800 Subject: Test git builder over annotated tag --- lib/gitlab/push_data_builder.rb | 7 +++++-- spec/lib/gitlab/push_data_builder_spec.rb | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb index 7f5d71376f1..faea6ae375c 100644 --- a/lib/gitlab/push_data_builder.rb +++ b/lib/gitlab/push_data_builder.rb @@ -66,8 +66,11 @@ module Gitlab if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') tag_name = Gitlab::Git.extract_ref_name(ref) tag = repository.find_tag(tag_name) - commit = repository.commit(tag.target) - commit.try(:sha) + + if tag + commit = repository.commit(tag.target) + commit.try(:sha) + end else newrev end diff --git a/spec/lib/gitlab/push_data_builder_spec.rb b/spec/lib/gitlab/push_data_builder_spec.rb index fbf767a167f..691fd133637 100644 --- a/spec/lib/gitlab/push_data_builder_spec.rb +++ b/spec/lib/gitlab/push_data_builder_spec.rb @@ -21,13 +21,14 @@ describe 'Gitlab::PushDataBuilder' do Gitlab::PushDataBuilder.build(project, user, Gitlab::Git::BLANK_SHA, - '5937ac0a7beb003549fc5fd26fc247adbce4a52e', + '8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b', 'refs/tags/v1.1.0') end it { data.should be_a(Hash) } it { data[:before].should == Gitlab::Git::BLANK_SHA } - it { data[:after].should == '5937ac0a7beb003549fc5fd26fc247adbce4a52e' } + it { data[:checkout_sha].should == '5937ac0a7beb003549fc5fd26fc247adbce4a52e' } + it { data[:after].should == '8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b' } it { data[:ref].should == 'refs/tags/v1.1.0' } it { data[:commits].should be_empty } it { data[:total_commits_count].should be_zero } -- cgit v1.2.1