目标:在SonarQube UI中使用自定义pylint规则
我为python硒webdriver的pylint创建了一个自定义规则。要检查是否使用了XPATH定位器。
#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))
在根目录中,我添加了.pylintrc
文件(以使用自定义检查器):
#.pylintrc
load-plugins=webdriver_locator_checker
然后我整理了文件以获得pylint报告: (pylint-checkers-带有webdriver_locator_checker.py的目录)
PYTHONPATH=./pylint-checkers/ pylint ./path/to/locators.py | tee ./pylint.out
已使用所需的规则检测正确创建了报告文件:
#pylint.out
path/to/locators.py:31: [WD001(xpath-locator-not-recommended), ] Locator strategy by xpath is not recommended
在项目中,我使用(运行Sonnar-scanner)设置了声纳配置文件:
#sonar-project.properties
sonar.python.pylint.reportPath=pylint.out
然后我为SonarQube运行了Sonnar-scanner:
sonar-scanner -Dsonar.projectKey=Project1 -Dsonar.host.url=http://localhost:9000 -Dsonar.login=xxxxxxxxxxxxxxxxx
但是终端结果包含(您在声纳配置文件中至少需要1个pylint规则才能使其与pylint报告一起使用):
WARN: Pylint rule 'WD001' is unknown in Sonar
此WD001检测在SonarQube UI中不可见。
问题:如何使此规则在SonarQube Analysis中可见? (WD001是自定义ID)
我使用SonarQube本地docker SonarQube服务器7.9.1
SonarQube扫描仪4.0.0.1744
python 3.6.8
pylint 2.4.2
答案 0 :(得分:0)
可能带有问题json描述,并将报告文件分配给声纳。变量 https://docs.sonarqube.org/latest/analysis/generic-issue/
答案 1 :(得分:0)
只有在SonarQube python分析器知道了pylint id的情况下,才能导入pylint问题。这是pylint当前与SonarQube集成方式的限制。将会解决when pylint issues are imported as external issues into SonarQube。同时,您必须依靠替代方法,例如找到的替代方法。