-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_config.py
More file actions
63 lines (50 loc) · 1.62 KB
/
generate_config.py
File metadata and controls
63 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import os
import sys
import requests
import yaml
def fetch_repos(org: str, token: str | None) -> list[dict]:
headers = {"Accept": "application/vnd.github+json"}
if token:
headers["Authorization"] = f"Bearer {token}"
repos = []
page = 1
while True:
r = requests.get(
f"https://api.github.com/orgs/{org}/repos",
headers=headers,
params={"type": "public", "per_page": 100, "page": page},
)
r.raise_for_status()
batch = r.json()
if not batch:
break
repos.extend(batch)
page += 1
return [r for r in repos if not r["archived"]]
def main():
parser = argparse.ArgumentParser(
description="Generate a git-sync config.yaml from a GitHub org's public repos."
)
parser.add_argument("source_org", help="GitHub org name (e.g. gbdev)")
parser.add_argument(
"destination_org",
help="Destination org URL prefix (e.g. https://codeberg.org/gbdev)",
)
args = parser.parse_args()
token = os.environ.get("GITHUB_TOKEN")
if not token:
print("Warning: GITHUB_TOKEN not set, using unauthenticated requests (lower rate limit)", file=sys.stderr)
repos = fetch_repos(args.source_org, token)
config = {
"repositories": [
{
"source": repo["clone_url"],
"destination": f"{args.destination_org.rstrip('/')}/{repo['name']}",
}
for repo in repos
]
}
print(yaml.dump(config, default_flow_style=False, sort_keys=False), end="")
if __name__ == "__main__":
main()