forked from puppetlabs/community_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.rb
executable file
·79 lines (66 loc) · 2.59 KB
/
labels.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
#!/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-labels', 'Add the missing labels to repo') { result[:fix_labels] = true }
opts.on('-d', '--delete-labels', 'Delete unwanted labels from repo') { result[:delete_labels] = 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_labels = [
{ name: 'backwards-incompatible', color: 'd63700' },
{ name: 'bugfix', color: '00d87b' },
{ name: 'dependencies', color: '0366d6' },
{ name: 'feature', color: '222222' },
{ name: 'maintenance', color: 'ffd86e' },
{ name: 'needs-docs', color: '149380' },
{ name: 'needs-rebase', color: '3880ff' },
{ name: 'needs-squash', color: 'bfe5bf' },
{ name: 'needs-tests', color: 'ff8091' },
{ name: 'tests-fail', color: 'e11d21' },
{ name: 'hacktoberfest', color: 'ff9100' },
]
label_names = []
wanted_labels.each do |wanted_label|
label_names.push(wanted_label[:name])
end
puts "Checking for the following labels: #{label_names}"
parsed.each do |_k, v|
repo_name = (v['github']).to_s
missing_labels = util.fetch_repo_missing_labels(repo_name, wanted_labels)
incorrect_labels = util.fetch_repo_incorrect_labels(repo_name, wanted_labels)
extra_labels = util.fetch_repo_extra_labels(repo_name, wanted_labels)
puts "Delete: #{repo_name}, #{extra_labels}"
puts "Create: #{repo_name}, #{missing_labels}"
puts "Fix: #{repo_name}, #{incorrect_labels}"
if options[:delete_labels]
util.delete_repo_labels(repo_name, extra_labels) unless extra_labels.empty?
end
next unless options[:fix_labels]
util.update_repo_labels(repo_name, incorrect_labels) unless incorrect_labels.empty?
util.add_repo_labels(repo_name, missing_labels) unless missing_labels.empty?
end