Skip to content

Instantly share code, notes, and snippets.

@op-ct
Forked from trevor-vaughan/gerrit_munge.rb
Created November 18, 2016 20:21
Show Gist options
  • Save op-ct/ec34fd2e702d4e031f26eb8cc5e3dd8a to your computer and use it in GitHub Desktop.
Save op-ct/ec34fd2e702d4e031f26eb8cc5e3dd8a to your computer and use it in GitHub Desktop.
Script for Importing things from GitHub into GerritHub
#!/usr/bin/env ruby
require 'json'
require 'yaml'
require 'tmpdir'
# Your GitHub Username
USERNAME='trevor-vaughan'
gerrit_url = %(ssh://#{USERNAME}@review.gerrithub.io:29418/)
project = '^simp/.* '
gerrit = 'ssh -p 29418 review.gerrithub.io gerrit '
gq = gerrit + 'query --format=JSON '
gerrit_open = gq + project + 'status:open'
gerrit_results = %x(#{gerrit_open}).lines
open_changes = gerrit_results.map{|result| JSON.parse(result)}
open_changes.each do |change|
#### ADD YOUR OWN LOGIC HERE #####
if change['commitMessage'] =~ /^\(SIMP-1868\)/
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
%x(rm -rf tmp)
change_url = gerrit_url + change['project']
system(%(git clone #{change_url} tmp))
Dir.chdir('tmp') do
system(%(git remote add gerrit #{change_url}))
system(%(git review -d #{change['number']}))
system(%(git checkout HEAD~1 .fixtures.yml))
fixtures = YAML.load_file('.fixtures.yml')
next unless fixtures
next unless fixtures['fixtures']
next unless fixtures['fixtures']['repositories']
next unless fixtures['fixtures']['repositories'].delete('compliance_markup')
fixtures['fixtures']['repositories'] = fixtures['fixtures']['repositories'].sort.to_h
File.open('.fixtures.yml','w') do |fh|
fh.puts(fixtures.to_yaml)
end
system(%(git commit -a --amend --no-edit))
sleep(1)
system(%(git review))
end
end
end
end
#### END ADDING YOUR OWN LOGIC #####
end
#!/usr/bin/env ruby
# Make sure you have a GITHUB_ACCESS_TOKEN set in your environment
#
# More information at
# https://help.github.com/articles/creating-an-access-token-for-command-line-use/
#
# Add your GitHub Username Here
USERNAME='trevor-vaughan'
# These must be in the *subject* of your Git commit and surrounded by
# parenthesis
PR_IDS=[
'SIMP-2003'
]
GERRIT_URL = %(ssh://#{USERNAME}@review.gerrithub.io:29418/simp)
require 'octokit'
require 'tmpdir'
fail('You must set the environment variable GITHUB_ACCESS_TOKEN') unless ENV['GITHUB_ACCESS_TOKEN']
Octokit.auto_paginate = true
client = Octokit::Client.new(:access_token => ENV['GITHUB_ACCESS_TOKEN'])
repos = client.org_repos('simp')
pull_requests = []
repos.each do |repo|
prs = client.pull_requests(repo.full_name)
prs.each do |pr|
PR_IDS.each do |pr_id|
if pr.title.include?("(#{pr_id})")
pr[:tgt_name] = repo[:name]
pull_requests << pr
end
end
end
end
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
pull_requests.each do |pr|
src_url = pr[:head][:repo][:html_url]
system(%(rm -rf tmp))
system(%(git clone #{src_url} tmp))
Dir.chdir('tmp') do
system(%(git remote add gerrit #{GERRIT_URL}/#{pr[:tgt_name]}))
system(%(git checkout #{pr[:head][:sha]}))
system(%(git review))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment