Commit 23255bff authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'send_checkout_sha_to_services' into 'master'

Send checkout sha to services

In case of tags it may be useful to have sha of commit this tag points to.
So now web hooks and services get additional parameter `checkout_sha`

See merge request !1400
parents 089df35f de27375d
module Gitlab module Gitlab
module Git module Git
BLANK_SHA = '0' * 40 BLANK_SHA = '0' * 40
def self.extract_ref_name(ref)
ref.gsub(/\Arefs\/(tags|heads)\//, '')
end
end end
end end
module Gitlab module Gitlab
class PushDataBuilder class PushDataBuilder
class << self
# Produce a hash of post-receive data # Produce a hash of post-receive data
# #
# data = { # data = {
...@@ -19,7 +20,7 @@ module Gitlab ...@@ -19,7 +20,7 @@ module Gitlab
# total_commits_count: Fixnum # total_commits_count: Fixnum
# } # }
# #
def self.build(project, user, oldrev, newrev, ref, commits = []) def build(project, user, oldrev, newrev, ref, commits = [])
# Total commits count # Total commits count
commits_count = commits.size commits_count = commits.size
...@@ -31,6 +32,7 @@ module Gitlab ...@@ -31,6 +32,7 @@ module Gitlab
before: oldrev, before: oldrev,
after: newrev, after: newrev,
ref: ref, ref: ref,
checkout_sha: checkout_sha(project.repository, newrev, ref),
user_id: user.id, user_id: user.id,
user_name: user.name, user_name: user.name,
project_id: project.id, project_id: project.id,
...@@ -55,9 +57,24 @@ module Gitlab ...@@ -55,9 +57,24 @@ module Gitlab
# This method provide a sample data generated with # This method provide a sample data generated with
# existing project and commits to test web hooks # existing project and commits to test web hooks
def self.build_sample(project, user) def build_sample(project, user)
commits = project.repository.commits(project.default_branch, nil, 3) commits = project.repository.commits(project.default_branch, nil, 3)
build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits) build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits)
end end
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)
if tag
commit = repository.commit(tag.target)
commit.try(:sha)
end
else
newrev
end
end
end
end end
end end
...@@ -21,13 +21,14 @@ describe 'Gitlab::PushDataBuilder' do ...@@ -21,13 +21,14 @@ describe 'Gitlab::PushDataBuilder' do
Gitlab::PushDataBuilder.build(project, Gitlab::PushDataBuilder.build(project,
user, user,
Gitlab::Git::BLANK_SHA, Gitlab::Git::BLANK_SHA,
'5937ac0a7beb003549fc5fd26fc247adbce4a52e', '8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b',
'refs/tags/v1.1.0') 'refs/tags/v1.1.0')
end end
it { data.should be_a(Hash) } it { data.should be_a(Hash) }
it { data[:before].should == Gitlab::Git::BLANK_SHA } 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[:ref].should == 'refs/tags/v1.1.0' }
it { data[:commits].should be_empty } it { data[:commits].should be_empty }
it { data[:total_commits_count].should be_zero } it { data[:total_commits_count].should be_zero }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment