From 0c5f7d8de90d7a1301b730002cd1ecfcbb082db3 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 | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 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..2f542e5385a 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,27 +20,40 @@ _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[str], +) -> list[str]: """ Get the codespell command. """ - codespell_config = config.get("codespell", {}) - codespell_config = codespell_config if isinstance(codespell_config, dict) else {} + config = context.module_config + code_spell_config = config.get("codespell", {}) + code_spell_config = code_spell_config if isinstance(code_spell_config, dict) else {} command = ["codespell"] for spell_ignore_file in ( ".github/spell-ignore-words.txt", "spell-ignore-words.txt", ".spell-ignore-words.txt", ): - if os.path.exists(spell_ignore_file): - command.append(f"--ignore-words={spell_ignore_file}") - break - dictionaries = codespell_config.get( + try: + content = context.github_project.repo.get_contents(spell_ignore_file) + if isinstance(content, github.ContentFile.ContentFile): + ignore_file.write(content.decoded_content.decode("utf-8")) + ignore_file.flush() + command.append(f"--ignore-words={ignore_file.name}") + break + except github.GithubException as exc: + if exc.status != 404: + raise + dictionaries = code_spell_config.get( "internal-dictionaries", checks_configuration.CODESPELL_DICTIONARIES_DEFAULT ) if dictionaries: command.append("--builtin=" + ",".join(dictionaries)) - command += codespell_config.get("arguments", checks_configuration.CODESPELL_ARGUMENTS_DEFAULT) + command += code_spell_config.get("arguments", checks_configuration.CODESPELL_ARGUMENTS_DEFAULT) return command @@ -151,10 +165,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 +202,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 +315,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])