-
Notifications
You must be signed in to change notification settings - Fork 37
feat: add github annotations reporter #1059
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d495627
feat: add github annotations reporter
OrenMe 2ca9816
add docs
OrenMe 9c66cb1
lint fix
OrenMe 841c587
Merge branch 'main' into feat/githubAnnotations
OrenMe 890c71e
Merge remote-tracking branch 'refs/remotes/origin/feat/githubAnnotati…
OrenMe 239a7f2
Merge branch 'main' into feat/githubAnnotations
OrenMe fc9962b
lint fix
OrenMe 6188552
Merge branch 'main' into feat/githubAnnotations
OrenMe a16f978
Update cli.py
OrenMe 4ca19af
Update python/deptry/cli.py
OrenMe 132d1f7
CR comments
OrenMe 77bbe53
Merge remote-tracking branch 'refs/remotes/origin/feat/githubAnnotati…
OrenMe a33bae1
Update docs/usage.md
OrenMe a7f6f89
more CR fixes
OrenMe 8950f31
Merge remote-tracking branch 'refs/remotes/origin/feat/githubAnnotati…
OrenMe b483f72
Merge branch 'main' into feat/githubAnnotations
OrenMe 36bca11
Merge branch 'main' into feat/githubAnnotations
OrenMe 54c6f05
Merge branch 'main' into feat/githubAnnotations
OrenMe 4b457a1
Merge branch 'main' of github.com:fpgmaas/deptry into feat/githubAnno…
mkniewallner 261aa89
feat(reporters): handle violations without line/column
mkniewallner c7fa470
test: add functional tests for GitHub reporter
mkniewallner a26e41c
docs(usage): tweak documentation
mkniewallner 4324cff
test: use tuple for `github_warning_errors` arg
mkniewallner 1c4bbbd
fix(cli): use tuple for `github_warning_errors` arg
mkniewallner e6f76d8
refactor(reporters): code/message are never `None`
mkniewallner 7fe1324
test: windows, as usual
mkniewallner 23031df
test: more windows
mkniewallner 9517654
Merge branch 'main' into feat/githubAnnotations
mkniewallner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from deptry.reporters.github import GithubReporter | ||
| from deptry.reporters.json import JSONReporter | ||
| from deptry.reporters.text import TextReporter | ||
|
|
||
| __all__ = ("JSONReporter", "TextReporter") | ||
| __all__ = ("GithubReporter", "JSONReporter", "TextReporter") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from deptry.reporters.base import Reporter | ||
|
|
||
| if TYPE_CHECKING: | ||
| from deptry.violations import Violation | ||
|
|
||
|
|
||
| @dataclass | ||
| class GithubReporter(Reporter): | ||
| warning_ids: tuple[str, ...] = field(default_factory=tuple) # tuple of error codes to print as warnings | ||
|
|
||
| def report(self) -> None: | ||
| self._log_and_exit() | ||
|
|
||
| def _log_and_exit(self) -> None: | ||
| self._log_violations(self.violations) | ||
|
|
||
| def _log_violations(self, violations: list[Violation]) -> None: | ||
| for violation in violations: | ||
| self._print_github_annotation(violation) | ||
|
|
||
| def _print_github_annotation(self, violation: Violation) -> None: | ||
| annotation_severity = "warning" if violation.error_code in self.warning_ids else "error" | ||
| file_name = violation.location.file | ||
|
|
||
| ret = _build_workflow_command( | ||
| annotation_severity, | ||
| violation.error_code, | ||
| violation.get_error_message(), | ||
| str(file_name), | ||
| # For dependency files (like "pyproject.toml"), we don't extract a line. Setting the first line in that case | ||
| # allows a comment to be added in GitHub, even if it's not on the proper line, otherwise it doesn't appear | ||
| # at all. | ||
| line=violation.location.line or 1, | ||
| column=violation.location.column, | ||
| ) | ||
| logging.info(ret) | ||
|
|
||
|
|
||
| def _build_workflow_command( | ||
| command_name: str, | ||
| title: str, | ||
| message: str, | ||
| file: str, | ||
| line: int, | ||
| end_line: int | None = None, | ||
| column: int | None = None, | ||
| end_column: int | None = None, | ||
| ) -> str: | ||
| """Build a command to annotate a workflow.""" | ||
| result = f"::{command_name} " | ||
|
|
||
| entries = [ | ||
| ("file", file), | ||
| ("line", line), | ||
| ("endLine", end_line), | ||
| ("col", column), | ||
| ("endColumn", end_column), | ||
| ("title", title), | ||
| ] | ||
|
|
||
| result += ",".join(f"{k}={v}" for k, v in entries if v is not None) | ||
|
|
||
| return f"{result}::{_escape(message)}" | ||
|
|
||
|
|
||
| def _escape(s: str) -> str: | ||
| return s.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| from deptry.imports.location import Location | ||
| from deptry.module import Module | ||
| from deptry.reporters.github import GithubReporter, _build_workflow_command, _escape | ||
| from deptry.violations import DEP001MissingDependencyViolation | ||
|
|
||
| if TYPE_CHECKING: | ||
| from _pytest.logging import LogCaptureFixture | ||
|
|
||
| from deptry.violations import Violation | ||
|
|
||
| # Extract violation instance as a parameter | ||
| violation_instance = DEP001MissingDependencyViolation( | ||
| Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2) | ||
| ) | ||
|
|
||
| expected_warning = _build_workflow_command( | ||
| "warning", | ||
| "DEP001", | ||
| "'foo' imported but missing from the dependency definitions", | ||
| "foo.py", | ||
| line=1, | ||
| column=2, | ||
| ) | ||
|
|
||
| expected_error = _build_workflow_command( | ||
| "error", "DEP001", "'foo' imported but missing from the dependency definitions", "foo.py", line=1, column=2 | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("violation", "warning_ids", "expected"), | ||
| [ | ||
| (violation_instance, ["DEP001"], expected_warning), | ||
| (violation_instance, [], expected_error), | ||
| ], | ||
| ) | ||
| def test_github_annotation( | ||
| caplog: LogCaptureFixture, violation: Violation, warning_ids: tuple[str, ...], expected: str | ||
| ) -> None: | ||
| reporter = GithubReporter(violations=[violation], warning_ids=warning_ids) | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| reporter.report() | ||
|
|
||
| assert expected in caplog.text.strip() | ||
|
|
||
|
|
||
| def test_build_workflow_command_escaping() -> None: | ||
| # Directly test _build_workflow_command with characters needing escape. | ||
| message = "Error % occurred\r\nNew line" | ||
| escaped_message = _escape(message) | ||
| command = _build_workflow_command("warning", "TEST", message, "file.py", line=10, column=2) | ||
| assert "::warning file=file.py,line=10,col=2,title=TEST::" in command | ||
| assert escaped_message in command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.