|
| 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 |
0 commit comments