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

Update coverage/tree route to parse flags and components filters #503

Merged
merged 33 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2d6d5a6
Add filter check to coverage tree
rohitvinnakota-codecov Apr 10, 2024
752012b
lint
rohitvinnakota-codecov Apr 10, 2024
f24f52f
refactor components
rohitvinnakota-codecov Apr 15, 2024
b38923b
sort imports
rohitvinnakota-codecov Apr 15, 2024
874359b
update tests
rohitvinnakota-codecov Apr 15, 2024
5c75035
Merge branch 'main' of https://github.com/codecov/codecov-api into rv…
rohitvinnakota-codecov Apr 15, 2024
8499a0b
reformat
rohitvinnakota-codecov Apr 15, 2024
88e5aef
fix import
rohitvinnakota-codecov Apr 15, 2024
6f60c65
lint again..
rohitvinnakota-codecov Apr 15, 2024
c4f09c0
update tests
rohitvinnakota-codecov Apr 15, 2024
98f8d1d
update patch
rohitvinnakota-codecov Apr 15, 2024
263c708
Testing
rohitvinnakota-codecov Apr 15, 2024
0aa13ab
Test1 fix
rohitvinnakota-codecov Apr 15, 2024
baa0a56
fix patch order
rohitvinnakota-codecov Apr 15, 2024
dd0b56e
more tests
rohitvinnakota-codecov Apr 15, 2024
0cc9460
Lint
rohitvinnakota-codecov Apr 15, 2024
88a60f8
remove test
rohitvinnakota-codecov Apr 16, 2024
959a1b3
update test
rohitvinnakota-codecov Apr 16, 2024
24f44e5
Merge branch 'main' into rvinnakota/sunburst-fix
rohitvinnakota-codecov Apr 16, 2024
03d58e8
mock components
rohitvinnakota-codecov Apr 16, 2024
2feeafb
Merge branch 'rvinnakota/sunburst-fix' of https://github.com/codecov/…
rohitvinnakota-codecov Apr 16, 2024
98a833c
no requirements changes
rohitvinnakota-codecov Apr 16, 2024
78f9f03
update path
rohitvinnakota-codecov Apr 16, 2024
4dfa626
debug
rohitvinnakota-codecov Apr 16, 2024
59d2adb
more testing
rohitvinnakota-codecov Apr 16, 2024
a07d0b1
do not use list
rohitvinnakota-codecov Apr 16, 2024
6d6366e
use res json
rohitvinnakota-codecov Apr 16, 2024
b6d3af2
Merge branch 'main' into rvinnakota/sunburst-fix
rohitvinnakota-codecov Apr 16, 2024
da058ec
Remove print
rohitvinnakota-codecov Apr 16, 2024
21199bb
Merge branch 'rvinnakota/sunburst-fix' of https://github.com/codecov/…
rohitvinnakota-codecov Apr 16, 2024
1bfccf0
Update test
rohitvinnakota-codecov Apr 17, 2024
cafe84d
Add flags test
rohitvinnakota-codecov Apr 19, 2024
d005176
Merge branch 'main' into rvinnakota/sunburst-fix
rohitvinnakota-codecov Apr 19, 2024
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
40 changes: 29 additions & 11 deletions api/internal/coverage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from rest_framework.decorators import action
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.views import APIView

import services.components as components_service
from api.shared.mixins import RepoPropertyMixin
from api.shared.permissions import RepositoryArtifactPermissions
from api.shared.report.serializers import TreeSerializer
Expand All @@ -20,31 +20,49 @@ def get_object(self):
branch = self.repo.branches.filter(name=branch_name).first()
if branch is None:
raise NotFound(
f"The branch '{branch_name}' in not in our records. Please provide a valid branch name.",
f"The branch '{branch_name}' is not in our records. Please provide a valid branch name.",
404,
)
commit_sha = branch.head

