From: Sebastián Ramírez Date: Tue, 5 Oct 2021 10:24:02 +0000 (+0200) Subject: ✨ Update GitHub Action: notify-translations, to avoid a race conditon (#3989) X-Git-Tag: 0.68.2~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=557fe61d9298d213f26357439c59bf3002b78979;p=thirdparty%2Ffastapi%2Ffastapi.git ✨ Update GitHub Action: notify-translations, to avoid a race conditon (#3989) --- diff --git a/.github/actions/notify-translations/app/main.py b/.github/actions/notify-translations/app/main.py index 79850513aa..7d6c1a4d20 100644 --- a/.github/actions/notify-translations/app/main.py +++ b/.github/actions/notify-translations/app/main.py @@ -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}"