Skip to content

Instantly share code, notes, and snippets.

@Skarlso
Last active June 8, 2018 08:50
Show Gist options
  • Save Skarlso/fd5bd5971a78a5fa9760b31683de690e to your computer and use it in GitHub Desktop.
Save Skarlso/fd5bd5971a78a5fa9760b31683de690e to your computer and use it in GitHub Desktop.
Ruby script to update all your forked repositories
#!/usr/bin/env ruby
require 'octokit'
require 'logger'
@logger = Logger.new("output.log")
def update_fork(repo)
repo_name = repo.name
# clone the repository -- octokit doesn't provide this feature as it's a github api library
@logger.info("cloning into #{repo.ssh_url}")
system("git clone #{repo.ssh_url} #{repo_name}")
# setup upstream for updating
@logger.info("setup upstream to #{repo.parent.ssh_url}")
system("cd #{repo_name} && git remote add upstream #{repo.parent.ssh_url}")
# do the update
@logger.info("doing the update with push")
system("cd #{repo_name} && git fetch upstream && git rebase upstream/master && git push origin")
ensure
# ensure that the folder is cleaned up
@logger.info("cleanup: removing the repository folder")
system("rm -fr #{repo_name}")
end
client = Octokit::Client.new(:access_token => ENV['GIT_TOKEN'], per_page: 100)
repos = client.repos({}, query: {type: 'owner', sort: 'asc'})
# Go through all the pages and add them to the list of repositories.
repos.concat(client.last_response.rels[:next].get.data)
repos = repos.select{ |r| r.fork }
@logger.info("going to update '#{repos.length}' repositories")
repos.each do |repo|
# get the repositories information
@logger.info("updating #{repo.name}")
r = client.repository(repo.id)
update_fork(r)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment