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

GitHub warning annotations #432

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions diff_cover/diff_cover_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
JsonReportGenerator,
MarkdownReportGenerator,
StringReportGenerator,
GitHubWarningAnnotationsReportGenerator,
)
from diff_cover.violationsreporters.violations_reporter import (
LcovCoverageReporter,
Expand All @@ -24,6 +25,7 @@
HTML_REPORT_HELP = "Diff coverage HTML output"
JSON_REPORT_HELP = "Diff coverage JSON output"
MARKDOWN_REPORT_HELP = "Diff coverage Markdown output"
GITHUB_WARNING_ANNOTATIONS_HELP = "Print diff coverage GitHub warning annotations to the console"
COMPARE_BRANCH_HELP = "Branch to compare"
CSS_FILE_HELP = "Write CSS into an external file"
FAIL_UNDER_HELP = (
Expand Down Expand Up @@ -92,6 +94,13 @@ def parse_coverage_args(argv):
help=MARKDOWN_REPORT_HELP,
)

parser.add_argument(
"--github-warning-annotations",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given the idea of moving to a plugin type framework, maybe this should be implemented as a top level --format flag, similar to some other programs, so that it's possible to do --format github-native or --format xyz?

There would have to be discussion then about whether multiple formats are allowed (and if so, how)... for example, would it allow --format json,markdown,github-native or multiple instances of the same flag, something like --format github-native --format markdown:foo.md (the latter case allowing specifying the format and filename together)? If it's too invasive to take over the markdown / json / etc. reports, could at least have a single flag to control all stdout / stderr reporting.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I like this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will say if we move to this style then we should have it support the existing formats as well as the new one.

We kinda have to keep the existing flags to avoid breaking compatibility, but if we are going to be adding more then moving to something like this makes sense

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually started down the path of attempting to pass something like --github-annotations=warning or --github-annotations=error.
But I saw that customizing the context that is passed to jinja template for specific template generators wasn't so easy to adapt.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timkrins if you wanted to have that context could that be achieved by adding some kind of context parameter to the init of BaseReportGenerator?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe - I'll try have a go later in the week

action="store_true",
default=None,
help=GITHUB_WARNING_ANNOTATIONS_HELP,
)

parser.add_argument(
"--show-uncovered", action="store_true", default=None, help=SHOW_UNCOVERED
)
Expand Down Expand Up @@ -207,6 +216,7 @@ def generate_coverage_report(
css_file=None,
json_report=None,
markdown_report=None,
github_warning_annotations=False,
ignore_staged=False,
ignore_unstaged=False,
include_untracked=False,
Expand Down Expand Up @@ -269,6 +279,10 @@ def generate_coverage_report(
with open(markdown_report, "wb") as output_file:
reporter.generate_report(output_file)

if github_warning_annotations:
reporter = GitHubWarningAnnotationsReportGenerator(coverage, diff)
reporter.generate_report(sys.stdout.buffer)

# Generate the report for stdout
reporter = StringReportGenerator(coverage, diff, show_uncovered)
output_file = io.BytesIO() if quiet else sys.stdout.buffer
Expand Down Expand Up @@ -311,6 +325,7 @@ def main(argv=None, directory=None):
html_report=arg_dict["html_report"],
json_report=arg_dict["json_report"],
markdown_report=arg_dict["markdown_report"],
github_warning_annotations=arg_dict["github_warning_annotations"],
css_file=arg_dict["external_css_file"],
ignore_staged=arg_dict["ignore_staged"],
ignore_unstaged=arg_dict["ignore_unstaged"],
Expand Down
8 changes: 8 additions & 0 deletions diff_cover/report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ def __init__(self, violations_reporter, diff_reporter, show_uncovered=False):
super().__init__(violations_reporter, diff_reporter)
self.include_snippets = show_uncovered

class GitHubWarningAnnotationsReportGenerator(TemplateReportGenerator):
"""
Generate a diff coverage report for GitHub warning annotations.
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-warning-message
"""

template_path = "github_coverage_warning_annotations.txt"


class HtmlReportGenerator(TemplateReportGenerator):
"""
Expand Down
10 changes: 10 additions & 0 deletions diff_cover/templates/github_coverage_warning_annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% if src_stats %}
{% for src_path, stats in src_stats|dictsort %}
{% if stats.percent_covered < 100 %}
{% for line in stats.violation_lines %}
{% set splitLines = line.split("-") %}
::warning file={{ src_path }},line={{ splitLines[0] }}{% if splitLines[1] %},endLine={{ splitLines[1] }}{% endif %},title=Missing Coverage::Line {{ line }} missing coverage
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
52 changes: 52 additions & 0 deletions tests/test_report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
MarkdownReportGenerator,
StringReportGenerator,
TemplateReportGenerator,
GitHubWarningAnnotationsReportGenerator,
)
from diff_cover.violationsreporters.violations_reporter import (
BaseViolationReporter,
Expand Down Expand Up @@ -415,6 +416,57 @@ def test_empty_report(self):

self.assert_report(expected)

class TestGitHubWarningAnnotationsReportGenerator(BaseReportGeneratorTest):
REPORT_GENERATOR_CLASS = GitHubWarningAnnotationsReportGenerator

def test_generate_report(self):
# Generate a default report
self.use_default_values()

# Verify that we got the expected string
expected = dedent(
"""
::warning file=file1.py,line=10,endLine=11,title=Missing Coverage::Line 10-11 missing coverage
::warning file=subdir/file2.py,line=10,endLine=11,title=Missing Coverage::Line 10-11 missing coverage
"""
).strip()

self.assert_report(expected)

def test_single_line(self):
self.set_src_paths_changed(["file.py"])
self.set_lines_changed("file.py", list(range(0, 100)))
self.set_violations("file.py", [Violation(10, None)])
self.set_measured("file.py", [2])

# Verify that we got the expected string
expected = dedent(
"""
::warning file=file.py,line=10,title=Missing Coverage::Line 10 missing coverage
"""
).strip()

self.assert_report(expected)

def test_hundred_percent(self):
# Have the dependencies return an empty report
self.set_src_paths_changed(["file.py"])
self.set_lines_changed("file.py", list(range(0, 100)))
self.set_violations("file.py", [])
self.set_measured("file.py", [2])

expected = ""

self.assert_report(expected)

def test_empty_report(self):
# Have the dependencies return an empty report
# (this is the default)

expected = ""

self.assert_report(expected)


class TestHtmlReportGenerator(BaseReportGeneratorTest):
REPORT_GENERATOR_CLASS = HtmlReportGenerator
Expand Down