]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Update GitHub Action: notify-translations, to avoid a race conditon (#3989)
authorSebastián Ramírez <tiangolo@gmail.com>
Tue, 5 Oct 2021 10:24:02 +0000 (12:24 +0200)
committerGitHub <noreply@github.com>
Tue, 5 Oct 2021 10:24:02 +0000 (10:24 +0000)
.github/actions/notify-translations/app/main.py

index 79850513aaba99f0ff04c50ef77d3965b80e0b63..7d6c1a4d20a7fb494574b04e255e7e828e88ded9 100644 (file)
@@ -1,5 +1,7 @@
 import logging
+import time
 from pathlib import Path
+import random
 from typing import Dict, Optional
 
 import yaml
@@ -45,8 +47,11 @@ if __name__ == "__main__":
     github_event = PartialGitHubEvent.parse_raw(contents)
     translations_map: Dict[str, int] = yaml.safe_load(translations_path.read_text())
     logging.debug(f"Using translations map: {translations_map}")
+    sleep_time = random.random() * 10  # random number between 0 and 10 seconds
     pr = repo.get_pull(github_event.pull_request.number)
-    logging.debug(f"Processing PR: {pr.number}")
+    logging.debug(
+        f"Processing PR: {pr.number}, with anti-race condition sleep time: {sleep_time}"
+    )
     if pr.state == "open":
         logging.debug(f"PR is open: {pr.number}")
         label_strs = set([label.name for label in pr.get_labels()])
@@ -67,19 +72,31 @@ if __name__ == "__main__":
             for lang in langs:
                 if lang in translations_map:
                     num = translations_map[lang]
-                    logging.info(f"Found a translation issue for language: {lang} in issue: {num}")
+                    logging.info(
+                        f"Found a translation issue for language: {lang} in issue: {num}"
+                    )
                     issue = repo.get_issue(num)
                     message = f"Good news everyone! 😉 There's a new translation PR to be reviewed: #{pr.number} 🎉"
                     already_notified = False
-                    logging.info(f"Checking current comments in issue: {num} to see if already notified about this PR: {pr.number}")
+                    time.sleep(sleep_time)
+                    logging.info(
+                        f"Sleeping for {sleep_time} seconds to avoid race conditions and multiple comments"
+                    )
+                    logging.info(
+                        f"Checking current comments in issue: {num} to see if already notified about this PR: {pr.number}"
+                    )
                     for comment in issue.get_comments():
                         if message in comment.body:
                             already_notified = True
                     if not already_notified:
-                        logging.info(f"Writing comment in issue: {num} about PR: {pr.number}")
+                        logging.info(
+                            f"Writing comment in issue: {num} about PR: {pr.number}"
+                        )
                         issue.create_comment(message)
                     else:
-                        logging.info(f"Issue: {num} was already notified of PR: {pr.number}")
+                        logging.info(
+                            f"Issue: {num} was already notified of PR: {pr.number}"
+                        )
     else:
         logging.info(
             f"Changing labels in a closed PR doesn't trigger comments, PR: {pr.number}"