Skip to content

Instantly share code, notes, and snippets.

@trevor-vaughan
Last active November 19, 2016 02:30
Show Gist options
  • Save trevor-vaughan/b8b79336b2748c6fdeecdfee3a7edf45 to your computer and use it in GitHub Desktop.
Save trevor-vaughan/b8b79336b2748c6fdeecdfee3a7edf45 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|
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']}))
#### ADD YOUR OWN LOGIC HERE #####
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
#### END ADDING YOUR OWN LOGIC #####
system(%(git commit -a --amend --no-edit))
sleep(1)
system(%(git review))
end
end
end
end
end
#!/usr/bin/env ruby
# You need to make sure that your source PRs have a Gerrit Change ID *prior* to
# running this script
#
# Use the following instructions to set this up globally for your account:
# mkdir -p ~/.git-templates/hooks
# git config --global init.templatedir '~/.git-templates'
# curl -Lo ~/.git-templates/hooks/commit-msg https://review.gerrithub.io/tools/hooks/commit-msg
# chmod +x ~/.git-templates/hooks/commit-msg
#
# Then run 'git init' in all of your repositories and use
# git commit -a --amend to add a Change-Id and force push your changes back to
# your PR
#
#
# 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=ARGV
GERRIT_URL = %(ssh://#{USERNAME}@review.gerrithub.io:29418/simp)
require 'octokit'
require 'tmpdir'
require 'fileutils'
fail('You must set the environment variable GITHUB_ACCESS_TOKEN') unless ENV['GITHUB_ACCESS_TOKEN']
fail('You must pass some SIMP ticket subject IDs to match') if PR_IDS.empty?
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
bad_prs = []
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
pull_requests.each do |pr|
if pr[:body] !~ /Change- Id:\s+.+/
bad_prs << pr[:url]
next
end
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 #{pr[:base][:ref]}))
end
end
end
end
unless bad_prs.empty?
$stderr.puts('='*20)
$stderr.puts('WARNING: Did not sync the following PRs due to lack of Change-ID:')
$stderr.puts(" * #{bad_prs.join("\n * ")}")
$stderr.puts('='*20)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment