Skip to content

Commit

Permalink
Add support for Gerrit review system
Browse files Browse the repository at this point in the history
This commits add supports gerrit system and will submit a review
everytime a change is made on the same branch.
  • Loading branch information
Spredzy committed Feb 10, 2015
1 parent a68f1a8 commit ab23e3b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
23 changes: 16 additions & 7 deletions lib/modulesync.rb
Expand Up @@ -40,6 +40,19 @@ def self.managed_modules(path, filter)
managed_modules
end

def self.get_git_base(options)
if options[:git_base]
git_base = options[:git_base]
else
case options[:git_provider]
when 'gerrit'
git_base = "ssh://#{options[:git_user]}@#{options[:git_provider_address]}:#{options[:git_provider_port]}/#{options[:namespace]}"
else # Default Github
git_base = "#{options[:git_user]}@#{options[:git_provider_address]}:#{options[:namespace]}"
end
end
end

def self.run(args)
cli = CLI.new
cli.parse_opts(args)
Expand All @@ -56,12 +69,8 @@ def self.run(args)
# managed_modules is either an array or a hash
managed_modules.each do |puppet_module, opts|
puts "Syncing #{puppet_module}"
if options[:git_base]
git_base = options[:git_base]
else
git_base = "#{options[:git_user]}@#{options[:git_provider_address]}:#{options[:namespace]}"
end
Git.pull(git_base, puppet_module, options[:branch], opts || {})
git_base = get_git_base(options)
Git.pull(git_base, puppet_module, options[:branch], options[:git_provider], opts || {})
module_configs = Util.parse_config("#{PROJ_ROOT}/#{puppet_module}/#{MODULE_CONF_FILE}")
files_to_manage = module_files | defaults.keys | module_configs.keys
files_to_delete = []
Expand All @@ -82,7 +91,7 @@ def self.run(args)
if options[:noop]
Git.update_noop(puppet_module, options[:branch])
else
Git.update(puppet_module, files_to_manage, options[:message], options[:branch])
Git.update(puppet_module, files_to_manage, options[:message], options[:branch], options[:git_provider], options[:git_user], options[:git_provider_address], options[:git_provider_port])
end
end
elsif options[:command] == 'hook'
Expand Down
1 change: 1 addition & 0 deletions lib/modulesync/cli.rb
Expand Up @@ -11,6 +11,7 @@ def defaults
:namespace => 'puppetlabs',
:branch => 'master',
:git_user => 'git',
:git_provider => 'github',
:git_provider_address => 'github.com',
:managed_modules_conf => 'managed_modules.yml',
:configs => '.',
Expand Down
31 changes: 23 additions & 8 deletions lib/modulesync/git.rb
Expand Up @@ -4,7 +4,7 @@ module ModuleSync
module Git
include Constants

def self.pull(git_base, name, branch, opts)
def self.pull(git_base, name, branch, git_provider, opts)
if ! Dir.exists?(PROJ_ROOT)
Dir.mkdir(PROJ_ROOT)
end
Expand All @@ -19,16 +19,18 @@ def self.pull(git_base, name, branch, opts)

# Repo already cloned, check out master and override local changes
else
puts "Overriding any local changes to repositories in #{PROJ_ROOT}"
repo = ::Git.open("#{PROJ_ROOT}/#{name}")
repo.branch(branch).checkout
repo.reset_hard
repo.pull('origin', branch)
if ! git_provider.eql? 'gerrit'
puts "Overriding any local changes to repositories in #{PROJ_ROOT}"
repo = ::Git.open("#{PROJ_ROOT}/#{name}")
repo.branch(branch).checkout
repo.reset_hard
repo.pull('origin', branch)
end
end
end

# Git add/rm, git commit, git push
def self.update(name, files, message, branch)
def self.update(name, files, message, branch, git_provider, git_user, git_provider_address, git_provider_port)
repo = ::Git.open("#{PROJ_ROOT}/#{name}")
repo.branch(branch).checkout
files.each do |file|
Expand All @@ -39,7 +41,20 @@ def self.update(name, files, message, branch)
end
end
begin
repo.commit(message)
opts = {}
case git_provider
when 'gerrit'
branch = "HEAD:refs/publish/master/#{branch}"
if ! File.exists? ("#{PROJ_ROOT}/#{name}/.git/hooks/commit-msg")
Dir.chdir("#{PROJ_ROOT}/#{name}") {
%x[gitdir=$(git rev-parse --git-dir); scp -p -P #{git_provider_port} #{git_user}@#{git_provider_address}:hooks/commit-msg ${gitdir}/hooks/]
}
else
opts = {:amend => true}
message = nil
end
end
repo.commit(message, opts)
repo.push('origin', branch)
rescue ::Git::GitExecuteError => git_error
if git_error.message.include? "nothing to commit, working directory clean"
Expand Down

0 comments on commit ab23e3b

Please sign in to comment.