Created
April 30, 2025 16:15
-
-
Save Richard-Barrett/40dc989bce1e2c8b50587b9c0166ec5d to your computer and use it in GitHub Desktop.
List Oauth Applications in GitHub Enterprise by Orgs Comma Separated
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import requests | |
| import argparse | |
| import csv | |
| import json | |
| import sys | |
| def list_github_apps(org, api_url, headers): | |
| url = f"{api_url}/orgs/{org}/installations" | |
| response = requests.get(url, headers=headers) | |
| if response.status_code == 404: | |
| print(f"⚠️ No apps or org '{org}' not found.") | |
| return [] | |
| response.raise_for_status() | |
| return response.json().get("installations", []) | |
| def main(): | |
| parser = argparse.ArgumentParser(description="List GitHub Apps installed across multiple orgs.") | |
| parser.add_argument("--token", required=True, help="GitHub Personal Access Token") | |
| parser.add_argument("--orgs", required=True, help="Comma-separated list of orgs (e.g., company,company-infra)") | |
| parser.add_argument("--output", choices=["json", "csv"], default="json", help="Output format (default: json)") | |
| parser.add_argument("--api-url", default="https://api.github.com", help="GitHub API URL") | |
| args = parser.parse_args() | |
| headers = { | |
| "Authorization": f"token {args.token}", | |
| "Accept": "application/vnd.github+json" | |
| } | |
| all_data = [] | |
| org_list = [o.strip() for o in args.orgs.split(",")] | |
| for org_login in org_list: | |
| try: | |
| apps = list_github_apps(org_login, args.api_url, headers) | |
| except requests.HTTPError as e: | |
| print(f"❌ Failed to fetch apps for org {org_login}: {e}") | |
| continue | |
| for app in apps: | |
| all_data.append({ | |
| "organization": org_login, | |
| "app_name": app['app_slug'], | |
| "app_id": app['id'], | |
| "description": app.get("description", "No description") | |
| }) | |
| if args.output == "csv": | |
| csv_file = f"github_apps_by_org.csv" | |
| with open(csv_file, mode="w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=["organization", "app_name", "app_id", "description"]) | |
| writer.writeheader() | |
| writer.writerows(all_data) | |
| print(f"📄 CSV written to: {csv_file}") | |
| else: | |
| print(json.dumps(all_data, indent=2)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment