Skip to content

Commit

Permalink
Merge pull request #12 from PaperMtn/feature/pylint-config
Browse files Browse the repository at this point in the history
Feature/pylint config
  • Loading branch information
PaperMtn authored Nov 11, 2024
2 parents 671688b + a4fc204 commit 57e7238
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 59 deletions.
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ pylint = "^3.3.1"
[tool.poetry.scripts]
gitlab-watchman = "gitlab_watchman:main"

[tool.pylint.messages_control]
max-line-length = 120
max-attributes = 10
max-args = 10
disable = [
"missing-module-docstring",
"too-few-public-methods",
"arguments-differ",
"logging-fstring-interpolation",
"no-else-return",
"no-else-raise",
"inconsistent-return-statements",
"broad-exception-caught",
"duplicate-code",
]


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2 changes: 2 additions & 0 deletions src/gitlab_watchman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def validate_variables() -> bool:
return True


# pylint: disable=too-many-locals, missing-function-docstring, global-variable-undefined
# pylint: disable=too-many-branches, disable=too-many-statements
def main():
global OUTPUT_LOGGER
try:
Expand Down
2 changes: 1 addition & 1 deletion src/gitlab_watchman/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import main

main()
main()
1 change: 0 additions & 1 deletion src/gitlab_watchman/clients/gitlab_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
GitlabSearchError,
GitlabHttpError
)

