Use Gitlab::Git helper methods and constants as much as possible.
This commit is contained in:
parent
e0caed91e2
commit
383c56efa1
|
|
@ -27,7 +27,7 @@ class Projects::TagsController < Projects::ApplicationController
|
|||
tag = @repository.find_tag(params[:id])
|
||||
|
||||
if tag && @repository.rm_tag(tag.name)
|
||||
EventCreateService.new.push_ref(@project, current_user, tag, 'rm', 'refs/tags')
|
||||
EventCreateService.new.push_ref(@project, current_user, tag, 'rm', Gitlab::Git::TAG_REF_PREFIX)
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ module Emails
|
|||
@compare = compare
|
||||
@commits = Commit.decorate(compare.commits)
|
||||
@diffs = compare.diffs
|
||||
@branch = branch.gsub("refs/heads/", "")
|
||||
@branch = Gitlab::Git.ref_name(branch)
|
||||
@disable_diffs = disable_diffs
|
||||
|
||||
@subject = "[#{@project.path_with_namespace}][#{@branch}] "
|
||||
|
|
|
|||
|
|
@ -190,19 +190,19 @@ class Event < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def tag?
|
||||
data[:ref]["refs/tags"]
|
||||
Gitlab::Git.tag_ref?(data[:ref])
|
||||
end
|
||||
|
||||
def branch?
|
||||
data[:ref]["refs/heads"]
|
||||
Gitlab::Git.branch_ref?(data[:ref])
|
||||
end
|
||||
|
||||
def new_ref?
|
||||
commit_from =~ /^00000/
|
||||
Gitlab::Git.blank_ref?(commit_from)
|
||||
end
|
||||
|
||||
def rm_ref?
|
||||
commit_to =~ /^00000/
|
||||
Gitlab::Git.blank_ref?(commit_to)
|
||||
end
|
||||
|
||||
def md_ref?
|
||||
|
|
@ -226,11 +226,11 @@ class Event < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def branch_name
|
||||
@branch_name ||= data[:ref].gsub("refs/heads/", "")
|
||||
@branch_name ||= Gitlab::Git.ref_name(data[:ref])
|
||||
end
|
||||
|
||||
def tag_name
|
||||
@tag_name ||= data[:ref].gsub("refs/tags/", "")
|
||||
@tag_name ||= Gitlab::Git.ref_name(data[:ref])
|
||||
end
|
||||
|
||||
# Max 20 commits from push DESC
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ automatically inspected. Leave blank to include all branches.'
|
|||
end
|
||||
|
||||
user = data[:user_name]
|
||||
branch = data[:ref].gsub('refs/heads/', '')
|
||||
branch = Gitlab::Git.ref_name(data[:ref])
|
||||
|
||||
branch_restriction = restrict_to_branch.to_s
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class CampfireService < Service
|
|||
end
|
||||
|
||||
def build_message(push)
|
||||
ref = push[:ref].gsub("refs/heads/", "")
|
||||
ref = Gitlab::Git.ref_name(push[:ref])
|
||||
before = push[:before]
|
||||
after = push[:after]
|
||||
|
||||
|
|
@ -72,9 +72,9 @@ class CampfireService < Service
|
|||
message << "[#{project.name_with_namespace}] "
|
||||
message << "#{push[:user_name]} "
|
||||
|
||||
if before.include?('000000')
|
||||
if Gitlab::Git.blank_ref?(before)
|
||||
message << "pushed new branch #{ref} \n"
|
||||
elsif after.include?('000000')
|
||||
elsif Gitlab::Git.blank_ref?(after)
|
||||
message << "removed branch #{ref} \n"
|
||||
else
|
||||
message << "pushed #{push[:total_commits_count]} commits to #{ref}. "
|
||||
|
|
|
|||
|
|
@ -79,24 +79,19 @@ class HipchatService < Service
|
|||
end
|
||||
|
||||
def create_push_message(push)
|
||||
if push[:ref].starts_with?('refs/tags/')
|
||||
ref_type = 'tag'
|
||||
ref = push[:ref].gsub('refs/tags/', '')
|
||||
else
|
||||
ref_type = 'branch'
|
||||
ref = push[:ref].gsub('refs/heads/', '')
|
||||
end
|
||||
ref_type = Gitlab::Git.tag_ref?(push[:ref]) ? 'tag' : 'branch'
|
||||
ref = Gitlab::Git.ref_name(push[:ref])
|
||||
|
||||
before = push[:before]
|
||||
after = push[:after]
|
||||
|
||||
message = ""
|
||||
message << "#{push[:user_name]} "
|
||||
if before.include?('000000')
|
||||
if Gitlab::Git.blank_ref?(before)
|
||||
message << "pushed new #{ref_type} <a href=\""\
|
||||
"#{project_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\
|
||||
" to #{project_link}\n"
|
||||
elsif after.include?('000000')
|
||||
elsif Gitlab::Git.blank_ref?(after)
|
||||
message << "removed #{ref_type} <b>#{ref}</b> from <a href=\"#{project.web_url}\">#{project_name}</a> \n"
|
||||
else
|
||||
message << "pushed to #{ref_type} <a href=\""\
|
||||
|
|
|
|||
|
|
@ -88,13 +88,13 @@ class PushoverService < Service
|
|||
def execute(data)
|
||||
return unless supported_events.include?(data[:object_kind])
|
||||
|
||||
ref = data[:ref].gsub('refs/heads/', '')
|
||||
ref = Gitlab::Git.ref_name(data[:ref])
|
||||
before = data[:before]
|
||||
after = data[:after]
|
||||
|
||||
if before.include?('000000')
|
||||
if Gitlab::Git.blank_ref?(before)
|
||||
message = "#{data[:user_name]} pushed new branch \"#{ref}\"."
|
||||
elsif after.include?('000000')
|
||||
elsif Gitlab::Git.blank_ref?(after)
|
||||
message = "#{data[:user_name]} deleted branch \"#{ref}\"."
|
||||
else
|
||||
message = "#{data[:user_name]} push to branch \"#{ref}\"."
|
||||
|
|
|
|||
|
|
@ -15,13 +15,8 @@ class SlackService
|
|||
@commits = params.fetch(:commits, [])
|
||||
@project_name = params[:project_name]
|
||||
@project_url = params[:project_url]
|
||||
if params[:ref].starts_with?('refs/tags/')
|
||||
@ref_type = 'tag'
|
||||
@ref = params[:ref].gsub('refs/tags/', '')
|
||||
else
|
||||
@ref_type = 'branch'
|
||||
@ref = params[:ref].gsub('refs/heads/', '')
|
||||
end
|
||||
@ref_type = Gitlab::Git.tag_ref?(params[:ref]) ? 'tag' : 'branch'
|
||||
@ref = Gitlab::Git.ref_name(params[:ref])
|
||||
@user_name = params[:user_name]
|
||||
end
|
||||
|
||||
|
|
@ -81,11 +76,11 @@ class SlackService
|
|||
end
|
||||
|
||||
def new_branch?
|
||||
before.include?('000000')
|
||||
Gitlab::Git.blank_ref?(before)
|
||||
end
|
||||
|
||||
def removed_branch?
|
||||
after.include?('000000')
|
||||
Gitlab::Git.blank_ref?(after)
|
||||
end
|
||||
|
||||
def branch_url
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ class TeamcityService < CiService
|
|||
password: password,
|
||||
}
|
||||
|
||||
branch = data[:ref].gsub('refs/heads/', '')
|
||||
branch = Gitlab::Git.ref_name(data[:ref])
|
||||
|
||||
self.class.post("#{teamcity_url}/httpAuth/app/rest/buildQueue",
|
||||
body: "<build branchName=\"#{branch}\">"\
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class CreateTagService < BaseService
|
|||
new_tag = repository.find_tag(tag_name)
|
||||
|
||||
if new_tag
|
||||
EventCreateService.new.push_ref(project, current_user, new_tag, 'add', 'refs/tags')
|
||||
EventCreateService.new.push_ref(project, current_user, new_tag, 'add', Gitlab::Git::TAG_REF_PREFIX)
|
||||
|
||||
push_data = create_push_data(project, current_user, new_tag)
|
||||
project.execute_hooks(push_data.dup, :tag_push_hooks)
|
||||
|
|
@ -41,7 +41,7 @@ class CreateTagService < BaseService
|
|||
|
||||
def create_push_data(project, user, tag)
|
||||
data = Gitlab::PushDataBuilder.
|
||||
build(project, user, Gitlab::Git::BLANK_SHA, tag.target, 'refs/tags/' + tag.name, [])
|
||||
build(project, user, Gitlab::Git::BLANK_SHA, tag.target, "#{Gitlab::Git::TAG_REF_PREFIX}#{tag.name}", [])
|
||||
data[:object_kind] = "tag_push"
|
||||
data
|
||||
end
|
||||
|
|
|
|||
|
|
@ -62,19 +62,19 @@ class EventCreateService
|
|||
create_event(project, current_user, Event::CREATED)
|
||||
end
|
||||
|
||||
def push_ref(project, current_user, ref, action = 'add', prefix = 'refs/heads')
|
||||
def push_ref(project, current_user, ref, action = 'add', prefix = Gitlab::Git::BRANCH_REF_PREFIX)
|
||||
commit = project.repository.commit(ref.target)
|
||||
|
||||
if action.to_s == 'add'
|
||||
before = '00000000'
|
||||
before = Gitlab::Git::BLANK_SHA
|
||||
after = commit.id
|
||||
else
|
||||
before = commit.id
|
||||
after = '00000000'
|
||||
after = Gitlab::Git::BLANK_SHA
|
||||
end
|
||||
|
||||
data = {
|
||||
ref: "#{prefix}/#{ref.name}",
|
||||
ref: "#{prefix}#{ref.name}",
|
||||
before: before,
|
||||
after: after
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,30 +107,24 @@ class GitPushService
|
|||
end
|
||||
|
||||
def push_to_existing_branch?(ref, oldrev)
|
||||
ref_parts = ref.split('/')
|
||||
|
||||
# Return if this is not a push to a branch (e.g. new commits)
|
||||
ref_parts[1].include?('heads') && oldrev != Gitlab::Git::BLANK_SHA
|
||||
Gitlab::Git.branch_ref?(ref) && oldrev != Gitlab::Git::BLANK_SHA
|
||||
end
|
||||
|
||||
def push_to_new_branch?(ref, oldrev)
|
||||
ref_parts = ref.split('/')
|
||||
|
||||
ref_parts[1].include?('heads') && oldrev == Gitlab::Git::BLANK_SHA
|
||||
Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(oldrev)
|
||||
end
|
||||
|
||||
def push_remove_branch?(ref, newrev)
|
||||
ref_parts = ref.split('/')
|
||||
|
||||
ref_parts[1].include?('heads') && newrev == Gitlab::Git::BLANK_SHA
|
||||
Gitlab::Git.branch_ref?(ref) && Gitlab::Git.blank_ref?(newrev)
|
||||
end
|
||||
|
||||
def push_to_branch?(ref)
|
||||
ref.include?('refs/heads')
|
||||
Gitlab::Git.branch_ref?(ref)
|
||||
end
|
||||
|
||||
def is_default_branch?(ref)
|
||||
ref == "refs/heads/#{project.default_branch}"
|
||||
Gitlab::Git.branch_ref?(ref) && Gitlab::Git.ref_name(ref) == project.default_branch
|
||||
end
|
||||
|
||||
def commit_user(commit)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
module MergeRequests
|
||||
class RefreshService < MergeRequests::BaseService
|
||||
def execute(oldrev, newrev, ref)
|
||||
return true unless ref =~ /heads/
|
||||
return true unless Gitlab::Git.branch_ref?(ref)
|
||||
|
||||
@oldrev, @newrev = oldrev, newrev
|
||||
@branch_name = ref.gsub("refs/heads/", "")
|
||||
@branch_name = Gitlab::Git.ref_name(ref)
|
||||
@fork_merge_requests = @project.fork_merge_requests.opened
|
||||
@commits = @project.repository.commits_between(oldrev, newrev)
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ class EmailsOnPushWorker
|
|||
branch = push_data["ref"]
|
||||
author_id = push_data["user_id"]
|
||||
|
||||
if before_sha =~ /^000000/ || after_sha =~ /^000000/
|
||||
if Gitlab::Git.blank_ref?(before_sha) || Gitlab::Git.blank_ref?(after_sha)
|
||||
# skip if new branch was pushed or branch was removed
|
||||
return true
|
||||
end
|
||||
|
|
|
|||
|
|
@ -57,9 +57,9 @@ class IrkerWorker
|
|||
end
|
||||
|
||||
def send_branch_updates(push_data, project, repo_name, committer, branch)
|
||||
if push_data['before'] =~ /^000000/
|
||||
if push_data['before'] == Gitlab::Git::BLANK_SHA
|
||||
send_new_branch project, repo_name, committer, branch
|
||||
elsif push_data['after'] =~ /^000000/
|
||||
elsif push_data['after'] == Gitlab::Git::BLANK_SHA
|
||||
send_del_branch repo_name, committer, branch
|
||||
end
|
||||
end
|
||||
|
|
@ -83,7 +83,7 @@ class IrkerWorker
|
|||
return if push_data['total_commits_count'] == 0
|
||||
|
||||
# Next message is for number of commit pushed, if any
|
||||
if push_data['before'] =~ /^000000/
|
||||
if push_data['before'] == Gitlab::Git::BLANK_SHA
|
||||
# Tweak on push_data["before"] in order to have a nice compare URL
|
||||
push_data['before'] = before_on_new_branch push_data, project
|
||||
end
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ class PostReceive
|
|||
return false
|
||||
end
|
||||
|
||||
if tag?(ref)
|
||||
if Gitlab::Git.tag_ref?(ref)
|
||||
GitTagPushService.new.execute(project, @user, oldrev, newrev, ref)
|
||||
else
|
||||
GitPushService.new.execute(project, @user, oldrev, newrev, ref)
|
||||
|
|
@ -44,10 +44,4 @@ class PostReceive
|
|||
def log(message)
|
||||
Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tag?(ref)
|
||||
!!(/refs\/tags\/(.*)/.match(ref))
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,25 @@
|
|||
module Gitlab
|
||||
module Git
|
||||
BLANK_SHA = '0' * 40
|
||||
TAG_REF_PREFIX = "refs/tags/"
|
||||
BRANCH_REF_PREFIX = "refs/heads/"
|
||||
|
||||
def self.extract_ref_name(ref)
|
||||
ref.gsub(/\Arefs\/(tags|heads)\//, '')
|
||||
class << self
|
||||
def ref_name(ref)
|
||||
ref.gsub(/\Arefs\/(tags|heads)\//, '')
|
||||
end
|
||||
|
||||
def tag_ref?(ref)
|
||||
ref.start_with?(TAG_REF_PREFIX)
|
||||
end
|
||||
|
||||
def branch_ref?(ref)
|
||||
ref.start_with?(BRANCH_REF_PREFIX)
|
||||
end
|
||||
|
||||
def blank_ref?(ref)
|
||||
ref == BLANK_SHA
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ module Gitlab
|
|||
# we dont allow force push to protected branch
|
||||
if forced_push?(project, oldrev, newrev)
|
||||
:force_push_code_to_protected_branches
|
||||
elsif newrev == Gitlab::Git::BLANK_SHA
|
||||
elsif Gitlab::Git.blank_ref?(newrev)
|
||||
# and we dont allow remove of protected branch
|
||||
:remove_protected_branches
|
||||
elsif project.developers_can_push_to_protected_branch?(branch_name)
|
||||
|
|
@ -135,8 +135,8 @@ module Gitlab
|
|||
|
||||
def branch_name(ref)
|
||||
ref = ref.to_s
|
||||
if ref.start_with?('refs/heads')
|
||||
ref.sub(%r{\Arefs/heads/}, '')
|
||||
if Gitlab::Git.branch_ref?(ref)
|
||||
Gitlab::Git.ref_name(ref)
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
|
@ -144,8 +144,8 @@ module Gitlab
|
|||
|
||||
def tag_name(ref)
|
||||
ref = ref.to_s
|
||||
if ref.start_with?('refs/tags')
|
||||
ref.sub(%r{\Arefs/tags/}, '')
|
||||
if Gitlab::Git.tag_ref?(ref)
|
||||
Gitlab::Git.ref_name(ref)
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
|
|
|||
|
|
@ -65,12 +65,13 @@ module Gitlab
|
|||
# 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)
|
||||
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
|
||||
build(project, user, commits.last.id, commits.first.id, ref, commits)
|
||||
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)
|
||||
if newrev != Gitlab::Git::BLANK_SHA && Gitlab::Git.tag_ref?(ref)
|
||||
tag_name = Gitlab::Git.ref_name(ref)
|
||||
tag = repository.find_tag(tag_name)
|
||||
|
||||
if tag
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ describe HipchatService do
|
|||
end
|
||||
|
||||
context 'tag_push events' do
|
||||
let(:push_sample_data) { Gitlab::PushDataBuilder.build(project, user, '000000', '111111', 'refs/tags/test', []) }
|
||||
let(:push_sample_data) { Gitlab::PushDataBuilder.build(project, user, Gitlab::Git::BLANK_SHA, '1' * 40, 'refs/tags/test', []) }
|
||||
|
||||
it "should call Hipchat API for tag push events" do
|
||||
hipchat.execute(push_sample_data)
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ describe SlackService::PushMessage do
|
|||
let(:args) {
|
||||
{
|
||||
after: 'after',
|
||||
before: '000000',
|
||||
before: Gitlab::Git::BLANK_SHA,
|
||||
project_name: 'project_name',
|
||||
ref: 'refs/tags/new_tag',
|
||||
user_name: 'user_name',
|
||||
|
|
@ -61,7 +61,7 @@ describe SlackService::PushMessage do
|
|||
|
||||
context 'new branch' do
|
||||
before do
|
||||
args[:before] = '000000'
|
||||
args[:before] = Gitlab::Git::BLANK_SHA
|
||||
end
|
||||
|
||||
it 'returns a message regarding a new branch' do
|
||||
|
|
@ -75,7 +75,7 @@ describe SlackService::PushMessage do
|
|||
|
||||
context 'removed branch' do
|
||||
before do
|
||||
args[:after] = '000000'
|
||||
args[:after] = Gitlab::Git::BLANK_SHA
|
||||
end
|
||||
|
||||
it 'returns a message regarding a removed branch' do
|
||||
|
|
|
|||
Loading…
Reference in New Issue