Skip to content

Instantly share code, notes, and snippets.

@emexelem
Created March 29, 2023 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emexelem/ec08c1e31d4e18bd9147a1131fee6bd2 to your computer and use it in GitHub Desktop.
Save emexelem/ec08c1e31d4e18bd9147a1131fee6bd2 to your computer and use it in GitHub Desktop.
import os
import requests
import csv
# Set the name of the csv file containing the list of dormant users to remove
# This file is generated by the Github Enterprise report "Dormant Users"
dormant_users_csv_report_filename = "export-github-enterprise-1679693671.csv"
# Set the name of the Github enterprise slug
enterprise_slug = "github-enterprise"
# Set up the GraphQL API endpoint
api_url = "https://api.github.com/graphql"
# Set up the GraphQL queries
query_enterprise_id = """
query ($enterprise: String!) {
enterprise(slug: $enterprise) {
id
}
}
"""
query_user_id = """
query ($login: String!) {
user(login: $login) {
id
}
}
"""
query_remove_user = """
mutation ($enterpriseId: ID!, $userId: ID!) {
removeEnterpriseMember(input: {enterpriseId: $enterpriseId, userId: $userId}) {
clientMutationId
}
}
"""
# Set up the authorization header with a bearer token
access_token = os.environ["GITHUB_TOKEN"]
headers = {
"Authorization": f"Bearer {access_token}",
"X-Github-Next-Global-ID": "1"
}
# Get the enterprise ID
response = requests.post(
api_url,
json = {
"query": query_enterprise_id,
"variables": { "enterprise": enterprise_slug }
},
headers = headers
)
if response.status_code != 200:
print(f"Failed to get the enterprise ID")
print(f"Response status code: {response.status_code}")
print(f"Response message: {response.text}")
exit(1)
enterprise_id = response.json()["data"]["enterprise"]["id"]
# Open the csv file and loop through the usernames to delete each one
with open(dormant_users_csv_report_filename, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
username = row["login"]
if username:
# Get the user ID
response = requests.post(
api_url,
json = {
"query": query_user_id,
"variables": { "login": username }
},
headers = headers
)
if response.status_code != 200:
print(f"Failed to get the user ID for user '{username}'")
print(f"Response status code: {response.status_code}")
print(f"Response message: {response.text}")
continue
user_id = response.json()["data"]["user"]["id"]
print(f"Deleting user '{username}")
# Make the API request to remove the user from the enterprise
response = requests.post(
api_url,
json = {
"query": query_remove_user,
"variables": { "enterpriseId": enterprise_id, "userId": user_id }
},
headers = headers
)
if response.status_code != 200:
print(f"Failed to remove the user '{username}' from the enterprise")
print(f"Response status code: {response.status_code}")
print(f"Response message: {response.text}")
continue
if response.json().get("errors"):
print("Response contains an error: " + str(response.json()["errors"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment