| 1 |
import logging |
| 2 |
import secrets |
| 3 |
import subprocess |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import yaml |
| 7 |
from github import Github |
| 8 |
from pydantic import BaseModel, SecretStr |
| 9 |
from pydantic_settings import BaseSettings |
| 10 |
|
| 11 |
|
| 12 |
class Settings(BaseSettings): |
| 13 |
github_repository: str |
| 14 |
github_token: SecretStr |
| 15 |
|
| 16 |
|
| 17 |
class Repo(BaseModel): |
| 18 |
name: str |
| 19 |
html_url: str |
| 20 |
stars: int |
| 21 |
owner_login: str |
| 22 |
owner_html_url: str |
| 23 |
|
| 24 |
|
| 25 |
def main() -> None: |
| 26 |
logging.basicConfig(level=logging.INFO) |
| 27 |
settings = Settings() # ty: ignore[missing-argument] |
| 28 |
|
| 29 |
logging.info(f"Using config: {settings.model_dump_json()}") |
| 30 |
g = Github(settings.github_token.get_secret_value(), per_page=100) |
| 31 |
r = g.get_repo(settings.github_repository) |
| 32 |
repos = g.search_repositories(query="topic:fastapi") |
| 33 |
repos_list = list(repos) |
| 34 |
final_repos: list[Repo] = [] |
| 35 |
for repo in repos_list[:100]: |
| 36 |
if repo.full_name == settings.github_repository: |
| 37 |
continue |
| 38 |
final_repos.append( |
| 39 |
Repo( |
| 40 |
name=repo.name, |
| 41 |
html_url=repo.html_url, |
| 42 |
stars=repo.stargazers_count, |
| 43 |
owner_login=repo.owner.login, |
| 44 |
owner_html_url=repo.owner.html_url, |
| 45 |
) |
| 46 |
) |
| 47 |
data = {"repos": [repo.model_dump() for repo in final_repos]} |
| 48 |
|
| 49 |
# Local development |
| 50 |
# repos_path = Path("../docs/en/data/topic_repos.yml") |
| 51 |
repos_path = Path("./docs/en/data/topic_repos.yml") |
| 52 |
repos_old_content = repos_path.read_text(encoding="utf-8") |
| 53 |
new_repos_content = yaml.dump(data, sort_keys=False, width=200, allow_unicode=True) |
| 54 |
if repos_old_content == new_repos_content: |
| 55 |
logging.info("The data hasn't changed. Finishing.") |
| 56 |
return |
| 57 |
repos_path.write_text(new_repos_content, encoding="utf-8") |
| 58 |
logging.info("Setting up GitHub Actions git user") |
| 59 |
subprocess.run(["git", "config", "user.name", "github-actions[bot]"], check=True) |
| 60 |
subprocess.run( |
| 61 |
["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"], |
| 62 |
check=True, |
| 63 |
) |
| 64 |
branch_name = f"fastapi-topic-repos-{secrets.token_hex(4)}" |
| 65 |
logging.info(f"Creating a new branch {branch_name}") |
| 66 |
subprocess.run(["git", "checkout", "-b", branch_name], check=True) |
| 67 |
logging.info("Adding updated file") |
| 68 |
subprocess.run(["git", "add", str(repos_path)], check=True) |
| 69 |
logging.info("Committing updated file") |
| 70 |
message = "👥 Update FastAPI GitHub topic repositories" |
| 71 |
subprocess.run(["git", "commit", "-m", message], check=True) |
| 72 |
logging.info("Pushing branch") |
| 73 |
subprocess.run(["git", "push", "origin", branch_name], check=True) |
| 74 |
logging.info("Creating PR") |
| 75 |
pr = r.create_pull(title=message, body=message, base="master", head=branch_name) |
| 76 |
logging.info(f"Created PR: {pr.number}") |
| 77 |
logging.info("Finished") |
| 78 |
|
| 79 |
|
| 80 |
if __name__ == "__main__": |
| 81 |
main() |