Skip to content

Commit

Permalink
Pulrequest check: Fix ignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jul 3, 2024
1 parent 0616456 commit fff1296
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions github_app_geo_project/module/pull_request/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import subprocess # nosec
import tempfile
from tempfile import NamedTemporaryFile
from typing import Any, cast

Expand All @@ -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"]
Expand All @@ -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
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])

Expand Down

0 comments on commit fff1296

Please sign in to comment.