#!/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")