From 16c00d0f8d02a105165b8d25f76eb9be882d67bb Mon Sep 17 00:00:00 2001 From: Steven Stone Date: Mon, 20 May 2024 10:57:18 -0400 Subject: [PATCH 1/3] Skip labeling events that have occured past issue close time --- labels.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/labels.py b/labels.py index 9b4e4df..5419827 100644 --- a/labels.py +++ b/labels.py @@ -55,6 +55,10 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: # Calculate the time to add or subtract to the time spent in label based on the label events for event in label_events: + # Skip labeling events that have occured past issue close time + if issue.closed_at is not None and (event.created_at >= datetime.fromisoformat(issue.closed_at)): + continue + if event.event == "labeled": labeled[event.label["name"]] = True if event.label["name"] in labels: From 2d023ab9ea042ede74fb2338642794dc70578afb Mon Sep 17 00:00:00 2001 From: Steven Stone Date: Mon, 20 May 2024 12:18:51 -0400 Subject: [PATCH 2/3] Skip processing label time if last event is unlabled and issue is open --- labels.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/labels.py b/labels.py index 5419827..adbb333 100644 --- a/labels.py +++ b/labels.py @@ -43,6 +43,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: """ label_metrics: dict = {} label_events = get_label_events(issue, labels) + label_last_event_type: dict = {} for label in labels: label_metrics[label] = None @@ -56,7 +57,9 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: # Calculate the time to add or subtract to the time spent in label based on the label events for event in label_events: # Skip labeling events that have occured past issue close time - if issue.closed_at is not None and (event.created_at >= datetime.fromisoformat(issue.closed_at)): + if issue.closed_at is not None and ( + event.created_at >= datetime.fromisoformat(issue.closed_at) + ): continue if event.event == "labeled": @@ -67,6 +70,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: label_metrics[ event.label["name"] ] -= event.created_at - datetime.fromisoformat(issue.created_at) + label_last_event_type[event.label["name"]] = "labeled" elif event.event == "unlabeled": unlabeled[event.label["name"]] = True if event.label["name"] in labels: @@ -75,9 +79,9 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: label_metrics[ event.label["name"] ] += event.created_at - datetime.fromisoformat(issue.created_at) + label_last_event_type[event.label["name"]] = "unlabeled" for label in labels: - # if the label is still on there, add the time from the last event to now if label in labeled: # if the issue is closed, add the time from the issue creation to the closed_at time if issue.state == "closed": @@ -85,6 +89,10 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: issue.closed_at ) - datetime.fromisoformat(issue.created_at) else: + # skip label if last labeling event is 'unlabled' and issue is still open + if label_last_event_type[label] == "unlabeled": + continue + # if the issue is open, add the time from the issue creation to now label_metrics[label] += datetime.now(pytz.utc) - datetime.fromisoformat( issue.created_at From 8815d92578b2983dfb90bbb6f875df800fa75316 Mon Sep 17 00:00:00 2001 From: Steven Stone Date: Mon, 20 May 2024 13:06:16 -0400 Subject: [PATCH 3/3] Add test for ensuring None time for label labeled past close date --- test_labels.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test_labels.py b/test_labels.py index d5b6d43..e7e8b5f 100644 --- a/test_labels.py +++ b/test_labels.py @@ -40,6 +40,12 @@ def setUp(self): label={"name": "bug"}, created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC), ), + # Label labeled after issue close date + MagicMock( + event="labeled", + label={"name": "foo"}, + created_at=datetime(2021, 1, 20, tzinfo=pytz.UTC), + ), ] def test_get_label_events(self): @@ -80,6 +86,13 @@ def test_get_label_metrics_open_issue(self): datetime.now(pytz.utc) - datetime(2021, 1, 4, tzinfo=pytz.UTC), ) + def test_get_label_metrics_closed_issue_labeled_past_closed_at(self): + """Test get_label_metrics using a closed issue that was labeled past issue closed_at""" + self.issue.state = "closed" + labels = ["foo"] + metrics = get_label_metrics(self.issue, labels) + self.assertEqual(metrics["foo"], None) + class TestGetAverageTimeInLabels(unittest.TestCase): """Unit tests for get_stats_time_in_labels"""