-
Notifications
You must be signed in to change notification settings - Fork 42
/
add-collaborators
executable file
·89 lines (74 loc) · 2.86 KB
/
add-collaborators
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
#!/usr/bin/env ruby
banner = <<-BANNER
Usage: add-collaborators [<issue URL> or options]
BANNER
# add as collaborators anyone who comments on a given issue in a given repo
# assumes you store the appropriate OAuth token as an ENV variable called GITHUBTEACHER_TOKEN
require 'octokit'
require 'optparse'
require_relative 'common'
access_token = ENV['GITHUBTEACHER_TOKEN'] or abort("You need a GitHub Teacher access token")
repo_name, issue_num = nil, nil
# Make sure arguments are specified
ARGV << '-h' if ARGV.empty?
ARGV.options do |opts|
opts.banner = banner
opts.on("-r", "--repo REPOSITORY", String) { |val| repo_name = val }
opts.on("-i", "--issue ISSUE", Numeric) { |val| issue_num = val }
opts.on_tail("-h", "--help") { abort opts.to_s }
opts.parse!
end
if ARGV.any?
r, i = parse_issue_url(ARGV.first)
repo_name ||= r
issue_num ||= i
end
abort ARGV.options.to_s unless [repo_name, issue_num].all?
puts({repo_name: repo_name, issue_num: issue_num}.inspect)
# Create a new Octokit Client
Octokit.auto_paginate = true
client = Octokit::Client.new :access_token => access_token
# Save a little time - build a hash of current collaborators so we can skip them later
current_collaborators = Hash[client.collaborators(repo_name).map { |collaborator|
[collaborator[:login], collaborator[:login]]
}]
# Get Issue Commenters and Add as Collaborators
successfully_added_users = []
begin
client.issue_comments(repo_name, issue_num).each do |comment|
username = comment[:user][:login]
next if current_collaborators[username] # skip adding if already a collaborator
if user_added = client.add_collaborator(repo_name, username)
successfully_added_users << username
else
puts "Failed to add #{username} as a collaborator (check: is githubteacher repository owner?)"
end
end
rescue Octokit::NotFound
abort "[404] - Repository not found:\nIf #{repo_name || "nil"} is correct, are you using the right Auth token?"
rescue Octokit::UnprocessableEntity
abort "[422] - Unprocessable Entity:\nAre you trying to add collaborators to an org-level repository?"
end
if successfully_added_users.any?
begin
names = "@#{successfully_added_users.first}"
verb = "is"
num = "a"
noun = "collaborator"
if successfully_added_users.size > 1
verb = "are"
num = ""
noun = "collaborators"
if successfully_added_users.size == 2
names = "@#{successfully_added_users.first} and @#{successfully_added_users.last}"
else
at_mentions = successfully_added_users.map { |name| "@#{name}" }
names = "#{at_mentions[0...-1].join(", ")}, and #{at_mentions[-1]}"
end
end
message = ":tada: #{names} #{verb} now #{num} repository #{noun}. :balloon:"
client.add_comment repo_name, issue_num, message
rescue => e
abort "ERR posting comment (#{e.inspect})"
end
end