Skip to content

Instantly share code, notes, and snippets.

@trevor-vaughan
Created September 8, 2016 21:26
Show Gist options
  • Save trevor-vaughan/18e7423420dcfa54267a93507ad15572 to your computer and use it in GitHub Desktop.
Save trevor-vaughan/18e7423420dcfa54267a93507ad15572 to your computer and use it in GitHub Desktop.
Push updated versions of all the Puppet modules
#!/usr/bin/ruby
require 'json'
github_base = 'git@github.com:simp/'
tmp_repo = 'fubarness'
failed_repos = []
Dir.glob('*').each do |dir|
next unless File.directory?(dir)
Dir.chdir(dir) do
begin
next unless File.exist?('metadata.json')
repos = %x{git remote -v}.split("\n")
src_repo = repos.select{|x| x =~ /github\.com\/simp\//}.first
if src_repo
repo_name = src_repo.split(/\s+/)[1].split('/').last
new_repo = github_base + repo_name
remotes = %x{git remote}.split("\n")
unless remotes.include?(tmp_repo)
%x{git remote add #{tmp_repo} #{new_repo}}
failed_repos << dir + ' - remote add failed' unless $?.success?
next
end
remote_tags = %x{git ls-remote --tags #{tmp_repo}}.split("\n").map{|x| x = x.split('/').last}
unless $?.success?
failed_repos << dir + ' - tag fetch failed'
next
end
metadata = JSON.parse(File.read('metadata.json'))
version = metadata['version']
unless version
failed_repos << dir + ' - Version not found in metadata.json'
next
end
remote_repos = %x{git branch -r}.split("\n").select{|x| x =~ /#{tmp_repo}/}.map{|x| x.split('/').last}
# So that we don't collide with everyone else's stuff
if remote_repos.include?('simp-master')
version = "simp-#{version}"
end
# If we already have this tag pushed, skip it!
next if remote_tags.include?(version)
%x{git tag -f #{version}}
%x{git push -f #{tmp_repo} #{version}}
if $?.success?
puts "Pushed new tag #{version} for #{repo_name}"
else
failed_repos << dir + " - failed to push tag #{version}"
next
end
else
failed_repos << dir
end
ensure
%x{git remote rm #{tmp_repo}}
end
end
end
$stderr.puts(%(Failed Repos:\n * #{failed_repos.join("\n * ")})) unless failed_repos.empty?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment