-
Notifications
You must be signed in to change notification settings - Fork 28
/
npc.rb
executable file
·71 lines (61 loc) · 2.64 KB
/
npc.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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
require_relative 'octokit_utils'
options = {}
options[:oauth] = ENV['GITHUB_COMMUNITY_TOKEN'] if ENV['GITHUB_COMMUNITY_TOKEN']
parser = OptionParser.new do |opts|
opts.banner = 'Usage: npc.rb [options]'
opts.on('-t', '--oauth-token TOKEN', 'OAuth token. Required.') { |v| options[:oauth] = v }
opts.on('-m', '--merge-conflicts', 'Comment / label PRs that have merge conflicts') { options[:merge_conflicts] = true }
opts.on('-N', '--no-op', 'No-op, dont actually edit the PRs') { options[:no_op] = true }
opts.on('-f', '--file NAME', String, 'Module file list') { |v| options[:file] = v }
end
parser.parse!
options[:file] = 'modules.json' if options[:file].nil?
missing = []
missing << '-t' if options[:oauth].nil?
unless missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts parser
exit
end
options[:repo_regex] = '.*' if options[:repo_regex].nil?
if options[:no_op]
puts 'RUNNING IN NO-OP MODE'
else
puts 'MAKING CHANGES TO YOUR REPOS'
end
util = OctokitUtils.new(options[:oauth])
parsed = util.load_module_list(options[:file])
parsed.each do |m|
next unless options[:merge_conflicts]
prs = util.fetch_pull_requests("#{m['github_namespace']}/#{m['repo_name']}")
prs.each do |pr|
# do we already have a label ?
pr_merges = util.does_pr_merge("#{m['github_namespace']}/#{m['repo_name']}", pr.number)
puts pr_merges
pr_has_label = util.does_pr_have_label("#{m['github_namespace']}/#{m['repo_name']}", pr.number, 'needs-rebase')
if pr_merges
# pr merges
# we have a label. should we remove the label if it is mergable
if pr_has_label
puts "#{m['github_namespace']}/#{m['repo_name']} #{pr.number} removing label"
util.remove_label_from_pr("#{m['github_namespace']}/#{m['repo_name']}", pr.number, 'needs-rebase') unless options[:no_op]
end
# pr does not merge
elsif pr_has_label
# has label
puts "#{m['github_namespace']}/#{m['repo_name']} #{pr.number} already labeled"
else
# pr does not have a label
puts "#{m['github_namespace']}/#{m['repo_name']} #{pr.number} adding comment and label"
unless options[:no_op]
# do comment
util.add_comment_to_pr("#{m['github_namespace']}/#{m['repo_name']}", pr.number, "Thanks @#{pr.user.login} for your work, but can't be merged as it has conflicts. Please rebase them on the current master, fix the conflicts and repush here. https://git-scm.com/book/en/v2/Git-Branching-Rebasing")
# do label
util.add_label_to_pr("#{m['github_namespace']}/#{m['repo_name']}", pr.number, 'needs-rebase')
end
end
end
end