Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit c7b8873

Browse files
committed
Remove most of the "labels" concept
1 parent fc29e8f commit c7b8873

File tree

8 files changed

+31
-1617
lines changed

8 files changed

+31
-1617
lines changed
Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sentry_sdk
22

33
from services.report.languages.base import BaseLanguageProcessor
4-
from services.report.report_builder import ReportBuilderSession, SpecialLabelsEnum
4+
from services.report.report_builder import ReportBuilderSession
55

66
COVERAGE_HIT = 1
77
COVERAGE_MISS = 0
@@ -16,8 +16,6 @@ def matches_content(self, content: dict, first_line: str, name: str) -> bool:
1616
def process(
1717
self, content: dict, report_builder_session: ReportBuilderSession
1818
) -> None:
19-
labels_table = LabelsTable(report_builder_session, content)
20-
2119
for filename, file_coverage in content["files"].items():
2220
_file = report_builder_session.create_coverage_file(filename)
2321
if _file is None:
@@ -28,70 +26,6 @@ def process(
2826
] + [(COVERAGE_MISS, ln) for ln in file_coverage["missing_lines"]]
2927
for cov, ln in lines_and_coverage:
3028
if ln > 0:
31-
# label_list_of_lists: list[list[str]] | list[list[int]] = []
32-
label_list_of_lists = [
33-
[labels_table._normalize_label(testname)]
34-
for testname in file_coverage.get("contexts", {}).get(
35-
str(ln), []
36-
)
37-
]
38-
_file.append(
39-
ln,
40-
report_builder_session.create_coverage_line(
41-
cov,
42-
labels_list_of_lists=label_list_of_lists,
43-
),
44-
)
29+
_line = report_builder_session.create_coverage_line(cov)
30+
_file.append(ln, _line)
4531
report_builder_session.append(_file)
46-
47-
48-
class LabelsTable:
49-
def __init__(
50-
self, report_builder_session: ReportBuilderSession, content: dict
51-
) -> None:
52-
self.labels_table: dict[str, str] = {}
53-
self.reverse_table: dict[str, int] = {}
54-
self.are_labels_already_encoded = False
55-
56-
# Compressed pycoverage files will include a labels_table
57-
if "labels_table" in content:
58-
self.labels_table = content["labels_table"]
59-
# We can pre-populate some of the indexes that will be used
60-
for idx, testname in self.labels_table.items():
61-
clean_label = self._normalize_label(testname)
62-
report_builder_session.label_index[int(idx)] = clean_label
63-
self.are_labels_already_encoded = True
64-
65-
def _normalize_label(self, testname: int | float | str) -> str:
66-
if isinstance(testname, int) or isinstance(testname, float):
67-
# This is from a compressed report.
68-
# Pull label from the labels_table
69-
# But the labels_table keys are strings, because of JSON format
70-
testname = self.labels_table[str(testname)]
71-
if testname == "":
72-
return SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label
73-
return testname.split("|", 1)[0]
74-
75-
def _get_list_of_label_ids(
76-
self,
77-
current_label_idx: dict[int, str],
78-
line_contexts: list[str | int],
79-
) -> list[int]:
80-
if self.are_labels_already_encoded:
81-
# The line contexts already include indexes in the table.
82-
# We can re-use the table and don't have to do anything with contexts.
83-
return sorted(map(int, line_contexts))
84-
85-
# In this case we do need to fix the labels
86-
label_ids_for_line = set()
87-
for testname in line_contexts:
88-
clean_label = self._normalize_label(testname)
89-
if clean_label in self.reverse_table:
90-
label_ids_for_line.add(self.reverse_table[clean_label])
91-
else:
92-
label_id = max([*current_label_idx.keys(), 0]) + 1
93-
current_label_idx[label_id] = clean_label
94-
self.reverse_table[clean_label] = label_id
95-
label_ids_for_line.add(label_id)
96-
97-
return sorted(label_ids_for_line)

services/report/report_builder.py

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import dataclasses
21
import logging
32
from enum import Enum
4-
from typing import Any, List, Sequence
3+
from typing import Any, Sequence
54

65
from shared.reports.resources import LineSession, Report, ReportFile, ReportLine
7-
from shared.reports.types import CoverageDatapoint
86
from shared.yaml.user_yaml import UserYaml
97

10-
from helpers.labels import SpecialLabelsEnum
118
from services.path_fixer import PathFixer
129
from services.yaml.reader import read_yaml_field
1310

