mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-12 05:26:45 +00:00
✨ Update GitHub Action: notify-translations, to avoid a race conditon (#3989)
This commit is contained in:
parent
458a7fbf15
commit
557fe61d92
1 changed files with 22 additions and 5 deletions
27
.github/actions/notify-translations/app/main.py
vendored
27
.github/actions/notify-translations/app/main.py
vendored
|
|
@ -1,5 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import random
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
@ -45,8 +47,11 @@ if __name__ == "__main__":
|
||||||
github_event = PartialGitHubEvent.parse_raw(contents)
|
github_event = PartialGitHubEvent.parse_raw(contents)
|
||||||
translations_map: Dict[str, int] = yaml.safe_load(translations_path.read_text())
|
translations_map: Dict[str, int] = yaml.safe_load(translations_path.read_text())
|
||||||
logging.debug(f"Using translations map: {translations_map}")
|
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)
|
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":
|
if pr.state == "open":
|
||||||
logging.debug(f"PR is open: {pr.number}")
|
logging.debug(f"PR is open: {pr.number}")
|
||||||
label_strs = set([label.name for label in pr.get_labels()])
|
label_strs = set([label.name for label in pr.get_labels()])
|
||||||
|
|
@ -67,19 +72,31 @@ if __name__ == "__main__":
|
||||||
for lang in langs:
|
for lang in langs:
|
||||||
if lang in translations_map:
|
if lang in translations_map:
|
||||||
num = translations_map[lang]
|
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)
|
issue = repo.get_issue(num)
|
||||||
message = f"Good news everyone! 😉 There's a new translation PR to be reviewed: #{pr.number} 🎉"
|
message = f"Good news everyone! 😉 There's a new translation PR to be reviewed: #{pr.number} 🎉"
|
||||||
already_notified = False
|
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():
|
for comment in issue.get_comments():
|
||||||
if message in comment.body:
|
if message in comment.body:
|
||||||
already_notified = True
|
already_notified = True
|
||||||
if not already_notified:
|
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)
|
issue.create_comment(message)
|
||||||
else:
|
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:
|
else:
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Changing labels in a closed PR doesn't trigger comments, PR: {pr.number}"
|
f"Changing labels in a closed PR doesn't trigger comments, PR: {pr.number}"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue