| 1 |
import logging |
| 2 |
import secrets |
| 3 |
import subprocess |
| 4 |
from collections import defaultdict |
| 5 |
from pathlib import Path |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
import httpx |
| 9 |
import yaml |
| 10 |
from github import Github |
| 11 |
from pydantic import BaseModel, SecretStr |
| 12 |
from pydantic_settings import BaseSettings |
| 13 |
|
| 14 |
github_graphql_url = "https://api.github.com/graphql" |
| 15 |
|
| 16 |
|
| 17 |
sponsors_query = """ |
| 18 |
query Q($after: String) { |
| 19 |
user(login: "tiangolo") { |
| 20 |
sponsorshipsAsMaintainer(first: 100, after: $after) { |
| 21 |
edges { |
| 22 |
cursor |
| 23 |
node { |
| 24 |
sponsorEntity { |
| 25 |
... on Organization { |
| 26 |
login |
| 27 |
avatarUrl |
| 28 |
url |
| 29 |
} |
| 30 |
... on User { |
| 31 |
login |
| 32 |
avatarUrl |
| 33 |
url |
| 34 |
} |
| 35 |
} |
| 36 |
tier { |
| 37 |
name |
| 38 |
monthlyPriceInDollars |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
""" |
| 46 |
|
| 47 |
|
| 48 |
class SponsorEntity(BaseModel): |
| 49 |
login: str |
| 50 |
avatarUrl: str |
| 51 |
url: str |
| 52 |
|
| 53 |
|
| 54 |
class Tier(BaseModel): |
| 55 |
name: str |
| 56 |
monthlyPriceInDollars: float |
| 57 |
|
| 58 |
|
| 59 |
class SponsorshipAsMaintainerNode(BaseModel): |
| 60 |
sponsorEntity: SponsorEntity |
| 61 |
tier: Tier |
| 62 |
|
| 63 |
|
| 64 |
class SponsorshipAsMaintainerEdge(BaseModel): |
| 65 |
cursor: str |
| 66 |
node: SponsorshipAsMaintainerNode |
| 67 |
|
| 68 |
|
| 69 |
class SponsorshipAsMaintainer(BaseModel): |
| 70 |
edges: list[SponsorshipAsMaintainerEdge] |
| 71 |
|
| 72 |
|
| 73 |
class SponsorsUser(BaseModel): |
| 74 |
sponsorshipsAsMaintainer: SponsorshipAsMaintainer |
| 75 |
|
| 76 |
|
| 77 |
class SponsorsResponseData(BaseModel): |
| 78 |
user: SponsorsUser |
| 79 |
|
| 80 |
|
| 81 |
class SponsorsResponse(BaseModel): |
| 82 |
data: SponsorsResponseData |
| 83 |
|
| 84 |
|
| 85 |
class Settings(BaseSettings): |
| 86 |
sponsors_token: SecretStr |
| 87 |
pr_token: SecretStr |
| 88 |
github_repository: str |
| 89 |
httpx_timeout: int = 30 |
| 90 |
|
| 91 |
|
| 92 |
def get_graphql_response( |
| 93 |
*, |
| 94 |
settings: Settings, |
| 95 |
query: str, |
| 96 |
after: str | None = None, |
| 97 |
) -> dict[str, Any]: |
| 98 |
headers = {"Authorization": f"token {settings.sponsors_token.get_secret_value()}"} |
| 99 |
variables = {"after": after} |
| 100 |
response = httpx.post( |
| 101 |
github_graphql_url, |
| 102 |
headers=headers, |
| 103 |
timeout=settings.httpx_timeout, |
| 104 |
json={"query": query, "variables": variables, "operationName": "Q"}, |
| 105 |
) |
| 106 |
if response.status_code != 200: |
| 107 |
logging.error(f"Response was not 200, after: {after}") |
| 108 |
logging.error(response.text) |
| 109 |
raise RuntimeError(response.text) |
| 110 |
data = response.json() |
| 111 |
if "errors" in data: |
| 112 |
logging.error(f"Errors in response, after: {after}") |
| 113 |
logging.error(data["errors"]) |
| 114 |
logging.error(response.text) |
| 115 |
raise RuntimeError(response.text) |
| 116 |
return data |
| 117 |
|
| 118 |
|
| 119 |
def get_graphql_sponsor_edges( |
| 120 |
*, settings: Settings, after: str | None = None |
| 121 |
) -> list[SponsorshipAsMaintainerEdge]: |
| 122 |
data = get_graphql_response(settings=settings, query=sponsors_query, after=after) |
| 123 |
graphql_response = SponsorsResponse.model_validate(data) |
| 124 |
return graphql_response.data.user.sponsorshipsAsMaintainer.edges |
| 125 |
|
| 126 |
|
| 127 |
def get_individual_sponsors( |
| 128 |
settings: Settings, |
| 129 |
) -> defaultdict[float, dict[str, SponsorEntity]]: |
| 130 |
nodes: list[SponsorshipAsMaintainerNode] = [] |
| 131 |
edges = get_graphql_sponsor_edges(settings=settings) |
| 132 |
|
| 133 |
while edges: |
| 134 |
for edge in edges: |
| 135 |
nodes.append(edge.node) |
| 136 |
last_edge = edges[-1] |
| 137 |
edges = get_graphql_sponsor_edges(settings=settings, after=last_edge.cursor) |
| 138 |
|
| 139 |
tiers: defaultdict[float, dict[str, SponsorEntity]] = defaultdict(dict) |
| 140 |
for node in nodes: |
| 141 |
tiers[node.tier.monthlyPriceInDollars][node.sponsorEntity.login] = ( |
| 142 |
node.sponsorEntity |
| 143 |
) |
| 144 |
return tiers |
| 145 |
|
| 146 |
|
| 147 |
def update_content(*, content_path: Path, new_content: Any) -> bool: |
| 148 |
old_content = content_path.read_text(encoding="utf-8") |
| 149 |
|
| 150 |
new_content = yaml.dump(new_content, sort_keys=False, width=200, allow_unicode=True) |
| 151 |
if old_content == new_content: |
| 152 |
logging.info(f"The content hasn't changed for {content_path}") |
| 153 |
return False |
| 154 |
content_path.write_text(new_content, encoding="utf-8") |
| 155 |
logging.info(f"Updated {content_path}") |
| 156 |
return True |
| 157 |
|
| 158 |
|
| 159 |
def main() -> None: |
| 160 |
logging.basicConfig(level=logging.INFO) |
| 161 |
settings = Settings() # ty: ignore[missing-argument] |
| 162 |
logging.info(f"Using config: {settings.model_dump_json()}") |
| 163 |
g = Github(settings.pr_token.get_secret_value()) |
| 164 |
repo = g.get_repo(settings.github_repository) |
| 165 |
|
| 166 |
tiers = get_individual_sponsors(settings=settings) |
| 167 |
keys = list(tiers.keys()) |
| 168 |
keys.sort(reverse=True) |
| 169 |
sponsors = [] |
| 170 |
for key in keys: |
| 171 |
sponsor_group = [] |
| 172 |
for login, sponsor in tiers[key].items(): |
| 173 |
sponsor_group.append( |
| 174 |
{"login": login, "avatarUrl": sponsor.avatarUrl, "url": sponsor.url} |
| 175 |
) |
| 176 |
sponsors.append(sponsor_group) |
| 177 |
github_sponsors = { |
| 178 |
"sponsors": sponsors, |
| 179 |
} |
| 180 |
|
| 181 |
# For local development |
| 182 |
# github_sponsors_path = Path("../docs/en/data/github_sponsors.yml") |
| 183 |
github_sponsors_path = Path("./docs/en/data/github_sponsors.yml") |
| 184 |
updated = update_content( |
| 185 |
content_path=github_sponsors_path, new_content=github_sponsors |
| 186 |
) |
| 187 |
|
| 188 |
if not updated: |
| 189 |
logging.info("The data hasn't changed, finishing.") |
| 190 |
return |
| 191 |
|
| 192 |
logging.info("Setting up GitHub Actions git user") |
| 193 |
subprocess.run(["git", "config", "user.name", "github-actions[bot]"], check=True) |
| 194 |
subprocess.run( |
| 195 |
["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"], |
| 196 |
check=True, |
| 197 |
) |
| 198 |
branch_name = f"fastapi-people-sponsors-{secrets.token_hex(4)}" |
| 199 |
logging.info(f"Creating a new branch {branch_name}") |
| 200 |
subprocess.run(["git", "checkout", "-b", branch_name], check=True) |
| 201 |
logging.info("Adding updated file") |
| 202 |
subprocess.run( |
| 203 |
[ |
| 204 |
"git", |
| 205 |
"add", |
| 206 |
str(github_sponsors_path), |
| 207 |
], |
| 208 |
check=True, |
| 209 |
) |
| 210 |
logging.info("Committing updated file") |
| 211 |
message = "👥 Update FastAPI People - Sponsors" |
| 212 |
subprocess.run(["git", "commit", "-m", message], check=True) |
| 213 |
logging.info("Pushing branch") |
| 214 |
subprocess.run(["git", "push", "origin", branch_name], check=True) |
| 215 |
logging.info("Creating PR") |
| 216 |
pr = repo.create_pull(title=message, body=message, base="master", head=branch_name) |
| 217 |
logging.info(f"Created PR: {pr.number}") |
| 218 |
logging.info("Finished") |
| 219 |
|
| 220 |
|
| 221 |
if __name__ == "__main__": |
| 222 |
main() |