Skip to content

Instantly share code, notes, and snippets.

@dylanmckay
Created October 20, 2017 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanmckay/ff6e61ff49643588ba0a68d324a3fe24 to your computer and use it in GitHub Desktop.
Save dylanmckay/ff6e61ff49643588ba0a68d324a3fe24 to your computer and use it in GitHub Desktop.
Script to update email address in GitHub Rust repositories containing Cargo.toml
#! /usr/bin/env ruby
require 'json'
require "net/http"
require "uri"
require 'fileutils'
USERNAME = "dylanmckay"
OLD_EMAIL = "dylanmckay34@gmail.com"
NEW_EMAIL = "me@dylanmckay.io"
TEMP_DIR = "/tmp/update-email-github-#{USERNAME}"
def repositories_for(username:, page:)
uri = URI.parse("https://api.github.com/users/#{username}/repos?page=#{page}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(Net::HTTP::Get.new(uri.request_uri))
JSON.parse(response.body)
end
def update_email_in_repository(repository)
FileUtils.mkdir_p(TEMP_DIR)
Dir.chdir(TEMP_DIR) do
repo_url = repository["ssh_url"]
repo_name = repository["name"]
default_branch = repository["default_branch"]
system("git clone #{repo_url}") unless Dir.exists?(repo_name)
Dir.chdir(File.join(TEMP_DIR, repo_name)) do
system("find . -exec sed -i'' 's/#{OLD_EMAIL}/#{NEW_EMAIL}/g' '{}' \\; 2>/dev/null") || (raise "failed to replace old email occurrences")
dirty_worktree = system("git diff-files --quiet")
if dirty_worktree
system("git add --all") || (raise "failed to add files to index")
system("git commit -m 'Update my email address'")
diff = `git show`
$stderr.puts diff
$stderr.puts "DOES THIS LOOK GOOD? (y/n)"
resp = $stdin.gets.chomp
if resp == 'Y' || resp == 'y'
system("git push origin #{default_branch}") || (raise "failed to push")
else
break
end
else
$stderr.puts "Repository '#{repo_name}' does not mention old email address"
end
end
end
end
page_number = 1
loop do
repositories = repositories_for(:username => 'dylanmckay', :page => page_number)
break if repositories.size == 0
rust_repositories = repositories.select { |repo| repo["language"] == "Rust" }
rust_repositories.each do |repo|
update_email_in_repository(repo)
end
page_number += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment