| 1 |
import logging |
| 2 |
import random |
| 3 |
import sys |
| 4 |
import time |
| 5 |
from pathlib import Path |
| 6 |
from typing import Any, cast |
| 7 |
|
| 8 |
import httpx |
| 9 |
from github import Github |
| 10 |
from pydantic import BaseModel, SecretStr |
| 11 |
from pydantic_settings import BaseSettings |
| 12 |
|
| 13 |
awaiting_label = "awaiting-review" |
| 14 |
lang_all_label = "lang-all" |
| 15 |
approved_label = "approved-1" |
| 16 |
|
| 17 |
|
| 18 |
github_graphql_url = "https://api.github.com/graphql" |
| 19 |
questions_translations_category_id = "DIC_kwDOCZduT84CT5P9" |
| 20 |
|
| 21 |
all_discussions_query = """ |
| 22 |
query Q($category_id: ID) { |
| 23 |
repository(name: "fastapi", owner: "fastapi") { |
| 24 |
discussions(categoryId: $category_id, first: 100) { |
| 25 |
nodes { |
| 26 |
title |
| 27 |
id |
| 28 |
number |
| 29 |
labels(first: 10) { |
| 30 |
edges { |
| 31 |
node { |
| 32 |
id |
| 33 |
name |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
""" |
| 42 |
|
| 43 |
translation_discussion_query = """ |
| 44 |
query Q($after: String, $discussion_number: Int!) { |
| 45 |
repository(name: "fastapi", owner: "fastapi") { |
| 46 |
discussion(number: $discussion_number) { |
| 47 |
comments(first: 100, after: $after) { |
| 48 |
edges { |
| 49 |
cursor |
| 50 |
node { |
| 51 |
id |
| 52 |
url |
| 53 |
body |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
""" |
| 61 |
|
| 62 |
add_comment_mutation = """ |
| 63 |
mutation Q($discussion_id: ID!, $body: String!) { |
| 64 |
addDiscussionComment(input: {discussionId: $discussion_id, body: $body}) { |
| 65 |
comment { |
| 66 |
id |
| 67 |
url |
| 68 |
body |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
""" |
| 73 |
|
| 74 |
update_comment_mutation = """ |
| 75 |
mutation Q($comment_id: ID!, $body: String!) { |
| 76 |
updateDiscussionComment(input: {commentId: $comment_id, body: $body}) { |
| 77 |
comment { |
| 78 |
id |
| 79 |
url |
| 80 |
body |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
""" |
| 85 |
|
| 86 |
|
| 87 |
class Comment(BaseModel): |
| 88 |
id: str |
| 89 |
url: str |
| 90 |
body: str |
| 91 |
|
| 92 |
|
| 93 |
class UpdateDiscussionComment(BaseModel): |
| 94 |
comment: Comment |
| 95 |
|
| 96 |
|
| 97 |
class UpdateCommentData(BaseModel): |
| 98 |
updateDiscussionComment: UpdateDiscussionComment |
| 99 |
|
| 100 |
|
| 101 |
class UpdateCommentResponse(BaseModel): |
| 102 |
data: UpdateCommentData |
| 103 |
|
| 104 |
|
| 105 |
class AddDiscussionComment(BaseModel): |
| 106 |
comment: Comment |
| 107 |
|
| 108 |
|
| 109 |
class AddCommentData(BaseModel): |
| 110 |
addDiscussionComment: AddDiscussionComment |
| 111 |
|
| 112 |
|
| 113 |
class AddCommentResponse(BaseModel): |
| 114 |
data: AddCommentData |
| 115 |
|
| 116 |
|
| 117 |
class CommentsEdge(BaseModel): |
| 118 |
node: Comment |
| 119 |
cursor: str |
| 120 |
|
| 121 |
|
| 122 |
class Comments(BaseModel): |
| 123 |
edges: list[CommentsEdge] |
| 124 |
|
| 125 |
|
| 126 |
class CommentsDiscussion(BaseModel): |
| 127 |
comments: Comments |
| 128 |
|
| 129 |
|
| 130 |
class CommentsRepository(BaseModel): |
| 131 |
discussion: CommentsDiscussion |
| 132 |
|
| 133 |
|
| 134 |
class CommentsData(BaseModel): |
| 135 |
repository: CommentsRepository |
| 136 |
|
| 137 |
|
| 138 |
class CommentsResponse(BaseModel): |
| 139 |
data: CommentsData |
| 140 |
|
| 141 |
|
| 142 |
class AllDiscussionsLabelNode(BaseModel): |
| 143 |
id: str |
| 144 |
name: str |
| 145 |
|
| 146 |
|
| 147 |
class AllDiscussionsLabelsEdge(BaseModel): |
| 148 |
node: AllDiscussionsLabelNode |
| 149 |
|
| 150 |
|
| 151 |
class AllDiscussionsDiscussionLabels(BaseModel): |
| 152 |
edges: list[AllDiscussionsLabelsEdge] |
| 153 |
|
| 154 |
|
| 155 |
class AllDiscussionsDiscussionNode(BaseModel): |
| 156 |
title: str |
| 157 |
id: str |
| 158 |
number: int |
| 159 |
labels: AllDiscussionsDiscussionLabels |
| 160 |
|
| 161 |
|
| 162 |
class AllDiscussionsDiscussions(BaseModel): |
| 163 |
nodes: list[AllDiscussionsDiscussionNode] |
| 164 |
|
| 165 |
|
| 166 |
class AllDiscussionsRepository(BaseModel): |
| 167 |
discussions: AllDiscussionsDiscussions |
| 168 |
|
| 169 |
|
| 170 |
class AllDiscussionsData(BaseModel): |
| 171 |
repository: AllDiscussionsRepository |
| 172 |
|
| 173 |
|
| 174 |
class AllDiscussionsResponse(BaseModel): |
| 175 |
data: AllDiscussionsData |
| 176 |
|
| 177 |
|
| 178 |
class Settings(BaseSettings): |
| 179 |
model_config = {"env_ignore_empty": True} |
| 180 |
|
| 181 |
github_repository: str |
| 182 |
github_token: SecretStr |
| 183 |
github_event_path: Path |
| 184 |
github_event_name: str | None = None |
| 185 |
httpx_timeout: int = 30 |
| 186 |
debug: bool | None = False |
| 187 |
number: int | None = None |
| 188 |
|
| 189 |
|
| 190 |
class PartialGitHubEventIssue(BaseModel): |
| 191 |
number: int | None = None |
| 192 |
|
| 193 |
|
| 194 |
class PartialGitHubEvent(BaseModel): |
| 195 |
pull_request: PartialGitHubEventIssue | None = None |
| 196 |
|
| 197 |
|
| 198 |
def get_graphql_response( |
| 199 |
*, |
| 200 |
settings: Settings, |
| 201 |
query: str, |
| 202 |
after: str | None = None, |
| 203 |
category_id: str | None = None, |
| 204 |
discussion_number: int | None = None, |
| 205 |
discussion_id: str | None = None, |
| 206 |
comment_id: str | None = None, |
| 207 |
body: str | None = None, |
| 208 |
) -> dict[str, Any]: |
| 209 |
headers = {"Authorization": f"token {settings.github_token.get_secret_value()}"} |
| 210 |
variables = { |
| 211 |
"after": after, |
| 212 |
"category_id": category_id, |
| 213 |
"discussion_number": discussion_number, |
| 214 |
"discussion_id": discussion_id, |
| 215 |
"comment_id": comment_id, |
| 216 |
"body": body, |
| 217 |
} |
| 218 |
response = httpx.post( |
| 219 |
github_graphql_url, |
| 220 |
headers=headers, |
| 221 |
timeout=settings.httpx_timeout, |
| 222 |
json={"query": query, "variables": variables, "operationName": "Q"}, |
| 223 |
) |
| 224 |
if response.status_code != 200: |
| 225 |
logging.error( |
| 226 |
f"Response was not 200, after: {after}, category_id: {category_id}" |
| 227 |
) |
| 228 |
logging.error(response.text) |
| 229 |
raise RuntimeError(response.text) |
| 230 |
data = response.json() |
| 231 |
if "errors" in data: |
| 232 |
logging.error(f"Errors in response, after: {after}, category_id: {category_id}") |
| 233 |
logging.error(data["errors"]) |
| 234 |
logging.error(response.text) |
| 235 |
raise RuntimeError(response.text) |
| 236 |
return cast(dict[str, Any], data) |
| 237 |
|
| 238 |
|
| 239 |
def get_graphql_translation_discussions( |
| 240 |
*, settings: Settings |
| 241 |
) -> list[AllDiscussionsDiscussionNode]: |
| 242 |
data = get_graphql_response( |
| 243 |
settings=settings, |
| 244 |
query=all_discussions_query, |
| 245 |
category_id=questions_translations_category_id, |
| 246 |
) |
| 247 |
graphql_response = AllDiscussionsResponse.model_validate(data) |
| 248 |
return graphql_response.data.repository.discussions.nodes |
| 249 |
|
| 250 |
|
| 251 |
def get_graphql_translation_discussion_comments_edges( |
| 252 |
*, settings: Settings, discussion_number: int, after: str | None = None |
| 253 |
) -> list[CommentsEdge]: |
| 254 |
data = get_graphql_response( |
| 255 |
settings=settings, |
| 256 |
query=translation_discussion_query, |
| 257 |
discussion_number=discussion_number, |
| 258 |
after=after, |
| 259 |
) |
| 260 |
graphql_response = CommentsResponse.model_validate(data) |
| 261 |
return graphql_response.data.repository.discussion.comments.edges |
| 262 |
|
| 263 |
|
| 264 |
def get_graphql_translation_discussion_comments( |
| 265 |
*, settings: Settings, discussion_number: int |
| 266 |
) -> list[Comment]: |
| 267 |
comment_nodes: list[Comment] = [] |
| 268 |
discussion_edges = get_graphql_translation_discussion_comments_edges( |
| 269 |
settings=settings, discussion_number=discussion_number |
| 270 |
) |
| 271 |
|
| 272 |
while discussion_edges: |
| 273 |
for discussion_edge in discussion_edges: |
| 274 |
comment_nodes.append(discussion_edge.node) |
| 275 |
last_edge = discussion_edges[-1] |
| 276 |
discussion_edges = get_graphql_translation_discussion_comments_edges( |
| 277 |
settings=settings, |
| 278 |
discussion_number=discussion_number, |
| 279 |
after=last_edge.cursor, |
| 280 |
) |
| 281 |
return comment_nodes |
| 282 |
|
| 283 |
|
| 284 |
def create_comment(*, settings: Settings, discussion_id: str, body: str) -> Comment: |
| 285 |
data = get_graphql_response( |
| 286 |
settings=settings, |
| 287 |
query=add_comment_mutation, |
| 288 |
discussion_id=discussion_id, |
| 289 |
body=body, |
| 290 |
) |
| 291 |
response = AddCommentResponse.model_validate(data) |
| 292 |
return response.data.addDiscussionComment.comment |
| 293 |
|
| 294 |
|
| 295 |
def update_comment(*, settings: Settings, comment_id: str, body: str) -> Comment: |
| 296 |
data = get_graphql_response( |
| 297 |
settings=settings, |
| 298 |
query=update_comment_mutation, |
| 299 |
comment_id=comment_id, |
| 300 |
body=body, |
| 301 |
) |
| 302 |
response = UpdateCommentResponse.model_validate(data) |
| 303 |
return response.data.updateDiscussionComment.comment |
| 304 |
|
| 305 |
|
| 306 |
def main() -> None: |
| 307 |
settings = Settings() # ty: ignore[missing-argument] |
| 308 |
if settings.debug: |
| 309 |
logging.basicConfig(level=logging.DEBUG) |
| 310 |
else: |
| 311 |
logging.basicConfig(level=logging.INFO) |
| 312 |
logging.debug(f"Using config: {settings.model_dump_json()}") |
| 313 |
g = Github(settings.github_token.get_secret_value()) |
| 314 |
repo = g.get_repo(settings.github_repository) |
| 315 |
if not settings.github_event_path.is_file(): |
| 316 |
raise RuntimeError( |
| 317 |
f"No github event file available at: {settings.github_event_path}" |
| 318 |
) |
| 319 |
contents = settings.github_event_path.read_text("utf-8") |
| 320 |
github_event = PartialGitHubEvent.model_validate_json(contents) |
| 321 |
logging.info(f"Using GitHub event: {github_event}") |
| 322 |
number = ( |
| 323 |
github_event.pull_request and github_event.pull_request.number |
| 324 |
) or settings.number |
| 325 |
if number is None: |
| 326 |
raise RuntimeError("No PR number available") |
| 327 |
number = cast(int, number) |
| 328 |
|
| 329 |
# Avoid race conditions with multiple labels |
| 330 |
sleep_time = random.random() * 10 # random number between 0 and 10 seconds |
| 331 |
logging.info( |
| 332 |
f"Sleeping for {sleep_time} seconds to avoid " |
| 333 |
"race conditions and multiple comments" |
| 334 |
) |
| 335 |
time.sleep(sleep_time) |
| 336 |
|
| 337 |
# Get PR |
| 338 |
logging.debug(f"Processing PR: #{number}") |
| 339 |
pr = repo.get_pull(number) |
| 340 |
label_strs = {label.name for label in pr.get_labels()} |
| 341 |
langs = [] |
| 342 |
for label in label_strs: |
| 343 |
if label.startswith("lang-") and not label == lang_all_label: |
| 344 |
langs.append(label[5:]) |
| 345 |
logging.info(f"PR #{pr.number} has labels: {label_strs}") |
| 346 |
if not langs or lang_all_label not in label_strs: |
| 347 |
logging.info(f"PR #{pr.number} doesn't seem to be a translation PR, skipping") |
| 348 |
sys.exit(0) |
| 349 |
|
| 350 |
# Generate translation map, lang ID to discussion |
| 351 |
discussions = get_graphql_translation_discussions(settings=settings) |
| 352 |
lang_to_discussion_map: dict[str, AllDiscussionsDiscussionNode] = {} |
| 353 |
for discussion in discussions: |
| 354 |
for edge in discussion.labels.edges: |
| 355 |
label = edge.node.name |
| 356 |
if label.startswith("lang-") and not label == lang_all_label: |
| 357 |
lang = label[5:] |
| 358 |
lang_to_discussion_map[lang] = discussion |
| 359 |
logging.debug(f"Using translations map: {lang_to_discussion_map}") |
| 360 |
|
| 361 |
# Messages to create or check |
| 362 |
new_translation_message = f"Good news everyone! 😉 There's a new translation PR to be reviewed: #{pr.number} by @{pr.user.login}. 🎉 This requires 2 approvals from native speakers to be merged. 🤓" |
| 363 |
done_translation_message = f"~There's a new translation PR to be reviewed: #{pr.number} by @{pr.user.login}~ Good job! This is done. 🍰☕" |
| 364 |
|
| 365 |
# Normally only one language, but still |
| 366 |
for lang in langs: |
| 367 |
if lang not in lang_to_discussion_map: |
| 368 |
log_message = f"Could not find discussion for language: {lang}" |
| 369 |
logging.error(log_message) |
| 370 |
raise RuntimeError(log_message) |
| 371 |
discussion = lang_to_discussion_map[lang] |
| 372 |
logging.info( |
| 373 |
f"Found a translation discussion for language: {lang} in discussion: #{discussion.number}" |
| 374 |
) |
| 375 |
|
| 376 |
already_notified_comment: Comment | None = None |
| 377 |
already_done_comment: Comment | None = None |
| 378 |
|
| 379 |
logging.info( |
| 380 |
f"Checking current comments in discussion: #{discussion.number} to see if already notified about this PR: #{pr.number}" |
| 381 |
) |
| 382 |
comments = get_graphql_translation_discussion_comments( |
| 383 |
settings=settings, discussion_number=discussion.number |
| 384 |
) |
| 385 |
for comment in comments: |
| 386 |
if new_translation_message in comment.body: |
| 387 |
already_notified_comment = comment |
| 388 |
elif done_translation_message in comment.body: |
| 389 |
already_done_comment = comment |
| 390 |
logging.info( |
| 391 |
f"Already notified comment: {already_notified_comment}, already done comment: {already_done_comment}" |
| 392 |
) |
| 393 |
|
| 394 |
if pr.state == "open" and awaiting_label in label_strs: |
| 395 |
logging.info( |
| 396 |
f"This PR seems to be a language translation and awaiting reviews: #{pr.number}" |
| 397 |
) |
| 398 |
if already_notified_comment: |
| 399 |
logging.info( |
| 400 |
f"This PR #{pr.number} was already notified in comment: {already_notified_comment.url}" |
| 401 |
) |
| 402 |
else: |
| 403 |
logging.info( |
| 404 |
f"Writing notification comment about PR #{pr.number} in Discussion: #{discussion.number}" |
| 405 |
) |
| 406 |
comment = create_comment( |
| 407 |
settings=settings, |
| 408 |
discussion_id=discussion.id, |
| 409 |
body=new_translation_message, |
| 410 |
) |
| 411 |
logging.info(f"Notified in comment: {comment.url}") |
| 412 |
elif pr.state == "closed" or approved_label in label_strs: |
| 413 |
logging.info(f"Already approved or closed PR #{pr.number}") |
| 414 |
if already_done_comment: |
| 415 |
logging.info( |
| 416 |
f"This PR #{pr.number} was already marked as done in comment: {already_done_comment.url}" |
| 417 |
) |
| 418 |
elif already_notified_comment: |
| 419 |
updated_comment = update_comment( |
| 420 |
settings=settings, |
| 421 |
comment_id=already_notified_comment.id, |
| 422 |
body=done_translation_message, |
| 423 |
) |
| 424 |
logging.info(f"Marked as done in comment: {updated_comment.url}") |
| 425 |
else: |
| 426 |
logging.info( |
| 427 |
f"There doesn't seem to be anything to be done about PR #{pr.number}" |
| 428 |
) |
| 429 |
logging.info("Finished") |
| 430 |
|
| 431 |
|
| 432 |
if __name__ == "__main__": |
| 433 |
main() |