from gitlab_watchman.exceptions import (
GitLabWatchmanAuthenticationError,
GitLabWatchmanGetObjectError,
Expand Down
3 changes: 1 addition & 2 deletions src/gitlab_watchman/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict, Any

class GitLabWatchmanError(Exception):
""" Base class for exceptions in GitLab Watchman.
"""


class ElasticsearchMissingError(GitLabWatchmanError):
""" Exception raised when Elasticsearch is not enabled on the instance.
"""
Expand Down
51 changes: 32 additions & 19 deletions src/gitlab_watchman/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@


class StdoutLogger:
""" Class to log to stdout """
def __init__(self, **kwargs):
self.debug = kwargs.get('debug')
self.print_header()
init()

# pylint: disable=too-many-branches
def log(self,
mes_type: str,
msg_level: str,
message: Any,
**kwargs) -> None:
""" Log to stdout
Args:
msg_level: Level message to log
message: Message data to log
"""

notify_type = kwargs.get('notify_type')
scope = kwargs.get('scope')

if not self.debug and mes_type == 'DEBUG':
if not self.debug and msg_level == 'DEBUG':
return

if dataclasses.is_dataclass(message):
Expand All @@ -44,7 +52,7 @@ def log(self,
f' URL: {message.get("kas").get("externalUrl")} \n'\
f' VERSION: {message.get("kas").get("version")} \n' \
f' ENTERPRISE: {message.get("enterprise")}'
mes_type = 'INSTANCE'
msg_level = 'INSTANCE'
if notify_type == "user":
message = f'USER: \n' \
f' ID: {message.get("id")} \n' \
Expand All @@ -57,7 +65,7 @@ def log(self,
f' CAN_CREATE_GROUP: {message.get("can_create_group")} \n'\
f' CAN_CREATE_PROJECT: {message.get("can_create_project")} \n' \
f' 2FA_ENABLED: {message.get("two_factor_enabled")}'
mes_type = 'USER'
msg_level = 'USER'
if notify_type == "token":
message = f'PERSONAL_ACCESS_TOKEN: \n' \
f' ID: {message.get("id")} \n' \
Expand All @@ -68,7 +76,7 @@ def log(self,
f' LAST_USED_AT: {message.get("last_used_at")} \n' \
f' ACTIVE: {message.get("active")} \n'\
f' EXPIRY: {message.get("expires_at", "Never")}'
mes_type = 'WARNING'
msg_level = 'WARNING'
if notify_type == "result":
if scope == 'blobs':
message = 'SCOPE: Blob' \
Expand Down Expand Up @@ -145,12 +153,12 @@ def log(self,
f' URL: {message.get("snippet").get("web_url")} \n' \
f' POTENTIAL_SECRET: {message.get("match_string")} \n' \
f' -----'
mes_type = 'RESULT'
msg_level = 'RESULT'
try:
self.log_to_stdout(message, mes_type)
self.log_to_stdout(message, msg_level)
except Exception as e:
print(e)
self.log_to_stdout(message, mes_type)
self.log_to_stdout(message, msg_level)

def log_to_stdout(self,
message: Any,
Expand Down Expand Up @@ -236,7 +244,10 @@ def log_to_stdout(self,
sys.exit(1)
print('Formatting error')

def print_header(self) -> None:
@staticmethod
def print_header() -> None:
""" Prints the header for the logger"""

print(" ".ljust(79) + Style.BRIGHT)

print(Fore.LIGHTRED_EX + Style.BRIGHT +
Expand Down Expand Up @@ -265,6 +276,7 @@ def print_header(self) -> None:


class JSONLogger(Logger):
""" Custom logger class for JSON logging"""
def __init__(self, name: str = 'gitlab_watchman', **kwargs):
super().__init__(name)
self.notify_format = logging.Formatter(
Expand All @@ -290,41 +302,42 @@ def __init__(self, name: str = 'gitlab_watchman', **kwargs):

def log(self,
level: str,
log_data: str or Dict,
msg: str or Dict,
**kwargs):
if level.upper() == 'NOTIFY':
self.handler.setFormatter(self.notify_format)
self.logger.info(
json.dumps(
log_data,
msg,
cls=EnhancedJSONEncoder),
extra={
'scope': kwargs.get('scope', ''),
'type': kwargs.get('detect_type', ''),
'severity': kwargs.get('severity', '')})
elif level.upper() == 'INFO':
self.handler.setFormatter(self.info_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
elif level.upper() == 'DEBUG':
self.handler.setFormatter(self.info_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
elif level.upper() == 'SUCCESS':
self.handler.setFormatter(self.success_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
elif level.upper() == 'INSTANCE':
self.handler.setFormatter(self.instance_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
elif level.upper() == 'USER':
self.handler.setFormatter(self.user_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
elif level.upper() == 'TOKEN':
self.handler.setFormatter(self.token_format)
self.logger.info(json.dumps(log_data))
self.logger.info(json.dumps(msg))
else:
self.handler.setFormatter(self.info_format)
self.logger.critical(log_data)
self.logger.critical(msg)


# pylint: disable=missing-class-docstring
class IsDataclass(Protocol):
__dataclass_fields__: ClassVar[Dict]

Expand Down Expand Up @@ -360,4 +373,4 @@ def init_logger(logging_type: str, debug: bool) -> JSONLogger | StdoutLogger:

if not logging_type or logging_type == 'stdout':
return StdoutLogger(debug=debug)
return JSONLogger(debug=debug)
return JSONLogger(debug=debug)
2 changes: 1 addition & 1 deletion src/gitlab_watchman/models/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


@dataclass(slots=True)
class Blob(object):
class Blob:
""" Class that defines Blob objects for GitLab blobs"""

basename: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


@dataclass(slots=True)
class Commit(object):
# pylint: disable=too-many-instance-attributes
class Commit:
""" Class that defines File objects for GitLab files"""

id: str
Expand Down
2 changes: 1 addition & 1 deletion src/gitlab_watchman/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


@dataclass(slots=True)
class File(object):
class File:
""" Class that defines File objects for GitLab files"""

file_name: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


@dataclass(slots=True)
class Group(object):
# pylint: disable=too-many-instance-attributes
class Group:
""" Class that defines User objects for GitLab groups"""

id: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


@dataclass(slots=True)
class Issue(object):
# pylint: disable=too-many-instance-attributes
class Issue:
""" Class that defines Issues objects for GitLab issues"""

id: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


@dataclass(slots=True)
class MergeRequest(object):
# pylint: disable=too-many-instance-attributes
class MergeRequest:
""" Class that defines MergeRequest objects for GitLab merge requests"""

id: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/milestone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


@dataclass(slots=True)
class Milestone(object):
# pylint: disable=too-many-instance-attributes
class Milestone:
""" Class that defines Milestone objects for GitLab milestones"""

id: str
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab_watchman/models/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


@dataclass(slots=True)
class Note(object):
# pylint: disable=too-many-instance-attributes
class Note:
""" Class that defines User objects for GitLab notes"""

id: str
Expand Down
5 changes: 3 additions & 2 deletions src/gitlab_watchman/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


@dataclass(slots=True)
class Namespace(object):
class Namespace:
""" Class that defines Namespace objects for GitLab Projects"""
id: str
name: str
path: str
Expand All @@ -20,7 +21,7 @@ class Namespace(object):


@dataclass(slots=True)
class Project(object):
class Project:
""" Class that defines User objects for GitLab projects"""

id: str
Expand Down
15 changes: 7 additions & 8 deletions src/gitlab_watchman/models/signature.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import datetime
from typing import Any, Dict
from typing import Any, Dict, List
from dataclasses import dataclass
from typing import List


@dataclass(slots=True)
class TestCases(object):
class TestCases:
""" Class that holds test cases for a signature """
match_cases: list
fail_cases: list


@dataclass(frozen=True, slots=True)
# pylint: disable=too-many-instance-attributes
class Signature:
""" Class that handles loaded signature objects. Signatures
define what to search for in Slack and where to search for it.
Expand All @@ -36,15 +37,13 @@ def __post_init__(self):
raise TypeError(f'Expected `status` to be of type str, received {type(self.status).__name__}')
if self.author and not isinstance(self.author, str):
raise TypeError(f'Expected `author` to be of type str, received {type(self.author).__name__}')
if self.date and not (isinstance(self.date, datetime.date)
or isinstance(self.date, str)
or isinstance(self.date, datetime.datetime)):
if self.date and not isinstance(self.date, (datetime.date, datetime.datetime, str)):
raise TypeError(f'Expected `date` to be of type str, received {type(self.date).__name__}')
if self.version and not isinstance(self.version, str):
raise TypeError(f'Expected `version` to be of type str, received {type(self.version).__name__}')
if self.description and not isinstance(self.description, str):
raise TypeError(f'Expected `description` to be of type str, received {type(self.description).__name__}')
if self.severity and not (isinstance(self.severity, int) or isinstance(self.severity, str)):
if self.severity and not isinstance(self.severity, (int, str)):
raise TypeError(f'Expected `severity` to be of type int or str, received {type(self.severity).__name__}')
if self.scope and not isinstance(self.scope, list):
raise TypeError(f'Expected `scope` to be of type list, received {type(self.scope).__name__}')
Expand Down Expand Up @@ -79,4 +78,4 @@ def create_from_dict(signature_dict: Dict[str, Any]) -> Signature:
fail_cases=signature_dict.get('test_cases', {}).get('fail_cases')
),
search_strings=signature_dict.get('watchman_apps', {}).get('gitlab', {}).get('search_strings'),
patterns=signature_dict.get('patterns'))
patterns=signature_dict.get('patterns'))
Loading

0 comments on commit 57e7238

Please sign in to comment.