From fff1296e36c615b84119b0f9db0023caf941bec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Wed, 3 Jul 2024 12:01:55 +0200 Subject: [PATCH] Pulrequest check: Fix ignore file --- .../module/pull_request/checks.py | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/github_app_geo_project/module/pull_request/checks.py b/github_app_geo_project/module/pull_request/checks.py index 50bb92c043e..e6a37cbc4cf 100644 --- a/github_app_geo_project/module/pull_request/checks.py +++ b/github_app_geo_project/module/pull_request/checks.py @@ -5,6 +5,7 @@ import os import re import subprocess # nosec +import tempfile from tempfile import NamedTemporaryFile from typing import Any, cast @@ -19,10 +20,16 @@ _LOGGER = logging.getLogger(__name__) -def _get_codespell_command(config: checks_configuration.PullRequestChecksConfiguration) -> list[str]: +def _get_codespell_command( + context: module.ProcessContext[ + checks_configuration.PullRequestChecksConfiguration, dict[str, Any], dict[str, Any] + ], + ignore_file: tempfile.NamedTemporaryFile, +) -> list[str]: """ Get the codespell command. """ + config = context.module_config codespell_config = config.get("codespell", {}) codespell_config = codespell_config if isinstance(codespell_config, dict) else {} command = ["codespell"] @@ -31,9 +38,14 @@ def _get_codespell_command(config: checks_configuration.PullRequestChecksConfigu "spell-ignore-words.txt", ".spell-ignore-words.txt", ): - if os.path.exists(spell_ignore_file): - command.append(f"--ignore-words={spell_ignore_file}") + try: + content = context.github_project.repo.get_contents(spell_ignore_file) + ignore_file.write(content.decoded_content.decode("utf-8")) + command.append(f"--ignore-words={ignore_file.name}") break + except github.GithubException as exc: + if exc.status != 404: + raise dictionaries = codespell_config.get( "internal-dictionaries", checks_configuration.CODESPELL_DICTIONARIES_DEFAULT ) @@ -151,10 +163,9 @@ def _commits_messages( def _commits_spell( config: checks_configuration.PullRequestChecksConfiguration, commits: list[github.Commit.Commit], + spellcheck_cmd: list[str], ) -> tuple[bool, list[str]]: """Check the spelling of the commits body.""" - spellcheck_cmd = _get_codespell_command(config) - messages = [] success = True for commit in commits: @@ -189,11 +200,11 @@ def _commits_spell( def _pull_request_spell( - config: checks_configuration.PullRequestChecksConfiguration, pull_request: github.PullRequest.PullRequest + config: checks_configuration.PullRequestChecksConfiguration, + pull_request: github.PullRequest.PullRequest, + spellcheck_cmd: list[str], ) -> tuple[bool, list[str]]: """Check the spelling of the pull request title and message.""" - spellcheck_cmd = _get_codespell_command(config) - messages = [] with NamedTemporaryFile("w+t") as temp_file: temp_file.write(pull_request.title) @@ -302,9 +313,11 @@ async def process( commit for commit in pull_request.get_commits() ] - success_1, messages_1 = _commits_messages(context.module_config, commits) - success_2, messages_2 = _commits_spell(context.module_config, commits) - success_3, messages_3 = _pull_request_spell(context.module_config, pull_request) + with tempfile.NamedTemporaryFile("w+t", encoding="utf-8") as ignore_file: + spellcheck_cmd = _get_codespell_command(context, ignore_file) + success_1, messages_1 = _commits_messages(context.module_config, commits) + success_2, messages_2 = _commits_spell(context.module_config, commits, spellcheck_cmd) + success_3, messages_3 = _pull_request_spell(context.module_config, pull_request, spellcheck_cmd) success = success_1 and success_2 and success_3 message = "\n".join([*messages_1, *messages_2, *messages_3])