Skip to content

Commit fbe4e7c

Browse files
authored
Merge pull request #287 from smstone/label-metrics-measurements
fix: Negative time duration for labels applied after issue close; Inflated metric numbers for unlabeled labels on open issues
2 parents 8136d81 + b8ab8a9 commit fbe4e7c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

labels.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
4343
"""
4444
label_metrics: dict = {}
4545
label_events = get_label_events(issue, labels)
46+
label_last_event_type: dict = {}
4647

4748
for label in labels:
4849
label_metrics[label] = None
@@ -55,6 +56,12 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
5556

5657
# Calculate the time to add or subtract to the time spent in label based on the label events
5758
for event in label_events:
59+
# Skip labeling events that have occured past issue close time
60+
if issue.closed_at is not None and (
61+
event.created_at >= datetime.fromisoformat(issue.closed_at)
62+
):
63+
continue
64+
5865
if event.event == "labeled":
5966
labeled[event.label["name"]] = True
6067
if event.label["name"] in labels:
@@ -63,6 +70,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
6370
label_metrics[
6471
event.label["name"]
6572
] -= event.created_at - datetime.fromisoformat(issue.created_at)
73+
label_last_event_type[event.label["name"]] = "labeled"
6674
elif event.event == "unlabeled":
6775
unlabeled[event.label["name"]] = True
6876
if event.label["name"] in labels:
@@ -71,16 +79,20 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
7179
label_metrics[
7280
event.label["name"]
7381
] += event.created_at - datetime.fromisoformat(issue.created_at)
82+
label_last_event_type[event.label["name"]] = "unlabeled"
7483

7584
for label in labels:
76-
# if the label is still on there, add the time from the last event to now
7785
if label in labeled:
7886
# if the issue is closed, add the time from the issue creation to the closed_at time
7987
if issue.state == "closed":
8088
label_metrics[label] += datetime.fromisoformat(
8189
issue.closed_at
8290
) - datetime.fromisoformat(issue.created_at)
8391
else:
92+
# skip label if last labeling event is 'unlabled' and issue is still open
93+
if label_last_event_type[label] == "unlabeled":
94+
continue
95+
8496
# if the issue is open, add the time from the issue creation to now
8597
label_metrics[label] += datetime.now(pytz.utc) - datetime.fromisoformat(
8698
issue.created_at

test_labels.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def setUp(self):
4040
label={"name": "bug"},
4141
created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC),
4242
),
43+
# Label labeled after issue close date
44+
MagicMock(
45+
event="labeled",
46+
label={"name": "foo"},
47+
created_at=datetime(2021, 1, 20, tzinfo=pytz.UTC),
48+
),
4349
]
4450

4551
def test_get_label_events(self):
@@ -80,6 +86,13 @@ def test_get_label_metrics_open_issue(self):
8086
datetime.now(pytz.utc) - datetime(2021, 1, 4, tzinfo=pytz.UTC),
8187
)
8288

89+
def test_get_label_metrics_closed_issue_labeled_past_closed_at(self):
90+
"""Test get_label_metrics using a closed issue that was labeled past issue closed_at"""
91+
self.issue.state = "closed"
92+
labels = ["foo"]
93+
metrics = get_label_metrics(self.issue, labels)
94+
self.assertEqual(metrics["foo"], None)
95+
8396

8497
class TestGetAverageTimeInLabels(unittest.TestCase):
8598
"""Unit tests for get_stats_time_in_labels"""

0 commit comments

Comments
 (0)