-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_and_del.rb
executable file
·94 lines (69 loc) · 2.14 KB
/
search_and_del.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env ruby
# frozen_string_literal: true
# Search for fork repos with names matching the given string and offer to delete them.
require 'finer_struct'
require 'octokit'
require 'optparse'
require_relative 'lib/config'
require_relative 'lib/github_auth'
RESULTS_SLICE = 50
DEFAULT_CONFIG_FILE = File.expand_path('~/.githubtools.conf')
def parse_cmdline
options = FinerStruct::Mutable.new(
config_file: DEFAULT_CONFIG_FILE,
force_auth: false,
search_string: nil
)
opts = OptionParser.new
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] search-string"
opts.on('-h', '-?', '--help', 'Option help') {
puts opts
exit
}
opts.on('--auth', 'Ignore saved access token and force reauthentication') {
options.force_auth = true
}
opts.on('--config-file=FNAME', "Config file name. Default is #{DEFAULT_CONFIG_FILE}") { |fname|
options.config_file = fname
}
opts.parse!
if ARGV.empty?
warn 'Error: search string argument missing!'
warn opts
exit 1
end
options.search_string = ARGV.shift
options
end
def search_repos(client, search_str)
repos =
client
.repos(nil)
.select { |repo|
# Consider only forks.
# Ignore repos with #keep in the description.
repo[:fork] && !repo[:description].to_s.downcase.include?('#keep') && repo[:name].downcase.include?(search_str.downcase)
}
repos.map { |repo| repo[:full_name] }
end
def delete_repos(client, reponames)
reponames.each { |reponame|
puts "Deleting #{reponame}..."
client.delete_repo(reponame)
}
end
options = parse_cmdline
config = Config.new
config.load_config(options.config_file)
auth = GithubAuth.new(config: config, force_auth: options.force_auth)
access_token = auth.access_token
client = Octokit::Client.new(auto_paginate: true, access_token: access_token)
results = search_repos(client, options.search_string)
results.each_slice(RESULTS_SLICE) { |rslice|
puts 'Found the following repos:'
rslice.each_with_index { |reponame, i| puts "#{i + 1}: #{reponame}" }
print "Enter 'yes' to delete: "
break unless $stdin.gets.strip.casecmp?('yes')
delete_repos(client, rslice)
}
__END__