commit = self.repo.commits.filter(commitid=commit_sha).first()
if commit is None:
raise NotFound(
f"The commit {commit_sha} is not in our records. Please specify valid commit.",
f"The commit {commit_sha} is not in our records. Please specify a valid commit.",
404,
)

report = commit.full_report
if report is None:
raise NotFound(f"Coverage report for {commit_sha} not found")

return report
components = self.request.query_params.getlist("components")
component_paths = []
if components:
all_components = components_service.commit_components(
commit, self.request.user
)
filtered_components = components_service.filter_components_by_name(
all_components, components
)

if not filtered_components:
raise NotFound(
f"Coverage report for components {filtered_components} not found"
)

for component in filtered_components:
component_paths.extend(component.paths)
flags = self.request.query_params.getlist("flags")

paths = ReportPaths(
report=report, filter_flags=flags, filter_paths=component_paths
)

return paths

@action(
detail=False,
methods=["get"],
url_path="tree",
)
@action(detail=False, methods=["get"], url_path="tree")
def tree(self, request, *args, **kwargs):
report = self.get_object()
paths = ReportPaths(report)
paths = self.get_object()
serializer = TreeSerializer(paths.single_directory(), many=True)
return Response(serializer.data)
76 changes: 73 additions & 3 deletions api/internal/tests/views/test_coverage_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from codecov_auth.tests.factories import OwnerFactory
from core.tests.factories import BranchFactory, CommitFactory, RepositoryFactory
from services.components import Component
from utils.test_utils import Client


Expand Down Expand Up @@ -87,10 +88,19 @@ def setUp(self):
self.client.force_login_owner(self.current_owner)

@patch("shared.reports.api_report_service.build_report_from_commit")
def test_tree(self, build_report_from_commit):
@patch("services.components.commit_components")
def test_tree(self, commit_components_mock, build_report_from_commit):
commit_components_mock.return_value = [
Component.from_dict(
{
"component_id": "global",
"name": "Global",
"paths": [".*/*.py"],
}
),
]
build_report_from_commit.return_value = sample_report()

res = self._tree()
res = self._tree(components="Global")
assert res.status_code == 200
assert res.json() == [
{
Expand Down Expand Up @@ -286,3 +296,63 @@ def test_tree_missing_report(self, build_report_from_commit):

res = self._tree()
assert res.status_code == 404

@patch("shared.reports.api_report_service.build_report_from_commit")
@patch("services.components.commit_components")
def test_tree_no_data_for_components(
self, commit_components_mock, build_report_from_commit
):
commit_components_mock.return_value = [
Component.from_dict(
{
"component_id": "c1",
"name": "ComponentOne",
"paths": ["dne.py"],
}
),
]
build_report_from_commit.return_value = sample_report()
res = self._tree(components="ComponentOne")
assert res.json() == []

@patch("shared.reports.api_report_service.build_report_from_commit")
@patch("services.components.commit_components")
def test_tree_not_found_for_components(
self, commit_components_mock, build_report_from_commit
):
commit_components_mock.return_value = [
Component.from_dict(
{
"component_id": "c1",
"name": "ComponentOne",
"paths": ["dne.py"],
}
),
]
build_report_from_commit.return_value = sample_report()
res = self._tree(components="Does_not_exist")
assert res.status_code == 404
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also add 1 more test for the flag filtering?


@patch("shared.reports.api_report_service.build_report_from_commit")
@patch("services.components.commit_components")
def test_tree_not_found_for_components(
self, commit_components_mock, build_report_from_commit
):
commit_components_mock.return_value = [
Component.from_dict(
{
"component_id": "c1",
"name": "ComponentOne",
"paths": ["dne.py"],
}
),
]
build_report_from_commit.return_value = sample_report()
res = self._tree(components="Does_not_exist")
assert res.status_code == 404

@patch("shared.reports.api_report_service.build_report_from_commit")
def test_tree_no_data_for_flags(self, build_report_from_commit):
build_report_from_commit.return_value = sample_report()
res = self._tree(flags="Does_not_exist")
assert res.json() == []
Loading