@@ -36,8 +33,6 @@ def __init__(
3633
self.filepath = report_filepath
3734
self._report_builder = report_builder
3835
self._report = Report()
39-
self.label_index = {}
40-
self._present_labels = set()
4136

4237
@property
4338
def path_fixer(self):
@@ -53,109 +48,11 @@ def get_file(self, filename: str) -> ReportFile | None:
5348
return self._report.get(filename)
5449

5550
def append(self, file: ReportFile):
56-
if file is not None:
57-
for line_number, line in file.lines:
58-
if line.datapoints:
59-
for datapoint in line.datapoints:
60-
if datapoint.label_ids:
61-
for label in datapoint.label_ids:
62-
self._present_labels.add(label)
6351
return self._report.append(file)
6452

6553
def output_report(self) -> Report:
66-
"""
67-
Outputs a Report.
68-
This function applies all the needed modifications before a report
69-
can be output
70-
71-
Returns:
72-
Report: The legacy report desired
73-
"""
74-
if self._present_labels:
75-
if self._present_labels and self._present_labels == {
76-
SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER
77-
}:
78-
log.warning(
79-
"Report only has SpecialLabels. Might indicate it was not generated with contexts"
80-
)
81-
for file in self._report:
82-
for line_number, line in file.lines:
83-
self._possibly_modify_line_to_account_for_special_labels(
84-
file, line_number, line
85-
)
86-
self._report._totals = None
8754
return self._report
8855

89-
def _possibly_modify_line_to_account_for_special_labels(
90-
self, file: ReportFile, line_number: int, line: ReportLine
91-
) -> None:
92-
"""Possibly modify the report line in the file
93-
to account for any label in the SpecialLabelsEnum
94-
95-
Args:
96-
file (ReportFile): The file we want to modify
97-
line_number (int): The line number in case we
98-
need to set the new line back into the files
99-
line (ReportLine): The original line
100-
"""
101-
if not line.datapoints:
102-
return
103-
104-
new_datapoints = [
105-
item
106-
for datapoint in line.datapoints
107-
for item in self._possibly_convert_datapoints(datapoint)
108-
]
109-
if new_datapoints and new_datapoints != line.datapoints:
110-
# A check to avoid unnecessary replacement
111-
file[line_number] = dataclasses.replace(
112-
line,
113-
datapoints=sorted(
114-
new_datapoints,
115-
key=lambda x: (
116-
x.sessionid,
117-
x.coverage,
118-
x.coverage_type,
119-
),
120-
),
121-
)
122-
file._totals = None
123-
124-
# TODO: This can be removed after label indexing is rolled out for all customers
125-
def _possibly_convert_datapoints(
126-
self, datapoint: CoverageDatapoint
127-
) -> List[CoverageDatapoint]:
128-
"""Possibly convert datapoints
129-
The datapoint that might need to be converted
130-
131-
Args:
132-
datapoint (CoverageDatapoint): The datapoint to convert
133-
"""
134-
if datapoint.label_ids and any(
135-
label == SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER
136-
for label in datapoint.label_ids
137-
):
138-
new_label = (
139-
SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER.corresponding_label
140-
)
141-
return [
142-
dataclasses.replace(
143-
datapoint,
144-
label_ids=sorted(
145-
set(
146-
[
147-
label
148-
for label in datapoint.label_ids
149-
if label
150-
!= SpecialLabelsEnum.CODECOV_ALL_LABELS_PLACEHOLDER
151-
]
152-
+ [new_label]
153-
)
154-
),
155-
)
156-
]
157-
return [datapoint]
158-
15956
def create_coverage_file(
16057
self, path: str, do_fix_path: bool = True
16158
) -> ReportFile | None:
@@ -171,30 +68,12 @@ def create_coverage_line(
17168
self,
17269
coverage: int | str,
17370
coverage_type: CoverageType | None = None,
174-
labels_list_of_lists: list[list[str | SpecialLabelsEnum]]
175-
| list[list[int]]
176-
| None = None,
17771
partials=None,
17872
missing_branches=None,
17973
complexity=None,
18074
) -> ReportLine:
18175
sessionid = self._report_builder.sessionid
18276
coverage_type_str = coverage_type.map_to_string() if coverage_type else None
183-
datapoints = (
184-
[
185-
CoverageDatapoint(
186-
sessionid=sessionid,
187-
coverage=coverage,
188-
coverage_type=coverage_type_str,
189-
label_ids=label_ids,
190-
)
191-
# Avoid creating datapoints that don't contain any labels
192-
for label_ids in (labels_list_of_lists or [])
193-
if label_ids
194-
]
195-
if self._report_builder.supports_labels()
196-
else None
197-
)
19877
return ReportLine.create(
19978
coverage=coverage,
20079
type=coverage_type_str,
@@ -209,7 +88,6 @@ def create_coverage_line(
20988
)
21089
)
21190
],
212-
datapoints=datapoints,
21391
complexity=complexity,
21492
)
21593

0 commit comments

Comments
 (0)