0
votes

Target: use custom pylint rules with SonarQube UI

I created a custom rule for pylint for python selenium webdriver. To check if XPATH locator is used.

#webdriver_locator_checker.py
from pylint.interfaces import IRawChecker
from pylint.checkers import BaseChecker

XPATH_LOCATORS = [
    b'XPATH',
    b'xpath'
]


class WebdriverLocatorChecker(BaseChecker):
    """Checks if XPATH locator is used."""
    __implements__ = IRawChecker

    name = 'webdriver-xpath-locator'
    msgs = {
        'WD001': (
            'Locator strategy by xpath is not recommended',
            'xpath-locator-not-recommended',
            'Xpath locators are not recommended, use css locators instead or use class name, id or name if possible.'
        ),
    }
    options = ()

    def process_module(self, node):
        with node.stream() as stream:
            for (lineno, line) in enumerate(stream):
                for locator in XPATH_LOCATORS:
                    if locator in line:
                        self.add_message('xpath-locator-not-recommended', line=lineno + 1)
                        break

def register(linter):
    """required method to auto register this checker"""
    linter.register_checker(WebdriverLocatorChecker(linter))

In root directory I added .pylintrc file with (to use custom checker):

#.pylintrc
load-plugins=webdriver_locator_checker

then I linted my files to get a pylint report: (pylint-checkers - dir with webdriver_locator_checker.py)

PYTHONPATH=./pylint-checkers/ pylint ./path/to/locators.py | tee ./pylint.out

report file was created correctly with desired rule detection:

#pylint.out
path/to/locators.py:31: [WD001(xpath-locator-not-recommended), ] Locator strategy by xpath is not recommended

In the project I set up a sonar config file with (to run sonnar-scanner):

#sonar-project.properties
sonar.python.pylint.reportPath=pylint.out

Then I ran sonnar-scanner for SonarQube:

sonar-scanner   -Dsonar.projectKey=Project1 -Dsonar.host.url=http://localhost:9000   -Dsonar.login=xxxxxxxxxxxxxxxxx

but the terminal result contains (you need at least 1 pylint rule in a sonar profile to make it working with pylint report):

WARN: Pylint rule 'WD001' is unknown in Sonar

And this WD001 detection is not visible in SonarQube UI.

Question: How to make this rule visible in SonarQube Analysis? (WD001 is custom id)

I use SonarQube local docker SonarQube server 7.9.1

SonarQube Scanner 4.0.0.1744

python 3.6.8

pylint 2.4.2

2

2 Answers

0
votes

possible with issue json description and assign report file to sonar. variable https://docs.sonarqube.org/latest/analysis/generic-issue/

0
votes

Pylint issues can be imported only if their pylint id is known by the SonarQube python analyzer. This is a limitation of the way pylint is currently integrated with SonarQube. That will be addressed when pylint issues are imported as external issues into SonarQube. In the meantime, you have to rely on workarounds such as the one you found.