()
| 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. 🍰☕" |
no test coverage detected