-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcomparison.py
1283 lines (1074 loc) · 45.1 KB
/
comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import copy
import functools
import json
import logging
from collections import Counter
from dataclasses import dataclass, field
from datetime import datetime
from typing import List, Optional, Tuple
import minio
import pytz
import shared.reports.api_report_service as report_service
from asgiref.sync import async_to_sync
from django.db.models import Prefetch, QuerySet
from django.utils.functional import cached_property
from shared.api_archive.archive import ArchiveService
from shared.helpers.yaml import walk
from shared.reports.types import ReportTotals
from shared.utils.merge import LineType, line_type
from compare.models import CommitComparison
from core.models import Commit, Pull
from reports.models import CommitReport
from services import ServiceException
from services.redis_configuration import get_redis_connection
from services.repo_providers import RepoProviderService
from utils.config import get_config
log = logging.getLogger(__name__)
redis = get_redis_connection()
MAX_DIFF_SIZE = 170
def _is_added(line_value):
return line_value and line_value[0] == "+"
def _is_removed(line_value):
return line_value and line_value[0] == "-"
class ComparisonException(ServiceException):
@property
def message(self):
return str(self)
class MissingComparisonCommit(ComparisonException):
pass
class MissingComparisonReport(ComparisonException):
pass
class FirstPullRequest:
message = "This is the first pull request for this repository"
class FileComparisonTraverseManager:
"""
The FileComparisonTraverseManager uses the visitor-pattern to execute a series
of arbitrary actions on each line in a FileComparison. The main entrypoint to
this class is the '.apply()' method, which is the only method client code should invoke.
"""
def __init__(self, head_file_eof=0, base_file_eof=0, segments=[], src=[]):
"""
head_file_eof -- end-line of the head_file we are traversing, plus 1
base_file_eof -- same as above, for base_file
^^ Generally client code should supply both, except in a couple cases:
1. The file is newly tracked. In this case, there is no base file, so we should
iterate only over the head file lines.
2. The file is deleted. As of right now (4/2/2020), we don't show deleted files in
comparisons, but if we were to support that, we would not supply a head_file_eof
and instead only iterate over lines in the base file.
segments -- these come from the provider API response related to the comparison, and
constitute the 'diff' between the base and head references. Each segment takes this form:
{
"header": [
base reference offset,
number of lines in file-segment before changes applied,
head reference offset,
number of lines in file-segment after changes applied
],
"lines": [ # line values for lines in the diff
"+this is an added line",
"-this is a removed line",
"this line is unchanged in the diff",
...
]
}
The segment["header"], also known as the hunk-header (https://en.wikipedia.org/wiki/Diff#Unified_format),
is an array of strings, which is why we have to use the int() builtin function
to compare with self.head_ln and self.base_ln. It is used by this algorithm to
1. Set initial values for the self.base_ln and self.head_ln line-counters, and
2. Detect if self.base and/or self.head refer to lines in the diff at any given time
This algorithm relies on the fact that segments are returned in ascending
order for each file, which means that the "nearest" segment to the current line
being traversed is located at segments[0].
src -- this is the source code of the file at the head-reference, where each line
is a cell in the array. If we are not traversing a segment, and src is provided,
the line value passed to the visitors will be the line at src[self.head_ln - 1].
"""
self.head_file_eof = head_file_eof
self.base_file_eof = base_file_eof
self.segments = copy.deepcopy(segments)
self.src = src
if self.segments:
# Base offsets can be 0 if files are added or removed
self.base_ln = min(1, int(self.segments[0]["header"][0]))
self.head_ln = min(1, int(self.segments[0]["header"][2]))
else:
self.base_ln, self.head_ln = 1, 1
def traverse_finished(self):
if self.segments:
return False
if self.src:
return self.head_ln > len(self.src)
return self.head_ln >= self.head_file_eof and self.base_ln >= self.base_file_eof
def traversing_diff(self):
if self.segments == []:
return False
base_ln_within_offset = (
int(self.segments[0]["header"][0])
<= self.base_ln
< int(self.segments[0]["header"][0])
+ int(self.segments[0]["header"][1] or 1)
)
head_ln_within_offset = (
int(self.segments[0]["header"][2])
<= self.head_ln
< int(self.segments[0]["header"][2])
+ int(self.segments[0]["header"][3] or 1)
)
return base_ln_within_offset or head_ln_within_offset
def pop_line(self):
if self.traversing_diff():
return self.segments[0]["lines"].pop(0)
if self.src:
return self.src[self.head_ln - 1]
def apply(self, visitors):
"""
Traverses the lines in a file comparison while accounting for the diff.
If a line only appears in the base file (removed in head), it is prefixed
with '-', and we only increment self.base_ln. If a line only appears in
the head file, it is newly added and prefixed with '+', and we only
increment self.head_ln.
visitors -- A list of visitors applied to each line.
"""
while not self.traverse_finished():
line_value = self.pop_line()
is_diff = self.traversing_diff()
for visitor in visitors:
visitor(
None if is_diff and _is_added(line_value) else self.base_ln,
None if is_diff and _is_removed(line_value) else self.head_ln,
line_value,
is_diff, # TODO(pierce): remove when upon combining diff + changes tabs in UI
)
if is_diff and _is_added(line_value):
self.head_ln += 1
elif is_diff and _is_removed(line_value):
self.base_ln += 1
else:
self.head_ln += 1
self.base_ln += 1
if self.segments and not self.segments[0]["lines"]:
# Either the segment has no lines (and is therefore of no use)
# or all lines have been popped and visited, which means we are
# done traversing it
self.segments.pop(0)
class FileComparisonVisitor:
"""
Abstract class with a convenience method for getting lines amongst
all the edge cases.
"""
def _get_line(self, report_file, ln):
"""
Kindof a hacky way to bypass the dataclasses used in `reports`
library, because they are extremely slow. This basically copies
some logic from ReportFile.get and ReportFile._line, which work
together to take an index and turn it into a ReportLine. Here
we do something similar, but just return the underlying array instead.
Not sure if this will be the final solution.
Note: the underlying array representation cn be seen here:
https://github.com/codecov/shared/blob/master/shared/reports/types.py#L75
The index in the array representation is 1-1 with the index of the
dataclass attribute for ReportLine.
"""
if report_file is None or ln is None:
return None
# copied from ReportFile.get
try:
line = report_file._lines[ln - 1]
except IndexError:
return None
# copied from ReportFile._line, minus dataclass instantiation
if line:
if isinstance(line, list):
return line
else:
# these are old versions
# note:(pierce) ^^ this comment is copied, not sure what it means
return json.loads(line)
def _get_lines(self, base_ln, head_ln):
base_line = self._get_line(self.base_file, base_ln)
head_line = self._get_line(self.head_file, head_ln)
return base_line, head_line
def __call__(self, base_ln, head_ln, value, is_diff):
pass
class CreateLineComparisonVisitor(FileComparisonVisitor):
"""
A visitor that creates LineComparisons, and stores the
result in self.lines. Only operates on lines that have
code-values derived from segments or src in FileComparisonTraverseManager.
"""
def __init__(self, base_file, head_file):
self.base_file, self.head_file = base_file, head_file
self.lines = []
def __call__(self, base_ln, head_ln, value, is_diff):
if value is None:
return
base_line, head_line = self._get_lines(base_ln, head_ln)
self.lines.append(
LineComparison(
base_line=base_line,
head_line=head_line,
base_ln=base_ln,
head_ln=head_ln,
value=value,
is_diff=is_diff,
)
)
class CreateChangeSummaryVisitor(FileComparisonVisitor):
"""
A visitor for summarizing the "unexpected coverage changes"
to a certain file. We specifically ignore lines that are changed
in the source code, which are prefixed with '+' or '-'. Result
is stored in self.summary.
"""
def __init__(self, base_file, head_file):
self.base_file, self.head_file = base_file, head_file
self.summary = Counter()
self.coverage_type_map = {
LineType.hit: "hits",
LineType.miss: "misses",
LineType.partial: "partials",
}
def _update_summary(self, base_line, head_line):
"""
Updates the change summary based on the coverage type (0
for miss, 1 for hit, 2 for partial) found at index 0 of the
line-array.
"""
self.summary[self.coverage_type_map[line_type(base_line[0])]] -= 1
self.summary[self.coverage_type_map[line_type(head_line[0])]] += 1
def __call__(self, base_ln, head_ln, value, is_diff):
if value and value[0] in ["+", "-"]:
return
base_line, head_line = self._get_lines(base_ln, head_ln)
if base_line is None or head_line is None:
return
if line_type(base_line[0]) == line_type(head_line[0]):
return
self._update_summary(base_line, head_line)
class LineComparison:
def __init__(self, base_line, head_line, base_ln, head_ln, value, is_diff):
self.base_line = base_line
self.head_line = head_line
self.head_ln = head_ln
self.base_ln = base_ln
self.value = value
self.is_diff = is_diff
self.added = is_diff and _is_added(value)
self.removed = is_diff and _is_removed(value)
@property
def number(self):
return {
"base": self.base_ln if not self.added else None,
"head": self.head_ln if not self.removed else None,
}
@property
def coverage(self):
return {
"base": None
if self.added or not self.base_line
else line_type(self.base_line[0]),
"head": None
if self.removed or not self.head_line
else line_type(self.head_line[0]),
}
@cached_property
def head_line_sessions(self) -> Optional[List[tuple]]:
if self.head_line is None:
return None
# `head_line` is the tuple representation of a `shared.reports.types.ReportLine`
# it has the following shape:
# (coverage, type, sessions, messages, complexity, datapoints)
# each session is a tuple representation of a `shared.reports.types.LineSession`
# is has the following shape:
# (id, coverage, branches, partials, complexity)
sessions = self.head_line[2]
return sessions
@cached_property
def hit_count(self) -> Optional[int]:
if self.head_line_sessions is None:
return None
hit_count = 0
for id, coverage, *rest in self.head_line_sessions:
if line_type(coverage) == LineType.hit:
hit_count += 1
if hit_count > 0:
return hit_count
@cached_property
def hit_session_ids(self) -> Optional[List[int]]:
if self.head_line_sessions is None:
return None
ids = []
for id, coverage, *rest in self.head_line_sessions:
if line_type(coverage) == LineType.hit:
ids.append(id)
if len(ids) > 0:
return ids
class Segment:
"""
A segment represents a contiguous subset of lines in a file where either
the coverage has changed or the code has changed (i.e. is part of a diff).
"""
# additional lines included before and after each segment
padding_lines = 3
# max distance between lines with coverage changes in a single segment
line_distance = 6
@classmethod
def segments(cls, file_comparison):
lines = file_comparison.lines
# line numbers of interest (i.e. coverage changed or code changed)
line_numbers = []
for idx, line in enumerate(lines):
if (
line.coverage["base"] != line.coverage["head"]
or line.added
or line.removed
):
line_numbers.append(idx)
segmented_lines = []
if len(line_numbers) > 0:
segmented_lines, last = [[]], None
for line_number in line_numbers:
if last is None or line_number - last <= cls.line_distance:
segmented_lines[-1].append(line_number)
else:
segmented_lines.append([line_number])
last = line_number
segments = []
for group in segmented_lines:
# padding lines before first line of interest
start_line_number = group[0] - cls.padding_lines
start_line_number = max(start_line_number, 0)
# padding lines after last line of interest
end_line_number = group[-1] + cls.padding_lines
end_line_number = min(end_line_number, len(lines) - 1)
segment = cls(lines[start_line_number : end_line_number + 1])
segments.append(segment)
return segments
def __init__(self, lines):
self._lines = lines
@property
def header(self):
base_start = None
head_start = None
num_removed = 0
num_added = 0
num_context = 0
for line in self.lines:
if base_start is None and line.number["base"] is not None:
base_start = int(line.number["base"])
if head_start is None and line.number["head"] is not None:
head_start = int(line.number["head"])
if line.added:
num_added += 1
elif line.removed:
num_removed += 1
else:
num_context += 1
return (
base_start or 0,
num_context + num_removed,
head_start or 0,
num_context + num_added,
)
@property
def lines(self):
return self._lines
@property
def has_diff_changes(self):
for line in self.lines:
if line.added or line.removed:
return True
return False
@property
def has_unintended_changes(self):
for line in self.lines:
head_coverage = line.coverage["base"]
base_coverage = line.coverage["head"]
if not (line.added or line.removed) and (base_coverage != head_coverage):
return True
return False
def remove_unintended_changes(self):
filtered = []
for line in self._lines:
base_cov = line.coverage["base"]
head_cov = line.coverage["head"]
if (line.added or line.removed) or (base_cov == head_cov):
filtered.append(line)
self._lines = filtered
class FileComparison:
def __init__(
self,
base_file,
head_file,
diff_data=None,
src=[],
bypass_max_diff=False,
should_search_for_changes=None,
):
"""
comparison -- the enclosing Comparison object that owns this FileComparison
base_file -- the ReportFile for this file from the base report
head_file -- the ReportFile for this file from the head report
diff_data -- the git-comparison between the base and head references in the instantiation
Comparison object. fields include:
stats: -- {"added": number of added lines, "removed": number of removed lines}
segments: (described in detail in the FileComparisonTraverseManager docstring)
before: the name of this file in the base reference, if different from name in head ref
If this file is unchanged in the comparison between base and head, the default will be used.
src -- The full source of the file in the head reference. Used in FileComparisonTraverseManager
to join src-code with coverage data. Default is used when retrieving full comparison,
whereas full-src is serialized when retrieving individual file comparison.
bypass_max_diff -- configuration paramater that tells this class to ignore max-diff truncating.
default is used when retrieving full comparison; True is passed when fetching individual
file comparison.
should_search_for_changes -- flag that indicates if this FileComparison has unexpected coverage changes,
according to a value cached during asynchronous processing. Has three values:
1. True - indicates this FileComparison has unexpected coverage changes according to worker,
and we should process the lines in this FileComparison using FileComparisonTraverseManager
to calculate a change summary.
2. False - indicates this FileComparison does not have unexpected coverage changes according to
worker, and we should not traverse this file or calculate a change summary.
3. None (default) - indicates we do not have information cached from worker to rely on here
(no value in cache), so we need to traverse this FileComparison and calculate a change
summary to find out.
"""
self.base_file = base_file
self.head_file = head_file
self.diff_data = diff_data
self.src = src
# Some extra fields for truncating large diffs in the initial response
self.total_diff_length = (
functools.reduce(
lambda a, b: a + b,
[len(segment["lines"]) for segment in self.diff_data["segments"]],
)
if self.diff_data is not None and self.diff_data.get("segments")
else 0
)
self.bypass_max_diff = bypass_max_diff
self.should_search_for_changes = should_search_for_changes
@property
def name(self):
return {
"base": self.base_file.name if self.base_file is not None else None,
"head": self.head_file.name if self.head_file is not None else None,
}
@property
def totals(self):
head_totals = self.head_file.totals if self.head_file is not None else None
# The call to '.apply_diff()' in 'Comparison.head_report' stores diff totals
# for each file in the diff_data for that file (in a field called 'totals').
# Here we pass this along to the frontend by assigning the diff totals
# to the head_totals' 'diff' attribute. It is absolutely worth considering
# modifying the behavior of shared.reports to implement something similar.
diff_totals = None
if head_totals and self.diff_data:
diff_totals = self.diff_data.get("totals")
head_totals.diff = diff_totals or 0
return {
"base": self.base_file.totals if self.base_file is not None else None,
"head": head_totals,
"diff": diff_totals,
}
@property
def has_diff(self):
return self.diff_data is not None
@property
def stats(self):
return self.diff_data["stats"] if self.diff_data else None
@cached_property
def _calculated_changes_and_lines(self):
"""
Applies visitors to the file to generate response data (line comparison representations
and change summary). Only applies visitors if
1. The file has a diff or src, in which case we need to generate response data for it anyway, or
2. The should_search_for_changes flag is defined (not None) and is True
This limitation improves performance by limiting searching for changes to only files that
have them.
"""
change_summary_visitor = CreateChangeSummaryVisitor(
self.base_file, self.head_file
)
create_lines_visitor = CreateLineComparisonVisitor(
self.base_file, self.head_file
)
if self.diff_data or self.src or self.should_search_for_changes is not False:
FileComparisonTraverseManager(
head_file_eof=self.head_file.eof if self.head_file is not None else 0,
base_file_eof=self.base_file.eof if self.base_file is not None else 0,
segments=self.diff_data["segments"]
if self.diff_data and "segments" in self.diff_data
else [],
src=self.src,
).apply([change_summary_visitor, create_lines_visitor])
return change_summary_visitor.summary, create_lines_visitor.lines
@cached_property
def change_summary(self):
return self._calculated_changes_and_lines[0]
@property
def has_changes(self):
return any(self.change_summary.values())
@cached_property
def lines(self):
if self.total_diff_length > MAX_DIFF_SIZE and not self.bypass_max_diff:
return None
return self._calculated_changes_and_lines[1]
@cached_property
def segments(self):
return Segment.segments(self)
class Comparison(object):
def __init__(self, user, base_commit, head_commit):
# TODO: rename to owner
self.user = user
self._base_commit = base_commit
self._head_commit = head_commit
def validate(self):
# make sure head and base reports exist (will throw an error if not)
self.head_report
self.base_report
@cached_property
def base_commit(self):
return self._base_commit
@cached_property
def head_commit(self):
return self._head_commit
@cached_property
def files(self):
for file_name in self.head_report.files:
yield self.get_file_comparison(file_name)
def get_file_comparison(self, file_name, with_src=False, bypass_max_diff=False):
head_file = self.head_report.get(file_name)
diff_data = self.git_comparison["diff"]["files"].get(file_name)
if self.base_report is not None:
base_file = self.base_report.get(file_name)
if base_file is None and diff_data:
base_file = self.base_report.get(diff_data.get("before"))
else:
base_file = None
if with_src:
adapter = RepoProviderService().get_adapter(
owner=self.user, repo=self.base_commit.repository
)
file_content = async_to_sync(adapter.get_source)(
file_name, self.head_commit.commitid
)["content"]
# make sure the file is str utf-8
if not isinstance(file_content, str):
file_content = str(file_content, "utf-8")
src = file_content.splitlines()
else:
src = []
return FileComparison(
base_file=base_file,
head_file=head_file,
diff_data=diff_data,
src=src,
bypass_max_diff=bypass_max_diff,
)
@property
def git_comparison(self):
return self._fetch_comparison[0]
@cached_property
def base_report(self):
try:
return report_service.build_report_from_commit(self.base_commit)
except minio.error.S3Error as e:
if e.code == "NoSuchKey":
raise MissingComparisonReport("Missing base report")
else:
raise e
@cached_property
def head_report(self):
try:
report = report_service.build_report_from_commit(self.head_commit)
except minio.error.S3Error as e:
if e.code == "NoSuchKey":
raise MissingComparisonReport("Missing head report")
else:
raise e
# Return the old report if the github API call fails for any reason
try:
report.apply_diff(self.git_comparison["diff"])
except Exception:
pass
return report
@cached_property
def has_different_number_of_head_and_base_sessions(self):
log.info("has_different_number_of_head_and_base_sessions - Start")
head_sessions = self.head_report.sessions
base_sessions = self.base_report.sessions
log.info(
f"has_different_number_of_head_and_base_sessions - Retrieved sessions - head {len(head_sessions)} / base {len(base_sessions)}"
)
# We're treating this case as false since considering CFF's complicates the logic
if self._has_cff_sessions(head_sessions) or self._has_cff_sessions(
base_sessions
):
return False
return len(head_sessions) != len(base_sessions)
# I feel this method should belong to the API Report class, but we're thinking of getting rid of that class soon
# In truth, this should be in the shared.Report class
def _has_cff_sessions(self, sessions) -> bool:
log.info(f"_has_cff_sessions - sessions count {len(sessions)}")
for session in sessions.values():
if session.session_type.value == "carriedforward":
log.info("_has_cff_sessions - Found carriedforward")
return True
log.info("_has_cff_sessions - No carriedforward")
return False
@property
def totals(self):
return {
"base": self.base_report.totals if self.base_report is not None else None,
"head": self.head_report.totals if self.head_report is not None else None,
"diff": self.git_comparison["diff"].get("totals"),
}
@property
def git_commits(self):
return self.git_comparison["commits"]
@property
def upload_commits(self):
"""
Returns the commits that have uploads between base and head.
:return: Queryset of core.models.Commit objects
"""
commit_ids = [commit["commitid"] for commit in self.git_commits]
commits_queryset = Commit.objects.filter(
commitid__in=commit_ids, repository=self.base_commit.repository
)
commits_queryset.exclude(deleted=True)
return commits_queryset
@cached_property
def _fetch_comparison(self):
"""
Fetches comparison, and caches the result.
"""
adapter = RepoProviderService().get_adapter(
self.user, self.base_commit.repository
)
comparison_coro = adapter.get_compare(
self.base_commit.commitid, self.head_commit.commitid
)
async def runnable():
return await asyncio.gather(comparison_coro)
return async_to_sync(runnable)()
def flag_comparison(self, flag_name):
return FlagComparison(self, flag_name)
@property
def non_carried_forward_flags(self):
flags_dict = self.head_report.flags
return [flag for flag, vals in flags_dict.items() if not vals.carriedforward]
class FlagComparison(object):
def __init__(self, comparison, flag_name):
self.comparison = comparison
self.flag_name = flag_name
@cached_property
def head_report(self):
return self.comparison.head_report.flags.get(self.flag_name)
@cached_property
def base_report(self):
return self.comparison.base_report.flags.get(self.flag_name)
@cached_property
def diff_totals(self):
if self.head_report is None:
return None
git_comparison = self.comparison.git_comparison
return self.head_report.apply_diff(git_comparison["diff"])
@dataclass
class ImpactedFile:
@dataclass
class Totals(ReportTotals):
def __post_init__(self):
nb_branches = self.hits + self.misses + self.partials
self.coverage = (100 * self.hits / nb_branches) if nb_branches > 0 else None
base_name: Optional[str] = None # will be `None` for created files
head_name: Optional[str] = None # will be `None` for deleted files
file_was_added_by_diff: bool = False
file_was_removed_by_diff: bool = False
base_coverage: Optional[Totals] = None # will be `None` for created files
head_coverage: Optional[Totals] = None # will be `None` for deleted files
# lists of (line number, coverage) tuples
added_diff_coverage: Optional[List[tuple[int, str]]] = None
removed_diff_coverage: Optional[List[tuple[int, str]]] = field(default_factory=list)
unexpected_line_changes: Optional[List[tuple[int, str]]] = field(
default_factory=list
)
lines_only_on_base: List[int] = field(default_factory=list)
lines_only_on_head: List[int] = field(default_factory=list)
@classmethod
def create(cls, **kwargs):
base_coverage = kwargs.pop("base_coverage")
head_coverage = kwargs.pop("head_coverage")
return cls(
**kwargs,
base_coverage=ImpactedFile.Totals(**base_coverage)
if base_coverage
else None,
head_coverage=ImpactedFile.Totals(**head_coverage)
if head_coverage
else None,
)
@cached_property
def has_diff(self) -> bool:
"""
Returns `True` if the file has any additions or removals in the diff
"""
return bool(
self.added_diff_coverage
and len(self.added_diff_coverage) > 0
or self.removed_diff_coverage
and len(self.removed_diff_coverage) > 0
or self.file_was_added_by_diff
or self.file_was_removed_by_diff
)
@cached_property
def has_changes(self) -> bool:
"""
Returns `True` if the file has any unexpected changes
"""
return (
self.unexpected_line_changes is not None
and len(self.unexpected_line_changes) > 0
)
@cached_property
def misses_count(self) -> int:
total_misses = 0
total_misses += self._direct_misses_count
total_misses += self._unintended_misses_count
return total_misses
@cached_property
def _unintended_misses_count(self) -> int:
"""
Returns the misses count for a unintended impacted file
"""
misses = 0
unexpected_line_changes = self.unexpected_line_changes or []
for [
base,
[head_line_number, head_coverage_value],
] in unexpected_line_changes:
if head_coverage_value == "m":
misses += 1
return misses
@cached_property
def _direct_misses_count(self) -> int:
"""
Returns the misses count for a direct impacted file
"""
misses = 0
diff_coverage = self.added_diff_coverage or []
for line_number, line_coverage_value in diff_coverage:
if line_coverage_value == "m":
misses += 1
return misses
@cached_property
def patch_coverage(self) -> Optional[Totals]:
"""
Sums of hits, misses and partials in the diff
"""
if self.added_diff_coverage and len(self.added_diff_coverage) > 0:
hits, misses, partials = (0, 0, 0)
for added_coverage in self.added_diff_coverage:
[_, type_coverage] = added_coverage
if type_coverage == "h":
hits += 1
if type_coverage == "m":
misses += 1
if type_coverage == "p":
partials += 1
return ImpactedFile.Totals(hits=hits, misses=misses, partials=partials)
@cached_property
def change_coverage(self) -> Optional[float]:
if (
self.base_coverage
and self.base_coverage.coverage
and self.head_coverage
and self.head_coverage.coverage
):
return float(
float(self.head_coverage.coverage or 0)
- float(self.base_coverage.coverage or 0)
)
@cached_property
def file_name(self) -> Optional[str]:
if self.head_name:
parts = self.head_name.split("/")
return parts[-1]
@dataclass
class ComparisonReport(object):
"""
This is a wrapper around the data computed by the worker's commit comparison task.
The raw data is stored in blob storage and accessible via the `report_storage_path`
on a `CommitComparison`
"""
commit_comparison: CommitComparison = None
@cached_property
def files(self) -> List[ImpactedFile]:
if not self.commit_comparison.report_storage_path:
return []
comparison_data = self._fetch_raw_comparison_data()
return [
ImpactedFile.create(**data) for data in comparison_data.get("files", [])
]
def impacted_file(self, path: str) -> Optional[ImpactedFile]:
for file in self.files:
if file.head_name == path:
return file
@cached_property
def impacted_files(self) -> List[ImpactedFile]:
return self.files
@cached_property
def impacted_files_with_unintended_changes(self) -> List[ImpactedFile]:
return [file for file in self.files if file.has_changes]