Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit e198f37

Browse files
authored
Merge pull request #19 from github/invite-to-org-and-add-to-team
Invite to org and add to team
2 parents a6e5787 + 033b4e2 commit e198f37

9 files changed

+129
-10
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
source 'https://rubygems.org'
22

3-
gem 'octokit'
3+
gem 'octokit', '~> 4.7'

Gemfile.lock

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
addressable (2.3.8)
5-
faraday (0.9.2)
4+
addressable (2.5.1)
5+
public_suffix (~> 2.0, >= 2.0.2)
6+
faraday (0.12.2)
67
multipart-post (>= 1.2, < 3)
78
multipart-post (2.0.0)
8-
octokit (4.2.0)
9-
sawyer (~> 0.6.0, >= 0.5.3)
10-
sawyer (0.6.0)
11-
addressable (~> 2.3.5)
12-
faraday (~> 0.8, < 0.10)
9+
octokit (4.7.0)
10+
sawyer (~> 0.8.0, >= 0.5.3)
11+
public_suffix (2.0.5)
12+
sawyer (0.8.1)
13+
addressable (>= 2.3.5, < 2.6)
14+
faraday (~> 0.8, < 1.0)
1315

1416
PLATFORMS
1517
ruby
1618

1719
DEPENDENCIES
18-
octokit
20+
octokit (~> 4.7)
1921

2022
BUNDLED WITH
21-
1.11.2
23+
1.12.0

add-to-org.rb

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env ruby
2+
# add USERNAME as member to ORGANIZATION and optionally to TEAM
3+
# assumes you created and stored the appropriate OAuth token as an ENV variable called GITHUBTEACHER_TOKEN
4+
5+
require 'octokit'
6+
require 'optparse'
7+
8+
# Make sure arguments are specified
9+
ARGV << '-h' if ARGV.empty?
10+
11+
# Create options hash
12+
options = {}
13+
14+
# Parse options
15+
OptionParser.new do |opts|
16+
opts.banner = "Usage: add-to-org.rb [options]
17+
NOTICE: This also assumes you have set up the variable GITHUBTEACHER_TOKEN with administrative privilages.\n\n"
18+
19+
opts.on("-u", "--username USERNAME", "Username for membership -- ex: githubstudent") do |u|
20+
options[:username] = u
21+
end
22+
23+
opts.on("-o", "--org ORGANIZATION", "Organization name -- ex: githubschool") do |o|
24+
options[:org] = o
25+
end
26+
27+
opts.on("-t", "--team TEAM_NAME", "[Optional] Team to add member to -- ex: developers") do |t|
28+
options[:team] = t
29+
end
30+
31+
opts.on_tail("-h", "--help", "Prints this help message") do |h|
32+
puts opts
33+
exit
34+
end
35+
36+
end.parse!
37+
38+
39+
TOKEN = ENV['GITHUBTEACHER_TOKEN']
40+
abort("Missing GITHUBTEACHER_TOKEN. Please set up an OAUTH token and set it in the environment by typing\n
41+
export GITHUBTEACHER_TOKEN=XXXXXXXXXXXXXXXXXXXXXXX\n
42+
and replace the Xs with your actual token. Reminder: This token needs admin privilages onto your organization in order to be inviting people.") unless TOKEN
43+
44+
# Assign variables
45+
# Team is optional, so you don't need to assign it if it's nil.
46+
username = options[:username]
47+
org = options[:org]
48+
team_name = options[:team] if !options[:team].nil?
49+
50+
51+
# Create a new Octokit Client
52+
Octokit.auto_paginate = true
53+
@client = Octokit::Client.new :access_token => TOKEN
54+
55+
# If the team doesn't exist yet, create it
56+
# otherwise, get the team_id from the list
57+
# Once we have the team_id we can add people to it
58+
# regardless of them being a member of the organization yet
59+
def add_to_team_and_org(team_name, username, org)
60+
61+
team_id = if team_id.nil?
62+
create_team(org, team_name, org)
63+
else
64+
get_team_id(team_name, org)
65+
end
66+
67+
# Add username to team_id
68+
# Slightly different than adding them as a member.
69+
# If the username includes ","s, split it because it's a full list!
70+
username.split(",").each do |name|
71+
@client.add_team_membership(team_id, name)
72+
puts "#{name} added to #{team_name}."
73+
end
74+
75+
end
76+
77+
# Returns team_id
78+
def create_team(team_id, team_name, org)
79+
response = @client.create_team(org, {:name => team_name})
80+
team_id = response.id
81+
puts "Team '#{team_name}' created at https://github.com/orgs/#{org}/teams" unless team_id.nil?
82+
83+
team_id
84+
end
85+
86+
def get_team_id(team_name, org)
87+
team_id = nil
88+
team_list = @client.org_teams(org)
89+
90+
team_list.each do |team|
91+
if team.name == team_name
92+
team_id = team.id
93+
end
94+
end
95+
team_id
96+
97+
end
98+
99+
# If they just want to add users as members, skip the team
100+
def add_to_org(org, username)
101+
username.split(",").each do |name|
102+
@client.update_organization_membership(org, {:user => name})
103+
puts "#{name} added to #{org}."
104+
end
105+
end
106+
107+
begin
108+
if !team_name.nil?
109+
add_to_team_and_org(team_name, username, org)
110+
else
111+
add_to_org(org, username)
112+
end
113+
rescue Octokit::Forbidden
114+
abort "[403] - Unable to add member to organization. Check that the GITHUBTEACHER_TOKEN was created with administrative privilages so that you can add members to the organization"
115+
rescue Octokit::Unauthorized
116+
abort "[401] - The credentials you've supplied are no longer valid. Please generate a new token."
117+
end

vendor/cache/addressable-2.5.1.gem

101 KB
Binary file not shown.

vendor/cache/faraday-0.12.2.gem

38 KB
Binary file not shown.

vendor/cache/multipart-post-2.0.0.gem

11 KB
Binary file not shown.

vendor/cache/octokit-4.7.0.gem

73 KB
Binary file not shown.

vendor/cache/public_suffix-2.0.5.gem

87.5 KB
Binary file not shown.

vendor/cache/sawyer-0.8.1.gem

15.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)