Skip to content

Instantly share code, notes, and snippets.

@binford2k
Created March 6, 2019 00:59
Show Gist options
  • Save binford2k/f372f17c2c084cac45bcd207d42c1025 to your computer and use it in GitHub Desktop.
Save binford2k/f372f17c2c084cac45bcd207d42c1025 to your computer and use it in GitHub Desktop.
clean up stale git repos
#! /usr/bin/env ruby
require 'tempfile'
require 'io/console'
begin
require 'octokit'
rescue LoadError => e
puts "This requires the octokit gem"
exit 1
end
TOKEN = `git config --global github.token`.chomp
EDITOR = ENV['EDITOR'] || 'vim'
begin
client = Octokit::Client.new(:access_token => TOKEN)
client.user.login
client.auto_paginate = true
rescue => e
puts "Github login error: #{e.message}"
exit 1
end
repos = client.repositories(client.user[:login])
forks, own = repos.partition {|r| r[:fork] }
maxlength = repos.reduce(0) { |acc, r| acc = [r[:full_name].size, acc].max }
Tempfile.open('repo-purge') do |file|
file.write "# Edit the character in the first column to describe what to do with each repo.\n"
file.write "#\n"
file.write "# k - Keep this repo; do nothing\n"
file.write "# p - Purge this repo; delete it\n"
file.write "# d - Alias for 'purge'\n"
file.write "# a - Archive this repo; do nothing\n"
file.write "#\n"
own.each do |repo|
description = repo[:description] || '(none)'
file.write(sprintf("k %-#{maxlength}s %s\n", repo[:full_name], description))
end
forks.each do |repo|
desc = repo[:description] || '(none)'
parent = client.repository(repo[:full_name])[:parent][:full_name]
file.write(sprintf("k %-#{maxlength}s %s\n", repo[:full_name], desc))
file.write(" ↳ forked from: #{parent}\n")
end
file.flush()
unless system(EDITOR, file.path)
raise "Cannot run editor[#{EDITOR}]!"
end
updates = File.readlines(file.path).map {|line| line.split[0..1] }
purge = updates.select {|r| ['p', 'd'].include? r.first.downcase}.map{ |r| r.last }
archive = updates.select {|r| ['a'].include? r.first.downcase}.map{ |r| r.last }
puts "About to purge these repositories:"
purge.each {|r| puts "* #{r}"}
puts "Type 'yes, delete #{purge.size} repos' to confirm deletion:"
ans = gets.chomp
if ans == "yes, delete #{purge.size} repos"
purge.each do |repo|
puts "Press 'y' to delete '#{repo}':"
case STDIN.getch
when 'y'
client.delete_repository(repo)
else
puts 'skipping...'
end
end
else
puts 'Skipping deletion by request'
end
puts
puts "About to archive these repositories:"
archive.each {|r| puts "* #{r}"}
puts "Type 'yes, archive #{archive.size} repos' to confirm archival:"
ans = gets.chomp
if ans == "yes, archive #{archive.size} repos"
archive.each do |repo|
puts "Press 'y' to archive '#{repo}':"
case STDIN.getch
when 'y'
client.edit_repository(repo, :archived => true)
else
puts 'skipping...'
end
end
else
puts 'Skipping archival by request'
end
end
@binford2k
Copy link
Author

# Edit the character in the first column to describe what to do with each repo.
#
# k - Keep this repo; do nothing
# p - Purge this repo; delete it
# d - Alias for 'purge'
# a - Archive this repo; do nothing
#
k  binford2k/abalone                            A simple Sinatra & hterm based web terminal.
k  binford2k/bash_completion                    (none)
k  binford2k/binford2k-drupal                   This is a module that allows you to manage many aspects of a multisite Drupal installation.
k  binford2k/binford2k-easter_egg               help help help help help
k  binford2k/binford2k-itemize                  Count the number of types, classes, functions used in Puppet manifest(s)
k  binford2k/binford2k-manageonce               Manage a Puppet resource type, one time only.
k  binford2k/binford2k-manifold                 a puppet relationship multiplier
k  binford2k/binford2k-mas                      Puppet package provider for the Mac App Store
k  binford2k/binford2k-node_encrypt             Encrypt secrets inside Puppet catalogs and reports
k  binford2k/binford2k-params                   A simple Puppet function to return a value from the module's params class, including it if required.
k  binford2k/binford2k-puppet_infrastructure    Barebones implementation of an Application Orchestrator for open source Puppet
k  binford2k/binford2k-puppet_script            Imperative scripts using the Puppet RAL
k  binford2k/binford2k-remote_agent             (none)
[.....]
k  binford2k/aws_resource_reaper                Services to control unmanaged aws resources
    ↳ forked from: puppetlabs/aws_resource_reaper
k  binford2k/bolt                               Execute commands remotely over SSH and WinRM
    ↳ forked from: puppetlabs/bolt
k  binford2k/brainfuck-pp                       (none)
    ↳ forked from: zacstewart/brainfuck-pp
k  binford2k/cfgmgmtcamp.eu                     This is the git repo for cfgmgmtcamp.eu
    ↳ forked from: cfgmgmtcamp/cfgmgmtcamp.eu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment