Fetch repos and members #32
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
| name: Fetch repos and members | |
| on: | |
| schedule: | |
| - cron: '0 3 * * *' # every night at 03:00 UTC | |
| workflow_dispatch: # allow manual trigger | |
| jobs: | |
| fetch: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Fetch org data via GitHub API | |
| env: | |
| GH_TOKEN: ${{ secrets.ORG_REPO_TOKEN }} | |
| ORG: fattacciolilab | |
| run: | | |
| python3 - <<'EOF' | |
| import os, json, urllib.request, urllib.error | |
| token = os.environ["GH_TOKEN"] | |
| org = os.environ["ORG"] | |
| headers = { | |
| "Authorization": f"Bearer {token}", | |
| "Accept": "application/vnd.github+json", | |
| "X-GitHub-Api-Version": "2022-11-28", | |
| } | |
| def get(url): | |
| results, page = [], 1 | |
| while True: | |
| paged = f"{url}{'&' if '?' in url else '?'}per_page=100&page={page}" | |
| req = urllib.request.Request(paged, headers=headers) | |
| try: | |
| with urllib.request.urlopen(req) as r: | |
| data = json.loads(r.read()) | |
| except urllib.error.HTTPError as e: | |
| print(f"HTTP {e.code} on {paged}") | |
| break | |
| if not data: | |
| break | |
| results.extend(data) | |
| page += 1 | |
| return results | |
| repos_raw = get(f"https://api.github.com/orgs/{org}/repos?type=all&sort=updated") | |
| repos = [] | |
| for r in repos_raw: | |
| # get collaborators for this repo | |
| collabs_raw = get(f"https://api.github.com/repos/{org}/{r['name']}/collaborators?affiliation=all") | |
| collabs = [{"login": c["login"], "role": c.get("role_name", "member")} for c in collabs_raw] | |
| repos.append({ | |
| "name": r["name"], | |
| "description": r.get("description") or "", | |
| "url": r["html_url"], | |
| "private": r["private"], | |
| "forks": r["forks_count"], | |
| "stars": r["stargazers_count"], | |
| "updated_at": r["updated_at"], | |
| "language": r.get("language") or "", | |
| "topics": r.get("topics", []), | |
| "collaborators": collabs, | |
| }) | |
| out = {"generated_at": __import__('datetime').datetime.utcnow().isoformat() + "Z", "repos": repos} | |
| with open("repos.json", "w") as f: | |
| json.dump(out, f, indent=2) | |
| print(f"Written {len(repos)} repos to repos.json") | |
| EOF | |
| - name: Commit updated repos.json | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add repos.json | |
| git diff --cached --quiet || git commit -m "chore: update repos.json [$(date -u +%Y-%m-%dT%H:%M:%SZ)]" | |
| git push |