Update Organization Repo List #93
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: Update Organization Repo List | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 */12 * * *" | |
| permissions: | |
| contents: write | |
| jobs: | |
| update: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.x" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install requests | |
| - name: Generate Repo List | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ORG_NAME: ha-china | |
| run: | | |
| python - << 'EOF' | |
| import os | |
| import requests | |
| import re | |
| org = os.environ["ORG_NAME"] | |
| token = os.environ["GH_TOKEN"] | |
| headers = { | |
| "Authorization": f"Bearer {token}", | |
| "Accept": "application/vnd.github+json" | |
| } | |
| repos = [] | |
| page = 1 | |
| while True: | |
| url = f"https://api.github.com/orgs/{org}/repos?per_page=100&page={page}" | |
| r = requests.get(url, headers=headers) | |
| data = r.json() | |
| if not data: | |
| break | |
| repos.extend(data) | |
| page += 1 | |
| repos = [ | |
| r for r in repos | |
| if not r["fork"] | |
| and not r["archived"] | |
| and not r["private"] | |
| and not r["name"].lower().startswith(".github") | |
| and r["name"] != "Flasher" | |
| ] | |
| def get_last_activity(repo): | |
| try: | |
| url = repo["releases_url"].replace("{/id}", "") | |
| r = requests.get(url, headers=headers, timeout=8) | |
| if r.status_code == 200: | |
| data = r.json() | |
| if data: | |
| return data[0].get("published_at") or repo.get("updated_at") | |
| except: | |
| pass | |
| return repo.get("updated_at", "1970-01-01T00:00:00Z") | |
| for r in repos: | |
| r["last_activity"] = get_last_activity(r) | |
| PINNED = ["haos-cn", "hassio-addons"] | |
| pinned = [] | |
| others = [] | |
| for r in repos: | |
| if r["name"].lower() in PINNED: | |
| pinned.append(r) | |
| else: | |
| others.append(r) | |
| pinned.sort(key=lambda x: PINNED.index(x["name"].lower())) | |
| others.sort(key=lambda x: x["last_activity"], reverse=True) | |
| repos = pinned + others | |
| cells = [] | |
| for repo in repos: | |
| name = repo["name"] | |
| desc = repo["description"] or "" | |
| stars = repo["stargazers_count"] | |
| url = repo["html_url"] | |
| activity = repo["last_activity"][:10] | |
| desc = desc.replace("\n", " ").replace("|", "\\|") | |
| if len(desc) > 90: | |
| desc = desc[:90] + "..." | |
| cells.append(f"""<td width=\"50%\" valign=\"top\">\n\n### [{name}]({url})\n\n⭐ {stars} · 📅 {activity}\n\n{desc}\n\n</td>""") | |
| rows = [] | |
| for i in range(0, len(cells), 2): | |
| left = cells[i] | |
| right = cells[i + 1] if i + 1 < len(cells) else "<td width=\"50%\"></td>" | |
| rows.append(f"<tr>\n{left}\n{right}\n</tr>") | |
| content = "<table>\n" + "\n".join(rows) + "\n</table>" | |
| with open("profile/README.md", "r", encoding="utf-8") as f: | |
| readme = f.read() | |
| pattern = r"<!-- repo-list-start -->.*?<!-- repo-list-end -->" | |
| replacement = "<!-- repo-list-start -->\n" + content + "\n<!-- repo-list-end -->" | |
| new_readme = re.sub(pattern, replacement, readme, flags=re.S) | |
| with open("profile/README.md", "w", encoding="utf-8") as f: | |
| f.write(new_readme) | |
| print("done") | |
| EOF | |
| - name: Commit Changes | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add profile/README.md | |
| git diff --cached --quiet || git commit -m "auto: update repo list" | |
| git push |