Skip to content

Instantly share code, notes, and snippets.

@skwashd
Created May 28, 2018 09:50
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 skwashd/2f9550d4c5fd3e8b08e879f5a009dd61 to your computer and use it in GitHub Desktop.
Save skwashd/2f9550d4c5fd3e8b08e879f5a009dd61 to your computer and use it in GitHub Desktop.
Remove all external collaborators from GitHub

This script removes all external collaborators in a GitHub organisation. Collaborators look like a good idea when you have a small organisation and you don't want to start managing a lot of teams. Once your organisation grows collaborators are a nightmare to manage. There is little or no context about why they have access, they're often missed when auditing user access and they get forgotten about.

Setup

  • Run pip install requests to install the one and only dependency
  • Configure the GITHUB_ORG and GITHUB_TOKEN environment variables

Running

Run the script like so python3 ./remove-collaborators.py. The output should look like this:

No outside collaborators found.
DONE

If you have collaborators it will remove all of them and your output will look similar to:

Removed BigBird
Removed CookieMonster
Removed Ernie
No outside collaborators found.
DONE

Hopefully you only see that once.

#!/usr/bin/env python3
import os
import requests
def main(token: str, org: str) -> bool:
"""Removes all outside collaborators from organisation.
:param token: The GitHub access token.
:param org: The name of the GitHub organisation.
:return: Were all users removed?
"""
url = f"https://api.github.com/orgs/{ORG}/outside_collaborators"
headers = headers = {"Authorization": f"token {TOKEN}"}
while True:
response = requests.get(url, headers=headers)
users = response.json()
if not len(users):
print("No outside collaborators found.")
return True
for user in users:
login = user["login"]
requests.delete(f"{url}/{login}", headers=headers)
print(f"Removed {login}")
if __name__ == "__main__":
TOKEN = os.environ.get("GITHUB_TOKEN")
ORG = os.environ.get("GITHUB_ORG")
main(TOKEN, ORG)
print("DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment