Skip to content
Open
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
9 changes: 9 additions & 0 deletions radon/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def cc(
xml=False,
md=False,
codeclimate=False,
rdjson=False,
output_file=_cfg.get_value('output_file', str, None),
include_ipynb=_cfg.get_value('include_ipynb', bool, False),
ipynb_cells=_cfg.get_value('ipynb_cells', bool, False),
Expand Down Expand Up @@ -142,6 +143,7 @@ def cc(
:param --xml: Format results in XML (compatible with CCM).
:param --md: Format results in Markdown.
:param --codeclimate: Format results for Code Climate.
:param --rdjson: Format results for ReviewDog.
:param --no-assert: Do not count `assert` statements when computing
complexity.
:param --show-closures: Add closures/inner classes to the output.
Expand Down Expand Up @@ -171,6 +173,7 @@ def cc(
xml=xml,
md=md,
codeclimate=codeclimate,
rdjson=rdjson,
stream=stream,
)

Expand Down Expand Up @@ -380,6 +383,12 @@ def log_result(harvester, **kwargs):
noformat=True,
**kwargs
)
elif kwargs.get('rdjson'):
log(
harvester.as_reviewdog_diagnostic_format(),
noformat=True,
**kwargs
)
elif kwargs.get('md'):
log(harvester.as_md(), noformat=True, **kwargs)
else:
Expand Down
13 changes: 13 additions & 0 deletions radon/cli/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
dict_to_md,
iter_filenames,
raw_to_dict,
dict_to_reviewdog_diagnostic_format,
strip_ipython,
)
from radon.complexity import (
Expand Down Expand Up @@ -163,6 +164,10 @@ def as_codeclimate_issues(self):
'''Format the results as Code Climate issues.'''
raise NotImplementedError

def as_reviewdog_diagnostic_format(self):
'''Format the results as ReviewDog Diagnostic Format (RDJSON).'''
raise NotImplementedError

def to_terminal(self):
'''Yields tuples representing lines to be printed to a terminal.

Expand Down Expand Up @@ -216,6 +221,14 @@ def as_codeclimate_issues(self):
'''Format the result as Code Climate issues.'''
return dict_to_codeclimate_issues(self._to_dicts(), self.config.min)

def as_reviewdog_diagnostic_format(self):
'''Formatthe result as Reviewdog Diagnostic Format

See https://github.com/reviewdog/reviewdog/tree/master/proto/rdf#rdjson
for more information about the format.
'''''
return dict_to_reviewdog_diagnostic_format(self._to_dicts(), self.config.min)

def to_terminal(self):
'''Yield lines to be printed in a terminal.'''
average_cc = 0.0
Expand Down
50 changes: 50 additions & 0 deletions radon/cli/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,56 @@ def dict_to_codeclimate_issues(results, threshold='B'):
return codeclimate_issues



def dict_to_reviewdog_diagnostic_format(results, threshold='B'):
diagnostics = []

for path in results:
info = results[path]
for offender in info:
beginline = offender['lineno']
col_offset = offender['col_offset']
endline = offender['endline']
complexity = offender['complexity']
rank = offender['rank']
description = (
'Cyclomatic complexity is too high in {0} {1}. '
'({2})'.format(
offender['type'], offender['name'], rank
)
)
remediation_points = get_remediation_points(
complexity, threshold
)

if remediation_points > 0:
diagnostics.append({
"message": description,
"location": {
"path": path,
"range": {
"start": {
"line": beginline,
"column": col_offset
},
"end": {
"line": endline,
"column": None
}
}
},
})

return json.dumps({
"source": {
"name": "radon cc",
"url": "https://radon.readthedocs.io/en/latest/commandline.html#the-cc-command"
},
"severity": "WARNING",
"diagnostics": diagnostics
})


def cc_to_terminal(results, show_complexity, min, max, total_average):
'''Transform Cyclomatic Complexity results into a 3-elements tuple:

Expand Down
1 change: 1 addition & 0 deletions radon/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def test_cc(mocker, log_mock):
log_mock.assert_called_once_with(
mocker.sentinel.harvester,
codeclimate=False,
rdjson=False,
json=True,
stream=sys.stdout,
xml=False,
Expand Down