Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pylint 3 #1

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# pylint-sonarjson
# pylint-sonarjson-catlab

This project is a fork of [pylint-sonarjson](https://github.com/omegacen/pylint-sonarjson), which is not maintained anymore.

A PyLint plugin that can output to [SonarQube-importable JSON](https://docs.sonarqube.org/latest/analysis/generic-issue/)
with configurable issue severity, effort, and type.

This is useful because when importing PyLint's
[parsable output](https://pylint.pycqa.org/en/latest/user_guide/output.html#output-options)
via [SonarQube mechanism for third-party issues](https://docs.sonarqube.org/latest/analysis/external-issues/)
all the severities are set to `MAJOR`. With `pylint-sonarjson` you can configure the
all the severities are set to `MAJOR`. With `pylint-sonarjson-catlab` you can configure the
issue severity per PyLint message ID, and import that as generic JSON in SonarQube.

## Usage

```
$ pylint \
--load-plugins=pylint_sonarjson \
--load-plugins=pylint_sonarjson_catlab \
--output-format=sonarjson \
--sonar-rules=<msg_id>:<severity>[:<effort>[:<type>]],... \
--sonar-default-severity=<severity> \
Expand Down Expand Up @@ -49,7 +51,7 @@ For example:

```
$ pylint \
--load-plugins=pylint_sonarjson \
--load-plugins=pylint_sonarjson_catlab \
--output-format=sonarjson \
--sonar-rules=C0114:INFO:10,C0328:MINOR:1 \
my_file.py
Expand Down Expand Up @@ -82,13 +84,13 @@ Output:
This output, when saved to a file, can be imported into SonarQube as follows:

```
$ sonar-scanner -Dsonar.externalIssuesReportPaths=<path_to_pylint_sonarjson_log>
$ sonar-scanner -Dsonar.externalIssuesReportPaths=<path_to_pylint_sonarjson_catlab_log>
```

## Installation

```
pip install pylint-sonarjson
pip install pylint-sonarjson_catlab
```

## Configuration via pylintrc or pyproject.toml
Expand All @@ -102,7 +104,7 @@ command line option:
```
[MAIN]

load-plugins=pylint_sonarjson
load-plugins=pylint_sonarjson_catlab


[REPORTS]
Expand Down
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
long_description = fh.read()

setuptools.setup(
name="pylint-sonarjson",
version="1.0.6",
author="Teake Nutma",
name="pylint-sonarjson-catlab",
version="2.0.0",
author="Teake Nutma, Topin2001",
description="A PyLint plugin that can output to SonarQube-importable JSON",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/omegacen/pylint-sonarjson",
url="https://github.com/cnescatlab/pylint-sonarjson-catlab ",
packages=setuptools.find_packages(where='src'),
package_dir={'': 'src'},
license_file='LICENSE',
Expand All @@ -19,9 +19,9 @@
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
python_requires='>=3.7',
install_requires=[
"pylint-plugin-utils>=0.7",
"pylint>=2.4,<3"
"pylint>=3.0.0,<4"
],
)
2 changes: 0 additions & 2 deletions src/pylint_sonarjson/sonarjson_reporter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from typing import Optional, List

from pylint.interfaces import IReporter
from pylint.message import Message
from pylint.reporters.base_reporter import BaseReporter
from pylint.reporters.ureports.nodes import Section
Expand All @@ -13,7 +12,6 @@
class SonarJSONReporter(BaseReporter):
"""Report messages and layouts in JSON that SonarQube can import."""

__implements__ = IReporter
name = "sonarjson"
extension = "json"

Expand Down
19 changes: 8 additions & 11 deletions src/pylint_sonarjson/sonaroptions_checker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict, Set
from sys import stderr
import sys

from pylint.checkers import BaseChecker
from pylint.exceptions import InvalidArgsError
from pylint.interfaces import IAstroidChecker
from pylint.message import Message

DEFAULT_SEVERITY = "MINOR"
Expand All @@ -26,8 +25,6 @@
class SonarOptionsChecker(BaseChecker):
"""Dummy checker that only registers options."""

__implements__ = IAstroidChecker

name = "SonarQube JSON output"
level = 0
options = (
Expand Down Expand Up @@ -104,28 +101,28 @@ def __init__(self, *args, **kwargs):
self._types: Dict[str, str] = {}

def severity(self, msg: Message):
return self._severities.get(msg.msg_id, self.option_value('sonar-default-severity'))
return self._severities.get(msg.msg_id, self._option_value('sonar-default-severity'))

def effort(self, msg: Message):
return self._efforts.get(msg.msg_id, self.option_value('sonar-default-effort'))
return self._efforts.get(msg.msg_id, self._option_value('sonar-default-effort'))

def type(self, msg: Message):
return self._types.get(msg.msg_id, self.option_value('sonar-default-type'))
return self._types.get(msg.msg_id, self._option_value('sonar-default-type'))

def load_configuration(self):
for sonar_rule in self.option_value('sonar-rules'):
for sonar_rule in self._option_value('sonar-rules'):
self._parse_sonar_rule(sonar_rule)
if self.option_value('only-enable-sonar-rules'):
if self._option_value('only-enable-sonar-rules'):
self._only_enable_sonar_rules()

def _parse_sonar_rule(self, sonar_rule: str):
split = sonar_rule.split(":")
msg_id = split[0]
if not self._is_valid_msg_id(msg_id):
if self.option_value('halt-on-invalid-sonar-rules'):
if self._option_value('halt-on-invalid-sonar-rules'):
raise InvalidArgsError(f"{msg_id} is not a known Pylint message id.")
else:
print(f"Disabling {msg_id} since it is not a known Pylint message id.", file=stderr)
print(f"Disabling {msg_id} since it is not a known Pylint message id.", file=sys.stderr)
return
self._msg_ids.add(msg_id)
if len(split) > 1:
Expand Down