Created
August 16, 2022 10:50
-
-
Save kenji-miyake/a97da5b3a591085b241cdc397965495b to your computer and use it in GitHub Desktop.
org2repos.py
This file contains 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 argparse | |
import os | |
import re | |
from pathlib import Path | |
from typing import Optional | |
import yaml | |
from github import Github | |
github = Github(os.getenv("GH_TOKEN")) | |
def org2repos(org_name: str, output_file: Path, regex: Optional[str], exclude: Optional[str]): | |
repo_records = {} | |
for repo in github.get_organization(org_name).get_repos(): | |
if regex and (not re.match(regex, repo.name)): | |
continue | |
if exclude and re.match(exclude, repo.name): | |
continue | |
repo_records[repo.name] = { | |
"type": "git", | |
"url": repo.ssh_url, | |
"version": repo.default_branch, | |
} | |
repos = { | |
"repositories": repo_records, | |
} | |
with open(output_file, "w") as f: | |
yaml.dump(repos, f, indent=2, sort_keys=False) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("org_name", type=str) | |
parser.add_argument("-o", "--output", dest="output_file", type=Path, default=None) | |
parser.add_argument("-e", "--regex", dest="regex", type=str, default=None) | |
parser.add_argument("-x", "--exclude", dest="exclude", type=str, default=None) | |
args = parser.parse_args() | |
if not args.output_file: | |
args.output_file = f"{args.org_name}.repos" | |
org2repos(args.org_name, args.output_file, args.regex, args.exclude) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment