Created
June 3, 2024 09:59
-
-
Save karabasosman/3a56174efd4c52d0ba3128772e980617 to your computer and use it in GitHub Desktop.
Bulk invite people to GitHub organization from CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import csv | |
import pandas as pd | |
# Your personal access token | |
token = "<PERSONAL_ACCESS_TOKEN>" | |
# Your GitHub organization name | |
org = "<ORGANIZATION-NAME>" | |
# Path to the CSV file containing email addresses | |
csv_file_path = 'emails.csv' | |
# GitHub API URL for inviting users | |
url = f"https://api.github.com/orgs/{org}/invitations" | |
headers = { | |
"Authorization": f"token {token}", | |
"Accept": "application/vnd.github.v3+json", | |
"X-GitHub-Api-Version": "2022-11-28" | |
} | |
# Delay in seconds between requests | |
delay = 60 # Adjust the delay as necessary | |
# Read the CSV file | |
df = pd.read_csv(csv_file_path, header=None) | |
# Convert the DataFrame to a list | |
email_list = df[0].tolist() | |
# Invite users | |
for email in email_list: | |
data = {"email": email,"role": "direct_member"} | |
response = requests.post(url, json=data, headers=headers) | |
if response.status_code == 201: | |
print(f"Invitation sent to {email}") | |
elif response.status_code == 403 and 'rate limit' in response.text.lower(): | |
print("Rate limit exceeded, sleeping for 1 minute...") | |
time.sleep(120) # Sleep for 1 minute if rate limit is hit | |
# Retry the request | |
response = requests.post(url, json=data, headers=headers) | |
if response.status_code == 201: | |
print(f"Invitation sent to {email} after waiting for rate limit reset") | |
else: | |
print(f"Failed to invite {email}: {response.status_code} - {response.text}") | |
else: | |
print(f"Failed to invite {email}: {response.status_code} - {response.text}") | |
# Wait before sending the next request | |
time.sleep(delay) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment