diff options
author | Christian Cleberg <hello@cleberg.net> | 2024-12-28 11:30:03 -0600 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2024-12-28 11:30:03 -0600 |
commit | 2ee3e7af7b9133da946dff2643505a3d7013b3c0 (patch) | |
tree | 30159ba605abc870507902bcbc53bf6733487476 /applications/github/github_admins.py | |
parent | be74e8cab3bbd8b702adce0127cec2bb48dd7b7b (diff) | |
download | audit-tools-2ee3e7af7b9133da946dff2643505a3d7013b3c0.tar.gz audit-tools-2ee3e7af7b9133da946dff2643505a3d7013b3c0.tar.bz2 audit-tools-2ee3e7af7b9133da946dff2643505a3d7013b3c0.zip |
restructure directories
Diffstat (limited to 'applications/github/github_admins.py')
-rw-r--r-- | applications/github/github_admins.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/applications/github/github_admins.py b/applications/github/github_admins.py new file mode 100644 index 0000000..d737887 --- /dev/null +++ b/applications/github/github_admins.py @@ -0,0 +1,76 @@ +""" +Gather all members of a GitHub organization, all repos within that organization, +and list each user's permission per repo. +""" + +import requests + +GITHUB_TOKEN = 'your_personal_access_token' +ORGANIZATION = 'your_organization' +TIMEOUT = 30 + +# Headers for authentication +headers = { + 'Authorization': f'token {GITHUB_TOKEN}', + 'Accept': 'application/vnd.github.v3+json' +} + +def get_org_members(org): + """ + Get members of an organization + """ + url = f'https://api.github.com/orgs/{org}/members' + response = requests.get(url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + return response.json() + +def get_org_repos(org): + """ + Get repositories of an organization + """ + url = f'https://api.github.com/orgs/{org}/repos' + response = requests.get(url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + return response.json() + +def get_repo_collaborators(org, repo): + """ + Get collaborators of a repository with their permissions + """ + url = f'https://api.github.com/repos/{org}/{repo}/collaborators' + response = requests.get(url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + return response.json() + +def get_user_permissions(org, repo, user): + """ + Get a user's permissions for a repository + """ + url = f'https://api.github.com/repos/{org}/{repo}/collaborators/{user}/permission' + response = requests.get(url, headers=headers, timeout=TIMEOUT) + response.raise_for_status() + return response.json() + +# Main script +if __name__ == '__main__': + # Get organization members + members = get_org_members(ORGANIZATION) + print(f"Members of the organization '{ORGANIZATION}':") + for member in members: + print(f"- {member['login']}") + + # Get organization repositories + repositories = get_org_repos(ORGANIZATION) + print(f"\nRepositories in the organization '{ORGANIZATION}':") + for repository in repositories: + print(f"- {repository['name']}") + + # Get collaborators for each repository and their permissions + for repository in repositories: + repository_name = repository['name'] + collaborators = get_repo_collaborators(ORGANIZATION, repository_name) + print(f"\nCollaborators for the repository '{repository_name}':") + for collaborator in collaborators: + user_login = collaborator['login'] + permissions = get_user_permissions(ORGANIZATION, repository_name, user_login) + print(f"- {user_login}: {permissions['permission']}") |