Skip to content

Commit

Permalink
Replace linters with Ruff (#323)
Browse files Browse the repository at this point in the history
* Pin pycodestyle to 2.7.0

* Add Change fragment

* Change stable to 1.6

* Pin Meraki to a version that supports 3.8

* Implement Ruff

Replaces Bandit, Black, Flake8 and Pydocstyle.

* Remove bandit, black, flake8 and pydocstyle

From CI.

* Add back in the Dispatcher imports

* Add ruff to tests.

* Update pyproject.toml

Remove django-not-configured from the disable list as it is no longer needed.

* Revert unused-import auto-fix

* Revert unused import fix in migrations

* Update base.py

Add back in he pylint disable
  • Loading branch information
smk4664 authored Aug 29, 2024
1 parent dddb9ed commit c3471ed
Show file tree
Hide file tree
Showing 101 changed files with 529 additions and 850 deletions.
7 changes: 0 additions & 7 deletions .bandit.yml

This file was deleted.

4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

52 changes: 8 additions & 44 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on: # yamllint disable
- "pull_request"

jobs:
black:
ruff-format:
runs-on: "ubuntu-20.04"
env:
INVOKE_NAUTOBOT_CHATOPS_LOCAL: "True"
Expand All @@ -19,9 +19,9 @@ jobs:
uses: "networktocode/gh-action-setup-poetry-environment@v2"
with:
python-version: "3.10"
- name: "Linting: black"
run: "poetry run invoke black"
bandit:
- name: "Linting: ruff format"
run: "poetry run invoke ruff --action format"
ruff-lint:
runs-on: "ubuntu-20.04"
env:
INVOKE_NAUTOBOT_CHATOPS_LOCAL: "True"
Expand All @@ -32,40 +32,8 @@ jobs:
uses: "networktocode/gh-action-setup-poetry-environment@v2"
with:
python-version: "3.10"
- name: "Linting: bandit"
run: "poetry run invoke bandit"
needs:
- "black"
pydocstyle:
runs-on: "ubuntu-20.04"
env:
INVOKE_NAUTOBOT_CHATOPS_LOCAL: "True"
steps:
- name: "Check out repository code"
uses: "actions/checkout@v2"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v2"
with:
python-version: "3.10"
- name: "Linting: pydocstyle"
run: "poetry run invoke pydocstyle"
needs:
- "black"
flake8:
runs-on: "ubuntu-20.04"
env:
INVOKE_NAUTOBOT_CHATOPS_LOCAL: "True"
steps:
- name: "Check out repository code"
uses: "actions/checkout@v2"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v2"
with:
python-version: "3.10"
- name: "Linting: flake8"
run: "poetry run invoke flake8"
needs:
- "black"
- name: "Linting: ruff"
run: "poetry run invoke ruff"
yamllint:
runs-on: "ubuntu-20.04"
env:
Expand All @@ -79,8 +47,6 @@ jobs:
python-version: "3.10"
- name: "Linting: yamllint"
run: "poetry run invoke yamllint"
needs:
- "black"
pylint:
runs-on: "ubuntu-20.04"
steps:
Expand All @@ -95,16 +61,14 @@ jobs:
- name: "Linting: Pylint"
run: "poetry run invoke pylint"
needs:
- "bandit"
- "pydocstyle"
- "flake8"
- "ruff-lint"
- "yamllint"
unittest:
strategy:
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10"]
nautobot-version: ["1.6.2", "stable"]
nautobot-version: ["1.6.2", "1.6"]
runs-on: "ubuntu-20.04"
env:
INVOKE_NAUTOBOT_CHATOPS_PYTHON_VER: "${{ matrix.python-version }}"
Expand Down
1 change: 1 addition & 0 deletions changes/323.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pinning pycodestyle 2.7.0 to resolve issue with Upstream Testing.
2 changes: 1 addition & 1 deletion development/mattermost/nautobot_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Bootstrap script for Nautobot to allow Mattermost integration."""

from nautobot_chatops.models import AccessGrantTypeChoices, CommandTokenPlatformChoices, AccessGrant, CommandToken
from nautobot_chatops.models import AccessGrant, AccessGrantTypeChoices, CommandToken, CommandTokenPlatformChoices

for grant_type in AccessGrantTypeChoices.values():
AccessGrant.objects.update_or_create(
Expand Down
1 change: 1 addition & 0 deletions development/nautobot_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Nautobot development configuration file."""

import os
import sys

Expand Down
3 changes: 2 additions & 1 deletion nautobot_chatops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Nautobot plugin implementing a chatbot."""

try:
from importlib import metadata
except ImportError:
Expand All @@ -10,7 +11,6 @@
from django.conf import settings
from nautobot.extras.plugins import PluginConfig


_CONFLICTING_APP_NAMES = [
# App names that conflict with nautobot_chatops
"nautobot_plugin_chatops_aci",
Expand Down Expand Up @@ -141,6 +141,7 @@ def ready(self):
super().ready()
# pylint: disable=import-outside-toplevel
from nautobot_capacity_metrics import register_metric_func

from .metrics_app import metric_commands

register_metric_func(metric_commands)
Expand Down
1 change: 1 addition & 0 deletions nautobot_chatops/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Administrative capabilities for nautobot_chatops plugin."""

from django.contrib import admin

from .models import CommandLog


Expand Down
1 change: 1 addition & 0 deletions nautobot_chatops/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""REST API module for nautobot_chatops app."""
2 changes: 1 addition & 1 deletion nautobot_chatops/api/nested_serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Nested Serializers for ChatOps Plugin."""

from nautobot.core.api import WritableNestedSerializer
from rest_framework import serializers

from nautobot.core.api import WritableNestedSerializer
from nautobot_chatops.models import AccessGrant, CommandToken


Expand Down
3 changes: 1 addition & 2 deletions nautobot_chatops/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""API Serializers for ChatOps Plugin."""

from rest_framework import serializers

from nautobot.core.api import ValidatedModelSerializer
from rest_framework import serializers

from nautobot_chatops.models import AccessGrant, CommandToken

Expand Down
5 changes: 3 additions & 2 deletions nautobot_chatops/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.urls import include, path
from nautobot.core.api import OrderedDefaultRouter

from nautobot_chatops.api.views.generic import AccessGrantViewSet, CommandTokenViewSet, NautobotChatopsRootView
from nautobot_chatops.api.views.lookup import AccessLookupView

Expand All @@ -15,7 +16,7 @@
urlpatterns = [path("lookup/", AccessLookupView.as_view(), name="access_lookup")]

if _APP_CONFIG.get("enable_slack"):
from nautobot_chatops.api.views.slack import SlackSlashCommandView, SlackInteractionView, SlackEventAPIView
from nautobot_chatops.api.views.slack import SlackEventAPIView, SlackInteractionView, SlackSlashCommandView

urlpatterns += [
path("slack/slash_command/", SlackSlashCommandView.as_view(), name="slack_slash_command"),
Expand All @@ -38,7 +39,7 @@
]

if _APP_CONFIG.get("enable_mattermost"):
from nautobot_chatops.api.views.mattermost import MattermostSlashCommandView, MattermostInteractionView
from nautobot_chatops.api.views.mattermost import MattermostInteractionView, MattermostSlashCommandView

urlpatterns += [
path("mattermost/slash_command/", MattermostSlashCommandView.as_view(), name="mattermost_slash_command"),
Expand Down
5 changes: 3 additions & 2 deletions nautobot_chatops/api/views/generic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""API Views for Nautobot Chatops."""
from rest_framework.routers import APIRootView

from nautobot.core.api.views import ModelViewSet
from rest_framework.routers import APIRootView

from nautobot_chatops.api.serializers import AccessGrantSerializer, CommandTokenSerializer
from nautobot_chatops.models import AccessGrant, CommandToken
from nautobot_chatops.filters import AccessGrantFilterSet, CommandTokenFilterSet
from nautobot_chatops.models import AccessGrant, CommandToken


class NautobotChatopsRootView(APIRootView):
Expand Down
2 changes: 1 addition & 1 deletion nautobot_chatops/api/views/lookup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""API views for dynamic lookup of platform-specific data."""

from django.http import JsonResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.http import HttpResponseBadRequest, HttpResponseNotFound, JsonResponse
from django.views import View

from nautobot_chatops.dispatchers import Dispatcher
Expand Down
8 changes: 4 additions & 4 deletions nautobot_chatops/api/views/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from nautobot_chatops.workers import get_commands_registry, commands_help, parse_command_string
from nautobot_chatops.dispatchers.mattermost import MattermostDispatcher, Driver
from nautobot_chatops.utils import check_and_enqueue_command
from nautobot_chatops.choices import CommandTokenPlatformChoices
from nautobot_chatops.dispatchers.mattermost import Driver, MattermostDispatcher
from nautobot_chatops.metrics import signature_error_cntr
from nautobot_chatops.models import CommandToken
from nautobot_chatops.choices import CommandTokenPlatformChoices
from nautobot_chatops.utils import check_and_enqueue_command
from nautobot_chatops.workers import commands_help, get_commands_registry, parse_command_string

# pylint: disable=logging-fstring-interpolation

Expand Down
6 changes: 2 additions & 4 deletions nautobot_chatops/api/views/ms_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import json
import re

import requests
import jwt

import requests
from django.conf import settings
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from nautobot_chatops.workers import get_commands_registry, commands_help, parse_command_string
from nautobot_chatops.dispatchers.ms_teams import MSTeamsDispatcher
from nautobot_chatops.utils import check_and_enqueue_command

from nautobot_chatops.workers import commands_help, get_commands_registry, parse_command_string

APP_ID = settings.PLUGINS_CONFIG["nautobot_chatops"]["microsoft_app_id"]

Expand Down
4 changes: 2 additions & 2 deletions nautobot_chatops/api/views/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from django.views.decorators.csrf import csrf_exempt
from slack_sdk import WebClient

from nautobot_chatops.workers import get_commands_registry, commands_help, parse_command_string
from nautobot_chatops.dispatchers.slack import SlackDispatcher
from nautobot_chatops.utils import check_and_enqueue_command
from nautobot_chatops.metrics import signature_error_cntr
from nautobot_chatops.utils import check_and_enqueue_command
from nautobot_chatops.workers import commands_help, get_commands_registry, parse_command_string

# pylint: disable=logging-fstring-interpolation

Expand Down
6 changes: 2 additions & 4 deletions nautobot_chatops/api/views/webex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from webexteamssdk import WebexTeamsAPI
from webexteamssdk.exceptions import AccessTokenError, ApiError

from nautobot_chatops.workers import get_commands_registry, commands_help, parse_command_string
from nautobot_chatops.dispatchers.webex import WebexDispatcher
from nautobot_chatops.dispatchers.webex import WEBEX_CONFIG
from nautobot_chatops.dispatchers.webex import WEBEX_CONFIG, WebexDispatcher
from nautobot_chatops.utils import check_and_enqueue_command
from nautobot_chatops.workers import commands_help, get_commands_registry, parse_command_string

logger = logging.getLogger(__name__)

Expand Down
6 changes: 2 additions & 4 deletions nautobot_chatops/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
# pylint: disable=line-too-long
ACCESS_GRANT_VALUE_HELP_TEXT = "Corresponding ID value to grant access to.<br>Enter <tt>*</tt> to grant access to all organizations, channels, or users"

COMMAND_TOKEN_COMMENT_HELP_TEXT = "Optional: Enter description of token" # nosec - skips Bandit B105 error
COMMAND_TOKEN_TOKEN_HELP_TEXT = (
"Token given by chat platform for signing or command validation" # nosec - skips Bandit B105 error
)
COMMAND_TOKEN_COMMENT_HELP_TEXT = "Optional: Enter description of token" # noqa: S105
COMMAND_TOKEN_TOKEN_HELP_TEXT = "Token given by chat platform for signing or command validation" # noqa: S105
7 changes: 3 additions & 4 deletions nautobot_chatops/dispatchers/adaptive_cards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Dispatcher subclass for chat platforms that use Adaptive Cards (https://adaptivecards.io/)."""

from .base import Dispatcher

# pylint: disable=abstract-method
Expand Down Expand Up @@ -152,9 +153,7 @@ def prompt_for_text(self, action_id, help_text, label, title="Your attention ple
blocks = [self.markdown_block(help_text), self.actions_block("TODO", [textentry, buttons])]
return self.send_blocks(blocks, ephemeral=True, title=title)

def prompt_from_menu(
self, action_id, help_text, choices, default=(None, None), confirm=False, offset=0
): # pylint: disable=too-many-arguments
def prompt_from_menu(self, action_id, help_text, choices, default=(None, None), confirm=False, offset=0): # pylint: disable=too-many-arguments
"""Prompt the user to make a selection from a menu of choices.
Args:
Expand Down Expand Up @@ -201,7 +200,7 @@ def select_element(self, action_id, choices, default=(None, None), confirm=False
Args:
action_id (str): Identifying string to associate with this element
choices (list): List of (display, value) tuples
default (tuple: Default (display, value) to preselect
default (tuple): Default (display, value) to preselect
confirm (bool): If true (and the platform supports it), prompt the user to confirm their selection
"""
return {
Expand Down
21 changes: 10 additions & 11 deletions nautobot_chatops/dispatchers/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Generic base class modeling the API for sending messages to a generic chat platform."""

import logging
from typing import Dict, Optional
from django.templatetags.static import static
from django.core.cache import cache
from django.conf import settings

from django.conf import settings
from django.core.cache import cache
from django.templatetags.static import static
from texttable import Texttable

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -93,16 +94,16 @@ def subclasses(cls):
# TODO: this should be dynamic using entry_points
# pylint: disable=import-outside-toplevel, unused-import, cyclic-import
if _APP_CONFIG.get("enable_slack"):
from .slack import SlackDispatcher
from .slack import SlackDispatcher # noqa: F401

if _APP_CONFIG.get("enable_ms_teams"):
from .ms_teams import MSTeamsDispatcher
from .ms_teams import MSTeamsDispatcher # noqa: F401

if _APP_CONFIG.get("enable_webex"):
from .webex import WebexDispatcher
from .webex import WebexDispatcher # noqa: F401

if _APP_CONFIG.get("enable_mattermost"):
from .mattermost import MattermostDispatcher
from .mattermost import MattermostDispatcher # noqa: F401

subclasses = set()
classes = [cls]
Expand Down Expand Up @@ -268,9 +269,7 @@ def prompt_for_text(self, action_id, help_text, label, title="Your attention ple
"""
raise NotImplementedError

def prompt_from_menu(
self, action_id, help_text, choices, default=(None, None), confirm=False, offset=0
): # pylint: disable=too-many-arguments
def prompt_from_menu(self, action_id, help_text, choices, default=(None, None), confirm=False, offset=0): # pylint: disable=too-many-arguments
"""Prompt the user to make a selection from a menu of choices.
Args:
Expand Down Expand Up @@ -330,7 +329,7 @@ def select_element(self, action_id, choices, default=(None, None), confirm=False
Args:
action_id (str): Identifying string to associate with this element
choices (list): List of (display, value) tuples
default (tuple: Default (display, value) to preselect
default (tuple): Default (display, value) to preselect
confirm (bool): If true (and the platform supports it), prompt the user to confirm their selection
"""
raise NotImplementedError
Expand Down
Loading

0 comments on commit c3471ed

Please sign in to comment.