From 8e6e9c31f63bbf65d2d4b9d3d8dc085ec653d535 Mon Sep 17 00:00:00 2001 From: Noelle Wang <73260931+No767@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:05:54 -0700 Subject: [PATCH] Optimize pre-commit workflow (#218) --- .pre-commit-config.yaml | 48 ------------------------------- bot/cogs/ext/prometheus.py | 1 - bot/cogs/meta.py | 4 ++- bot/cogs/pride_profiles.py | 1 - bot/cogs/pronouns.py | 7 ++++- bot/libs/utils/pages/paginator.py | 5 +++- bot/libs/utils/tree.py | 4 ++- docs/source/guides/dev/setup.rst | 2 +- lefthook.yml | 14 +++++++++ pyproject.toml | 42 ++++++++++++++++++++++----- requirements-dev.txt | 3 +- taskfile.yml | 19 +++++++++++- 12 files changed, 86 insertions(+), 64 deletions(-) delete mode 100644 .pre-commit-config.yaml create mode 100644 lefthook.yml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 74e7489..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,48 +0,0 @@ -default_language_version: - python: python3 -files: '.py' -exclude: ".env,.yml,.gitignore,.git,.md,.txt" -default_stages: [pre-commit] -repos: -- repo: https://github.com/PyCQA/bandit - rev: 1.7.10 - hooks: - - id: bandit - args: ["-c", "pyproject.toml"] - name: Bandit - stages: [pre-commit] - additional_dependencies: ["bandit[toml]"] - -- repo: https://github.com/psf/black - rev: 24.10.0 - hooks: - - id: black - name: Black - stages: [pre-commit] - -- repo: https://github.com/PyCQA/autoflake - rev: v2.3.1 - hooks: - - id: autoflake - args: ["--in-place", "--remove-unused-variables", "--recursive"] - name: AutoFlake - description: "Format with AutoFlake" - stages: [pre-commit] - -- repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.7.0 - hooks: - - id: ruff - name: Ruff - args: ["--fix", "--exit-non-zero-on-fix", "--config", "pyproject.toml"] - stages: [pre-commit] - -- repo: https://github.com/PyCQA/isort - rev: 5.13.2 - hooks: - - id: isort - name: ISort - description: "Format with Isort" - stages: [pre-commit] - - diff --git a/bot/cogs/ext/prometheus.py b/bot/cogs/ext/prometheus.py index bb4770d..c9cb3e5 100644 --- a/bot/cogs/ext/prometheus.py +++ b/bot/cogs/ext/prometheus.py @@ -8,7 +8,6 @@ from discord.ext import commands, tasks try: - from prometheus_async.aio.web import start_http_server from prometheus_client import Counter, Enum, Gauge, Info, Summary except ImportError: diff --git a/bot/cogs/meta.py b/bot/cogs/meta.py index 8c7d008..ee2a1fb 100644 --- a/bot/cogs/meta.py +++ b/bot/cogs/meta.py @@ -86,7 +86,9 @@ async def about(self, interaction: discord.Interaction) -> None: revisions = self.get_last_commits() embed = Embed() - embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.display_avatar.url) # type: ignore + embed.set_author( + name=self.bot.user.name, icon_url=self.bot.user.display_avatar.url + ) # type: ignore embed.title = "Support Server Invite" embed.url = "https://discord.gg/ns3e74frqn" desc = [ diff --git a/bot/cogs/pride_profiles.py b/bot/cogs/pride_profiles.py index 3c2c76f..ea6ea12 100644 --- a/bot/cogs/pride_profiles.py +++ b/bot/cogs/pride_profiles.py @@ -213,7 +213,6 @@ def disambiguate(self, rows) -> str: async def send_profile( self, interaction: discord.Interaction, profile: PrideProfile ) -> None: - query = """ UPDATE pride_profiles SET views = views + 1 diff --git a/bot/cogs/pronouns.py b/bot/cogs/pronouns.py index 1479b58..6a6fbba 100644 --- a/bot/cogs/pronouns.py +++ b/bot/cogs/pronouns.py @@ -376,7 +376,12 @@ def format_info(self, entry: ProfileInfo) -> PartialProfileInfo: for sub_entry in entry.pronouns ), flags=", ".join(flag for flag in entry.flags), - circle="\n".join(f"- {self.determine_mutual(entity['username'], entity['circleMutual'])}\n\t- Relationship: {entity['relationship']}" for entity in entry.meta.circle) if len(entry.meta.circle) != 0 else "None", # type: ignore + circle="\n".join( + f"- {self.determine_mutual(entity['username'], entity['circleMutual'])}\n\t- Relationship: {entity['relationship']}" + for entity in entry.meta.circle + ) + if len(entry.meta.circle) != 0 + else "None", # type: ignore ) async def format_page(self, menu: ProfilePages, page): diff --git a/bot/libs/utils/pages/paginator.py b/bot/libs/utils/pages/paginator.py index 3598231..b672050 100644 --- a/bot/libs/utils/pages/paginator.py +++ b/bot/libs/utils/pages/paginator.py @@ -364,7 +364,10 @@ def _update_labels(self, page_number: int) -> None: async def start( self, *, content: Optional[str] = None, ephemeral: bool = False ) -> None: - if self.check_embeds and not self.ctx.channel.permissions_for(self.ctx.me).embed_links: # type: ignore + if ( + self.check_embeds + and not self.ctx.channel.permissions_for(self.ctx.me).embed_links + ): # type: ignore await self.ctx.send( "Bot does not have embed links permission in this channel.", ephemeral=True, diff --git a/bot/libs/utils/tree.py b/bot/libs/utils/tree.py index 578784d..d885d45 100644 --- a/bot/libs/utils/tree.py +++ b/bot/libs/utils/tree.py @@ -66,7 +66,9 @@ async def on_error( elif isinstance(error, app_commands.CommandInvokeError): original = error.original if not isinstance(original, discord.HTTPException): - bot.logger.exception("In %s: ", interaction.command.qualified_name, exc_info=original) # type: ignore + bot.logger.exception( + "In %s: ", interaction.command.qualified_name, exc_info=original + ) # type: ignore await interaction.response.send_message( embed=FullErrorEmbed(error), ephemeral=True ) diff --git a/docs/source/guides/dev/setup.rst b/docs/source/guides/dev/setup.rst index df8d2b2..7cf5964 100644 --- a/docs/source/guides/dev/setup.rst +++ b/docs/source/guides/dev/setup.rst @@ -33,7 +33,7 @@ Local Setup .. code-block:: bash pip install -r requirements-dev.txt \ - && pre-commit install + && lefthook install 5. Copy over the ``config-example.yml`` template over to the ``bot`` directory. Modify the values as appropriate. diff --git a/lefthook.yml b/lefthook.yml new file mode 100644 index 0000000..f62cc97 --- /dev/null +++ b/lefthook.yml @@ -0,0 +1,14 @@ +pre-commit: + commands: + bandit: + glob: "*.py" + staged_files: true + run: bandit {staged_files} -c pyproject.toml + ruff-check: + glob: "*.py" + staged_files: true + run: ruff check {staged_files} --fix --exit-non-zero-on-fix --config pyproject.toml + ruff-fmt: + glob: "*.py" + staged_files: true + run: ruff format {staged_files} --config pyproject.toml \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 750f27c..36473c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,12 +6,6 @@ readme = "README.md" license = {file = "LICENSE"} requires-python = ">=3.9,<4.0" -[tool.isort] -profile = 'black' -combine_as_imports = true -combine_star = true -line_length = 80 - [tool.pyright] include = ["bot/**"] exclude = [ @@ -29,9 +23,43 @@ reportUnnecessaryTypeIgnoreComment = "warning" [tool.bandit] skips = ["B311", "B101"] +[tool.ruff] +line-length = 88 +indent-width = 4 + [tool.ruff.lint] -ignore = ["E501", "N999", "E402", "S101", "ASYNC109"] +ignore = [ + "E501", + "N999", + "E402", + "S311", + "ASYNC109", + "S101", + + # These are recommended by Ruff if the formatter is to be used: https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules + "W191", + "E111", + "E114", + "E117", + "D206", + "D300", + "Q000", + "Q001", + "Q002", + "Q003", + "COM812", + "COM819", + "ISC001", + "ISC002" + ] select = ["E", "F", "N", "ASYNC", "S", "ERA"] +fixable = ["ALL"] + +[tool.ruff.lint.isort] +combine-as-imports = true + +[tool.ruff.format] +docstring-code-format = true [tool.pytest.ini_options] minversion = "8.0" diff --git a/requirements-dev.txt b/requirements-dev.txt index bbaefbe..2e28358 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,8 +1,9 @@ -r requirements.txt # Development libraries -pre-commit>=3.5.0,<5 +lefthook>=1.7.22,<2 pyright>=1.1.355,<2 +bandit[toml]>=1.7.10,<2 ruff>=0.3.4,<1 tox>=4.14.2,<5 diff --git a/taskfile.yml b/taskfile.yml index 8e6a366..f78203b 100644 --- a/taskfile.yml +++ b/taskfile.yml @@ -11,4 +11,21 @@ tasks: preconditions: - test -f docker/docker-compose.dev.yml cmds: - - docker compose -f docker/docker-compose.dev.yml stop \ No newline at end of file + - docker compose -f docker/docker-compose.dev.yml stop + + bot: + preconditions: + - test -f bot/config.yml + cmds: + - python3 bot/catherinebot.py + silent: true + + check: + cmds: + - ruff check bot --fix --exit-non-zero-on-fix --config pyproject.toml + silent: true + + fmt: + cmds: + - ruff format bot --config pyproject.toml + silent: true \ No newline at end of file