forked from puppetlabs/community_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopics.rb
executable file
·68 lines (53 loc) · 1.86 KB
/
topics.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
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'octokit_utils'
require_relative 'options'
def primary_remote
['upstream', 'origin'].map do |name|
get_remote(name)
end.compact.first
end
def get_remote(name)
require 'rugged'
remote = Rugged::Repository.new('.').remotes[name] rescue nil
parts = remote.url.match(/(\w+\/[\w-]+)(?:\.git)?$/) if remote
parts[1] if parts
end
options = parse_options do |opts, result|
opts.on('-f', '--fix-topics', 'Add the missing topics to repo') { result[:fix_topics] = true }
opts.on('-d', '--delete-topics', 'Delete unwanted topics from repo') { result[:delete_topics] = true }
opts.on('--repo [REPO]', 'Pass a repository name, defaults to the current upstream.') do |v|
result[:remote] = v || primary_remote
raise 'Could not guess primary remote. Try using --remote instead.' unless result[:remote]
end
opts.on('--remote REMOTE', 'Name of a remote to work on.') do |v|
result[:remote] = get_remote(v)
raise "No url set for remote #{v}" unless result[:remote]
end
end
if options[:remote]
parsed = { options[:remote] => { 'github' => options[:remote] } }
else
parsed = load_url(options)
end
util = OctokitUtils.new(options[:oauth])
wanted_topics = [ 'hacktoberfest' ]
puts "Checking for the following topics: #{wanted_topics}"
parsed.each do |_k, v|
repo_name = (v['github']).to_s
topics = util.client.topics(repo_name)[:names]
extra = topics - wanted_topics
missing = wanted_topics - topics
final = topics
if options[:delete_topics]
final -= extra
end
if options[:fix_topics]
final += missing
end
puts "Delete: #{repo_name}, #{extra}"
puts "Create: #{repo_name}, #{missing}"
puts "Topics: #{repo_name}, #{final}"
next unless options.include?(:delete_topics) or options.include?(:fix_topics)
util.client.replace_all_topics(repo_name, final)
end